blob: dcca9fa29bf4ae62b61d984a8f47b0e4dc8a9381 [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
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
68#define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
70#else
71#define INIT_REGEX_LOCK(opsname)
72#endif
73
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040074static struct ftrace_ops ftrace_list_end __read_mostly = {
75 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040076 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077};
78
Steven Rostedt4eebcc82008-05-12 21:20:48 +020079/* ftrace_enabled is a method to turn ftrace on or off */
80int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020081static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020082
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050083/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040084int function_trace_stop __read_mostly;
85
86/* Current function tracing op */
87struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050088
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040089/* List for set_ftrace_pid's pids. */
90LIST_HEAD(ftrace_pids);
91struct ftrace_pid {
92 struct list_head list;
93 struct pid *pid;
94};
95
Steven Rostedt4eebcc82008-05-12 21:20:48 +020096/*
97 * ftrace_disabled is set when an anomaly is discovered.
98 * ftrace_disabled is much stronger than ftrace_enabled.
99 */
100static int ftrace_disabled __read_mostly;
101
Steven Rostedt52baf112009-02-14 01:15:39 -0500102static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200103
Steven Rostedtb8489142011-05-04 09:27:52 -0400104static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
Jiri Olsae2484912012-02-15 15:51:48 +0100105static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400106static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200107ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500108ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400109static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100110static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200111
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400112#if ARCH_SUPPORTS_FTRACE_OPS
113static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400114 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400115#else
116/* See comment below, where ftrace_ops_list_func is defined */
117static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
118#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
119#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400120
Steven Rostedt0a016402012-11-02 17:03:03 -0400121/*
122 * Traverse the ftrace_global_list, invoking all entries. The reason that we
123 * can use rcu_dereference_raw() is that elements removed from this list
124 * are simply leaked, so there is no need to interact with a grace-period
125 * mechanism. The rcu_dereference_raw() calls are needed to handle
126 * concurrent insertions into the ftrace_global_list.
127 *
128 * Silly Alpha and silly pointer-speculation compiler optimizations!
129 */
130#define do_for_each_ftrace_op(op, list) \
131 op = rcu_dereference_raw(list); \
132 do
133
134/*
135 * Optimized for just a single item in the list (as that is the normal case).
136 */
137#define while_for_each_ftrace_op(op) \
138 while (likely(op = rcu_dereference_raw((op)->next)) && \
139 unlikely((op) != &ftrace_list_end))
140
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900141static inline void ftrace_ops_init(struct ftrace_ops *ops)
142{
143#ifdef CONFIG_DYNAMIC_FTRACE
144 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
145 mutex_init(&ops->regex_lock);
146 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
147 }
148#endif
149}
150
Steven Rostedtea701f12012-07-20 13:08:05 -0400151/**
152 * ftrace_nr_registered_ops - return number of ops registered
153 *
154 * Returns the number of ftrace_ops registered and tracing functions
155 */
156int ftrace_nr_registered_ops(void)
157{
158 struct ftrace_ops *ops;
159 int cnt = 0;
160
161 mutex_lock(&ftrace_lock);
162
163 for (ops = ftrace_ops_list;
164 ops != &ftrace_list_end; ops = ops->next)
165 cnt++;
166
167 mutex_unlock(&ftrace_lock);
168
169 return cnt;
170}
171
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400172static void
173ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400174 struct ftrace_ops *op, struct pt_regs *regs)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200175{
Steven Rostedtc29f1222012-11-02 17:17:59 -0400176 int bit;
177
Steven Rostedtedc15ca2012-11-02 17:47:21 -0400178 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
179 if (bit < 0)
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400180 return;
181
Steven Rostedt0a016402012-11-02 17:03:03 -0400182 do_for_each_ftrace_op(op, ftrace_global_list) {
Steven Rostedta1e2e312011-08-09 12:50:46 -0400183 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -0400184 } while_for_each_ftrace_op(op);
Steven Rostedtedc15ca2012-11-02 17:47:21 -0400185
186 trace_clear_recursion(bit);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200187}
188
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400189static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400190 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500191{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500192 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500193 return;
194
Steven Rostedta1e2e312011-08-09 12:50:46 -0400195 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500196}
197
198static void set_ftrace_pid_function(ftrace_func_t func)
199{
200 /* do not set ftrace_pid_function to itself! */
201 if (func != ftrace_pid_func)
202 ftrace_pid_function = func;
203}
204
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200205/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200206 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200207 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200208 * This NULLs the ftrace function and in essence stops
209 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200210 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200211void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200212{
Steven Rostedt3d083392008-05-12 21:20:42 +0200213 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500214 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200215}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200216
Jiri Olsae2484912012-02-15 15:51:48 +0100217static void control_ops_disable_all(struct ftrace_ops *ops)
218{
219 int cpu;
220
221 for_each_possible_cpu(cpu)
222 *per_cpu_ptr(ops->disabled, cpu) = 1;
223}
224
225static int control_ops_alloc(struct ftrace_ops *ops)
226{
227 int __percpu *disabled;
228
229 disabled = alloc_percpu(int);
230 if (!disabled)
231 return -ENOMEM;
232
233 ops->disabled = disabled;
234 control_ops_disable_all(ops);
235 return 0;
236}
237
238static void control_ops_free(struct ftrace_ops *ops)
239{
240 free_percpu(ops->disabled);
241}
242
Steven Rostedt2b499382011-05-03 22:49:52 -0400243static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400244{
245 ftrace_func_t func;
246
247 /*
248 * If there's only one function registered, then call that
249 * function directly. Otherwise, we need to iterate over the
250 * registered callers.
251 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400252 if (ftrace_global_list == &ftrace_list_end ||
Steven Rostedt63503792012-11-02 16:58:56 -0400253 ftrace_global_list->next == &ftrace_list_end) {
Steven Rostedtb8489142011-05-04 09:27:52 -0400254 func = ftrace_global_list->func;
Steven Rostedt63503792012-11-02 16:58:56 -0400255 /*
256 * As we are calling the function directly.
257 * If it does not have recursion protection,
258 * the function_trace_op needs to be updated
259 * accordingly.
260 */
261 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
262 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
263 else
264 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
265 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -0400266 func = ftrace_global_list_func;
Steven Rostedt63503792012-11-02 16:58:56 -0400267 /* The list has its own recursion protection. */
268 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
269 }
270
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400271
272 /* If we filter on pids, update to use the pid function */
273 if (!list_empty(&ftrace_pids)) {
274 set_ftrace_pid_function(func);
275 func = ftrace_pid_func;
276 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400277
278 global_ops.func = func;
279}
280
281static void update_ftrace_function(void)
282{
283 ftrace_func_t func;
284
285 update_global_ops();
286
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400287 /*
288 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400289 * recursion safe and not dynamic and the arch supports passing ops,
290 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400291 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400292 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400293 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400294 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400295 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400296 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400297 /* Set the ftrace_ops that the arch callback uses */
298 if (ftrace_ops_list == &global_ops)
299 function_trace_op = ftrace_global_list;
300 else
301 function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400302 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400303 } else {
304 /* Just use the default ftrace_ops */
305 function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400306 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400307 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400308
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400309 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400310}
311
Steven Rostedt2b499382011-05-03 22:49:52 -0400312static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200313{
Steven Rostedt2b499382011-05-03 22:49:52 -0400314 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200315 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400316 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200317 * CPU might be walking that list. We need to make sure
318 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400319 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200320 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400321 rcu_assign_pointer(*list, ops);
322}
Steven Rostedt3d083392008-05-12 21:20:42 +0200323
Steven Rostedt2b499382011-05-03 22:49:52 -0400324static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
325{
326 struct ftrace_ops **p;
327
328 /*
329 * If we are removing the last function, then simply point
330 * to the ftrace_stub.
331 */
332 if (*list == ops && ops->next == &ftrace_list_end) {
333 *list = &ftrace_list_end;
334 return 0;
335 }
336
337 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
338 if (*p == ops)
339 break;
340
341 if (*p != ops)
342 return -1;
343
344 *p = (*p)->next;
345 return 0;
346}
347
Jiri Olsae2484912012-02-15 15:51:48 +0100348static void add_ftrace_list_ops(struct ftrace_ops **list,
349 struct ftrace_ops *main_ops,
350 struct ftrace_ops *ops)
351{
352 int first = *list == &ftrace_list_end;
353 add_ftrace_ops(list, ops);
354 if (first)
355 add_ftrace_ops(&ftrace_ops_list, main_ops);
356}
357
358static int remove_ftrace_list_ops(struct ftrace_ops **list,
359 struct ftrace_ops *main_ops,
360 struct ftrace_ops *ops)
361{
362 int ret = remove_ftrace_ops(list, ops);
363 if (!ret && *list == &ftrace_list_end)
364 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
365 return ret;
366}
367
Steven Rostedt2b499382011-05-03 22:49:52 -0400368static int __register_ftrace_function(struct ftrace_ops *ops)
369{
Borislav Petkov8d240dd2012-03-29 19:11:40 +0200370 if (unlikely(ftrace_disabled))
Steven Rostedt2b499382011-05-03 22:49:52 -0400371 return -ENODEV;
372
373 if (FTRACE_WARN_ON(ops == &global_ops))
374 return -EINVAL;
375
Steven Rostedtb8489142011-05-04 09:27:52 -0400376 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
377 return -EBUSY;
378
Jiri Olsae2484912012-02-15 15:51:48 +0100379 /* We don't support both control and global flags set. */
380 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
381 return -EINVAL;
382
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900383#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400384 /*
385 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
386 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
387 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
388 */
389 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
390 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
391 return -EINVAL;
392
393 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
394 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
395#endif
396
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400397 if (!core_kernel_data((unsigned long)ops))
398 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
399
Steven Rostedtb8489142011-05-04 09:27:52 -0400400 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100401 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400402 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100403 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
404 if (control_ops_alloc(ops))
405 return -ENOMEM;
406 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400407 } else
408 add_ftrace_ops(&ftrace_ops_list, ops);
409
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400410 if (ftrace_enabled)
411 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200412
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200413 return 0;
414}
415
Ingo Molnare309b412008-05-12 21:20:51 +0200416static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200417{
Steven Rostedt2b499382011-05-03 22:49:52 -0400418 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200419
Steven Rostedt2b499382011-05-03 22:49:52 -0400420 if (ftrace_disabled)
421 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200422
Steven Rostedtb8489142011-05-04 09:27:52 -0400423 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
424 return -EBUSY;
425
Steven Rostedt2b499382011-05-03 22:49:52 -0400426 if (FTRACE_WARN_ON(ops == &global_ops))
427 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200428
Steven Rostedtb8489142011-05-04 09:27:52 -0400429 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100430 ret = remove_ftrace_list_ops(&ftrace_global_list,
431 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400432 if (!ret)
433 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100434 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
435 ret = remove_ftrace_list_ops(&ftrace_control_list,
436 &control_ops, ops);
437 if (!ret) {
438 /*
439 * The ftrace_ops is now removed from the list,
440 * so there'll be no new users. We must ensure
441 * all current users are done before we free
442 * the control data.
443 */
444 synchronize_sched();
445 control_ops_free(ops);
446 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400447 } else
448 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
449
Steven Rostedt2b499382011-05-03 22:49:52 -0400450 if (ret < 0)
451 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400452
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400453 if (ftrace_enabled)
454 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200455
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400456 /*
457 * Dynamic ops may be freed, we must make sure that all
458 * callers are done before leaving this function.
459 */
460 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
461 synchronize_sched();
462
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500463 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200464}
465
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500466static void ftrace_update_pid_func(void)
467{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400468 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500469 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900470 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500471
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400472 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500473}
474
Steven Rostedt493762f2009-03-23 17:12:36 -0400475#ifdef CONFIG_FUNCTION_PROFILER
476struct ftrace_profile {
477 struct hlist_node node;
478 unsigned long ip;
479 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400480#ifdef CONFIG_FUNCTION_GRAPH_TRACER
481 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400482 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400483#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400484};
485
486struct ftrace_profile_page {
487 struct ftrace_profile_page *next;
488 unsigned long index;
489 struct ftrace_profile records[];
490};
491
Steven Rostedtcafb1682009-03-24 20:50:39 -0400492struct ftrace_profile_stat {
493 atomic_t disabled;
494 struct hlist_head *hash;
495 struct ftrace_profile_page *pages;
496 struct ftrace_profile_page *start;
497 struct tracer_stat stat;
498};
499
Steven Rostedt493762f2009-03-23 17:12:36 -0400500#define PROFILE_RECORDS_SIZE \
501 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
502
503#define PROFILES_PER_PAGE \
504 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
505
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400506static int ftrace_profile_enabled __read_mostly;
507
508/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400509static DEFINE_MUTEX(ftrace_profile_lock);
510
Steven Rostedtcafb1682009-03-24 20:50:39 -0400511static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400512
Namhyung Kim20079eb2013-04-10 08:55:50 +0900513#define FTRACE_PROFILE_HASH_BITS 10
514#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400515
Steven Rostedt493762f2009-03-23 17:12:36 -0400516static void *
517function_stat_next(void *v, int idx)
518{
519 struct ftrace_profile *rec = v;
520 struct ftrace_profile_page *pg;
521
522 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
523
524 again:
Li Zefan0296e422009-06-26 11:15:37 +0800525 if (idx != 0)
526 rec++;
527
Steven Rostedt493762f2009-03-23 17:12:36 -0400528 if ((void *)rec >= (void *)&pg->records[pg->index]) {
529 pg = pg->next;
530 if (!pg)
531 return NULL;
532 rec = &pg->records[0];
533 if (!rec->counter)
534 goto again;
535 }
536
537 return rec;
538}
539
540static void *function_stat_start(struct tracer_stat *trace)
541{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400542 struct ftrace_profile_stat *stat =
543 container_of(trace, struct ftrace_profile_stat, stat);
544
545 if (!stat || !stat->start)
546 return NULL;
547
548 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400549}
550
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400551#ifdef CONFIG_FUNCTION_GRAPH_TRACER
552/* function graph compares on total time */
553static int function_stat_cmp(void *p1, void *p2)
554{
555 struct ftrace_profile *a = p1;
556 struct ftrace_profile *b = p2;
557
558 if (a->time < b->time)
559 return -1;
560 if (a->time > b->time)
561 return 1;
562 else
563 return 0;
564}
565#else
566/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400567static int function_stat_cmp(void *p1, void *p2)
568{
569 struct ftrace_profile *a = p1;
570 struct ftrace_profile *b = p2;
571
572 if (a->counter < b->counter)
573 return -1;
574 if (a->counter > b->counter)
575 return 1;
576 else
577 return 0;
578}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400579#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400580
581static int function_stat_headers(struct seq_file *m)
582{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400583#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400584 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400585 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400586 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400587 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400588#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400589 seq_printf(m, " Function Hit\n"
590 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400591#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400592 return 0;
593}
594
595static int function_stat_show(struct seq_file *m, void *v)
596{
597 struct ftrace_profile *rec = v;
598 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800599 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400600#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400601 static struct trace_seq s;
602 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400603 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400604#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800605 mutex_lock(&ftrace_profile_lock);
606
607 /* we raced with function_profile_reset() */
608 if (unlikely(rec->counter == 0)) {
609 ret = -EBUSY;
610 goto out;
611 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400612
613 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400614 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400615
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400616#ifdef CONFIG_FUNCTION_GRAPH_TRACER
617 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400618 avg = rec->time;
619 do_div(avg, rec->counter);
620
Chase Douglase330b3b2010-04-26 14:02:05 -0400621 /* Sample standard deviation (s^2) */
622 if (rec->counter <= 1)
623 stddev = 0;
624 else {
625 stddev = rec->time_squared - rec->counter * avg * avg;
626 /*
627 * Divide only 1000 for ns^2 -> us^2 conversion.
628 * trace_print_graph_duration will divide 1000 again.
629 */
630 do_div(stddev, (rec->counter - 1) * 1000);
631 }
632
Steven Rostedt34886c82009-03-25 21:00:47 -0400633 trace_seq_init(&s);
634 trace_print_graph_duration(rec->time, &s);
635 trace_seq_puts(&s, " ");
636 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400637 trace_seq_puts(&s, " ");
638 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400639 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400640#endif
641 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800642out:
643 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400644
Li Zefan3aaba202010-08-23 16:50:12 +0800645 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400646}
647
Steven Rostedtcafb1682009-03-24 20:50:39 -0400648static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400649{
650 struct ftrace_profile_page *pg;
651
Steven Rostedtcafb1682009-03-24 20:50:39 -0400652 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400653
654 while (pg) {
655 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
656 pg->index = 0;
657 pg = pg->next;
658 }
659
Steven Rostedtcafb1682009-03-24 20:50:39 -0400660 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400661 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
662}
663
Steven Rostedtcafb1682009-03-24 20:50:39 -0400664int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400665{
666 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400667 int functions;
668 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400669 int i;
670
671 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400672 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400673 return 0;
674
Steven Rostedtcafb1682009-03-24 20:50:39 -0400675 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
676 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400677 return -ENOMEM;
678
Steven Rostedt318e0a72009-03-25 20:06:34 -0400679#ifdef CONFIG_DYNAMIC_FTRACE
680 functions = ftrace_update_tot_cnt;
681#else
682 /*
683 * We do not know the number of functions that exist because
684 * dynamic tracing is what counts them. With past experience
685 * we have around 20K functions. That should be more than enough.
686 * It is highly unlikely we will execute every function in
687 * the kernel.
688 */
689 functions = 20000;
690#endif
691
Steven Rostedtcafb1682009-03-24 20:50:39 -0400692 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400693
Steven Rostedt318e0a72009-03-25 20:06:34 -0400694 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
695
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900696 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400697 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400698 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400699 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400700 pg = pg->next;
701 }
702
703 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400704
705 out_free:
706 pg = stat->start;
707 while (pg) {
708 unsigned long tmp = (unsigned long)pg;
709
710 pg = pg->next;
711 free_page(tmp);
712 }
713
Steven Rostedt318e0a72009-03-25 20:06:34 -0400714 stat->pages = NULL;
715 stat->start = NULL;
716
717 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400718}
719
Steven Rostedtcafb1682009-03-24 20:50:39 -0400720static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400721{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400722 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400723 int size;
724
Steven Rostedtcafb1682009-03-24 20:50:39 -0400725 stat = &per_cpu(ftrace_profile_stats, cpu);
726
727 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400728 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400729 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400730 return 0;
731 }
732
733 /*
734 * We are profiling all functions, but usually only a few thousand
735 * functions are hit. We'll make a hash of 1024 items.
736 */
737 size = FTRACE_PROFILE_HASH_SIZE;
738
Steven Rostedtcafb1682009-03-24 20:50:39 -0400739 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400740
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400742 return -ENOMEM;
743
Steven Rostedt318e0a72009-03-25 20:06:34 -0400744 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400745 if (ftrace_profile_pages_init(stat) < 0) {
746 kfree(stat->hash);
747 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400748 return -ENOMEM;
749 }
750
751 return 0;
752}
753
Steven Rostedtcafb1682009-03-24 20:50:39 -0400754static int ftrace_profile_init(void)
755{
756 int cpu;
757 int ret = 0;
758
759 for_each_online_cpu(cpu) {
760 ret = ftrace_profile_init_cpu(cpu);
761 if (ret)
762 break;
763 }
764
765 return ret;
766}
767
Steven Rostedt493762f2009-03-23 17:12:36 -0400768/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400769static struct ftrace_profile *
770ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400771{
772 struct ftrace_profile *rec;
773 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400774 unsigned long key;
775
Namhyung Kim20079eb2013-04-10 08:55:50 +0900776 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400777 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400778
779 if (hlist_empty(hhd))
780 return NULL;
781
Sasha Levinb67bfe02013-02-27 17:06:00 -0800782 hlist_for_each_entry_rcu(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400783 if (rec->ip == ip)
784 return rec;
785 }
786
787 return NULL;
788}
789
Steven Rostedtcafb1682009-03-24 20:50:39 -0400790static void ftrace_add_profile(struct ftrace_profile_stat *stat,
791 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400792{
793 unsigned long key;
794
Namhyung Kim20079eb2013-04-10 08:55:50 +0900795 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400796 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400797}
798
Steven Rostedt318e0a72009-03-25 20:06:34 -0400799/*
800 * The memory is already allocated, this simply finds a new record to use.
801 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400802static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400803ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400804{
805 struct ftrace_profile *rec = NULL;
806
Steven Rostedt318e0a72009-03-25 20:06:34 -0400807 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400808 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400809 goto out;
810
Steven Rostedt493762f2009-03-23 17:12:36 -0400811 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400812 * Try to find the function again since an NMI
813 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400814 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400815 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400816 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400817 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400818
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 if (stat->pages->index == PROFILES_PER_PAGE) {
820 if (!stat->pages->next)
821 goto out;
822 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400823 }
824
Steven Rostedtcafb1682009-03-24 20:50:39 -0400825 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400826 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400827 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400828
Steven Rostedt493762f2009-03-23 17:12:36 -0400829 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400830 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400831
832 return rec;
833}
834
Steven Rostedt493762f2009-03-23 17:12:36 -0400835static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400836function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400837 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400838{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400839 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400840 struct ftrace_profile *rec;
841 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400842
843 if (!ftrace_profile_enabled)
844 return;
845
Steven Rostedt493762f2009-03-23 17:12:36 -0400846 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400847
848 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400849 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400850 goto out;
851
852 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400853 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400854 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400855 if (!rec)
856 goto out;
857 }
858
859 rec->counter++;
860 out:
861 local_irq_restore(flags);
862}
863
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400864#ifdef CONFIG_FUNCTION_GRAPH_TRACER
865static int profile_graph_entry(struct ftrace_graph_ent *trace)
866{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400867 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400868 return 1;
869}
870
871static void profile_graph_return(struct ftrace_graph_ret *trace)
872{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400873 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400874 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400875 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400876 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400877
878 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400879 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400880 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400881 goto out;
882
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400883 /* If the calltime was zero'd ignore it */
884 if (!trace->calltime)
885 goto out;
886
Steven Rostedta2a16d62009-03-24 23:17:58 -0400887 calltime = trace->rettime - trace->calltime;
888
889 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
890 int index;
891
892 index = trace->depth;
893
894 /* Append this call time to the parent time to subtract */
895 if (index)
896 current->ret_stack[index - 1].subtime += calltime;
897
898 if (current->ret_stack[index].subtime < calltime)
899 calltime -= current->ret_stack[index].subtime;
900 else
901 calltime = 0;
902 }
903
Steven Rostedtcafb1682009-03-24 20:50:39 -0400904 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400905 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400906 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400907 rec->time_squared += calltime * calltime;
908 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400909
Steven Rostedtcafb1682009-03-24 20:50:39 -0400910 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400911 local_irq_restore(flags);
912}
913
914static int register_ftrace_profiler(void)
915{
916 return register_ftrace_graph(&profile_graph_return,
917 &profile_graph_entry);
918}
919
920static void unregister_ftrace_profiler(void)
921{
922 unregister_ftrace_graph();
923}
924#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100925static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400926 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900927 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
928 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400929};
930
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400931static int register_ftrace_profiler(void)
932{
933 return register_ftrace_function(&ftrace_profile_ops);
934}
935
936static void unregister_ftrace_profiler(void)
937{
938 unregister_ftrace_function(&ftrace_profile_ops);
939}
940#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
941
Steven Rostedt493762f2009-03-23 17:12:36 -0400942static ssize_t
943ftrace_profile_write(struct file *filp, const char __user *ubuf,
944 size_t cnt, loff_t *ppos)
945{
946 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400947 int ret;
948
Peter Huewe22fe9b52011-06-07 21:58:27 +0200949 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
950 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400951 return ret;
952
953 val = !!val;
954
955 mutex_lock(&ftrace_profile_lock);
956 if (ftrace_profile_enabled ^ val) {
957 if (val) {
958 ret = ftrace_profile_init();
959 if (ret < 0) {
960 cnt = ret;
961 goto out;
962 }
963
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400964 ret = register_ftrace_profiler();
965 if (ret < 0) {
966 cnt = ret;
967 goto out;
968 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400969 ftrace_profile_enabled = 1;
970 } else {
971 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400972 /*
973 * unregister_ftrace_profiler calls stop_machine
974 * so this acts like an synchronize_sched.
975 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400976 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400977 }
978 }
979 out:
980 mutex_unlock(&ftrace_profile_lock);
981
Jiri Olsacf8517c2009-10-23 19:36:16 -0400982 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400983
984 return cnt;
985}
986
987static ssize_t
988ftrace_profile_read(struct file *filp, char __user *ubuf,
989 size_t cnt, loff_t *ppos)
990{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400991 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400992 int r;
993
994 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
995 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
996}
997
998static const struct file_operations ftrace_profile_fops = {
999 .open = tracing_open_generic,
1000 .read = ftrace_profile_read,
1001 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001002 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001003};
1004
Steven Rostedtcafb1682009-03-24 20:50:39 -04001005/* used to initialize the real stat files */
1006static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001007 .name = "functions",
1008 .stat_start = function_stat_start,
1009 .stat_next = function_stat_next,
1010 .stat_cmp = function_stat_cmp,
1011 .stat_headers = function_stat_headers,
1012 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001013};
1014
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001015static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001016{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001017 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001018 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001019 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001020 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001021 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001022
Steven Rostedtcafb1682009-03-24 20:50:39 -04001023 for_each_possible_cpu(cpu) {
1024 stat = &per_cpu(ftrace_profile_stats, cpu);
1025
1026 /* allocate enough for function name + cpu number */
1027 name = kmalloc(32, GFP_KERNEL);
1028 if (!name) {
1029 /*
1030 * The files created are permanent, if something happens
1031 * we still do not free memory.
1032 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001033 WARN(1,
1034 "Could not allocate stat file for cpu %d\n",
1035 cpu);
1036 return;
1037 }
1038 stat->stat = function_stats;
1039 snprintf(name, 32, "function%d", cpu);
1040 stat->stat.name = name;
1041 ret = register_stat_tracer(&stat->stat);
1042 if (ret) {
1043 WARN(1,
1044 "Could not register function stat for cpu %d\n",
1045 cpu);
1046 kfree(name);
1047 return;
1048 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001049 }
1050
1051 entry = debugfs_create_file("function_profile_enabled", 0644,
1052 d_tracer, NULL, &ftrace_profile_fops);
1053 if (!entry)
1054 pr_warning("Could not create debugfs "
1055 "'function_profile_enabled' entry\n");
1056}
1057
1058#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001059static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001060{
1061}
1062#endif /* CONFIG_FUNCTION_PROFILER */
1063
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001064static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1065
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04001066loff_t
1067ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1068{
1069 loff_t ret;
1070
1071 if (file->f_mode & FMODE_READ)
1072 ret = seq_lseek(file, offset, whence);
1073 else
1074 file->f_pos = ret = 1;
1075
1076 return ret;
1077}
1078
Steven Rostedt3d083392008-05-12 21:20:42 +02001079#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001080
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001081#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001082# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001083#endif
1084
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001085static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1086
Steven Rostedtb6887d72009-02-17 12:32:04 -05001087struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001088 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001089 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001090 unsigned long flags;
1091 unsigned long ip;
1092 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001093 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001094};
1095
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001096struct ftrace_func_entry {
1097 struct hlist_node hlist;
1098 unsigned long ip;
1099};
1100
1101struct ftrace_hash {
1102 unsigned long size_bits;
1103 struct hlist_head *buckets;
1104 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001105 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001106};
1107
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001108/*
1109 * We make these constant because no one should touch them,
1110 * but they are used as the default "empty hash", to avoid allocating
1111 * it all the time. These are in a read only section such that if
1112 * anyone does try to modify it, it will cause an exception.
1113 */
1114static const struct hlist_head empty_buckets[1];
1115static const struct ftrace_hash empty_hash = {
1116 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001117};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001118#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001119
Steven Rostedt2b499382011-05-03 22:49:52 -04001120static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001121 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001122 .notrace_hash = EMPTY_HASH,
1123 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001124 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1125 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001126};
1127
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001128struct ftrace_page {
1129 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001130 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001131 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001132 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001133};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001134
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001135static struct ftrace_page *ftrace_new_pgs;
1136
Steven Rostedta7900872011-12-16 16:23:44 -05001137#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1138#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001139
1140/* estimate from running different kernels */
1141#define NR_TO_INIT 10000
1142
1143static struct ftrace_page *ftrace_pages_start;
1144static struct ftrace_page *ftrace_pages;
1145
Steven Rostedt06a51d92011-12-19 19:07:36 -05001146static bool ftrace_hash_empty(struct ftrace_hash *hash)
1147{
1148 return !hash || !hash->count;
1149}
1150
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001151static struct ftrace_func_entry *
1152ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1153{
1154 unsigned long key;
1155 struct ftrace_func_entry *entry;
1156 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001157
Steven Rostedt06a51d92011-12-19 19:07:36 -05001158 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001159 return NULL;
1160
1161 if (hash->size_bits > 0)
1162 key = hash_long(ip, hash->size_bits);
1163 else
1164 key = 0;
1165
1166 hhd = &hash->buckets[key];
1167
Sasha Levinb67bfe02013-02-27 17:06:00 -08001168 hlist_for_each_entry_rcu(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001169 if (entry->ip == ip)
1170 return entry;
1171 }
1172 return NULL;
1173}
1174
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001175static void __add_hash_entry(struct ftrace_hash *hash,
1176 struct ftrace_func_entry *entry)
1177{
1178 struct hlist_head *hhd;
1179 unsigned long key;
1180
1181 if (hash->size_bits)
1182 key = hash_long(entry->ip, hash->size_bits);
1183 else
1184 key = 0;
1185
1186 hhd = &hash->buckets[key];
1187 hlist_add_head(&entry->hlist, hhd);
1188 hash->count++;
1189}
1190
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001191static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1192{
1193 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001194
1195 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1196 if (!entry)
1197 return -ENOMEM;
1198
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001199 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001200 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001201
1202 return 0;
1203}
1204
1205static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001206free_hash_entry(struct ftrace_hash *hash,
1207 struct ftrace_func_entry *entry)
1208{
1209 hlist_del(&entry->hlist);
1210 kfree(entry);
1211 hash->count--;
1212}
1213
1214static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001215remove_hash_entry(struct ftrace_hash *hash,
1216 struct ftrace_func_entry *entry)
1217{
1218 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001219 hash->count--;
1220}
1221
1222static void ftrace_hash_clear(struct ftrace_hash *hash)
1223{
1224 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001225 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001226 struct ftrace_func_entry *entry;
1227 int size = 1 << hash->size_bits;
1228 int i;
1229
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001230 if (!hash->count)
1231 return;
1232
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001233 for (i = 0; i < size; i++) {
1234 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001235 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001236 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001237 }
1238 FTRACE_WARN_ON(hash->count);
1239}
1240
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001241static void free_ftrace_hash(struct ftrace_hash *hash)
1242{
1243 if (!hash || hash == EMPTY_HASH)
1244 return;
1245 ftrace_hash_clear(hash);
1246 kfree(hash->buckets);
1247 kfree(hash);
1248}
1249
Steven Rostedt07fd5512011-05-05 18:03:47 -04001250static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1251{
1252 struct ftrace_hash *hash;
1253
1254 hash = container_of(rcu, struct ftrace_hash, rcu);
1255 free_ftrace_hash(hash);
1256}
1257
1258static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1259{
1260 if (!hash || hash == EMPTY_HASH)
1261 return;
1262 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1263}
1264
Jiri Olsa5500fa52012-02-15 15:51:54 +01001265void ftrace_free_filter(struct ftrace_ops *ops)
1266{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001267 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001268 free_ftrace_hash(ops->filter_hash);
1269 free_ftrace_hash(ops->notrace_hash);
1270}
1271
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001272static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1273{
1274 struct ftrace_hash *hash;
1275 int size;
1276
1277 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1278 if (!hash)
1279 return NULL;
1280
1281 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001282 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001283
1284 if (!hash->buckets) {
1285 kfree(hash);
1286 return NULL;
1287 }
1288
1289 hash->size_bits = size_bits;
1290
1291 return hash;
1292}
1293
1294static struct ftrace_hash *
1295alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1296{
1297 struct ftrace_func_entry *entry;
1298 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001299 int size;
1300 int ret;
1301 int i;
1302
1303 new_hash = alloc_ftrace_hash(size_bits);
1304 if (!new_hash)
1305 return NULL;
1306
1307 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001308 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001309 return new_hash;
1310
1311 size = 1 << hash->size_bits;
1312 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001313 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001314 ret = add_hash_entry(new_hash, entry->ip);
1315 if (ret < 0)
1316 goto free_hash;
1317 }
1318 }
1319
1320 FTRACE_WARN_ON(new_hash->count != hash->count);
1321
1322 return new_hash;
1323
1324 free_hash:
1325 free_ftrace_hash(new_hash);
1326 return NULL;
1327}
1328
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001329static void
1330ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1331static void
1332ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1333
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001334static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001335ftrace_hash_move(struct ftrace_ops *ops, int enable,
1336 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001337{
1338 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001339 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001340 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001341 struct ftrace_hash *old_hash;
1342 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001343 int size = src->count;
1344 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001345 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001346 int i;
1347
1348 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001349 * Remove the current set, update the hash and add
1350 * them back.
1351 */
1352 ftrace_hash_rec_disable(ops, enable);
1353
1354 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001355 * If the new source is empty, just free dst and assign it
1356 * the empty_hash.
1357 */
1358 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001359 free_ftrace_hash_rcu(*dst);
1360 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001361 /* still need to update the function records */
1362 ret = 0;
1363 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001364 }
1365
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001366 /*
1367 * Make the hash size about 1/2 the # found
1368 */
1369 for (size /= 2; size; size >>= 1)
1370 bits++;
1371
1372 /* Don't allocate too much */
1373 if (bits > FTRACE_HASH_MAX_BITS)
1374 bits = FTRACE_HASH_MAX_BITS;
1375
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001376 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001377 new_hash = alloc_ftrace_hash(bits);
1378 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001379 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001380
1381 size = 1 << src->size_bits;
1382 for (i = 0; i < size; i++) {
1383 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001384 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001385 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001386 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001387 }
1388 }
1389
Steven Rostedt07fd5512011-05-05 18:03:47 -04001390 old_hash = *dst;
1391 rcu_assign_pointer(*dst, new_hash);
1392 free_ftrace_hash_rcu(old_hash);
1393
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001394 ret = 0;
1395 out:
1396 /*
1397 * Enable regardless of ret:
1398 * On success, we enable the new hash.
1399 * On failure, we re-enable the original hash.
1400 */
1401 ftrace_hash_rec_enable(ops, enable);
1402
1403 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001404}
1405
Steven Rostedt265c8312009-02-13 12:43:56 -05001406/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001407 * Test the hashes for this ops to see if we want to call
1408 * the ops->func or not.
1409 *
1410 * It's a match if the ip is in the ops->filter_hash or
1411 * the filter_hash does not exist or is empty,
1412 * AND
1413 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001414 *
1415 * This needs to be called with preemption disabled as
1416 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001417 */
1418static int
1419ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1420{
1421 struct ftrace_hash *filter_hash;
1422 struct ftrace_hash *notrace_hash;
1423 int ret;
1424
Steven Rostedtb8489142011-05-04 09:27:52 -04001425 filter_hash = rcu_dereference_raw(ops->filter_hash);
1426 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1427
Steven Rostedt06a51d92011-12-19 19:07:36 -05001428 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001429 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001430 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001431 !ftrace_lookup_ip(notrace_hash, ip)))
1432 ret = 1;
1433 else
1434 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001435
1436 return ret;
1437}
1438
1439/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001440 * This is a double for. Do not use 'break' to break out of the loop,
1441 * you must use a goto.
1442 */
1443#define do_for_each_ftrace_rec(pg, rec) \
1444 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1445 int _____i; \
1446 for (_____i = 0; _____i < pg->index; _____i++) { \
1447 rec = &pg->records[_____i];
1448
1449#define while_for_each_ftrace_rec() \
1450 } \
1451 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301452
Steven Rostedt5855fea2011-12-16 19:27:42 -05001453
1454static int ftrace_cmp_recs(const void *a, const void *b)
1455{
Steven Rostedta650e022012-04-25 13:48:13 -04001456 const struct dyn_ftrace *key = a;
1457 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001458
Steven Rostedta650e022012-04-25 13:48:13 -04001459 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001460 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001461 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1462 return 1;
1463 return 0;
1464}
1465
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001466static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001467{
1468 struct ftrace_page *pg;
1469 struct dyn_ftrace *rec;
1470 struct dyn_ftrace key;
1471
1472 key.ip = start;
1473 key.flags = end; /* overload flags, as it is unsigned long */
1474
1475 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1476 if (end < pg->records[0].ip ||
1477 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1478 continue;
1479 rec = bsearch(&key, pg->records, pg->index,
1480 sizeof(struct dyn_ftrace),
1481 ftrace_cmp_recs);
1482 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001483 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001484 }
1485
Steven Rostedt5855fea2011-12-16 19:27:42 -05001486 return 0;
1487}
1488
Steven Rostedtc88fd862011-08-16 09:53:39 -04001489/**
1490 * ftrace_location - return true if the ip giving is a traced location
1491 * @ip: the instruction pointer to check
1492 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001493 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001494 * That is, the instruction that is either a NOP or call to
1495 * the function tracer. It checks the ftrace internal tables to
1496 * determine if the address belongs or not.
1497 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001498unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001499{
Steven Rostedta650e022012-04-25 13:48:13 -04001500 return ftrace_location_range(ip, ip);
1501}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001502
Steven Rostedta650e022012-04-25 13:48:13 -04001503/**
1504 * ftrace_text_reserved - return true if range contains an ftrace location
1505 * @start: start of range to search
1506 * @end: end of range to search (inclusive). @end points to the last byte to check.
1507 *
1508 * Returns 1 if @start and @end contains a ftrace location.
1509 * That is, the instruction that is either a NOP or call to
1510 * the function tracer. It checks the ftrace internal tables to
1511 * determine if the address belongs or not.
1512 */
1513int ftrace_text_reserved(void *start, void *end)
1514{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001515 unsigned long ret;
1516
1517 ret = ftrace_location_range((unsigned long)start,
1518 (unsigned long)end);
1519
1520 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001521}
1522
Steven Rostedted926f92011-05-03 13:25:24 -04001523static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1524 int filter_hash,
1525 bool inc)
1526{
1527 struct ftrace_hash *hash;
1528 struct ftrace_hash *other_hash;
1529 struct ftrace_page *pg;
1530 struct dyn_ftrace *rec;
1531 int count = 0;
1532 int all = 0;
1533
1534 /* Only update if the ops has been registered */
1535 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1536 return;
1537
1538 /*
1539 * In the filter_hash case:
1540 * If the count is zero, we update all records.
1541 * Otherwise we just update the items in the hash.
1542 *
1543 * In the notrace_hash case:
1544 * We enable the update in the hash.
1545 * As disabling notrace means enabling the tracing,
1546 * and enabling notrace means disabling, the inc variable
1547 * gets inversed.
1548 */
1549 if (filter_hash) {
1550 hash = ops->filter_hash;
1551 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001552 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001553 all = 1;
1554 } else {
1555 inc = !inc;
1556 hash = ops->notrace_hash;
1557 other_hash = ops->filter_hash;
1558 /*
1559 * If the notrace hash has no items,
1560 * then there's nothing to do.
1561 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001562 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001563 return;
1564 }
1565
1566 do_for_each_ftrace_rec(pg, rec) {
1567 int in_other_hash = 0;
1568 int in_hash = 0;
1569 int match = 0;
1570
1571 if (all) {
1572 /*
1573 * Only the filter_hash affects all records.
1574 * Update if the record is not in the notrace hash.
1575 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001576 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001577 match = 1;
1578 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001579 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1580 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001581
1582 /*
1583 *
1584 */
1585 if (filter_hash && in_hash && !in_other_hash)
1586 match = 1;
1587 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001588 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001589 match = 1;
1590 }
1591 if (!match)
1592 continue;
1593
1594 if (inc) {
1595 rec->flags++;
1596 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1597 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001598 /*
1599 * If any ops wants regs saved for this function
1600 * then all ops will get saved regs.
1601 */
1602 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1603 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001604 } else {
1605 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1606 return;
1607 rec->flags--;
1608 }
1609 count++;
1610 /* Shortcut, if we handled all records, we are done. */
1611 if (!all && count == hash->count)
1612 return;
1613 } while_for_each_ftrace_rec();
1614}
1615
1616static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1617 int filter_hash)
1618{
1619 __ftrace_hash_rec_update(ops, filter_hash, 0);
1620}
1621
1622static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1623 int filter_hash)
1624{
1625 __ftrace_hash_rec_update(ops, filter_hash, 1);
1626}
1627
Steven Rostedt05736a42008-09-22 14:55:47 -07001628static void print_ip_ins(const char *fmt, unsigned char *p)
1629{
1630 int i;
1631
1632 printk(KERN_CONT "%s", fmt);
1633
1634 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1635 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1636}
1637
Steven Rostedtc88fd862011-08-16 09:53:39 -04001638/**
1639 * ftrace_bug - report and shutdown function tracer
1640 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1641 * @ip: The address that failed
1642 *
1643 * The arch code that enables or disables the function tracing
1644 * can call ftrace_bug() when it has detected a problem in
1645 * modifying the code. @failed should be one of either:
1646 * EFAULT - if the problem happens on reading the @ip address
1647 * EINVAL - if what is read at @ip is not what was expected
1648 * EPERM - if the problem happens on writting to the @ip address
1649 */
1650void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001651{
1652 switch (failed) {
1653 case -EFAULT:
1654 FTRACE_WARN_ON_ONCE(1);
1655 pr_info("ftrace faulted on modifying ");
1656 print_ip_sym(ip);
1657 break;
1658 case -EINVAL:
1659 FTRACE_WARN_ON_ONCE(1);
1660 pr_info("ftrace failed to modify ");
1661 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001662 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001663 printk(KERN_CONT "\n");
1664 break;
1665 case -EPERM:
1666 FTRACE_WARN_ON_ONCE(1);
1667 pr_info("ftrace faulted on writing ");
1668 print_ip_sym(ip);
1669 break;
1670 default:
1671 FTRACE_WARN_ON_ONCE(1);
1672 pr_info("ftrace faulted on unknown error ");
1673 print_ip_sym(ip);
1674 }
1675}
1676
Steven Rostedtc88fd862011-08-16 09:53:39 -04001677static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001678{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001679 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001680
Steven Rostedt982c3502008-11-15 16:31:41 -05001681 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001682 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001683 *
Steven Rostedted926f92011-05-03 13:25:24 -04001684 * If the record has a ref count, then we need to enable it
1685 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001686 *
Steven Rostedted926f92011-05-03 13:25:24 -04001687 * Otherwise we make sure its disabled.
1688 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001689 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001690 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001691 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001692 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001693 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001694
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001695 /*
1696 * If enabling and the REGS flag does not match the REGS_EN, then
1697 * do not ignore this record. Set flags to fail the compare against
1698 * ENABLED.
1699 */
1700 if (flag &&
1701 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1702 flag |= FTRACE_FL_REGS;
1703
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001704 /* If the state of this record hasn't changed, then do nothing */
1705 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001706 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001707
1708 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001709 /* Save off if rec is being enabled (for return value) */
1710 flag ^= rec->flags & FTRACE_FL_ENABLED;
1711
1712 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001713 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001714 if (flag & FTRACE_FL_REGS) {
1715 if (rec->flags & FTRACE_FL_REGS)
1716 rec->flags |= FTRACE_FL_REGS_EN;
1717 else
1718 rec->flags &= ~FTRACE_FL_REGS_EN;
1719 }
1720 }
1721
1722 /*
1723 * If this record is being updated from a nop, then
1724 * return UPDATE_MAKE_CALL.
1725 * Otherwise, if the EN flag is set, then return
1726 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1727 * from the non-save regs, to a save regs function.
1728 * Otherwise,
1729 * return UPDATE_MODIFY_CALL to tell the caller to convert
1730 * from the save regs, to a non-save regs function.
1731 */
1732 if (flag & FTRACE_FL_ENABLED)
1733 return FTRACE_UPDATE_MAKE_CALL;
1734 else if (rec->flags & FTRACE_FL_REGS_EN)
1735 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1736 else
1737 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001738 }
1739
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001740 if (update) {
1741 /* If there's no more users, clear all flags */
1742 if (!(rec->flags & ~FTRACE_FL_MASK))
1743 rec->flags = 0;
1744 else
1745 /* Just disable the record (keep REGS state) */
1746 rec->flags &= ~FTRACE_FL_ENABLED;
1747 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001748
1749 return FTRACE_UPDATE_MAKE_NOP;
1750}
1751
1752/**
1753 * ftrace_update_record, set a record that now is tracing or not
1754 * @rec: the record to update
1755 * @enable: set to 1 if the record is tracing, zero to force disable
1756 *
1757 * The records that represent all functions that can be traced need
1758 * to be updated when tracing has been enabled.
1759 */
1760int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1761{
1762 return ftrace_check_record(rec, enable, 1);
1763}
1764
1765/**
1766 * ftrace_test_record, check if the record has been enabled or not
1767 * @rec: the record to test
1768 * @enable: set to 1 to check if enabled, 0 if it is disabled
1769 *
1770 * The arch code may need to test if a record is already set to
1771 * tracing to determine how to modify the function code that it
1772 * represents.
1773 */
1774int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1775{
1776 return ftrace_check_record(rec, enable, 0);
1777}
1778
1779static int
1780__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1781{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001782 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001783 unsigned long ftrace_addr;
1784 int ret;
1785
Steven Rostedtc88fd862011-08-16 09:53:39 -04001786 ret = ftrace_update_record(rec, enable);
1787
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001788 if (rec->flags & FTRACE_FL_REGS)
1789 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1790 else
1791 ftrace_addr = (unsigned long)FTRACE_ADDR;
1792
Steven Rostedtc88fd862011-08-16 09:53:39 -04001793 switch (ret) {
1794 case FTRACE_UPDATE_IGNORE:
1795 return 0;
1796
1797 case FTRACE_UPDATE_MAKE_CALL:
1798 return ftrace_make_call(rec, ftrace_addr);
1799
1800 case FTRACE_UPDATE_MAKE_NOP:
1801 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001802
1803 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1804 case FTRACE_UPDATE_MODIFY_CALL:
1805 if (rec->flags & FTRACE_FL_REGS)
1806 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1807 else
1808 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1809
1810 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001811 }
1812
1813 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001814}
1815
Steven Rostedte4f5d542012-04-27 09:13:18 -04001816void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001817{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001818 struct dyn_ftrace *rec;
1819 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001820 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001821
Steven Rostedt45a4a232011-04-21 23:16:46 -04001822 if (unlikely(ftrace_disabled))
1823 return;
1824
Steven Rostedt265c8312009-02-13 12:43:56 -05001825 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001826 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001827 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001828 ftrace_bug(failed, rec->ip);
1829 /* Stop processing */
1830 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001831 }
1832 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001833}
1834
Steven Rostedtc88fd862011-08-16 09:53:39 -04001835struct ftrace_rec_iter {
1836 struct ftrace_page *pg;
1837 int index;
1838};
1839
1840/**
1841 * ftrace_rec_iter_start, start up iterating over traced functions
1842 *
1843 * Returns an iterator handle that is used to iterate over all
1844 * the records that represent address locations where functions
1845 * are traced.
1846 *
1847 * May return NULL if no records are available.
1848 */
1849struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1850{
1851 /*
1852 * We only use a single iterator.
1853 * Protected by the ftrace_lock mutex.
1854 */
1855 static struct ftrace_rec_iter ftrace_rec_iter;
1856 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1857
1858 iter->pg = ftrace_pages_start;
1859 iter->index = 0;
1860
1861 /* Could have empty pages */
1862 while (iter->pg && !iter->pg->index)
1863 iter->pg = iter->pg->next;
1864
1865 if (!iter->pg)
1866 return NULL;
1867
1868 return iter;
1869}
1870
1871/**
1872 * ftrace_rec_iter_next, get the next record to process.
1873 * @iter: The handle to the iterator.
1874 *
1875 * Returns the next iterator after the given iterator @iter.
1876 */
1877struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1878{
1879 iter->index++;
1880
1881 if (iter->index >= iter->pg->index) {
1882 iter->pg = iter->pg->next;
1883 iter->index = 0;
1884
1885 /* Could have empty pages */
1886 while (iter->pg && !iter->pg->index)
1887 iter->pg = iter->pg->next;
1888 }
1889
1890 if (!iter->pg)
1891 return NULL;
1892
1893 return iter;
1894}
1895
1896/**
1897 * ftrace_rec_iter_record, get the record at the iterator location
1898 * @iter: The current iterator location
1899 *
1900 * Returns the record that the current @iter is at.
1901 */
1902struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1903{
1904 return &iter->pg->records[iter->index];
1905}
1906
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301907static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001908ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001909{
1910 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001911 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001912
1913 ip = rec->ip;
1914
Steven Rostedt45a4a232011-04-21 23:16:46 -04001915 if (unlikely(ftrace_disabled))
1916 return 0;
1917
Shaohua Li25aac9d2009-01-09 11:29:40 +08001918 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001919 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001920 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301921 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001922 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301923 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001924}
1925
Steven Rostedt000ab692009-02-17 13:35:06 -05001926/*
1927 * archs can override this function if they must do something
1928 * before the modifying code is performed.
1929 */
1930int __weak ftrace_arch_code_modify_prepare(void)
1931{
1932 return 0;
1933}
1934
1935/*
1936 * archs can override this function if they must do something
1937 * after the modifying code is performed.
1938 */
1939int __weak ftrace_arch_code_modify_post_process(void)
1940{
1941 return 0;
1942}
1943
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001944void ftrace_modify_all_code(int command)
1945{
1946 if (command & FTRACE_UPDATE_CALLS)
1947 ftrace_replace_code(1);
1948 else if (command & FTRACE_DISABLE_CALLS)
1949 ftrace_replace_code(0);
1950
1951 if (command & FTRACE_UPDATE_TRACE_FUNC)
1952 ftrace_update_ftrace_func(ftrace_trace_function);
1953
1954 if (command & FTRACE_START_FUNC_RET)
1955 ftrace_enable_ftrace_graph_caller();
1956 else if (command & FTRACE_STOP_FUNC_RET)
1957 ftrace_disable_ftrace_graph_caller();
1958}
1959
Ingo Molnare309b412008-05-12 21:20:51 +02001960static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001961{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001962 int *command = data;
1963
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001964 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001965
Steven Rostedtc88fd862011-08-16 09:53:39 -04001966 return 0;
1967}
1968
1969/**
1970 * ftrace_run_stop_machine, go back to the stop machine method
1971 * @command: The command to tell ftrace what to do
1972 *
1973 * If an arch needs to fall back to the stop machine method, the
1974 * it can call this function.
1975 */
1976void ftrace_run_stop_machine(int command)
1977{
1978 stop_machine(__ftrace_modify_code, &command, NULL);
1979}
1980
1981/**
1982 * arch_ftrace_update_code, modify the code to trace or not trace
1983 * @command: The command that needs to be done
1984 *
1985 * Archs can override this function if it does not need to
1986 * run stop_machine() to modify code.
1987 */
1988void __weak arch_ftrace_update_code(int command)
1989{
1990 ftrace_run_stop_machine(command);
1991}
1992
1993static void ftrace_run_update_code(int command)
1994{
1995 int ret;
1996
1997 ret = ftrace_arch_code_modify_prepare();
1998 FTRACE_WARN_ON(ret);
1999 if (ret)
2000 return;
2001 /*
2002 * Do not call function tracer while we update the code.
2003 * We are in stop machine.
2004 */
2005 function_trace_stop++;
2006
2007 /*
2008 * By default we use stop_machine() to modify the code.
2009 * But archs can do what ever they want as long as it
2010 * is safe. The stop_machine() is the safest, but also
2011 * produces the most overhead.
2012 */
2013 arch_ftrace_update_code(command);
2014
Steven Rostedt6331c282011-07-13 15:11:02 -04002015 function_trace_stop--;
2016
Steven Rostedt000ab692009-02-17 13:35:06 -05002017 ret = ftrace_arch_code_modify_post_process();
2018 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002019}
2020
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002021static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002022static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002023static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002024
2025static void ftrace_startup_enable(int command)
2026{
2027 if (saved_ftrace_func != ftrace_trace_function) {
2028 saved_ftrace_func = ftrace_trace_function;
2029 command |= FTRACE_UPDATE_TRACE_FUNC;
2030 }
2031
2032 if (!command || !ftrace_enabled)
2033 return;
2034
2035 ftrace_run_update_code(command);
2036}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002037
Steven Rostedta1cd6172011-05-23 15:24:25 -04002038static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002039{
Steven Rostedtb8489142011-05-04 09:27:52 -04002040 bool hash_enable = true;
2041
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002042 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002043 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002044
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002045 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002046 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002047
Steven Rostedtb8489142011-05-04 09:27:52 -04002048 /* ops marked global share the filter hashes */
2049 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2050 ops = &global_ops;
2051 /* Don't update hash if global is already set */
2052 if (global_start_up)
2053 hash_enable = false;
2054 global_start_up++;
2055 }
2056
Steven Rostedted926f92011-05-03 13:25:24 -04002057 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002058 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002059 ftrace_hash_rec_enable(ops, 1);
2060
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002061 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002062
2063 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002064}
2065
Steven Rostedtbd69c302011-05-03 21:55:54 -04002066static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002067{
Steven Rostedtb8489142011-05-04 09:27:52 -04002068 bool hash_disable = true;
2069
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002070 if (unlikely(ftrace_disabled))
2071 return;
2072
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002073 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002074 /*
2075 * Just warn in case of unbalance, no need to kill ftrace, it's not
2076 * critical but the ftrace_call callers may be never nopped again after
2077 * further ftrace uses.
2078 */
2079 WARN_ON_ONCE(ftrace_start_up < 0);
2080
Steven Rostedtb8489142011-05-04 09:27:52 -04002081 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2082 ops = &global_ops;
2083 global_start_up--;
2084 WARN_ON_ONCE(global_start_up < 0);
2085 /* Don't update hash if global still has users */
2086 if (global_start_up) {
2087 WARN_ON_ONCE(!ftrace_start_up);
2088 hash_disable = false;
2089 }
2090 }
2091
2092 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002093 ftrace_hash_rec_disable(ops, 1);
2094
Steven Rostedtb8489142011-05-04 09:27:52 -04002095 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002096 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002097
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002098 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002099
2100 if (saved_ftrace_func != ftrace_trace_function) {
2101 saved_ftrace_func = ftrace_trace_function;
2102 command |= FTRACE_UPDATE_TRACE_FUNC;
2103 }
2104
2105 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002106 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002107
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002108 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002109}
2110
Ingo Molnare309b412008-05-12 21:20:51 +02002111static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002112{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002113 if (unlikely(ftrace_disabled))
2114 return;
2115
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002116 /* Force update next time */
2117 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002118 /* ftrace_start_up is true if we want ftrace running */
2119 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002120 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002121}
2122
Ingo Molnare309b412008-05-12 21:20:51 +02002123static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002124{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002125 if (unlikely(ftrace_disabled))
2126 return;
2127
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002128 /* ftrace_start_up is true if ftrace is running */
2129 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002130 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002131}
2132
Steven Rostedt3d083392008-05-12 21:20:42 +02002133static cycle_t ftrace_update_time;
2134static unsigned long ftrace_update_cnt;
2135unsigned long ftrace_update_tot_cnt;
2136
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002137static int ops_traces_mod(struct ftrace_ops *ops)
2138{
2139 struct ftrace_hash *hash;
2140
2141 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002142 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002143}
2144
Steven Rostedt31e88902008-11-14 16:21:19 -08002145static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002146{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002147 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002148 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302149 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002150 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002151 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002152
2153 /*
2154 * When adding a module, we need to check if tracers are
2155 * currently enabled and if they are set to trace all functions.
2156 * If they are, we need to enable the module functions as well
2157 * as update the reference counts for those function records.
2158 */
2159 if (mod) {
2160 struct ftrace_ops *ops;
2161
2162 for (ops = ftrace_ops_list;
2163 ops != &ftrace_list_end; ops = ops->next) {
2164 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2165 ops_traces_mod(ops))
2166 ref++;
2167 }
2168 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002169
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002170 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002171 ftrace_update_cnt = 0;
2172
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002173 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302174
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002175 for (i = 0; i < pg->index; i++) {
2176 /* If something went wrong, bail without enabling anything */
2177 if (unlikely(ftrace_disabled))
2178 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002179
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002180 p = &pg->records[i];
2181 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302182
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002183 /*
2184 * Do the initial record conversion from mcount jump
2185 * to the NOP instructions.
2186 */
2187 if (!ftrace_code_disable(mod, p))
2188 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002189
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002190 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002191
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002192 /*
2193 * If the tracing is enabled, go ahead and enable the record.
2194 *
2195 * The reason not to enable the record immediatelly is the
2196 * inherent check of ftrace_make_nop/ftrace_make_call for
2197 * correct previous instructions. Making first the NOP
2198 * conversion puts the module to the correct state, thus
2199 * passing the ftrace_make_call check.
2200 */
2201 if (ftrace_start_up && ref) {
2202 int failed = __ftrace_replace_code(p, 1);
2203 if (failed)
2204 ftrace_bug(failed, p->ip);
2205 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002206 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002207 }
2208
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002209 ftrace_new_pgs = NULL;
2210
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002211 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002212 ftrace_update_time = stop - start;
2213 ftrace_update_tot_cnt += ftrace_update_cnt;
2214
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002215 return 0;
2216}
2217
Steven Rostedta7900872011-12-16 16:23:44 -05002218static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002219{
Steven Rostedta7900872011-12-16 16:23:44 -05002220 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002221 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002222
Steven Rostedta7900872011-12-16 16:23:44 -05002223 if (WARN_ON(!count))
2224 return -EINVAL;
2225
2226 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002227
2228 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002229 * We want to fill as much as possible. No more than a page
2230 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002231 */
Steven Rostedta7900872011-12-16 16:23:44 -05002232 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2233 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002234
Steven Rostedta7900872011-12-16 16:23:44 -05002235 again:
2236 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2237
2238 if (!pg->records) {
2239 /* if we can't allocate this size, try something smaller */
2240 if (!order)
2241 return -ENOMEM;
2242 order >>= 1;
2243 goto again;
2244 }
2245
2246 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2247 pg->size = cnt;
2248
2249 if (cnt > count)
2250 cnt = count;
2251
2252 return cnt;
2253}
2254
2255static struct ftrace_page *
2256ftrace_allocate_pages(unsigned long num_to_init)
2257{
2258 struct ftrace_page *start_pg;
2259 struct ftrace_page *pg;
2260 int order;
2261 int cnt;
2262
2263 if (!num_to_init)
2264 return 0;
2265
2266 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2267 if (!pg)
2268 return NULL;
2269
2270 /*
2271 * Try to allocate as much as possible in one continues
2272 * location that fills in all of the space. We want to
2273 * waste as little space as possible.
2274 */
2275 for (;;) {
2276 cnt = ftrace_allocate_records(pg, num_to_init);
2277 if (cnt < 0)
2278 goto free_pages;
2279
2280 num_to_init -= cnt;
2281 if (!num_to_init)
2282 break;
2283
2284 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2285 if (!pg->next)
2286 goto free_pages;
2287
2288 pg = pg->next;
2289 }
2290
2291 return start_pg;
2292
2293 free_pages:
2294 while (start_pg) {
2295 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2296 free_pages((unsigned long)pg->records, order);
2297 start_pg = pg->next;
2298 kfree(pg);
2299 pg = start_pg;
2300 }
2301 pr_info("ftrace: FAILED to allocate memory for functions\n");
2302 return NULL;
2303}
2304
2305static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2306{
2307 int cnt;
2308
2309 if (!num_to_init) {
2310 pr_info("ftrace: No functions to be traced?\n");
2311 return -1;
2312 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002313
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002314 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002315 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002316 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002317
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002318 return 0;
2319}
2320
Steven Rostedt5072c592008-05-12 21:20:43 +02002321#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2322
2323struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002324 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002325 loff_t func_pos;
2326 struct ftrace_page *pg;
2327 struct dyn_ftrace *func;
2328 struct ftrace_func_probe *probe;
2329 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002330 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002331 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002332 int hidx;
2333 int idx;
2334 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002335};
2336
Ingo Molnare309b412008-05-12 21:20:51 +02002337static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002338t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002339{
2340 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002341 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002342 struct hlist_head *hhd;
2343
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002344 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002345 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002346
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002347 if (iter->probe)
2348 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002349 retry:
2350 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2351 return NULL;
2352
2353 hhd = &ftrace_func_hash[iter->hidx];
2354
2355 if (hlist_empty(hhd)) {
2356 iter->hidx++;
2357 hnd = NULL;
2358 goto retry;
2359 }
2360
2361 if (!hnd)
2362 hnd = hhd->first;
2363 else {
2364 hnd = hnd->next;
2365 if (!hnd) {
2366 iter->hidx++;
2367 goto retry;
2368 }
2369 }
2370
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002371 if (WARN_ON_ONCE(!hnd))
2372 return NULL;
2373
2374 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2375
2376 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002377}
2378
2379static void *t_hash_start(struct seq_file *m, loff_t *pos)
2380{
2381 struct ftrace_iterator *iter = m->private;
2382 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002383 loff_t l;
2384
Steven Rostedt69a30832011-12-19 15:21:16 -05002385 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2386 return NULL;
2387
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002388 if (iter->func_pos > *pos)
2389 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002390
Li Zefand82d6242009-06-24 09:54:54 +08002391 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002392 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002393 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002394 if (!p)
2395 break;
2396 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002397 if (!p)
2398 return NULL;
2399
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002400 /* Only set this if we have an item */
2401 iter->flags |= FTRACE_ITER_HASH;
2402
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002403 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002404}
2405
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002406static int
2407t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002408{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002409 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002410
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002411 rec = iter->probe;
2412 if (WARN_ON_ONCE(!rec))
2413 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002414
Steven Rostedt809dcf22009-02-16 23:06:01 -05002415 if (rec->ops->print)
2416 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2417
Steven Rostedtb375a112009-09-17 00:05:58 -04002418 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002419
2420 if (rec->data)
2421 seq_printf(m, ":%p", rec->data);
2422 seq_putc(m, '\n');
2423
2424 return 0;
2425}
2426
2427static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002428t_next(struct seq_file *m, void *v, loff_t *pos)
2429{
2430 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002431 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002432 struct dyn_ftrace *rec = NULL;
2433
Steven Rostedt45a4a232011-04-21 23:16:46 -04002434 if (unlikely(ftrace_disabled))
2435 return NULL;
2436
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002437 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002438 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002439
Steven Rostedt5072c592008-05-12 21:20:43 +02002440 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002441 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002442
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002443 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002444 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002445
Steven Rostedt5072c592008-05-12 21:20:43 +02002446 retry:
2447 if (iter->idx >= iter->pg->index) {
2448 if (iter->pg->next) {
2449 iter->pg = iter->pg->next;
2450 iter->idx = 0;
2451 goto retry;
2452 }
2453 } else {
2454 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002455 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002456 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002457
Steven Rostedt41c52c02008-05-22 11:46:33 -04002458 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002459 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2460
2461 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002462 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002463
Steven Rostedt5072c592008-05-12 21:20:43 +02002464 rec = NULL;
2465 goto retry;
2466 }
2467 }
2468
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002469 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002470 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002471
2472 iter->func = rec;
2473
2474 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002475}
2476
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002477static void reset_iter_read(struct ftrace_iterator *iter)
2478{
2479 iter->pos = 0;
2480 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002481 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002482}
2483
2484static void *t_start(struct seq_file *m, loff_t *pos)
2485{
2486 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002487 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002488 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002489 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002490
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002491 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002492
2493 if (unlikely(ftrace_disabled))
2494 return NULL;
2495
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002496 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002497 * If an lseek was done, then reset and start from beginning.
2498 */
2499 if (*pos < iter->pos)
2500 reset_iter_read(iter);
2501
2502 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002503 * For set_ftrace_filter reading, if we have the filter
2504 * off, we can short cut and just print out that all
2505 * functions are enabled.
2506 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002507 if (iter->flags & FTRACE_ITER_FILTER &&
2508 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002509 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002510 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002511 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002512 /* reset in case of seek/pread */
2513 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002514 return iter;
2515 }
2516
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002517 if (iter->flags & FTRACE_ITER_HASH)
2518 return t_hash_start(m, pos);
2519
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002520 /*
2521 * Unfortunately, we need to restart at ftrace_pages_start
2522 * every time we let go of the ftrace_mutex. This is because
2523 * those pointers can change without the lock.
2524 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002525 iter->pg = ftrace_pages_start;
2526 iter->idx = 0;
2527 for (l = 0; l <= *pos; ) {
2528 p = t_next(m, p, &l);
2529 if (!p)
2530 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002531 }
walimis5821e1b2008-11-15 15:19:06 +08002532
Steven Rostedt69a30832011-12-19 15:21:16 -05002533 if (!p)
2534 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002535
2536 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002537}
2538
2539static void t_stop(struct seq_file *m, void *p)
2540{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002541 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002542}
2543
2544static int t_show(struct seq_file *m, void *v)
2545{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002546 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002547 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002548
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002549 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002550 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002551
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002552 if (iter->flags & FTRACE_ITER_PRINTALL) {
2553 seq_printf(m, "#### all functions enabled ####\n");
2554 return 0;
2555 }
2556
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002557 rec = iter->func;
2558
Steven Rostedt5072c592008-05-12 21:20:43 +02002559 if (!rec)
2560 return 0;
2561
Steven Rostedt647bcd02011-05-03 14:39:21 -04002562 seq_printf(m, "%ps", (void *)rec->ip);
2563 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002564 seq_printf(m, " (%ld)%s",
2565 rec->flags & ~FTRACE_FL_MASK,
2566 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002567 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002568
2569 return 0;
2570}
2571
James Morris88e9d342009-09-22 16:43:43 -07002572static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002573 .start = t_start,
2574 .next = t_next,
2575 .stop = t_stop,
2576 .show = t_show,
2577};
2578
Ingo Molnare309b412008-05-12 21:20:51 +02002579static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002580ftrace_avail_open(struct inode *inode, struct file *file)
2581{
2582 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002583
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002584 if (unlikely(ftrace_disabled))
2585 return -ENODEV;
2586
Jiri Olsa50e18b92012-04-25 10:23:39 +02002587 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2588 if (iter) {
2589 iter->pg = ftrace_pages_start;
2590 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002591 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002592
Jiri Olsa50e18b92012-04-25 10:23:39 +02002593 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002594}
2595
Steven Rostedt647bcd02011-05-03 14:39:21 -04002596static int
2597ftrace_enabled_open(struct inode *inode, struct file *file)
2598{
2599 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002600
2601 if (unlikely(ftrace_disabled))
2602 return -ENODEV;
2603
Jiri Olsa50e18b92012-04-25 10:23:39 +02002604 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2605 if (iter) {
2606 iter->pg = ftrace_pages_start;
2607 iter->flags = FTRACE_ITER_ENABLED;
2608 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002609 }
2610
Jiri Olsa50e18b92012-04-25 10:23:39 +02002611 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002612}
2613
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002614static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002615{
Steven Rostedt52baf112009-02-14 01:15:39 -05002616 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002617 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002618 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002619}
2620
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002621/**
2622 * ftrace_regex_open - initialize function tracer filter files
2623 * @ops: The ftrace_ops that hold the hash filters
2624 * @flag: The type of filter to process
2625 * @inode: The inode, usually passed in to your open routine
2626 * @file: The file, usually passed in to your open routine
2627 *
2628 * ftrace_regex_open() initializes the filter files for the
2629 * @ops. Depending on @flag it may process the filter hash or
2630 * the notrace hash of @ops. With this called from the open
2631 * routine, you can use ftrace_filter_write() for the write
2632 * routine if @flag has FTRACE_ITER_FILTER set, or
2633 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04002634 * ftrace_filter_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002635 * release must call ftrace_regex_release().
2636 */
2637int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002638ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002639 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002640{
2641 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002642 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002643 int ret = 0;
2644
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002645 ftrace_ops_init(ops);
2646
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002647 if (unlikely(ftrace_disabled))
2648 return -ENODEV;
2649
Steven Rostedt5072c592008-05-12 21:20:43 +02002650 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2651 if (!iter)
2652 return -ENOMEM;
2653
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002654 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2655 kfree(iter);
2656 return -ENOMEM;
2657 }
2658
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002659 iter->ops = ops;
2660 iter->flags = flag;
2661
2662 mutex_lock(&ops->regex_lock);
2663
Steven Rostedtf45948e2011-05-02 12:29:25 -04002664 if (flag & FTRACE_ITER_NOTRACE)
2665 hash = ops->notrace_hash;
2666 else
2667 hash = ops->filter_hash;
2668
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002669 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002670 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002671 if (!iter->hash) {
2672 trace_parser_put(&iter->parser);
2673 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002674 ret = -ENOMEM;
2675 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002676 }
2677 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002678
Steven Rostedt5072c592008-05-12 21:20:43 +02002679 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002680 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002681 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002682
2683 if (file->f_mode & FMODE_READ) {
2684 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002685
2686 ret = seq_open(file, &show_ftrace_seq_ops);
2687 if (!ret) {
2688 struct seq_file *m = file->private_data;
2689 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002690 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002691 /* Failed */
2692 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002693 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002694 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002695 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002696 } else
2697 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002698
2699 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002700 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002701
2702 return ret;
2703}
2704
Steven Rostedt41c52c02008-05-22 11:46:33 -04002705static int
2706ftrace_filter_open(struct inode *inode, struct file *file)
2707{
Steven Rostedt69a30832011-12-19 15:21:16 -05002708 return ftrace_regex_open(&global_ops,
2709 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2710 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002711}
2712
2713static int
2714ftrace_notrace_open(struct inode *inode, struct file *file)
2715{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002716 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002717 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002718}
2719
Steven Rostedt64e7c442009-02-13 17:08:48 -05002720static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002721{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002722 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002723 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002724
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002725 switch (type) {
2726 case MATCH_FULL:
2727 if (strcmp(str, regex) == 0)
2728 matched = 1;
2729 break;
2730 case MATCH_FRONT_ONLY:
2731 if (strncmp(str, regex, len) == 0)
2732 matched = 1;
2733 break;
2734 case MATCH_MIDDLE_ONLY:
2735 if (strstr(str, regex))
2736 matched = 1;
2737 break;
2738 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002739 slen = strlen(str);
2740 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002741 matched = 1;
2742 break;
2743 }
2744
2745 return matched;
2746}
2747
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002748static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002749enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002750{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002751 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002752 int ret = 0;
2753
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002754 entry = ftrace_lookup_ip(hash, rec->ip);
2755 if (not) {
2756 /* Do nothing if it doesn't exist */
2757 if (!entry)
2758 return 0;
2759
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002760 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002761 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002762 /* Do nothing if it exists */
2763 if (entry)
2764 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002765
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002766 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002767 }
2768 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002769}
2770
Steven Rostedt64e7c442009-02-13 17:08:48 -05002771static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002772ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2773 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002774{
2775 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002776 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002777
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002778 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2779
2780 if (mod) {
2781 /* module lookup requires matching the module */
2782 if (!modname || strcmp(modname, mod))
2783 return 0;
2784
2785 /* blank search means to match all funcs in the mod */
2786 if (!len)
2787 return 1;
2788 }
2789
Steven Rostedt64e7c442009-02-13 17:08:48 -05002790 return ftrace_match(str, regex, len, type);
2791}
2792
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002793static int
2794match_records(struct ftrace_hash *hash, char *buff,
2795 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002796{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002797 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002798 struct ftrace_page *pg;
2799 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002800 int type = MATCH_FULL;
2801 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002802 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002803 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002804
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002805 if (len) {
2806 type = filter_parse_regex(buff, len, &search, &not);
2807 search_len = strlen(search);
2808 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002809
Steven Rostedt52baf112009-02-14 01:15:39 -05002810 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002811
2812 if (unlikely(ftrace_disabled))
2813 goto out_unlock;
2814
Steven Rostedt265c8312009-02-13 12:43:56 -05002815 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002816 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002817 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002818 if (ret < 0) {
2819 found = ret;
2820 goto out_unlock;
2821 }
Li Zefan311d16d2009-12-08 11:15:11 +08002822 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002823 }
2824 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002825 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002826 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002827
2828 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002829}
2830
Steven Rostedt64e7c442009-02-13 17:08:48 -05002831static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002832ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002833{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002834 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002835}
2836
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002837static int
2838ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002839{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002840 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002841
Steven Rostedt64e7c442009-02-13 17:08:48 -05002842 /* blank or '*' mean the same */
2843 if (strcmp(buff, "*") == 0)
2844 buff[0] = 0;
2845
2846 /* handle the case of 'dont filter this module' */
2847 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2848 buff[0] = 0;
2849 not = 1;
2850 }
2851
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002852 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002853}
2854
Steven Rostedtf6180772009-02-14 00:40:25 -05002855/*
2856 * We register the module command as a template to show others how
2857 * to register the a command as well.
2858 */
2859
2860static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002861ftrace_mod_callback(struct ftrace_hash *hash,
2862 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002863{
2864 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002865 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002866
2867 /*
2868 * cmd == 'mod' because we only registered this func
2869 * for the 'mod' ftrace_func_command.
2870 * But if you register one func with multiple commands,
2871 * you can tell which command was used by the cmd
2872 * parameter.
2873 */
2874
2875 /* we must have a module name */
2876 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002877 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002878
2879 mod = strsep(&param, ":");
2880 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002881 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002882
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002883 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002884 if (!ret)
2885 ret = -EINVAL;
2886 if (ret < 0)
2887 return ret;
2888
2889 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002890}
2891
2892static struct ftrace_func_command ftrace_mod_cmd = {
2893 .name = "mod",
2894 .func = ftrace_mod_callback,
2895};
2896
2897static int __init ftrace_mod_cmd_init(void)
2898{
2899 return register_ftrace_command(&ftrace_mod_cmd);
2900}
Steven Rostedt6f415672012-10-05 12:13:07 -04002901core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002902
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002903static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002904 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002905{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002906 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002907 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002908 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002909
2910 key = hash_long(ip, FTRACE_HASH_BITS);
2911
2912 hhd = &ftrace_func_hash[key];
2913
2914 if (hlist_empty(hhd))
2915 return;
2916
2917 /*
2918 * Disable preemption for these calls to prevent a RCU grace
2919 * period. This syncs the hash iteration and freeing of items
2920 * on the hash. rcu_read_lock is too dangerous here.
2921 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002922 preempt_disable_notrace();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002923 hlist_for_each_entry_rcu(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002924 if (entry->ip == ip)
2925 entry->ops->func(ip, parent_ip, &entry->data);
2926 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002927 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002928}
2929
Steven Rostedtb6887d72009-02-17 12:32:04 -05002930static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002931{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002932 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002933 .flags = FTRACE_OPS_FL_INITIALIZED,
2934 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002935};
2936
Steven Rostedtb6887d72009-02-17 12:32:04 -05002937static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002938
Steven Rostedtb6887d72009-02-17 12:32:04 -05002939static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002940{
Steven Rostedtb8489142011-05-04 09:27:52 -04002941 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002942 int i;
2943
Steven Rostedtb6887d72009-02-17 12:32:04 -05002944 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002945 return;
2946
2947 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2948 struct hlist_head *hhd = &ftrace_func_hash[i];
2949 if (hhd->first)
2950 break;
2951 }
2952 /* Nothing registered? */
2953 if (i == FTRACE_FUNC_HASHSIZE)
2954 return;
2955
Steven Rostedtb8489142011-05-04 09:27:52 -04002956 ret = __register_ftrace_function(&trace_probe_ops);
2957 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002958 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002959
Steven Rostedtb6887d72009-02-17 12:32:04 -05002960 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002961}
2962
Steven Rostedtb6887d72009-02-17 12:32:04 -05002963static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002964{
Steven Rostedtb8489142011-05-04 09:27:52 -04002965 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002966 int i;
2967
Steven Rostedtb6887d72009-02-17 12:32:04 -05002968 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002969 return;
2970
2971 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2972 struct hlist_head *hhd = &ftrace_func_hash[i];
2973 if (hhd->first)
2974 return;
2975 }
2976
2977 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002978 ret = __unregister_ftrace_function(&trace_probe_ops);
2979 if (!ret)
2980 ftrace_shutdown(&trace_probe_ops, 0);
2981
Steven Rostedtb6887d72009-02-17 12:32:04 -05002982 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002983}
2984
2985
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04002986static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002987{
Steven Rostedt59df055f2009-02-14 15:29:06 -05002988 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04002989 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002990 kfree(entry);
2991}
2992
Steven Rostedt59df055f2009-02-14 15:29:06 -05002993int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002994register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002995 void *data)
2996{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002997 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04002998 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
2999 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003000 struct ftrace_page *pg;
3001 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003002 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003003 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003004 int count = 0;
3005 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003006 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003007
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003008 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003009 len = strlen(search);
3010
Steven Rostedtb6887d72009-02-17 12:32:04 -05003011 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003012 if (WARN_ON(not))
3013 return -EINVAL;
3014
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003015 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003016
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003017 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3018 if (!hash) {
3019 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003020 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003021 }
3022
3023 if (unlikely(ftrace_disabled)) {
3024 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003025 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003026 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003027
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003028 mutex_lock(&ftrace_lock);
3029
Steven Rostedt59df055f2009-02-14 15:29:06 -05003030 do_for_each_ftrace_rec(pg, rec) {
3031
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003032 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003033 continue;
3034
3035 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3036 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003037 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003038 if (!count)
3039 count = -ENOMEM;
3040 goto out_unlock;
3041 }
3042
3043 count++;
3044
3045 entry->data = data;
3046
3047 /*
3048 * The caller might want to do something special
3049 * for each function we find. We call the callback
3050 * to give the caller an opportunity to do so.
3051 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003052 if (ops->init) {
3053 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003054 /* caller does not like this func */
3055 kfree(entry);
3056 continue;
3057 }
3058 }
3059
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003060 ret = enter_record(hash, rec, 0);
3061 if (ret < 0) {
3062 kfree(entry);
3063 count = ret;
3064 goto out_unlock;
3065 }
3066
Steven Rostedt59df055f2009-02-14 15:29:06 -05003067 entry->ops = ops;
3068 entry->ip = rec->ip;
3069
3070 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3071 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3072
3073 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003074
3075 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3076 if (ret < 0)
3077 count = ret;
3078
Steven Rostedtb6887d72009-02-17 12:32:04 -05003079 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003080
3081 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003082 mutex_unlock(&ftrace_lock);
3083 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003084 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003085 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003086
3087 return count;
3088}
3089
3090enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003091 PROBE_TEST_FUNC = 1,
3092 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003093};
3094
3095static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003096__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003097 void *data, int flags)
3098{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003099 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003100 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003101 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003102 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003103 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003104 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003105 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003106 char str[KSYM_SYMBOL_LEN];
3107 int type = MATCH_FULL;
3108 int i, len = 0;
3109 char *search;
3110
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003111 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003112 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003113 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003114 int not;
3115
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003116 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003117 len = strlen(search);
3118
Steven Rostedtb6887d72009-02-17 12:32:04 -05003119 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003120 if (WARN_ON(not))
3121 return;
3122 }
3123
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003124 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003125
3126 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3127 if (!hash)
3128 /* Hmm, should report this somehow */
3129 goto out_unlock;
3130
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003131 INIT_LIST_HEAD(&free_list);
3132
Steven Rostedt59df055f2009-02-14 15:29:06 -05003133 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3134 struct hlist_head *hhd = &ftrace_func_hash[i];
3135
Sasha Levinb67bfe02013-02-27 17:06:00 -08003136 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003137
3138 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003139 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003140 continue;
3141
Steven Rostedtb6887d72009-02-17 12:32:04 -05003142 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003143 continue;
3144
3145 /* do this last, since it is the most expensive */
3146 if (glob) {
3147 kallsyms_lookup(entry->ip, NULL, NULL,
3148 NULL, str);
3149 if (!ftrace_match(str, glob, len, type))
3150 continue;
3151 }
3152
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003153 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3154 /* It is possible more than one entry had this ip */
3155 if (rec_entry)
3156 free_hash_entry(hash, rec_entry);
3157
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003158 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003159 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003160 }
3161 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003162 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003163 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003164 /*
3165 * Remove after the disable is called. Otherwise, if the last
3166 * probe is removed, a null hash means *all enabled*.
3167 */
3168 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003169 synchronize_sched();
3170 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3171 list_del(&entry->free_list);
3172 ftrace_free_entry(entry);
3173 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003174 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003175
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003176 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003177 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003178 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003179}
3180
3181void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003182unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003183 void *data)
3184{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003185 __unregister_ftrace_function_probe(glob, ops, data,
3186 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003187}
3188
3189void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003190unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003191{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003192 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003193}
3194
Steven Rostedtb6887d72009-02-17 12:32:04 -05003195void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003196{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003197 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003198}
3199
Steven Rostedtf6180772009-02-14 00:40:25 -05003200static LIST_HEAD(ftrace_commands);
3201static DEFINE_MUTEX(ftrace_cmd_mutex);
3202
3203int register_ftrace_command(struct ftrace_func_command *cmd)
3204{
3205 struct ftrace_func_command *p;
3206 int ret = 0;
3207
3208 mutex_lock(&ftrace_cmd_mutex);
3209 list_for_each_entry(p, &ftrace_commands, list) {
3210 if (strcmp(cmd->name, p->name) == 0) {
3211 ret = -EBUSY;
3212 goto out_unlock;
3213 }
3214 }
3215 list_add(&cmd->list, &ftrace_commands);
3216 out_unlock:
3217 mutex_unlock(&ftrace_cmd_mutex);
3218
3219 return ret;
3220}
3221
3222int unregister_ftrace_command(struct ftrace_func_command *cmd)
3223{
3224 struct ftrace_func_command *p, *n;
3225 int ret = -ENODEV;
3226
3227 mutex_lock(&ftrace_cmd_mutex);
3228 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3229 if (strcmp(cmd->name, p->name) == 0) {
3230 ret = 0;
3231 list_del_init(&p->list);
3232 goto out_unlock;
3233 }
3234 }
3235 out_unlock:
3236 mutex_unlock(&ftrace_cmd_mutex);
3237
3238 return ret;
3239}
3240
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003241static int ftrace_process_regex(struct ftrace_hash *hash,
3242 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003243{
Steven Rostedtf6180772009-02-14 00:40:25 -05003244 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003245 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003246 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003247
3248 func = strsep(&next, ":");
3249
3250 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003251 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003252 if (!ret)
3253 ret = -EINVAL;
3254 if (ret < 0)
3255 return ret;
3256 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003257 }
3258
Steven Rostedtf6180772009-02-14 00:40:25 -05003259 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003260
3261 command = strsep(&next, ":");
3262
Steven Rostedtf6180772009-02-14 00:40:25 -05003263 mutex_lock(&ftrace_cmd_mutex);
3264 list_for_each_entry(p, &ftrace_commands, list) {
3265 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003266 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003267 goto out_unlock;
3268 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003269 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003270 out_unlock:
3271 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003272
Steven Rostedtf6180772009-02-14 00:40:25 -05003273 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003274}
3275
Ingo Molnare309b412008-05-12 21:20:51 +02003276static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003277ftrace_regex_write(struct file *file, const char __user *ubuf,
3278 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003279{
3280 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003281 struct trace_parser *parser;
3282 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003283
Li Zefan4ba79782009-09-22 13:52:20 +08003284 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003285 return 0;
3286
Steven Rostedt5072c592008-05-12 21:20:43 +02003287 if (file->f_mode & FMODE_READ) {
3288 struct seq_file *m = file->private_data;
3289 iter = m->private;
3290 } else
3291 iter = file->private_data;
3292
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003293 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003294 return -ENODEV;
3295
3296 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003297
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003298 parser = &iter->parser;
3299 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003300
Li Zefan4ba79782009-09-22 13:52:20 +08003301 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003302 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003303 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003304 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003305 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003306 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003307 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003308 }
3309
Steven Rostedt5072c592008-05-12 21:20:43 +02003310 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003311 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003312 return ret;
3313}
3314
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003315ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003316ftrace_filter_write(struct file *file, const char __user *ubuf,
3317 size_t cnt, loff_t *ppos)
3318{
3319 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3320}
3321
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003322ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003323ftrace_notrace_write(struct file *file, const char __user *ubuf,
3324 size_t cnt, loff_t *ppos)
3325{
3326 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3327}
3328
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003329static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003330ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3331{
3332 struct ftrace_func_entry *entry;
3333
3334 if (!ftrace_location(ip))
3335 return -EINVAL;
3336
3337 if (remove) {
3338 entry = ftrace_lookup_ip(hash, ip);
3339 if (!entry)
3340 return -ENOENT;
3341 free_hash_entry(hash, entry);
3342 return 0;
3343 }
3344
3345 return add_hash_entry(hash, ip);
3346}
3347
3348static int
3349ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3350 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003351{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003352 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003353 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003354 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003355
Steven Rostedt936e0742011-05-05 22:54:01 -04003356 /* All global ops uses the global ops filters */
3357 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3358 ops = &global_ops;
3359
Steven Rostedt41c52c02008-05-22 11:46:33 -04003360 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003361 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003362
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003363 mutex_lock(&ops->regex_lock);
3364
Steven Rostedtf45948e2011-05-02 12:29:25 -04003365 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003366 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003367 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003368 orig_hash = &ops->notrace_hash;
3369
3370 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003371 if (!hash) {
3372 ret = -ENOMEM;
3373 goto out_regex_unlock;
3374 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003375
Steven Rostedt41c52c02008-05-22 11:46:33 -04003376 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003377 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003378 if (buf && !ftrace_match_records(hash, buf, len)) {
3379 ret = -EINVAL;
3380 goto out_regex_unlock;
3381 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003382 if (ip) {
3383 ret = ftrace_match_addr(hash, ip, remove);
3384 if (ret < 0)
3385 goto out_regex_unlock;
3386 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003387
3388 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003389 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003390 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3391 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003392 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003393
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003394 mutex_unlock(&ftrace_lock);
3395
Jiri Olsaac483c42012-01-02 10:04:14 +01003396 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003397 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003398
3399 free_ftrace_hash(hash);
3400 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003401}
3402
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003403static int
3404ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3405 int reset, int enable)
3406{
3407 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3408}
3409
3410/**
3411 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3412 * @ops - the ops to set the filter with
3413 * @ip - the address to add to or remove from the filter.
3414 * @remove - non zero to remove the ip from the filter
3415 * @reset - non zero to reset all filters before applying this filter.
3416 *
3417 * Filters denote which functions should be enabled when tracing is enabled
3418 * If @ip is NULL, it failes to update filter.
3419 */
3420int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3421 int remove, int reset)
3422{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003423 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003424 return ftrace_set_addr(ops, ip, remove, reset, 1);
3425}
3426EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3427
3428static int
3429ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3430 int reset, int enable)
3431{
3432 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3433}
3434
Steven Rostedt77a2b372008-05-12 21:20:45 +02003435/**
3436 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003437 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003438 * @buf - the string that holds the function filter text.
3439 * @len - the length of the string.
3440 * @reset - non zero to reset all filters before applying this filter.
3441 *
3442 * Filters denote which functions should be enabled when tracing is enabled.
3443 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3444 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003445int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003446 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003447{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003448 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003449 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003450}
Steven Rostedt936e0742011-05-05 22:54:01 -04003451EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003452
Steven Rostedt41c52c02008-05-22 11:46:33 -04003453/**
3454 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003455 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003456 * @buf - the string that holds the function notrace text.
3457 * @len - the length of the string.
3458 * @reset - non zero to reset all filters before applying this filter.
3459 *
3460 * Notrace Filters denote which functions should not be enabled when tracing
3461 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3462 * for tracing.
3463 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003464int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003465 int len, int reset)
3466{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003467 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003468 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003469}
3470EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3471/**
3472 * ftrace_set_filter - set a function to filter on in ftrace
3473 * @ops - the ops to set the filter with
3474 * @buf - the string that holds the function filter text.
3475 * @len - the length of the string.
3476 * @reset - non zero to reset all filters before applying this filter.
3477 *
3478 * Filters denote which functions should be enabled when tracing is enabled.
3479 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3480 */
3481void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3482{
3483 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3484}
3485EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3486
3487/**
3488 * ftrace_set_notrace - set a function to not trace in ftrace
3489 * @ops - the ops to set the notrace filter with
3490 * @buf - the string that holds the function notrace text.
3491 * @len - the length of the string.
3492 * @reset - non zero to reset all filters before applying this filter.
3493 *
3494 * Notrace Filters denote which functions should not be enabled when tracing
3495 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3496 * for tracing.
3497 */
3498void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003499{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003500 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003501}
Steven Rostedt936e0742011-05-05 22:54:01 -04003502EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003503
Steven Rostedt2af15d62009-05-28 13:37:24 -04003504/*
3505 * command line interface to allow users to set filters on boot up.
3506 */
3507#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3508static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3509static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3510
3511static int __init set_ftrace_notrace(char *str)
3512{
Chen Gang75761cc2013-04-08 12:12:39 +08003513 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003514 return 1;
3515}
3516__setup("ftrace_notrace=", set_ftrace_notrace);
3517
3518static int __init set_ftrace_filter(char *str)
3519{
Chen Gang75761cc2013-04-08 12:12:39 +08003520 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003521 return 1;
3522}
3523__setup("ftrace_filter=", set_ftrace_filter);
3524
Stefan Assmann369bc182009-10-12 22:17:21 +02003525#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003526static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003527static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3528
Stefan Assmann369bc182009-10-12 22:17:21 +02003529static int __init set_graph_function(char *str)
3530{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003531 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003532 return 1;
3533}
3534__setup("ftrace_graph_filter=", set_graph_function);
3535
3536static void __init set_ftrace_early_graph(char *buf)
3537{
3538 int ret;
3539 char *func;
3540
3541 while (buf) {
3542 func = strsep(&buf, ",");
3543 /* we allow only one expression at a time */
3544 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3545 func);
3546 if (ret)
3547 printk(KERN_DEBUG "ftrace: function %s not "
3548 "traceable\n", func);
3549 }
3550}
3551#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3552
Steven Rostedt2a85a372011-12-19 21:57:44 -05003553void __init
3554ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003555{
3556 char *func;
3557
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003558 ftrace_ops_init(ops);
3559
Steven Rostedt2af15d62009-05-28 13:37:24 -04003560 while (buf) {
3561 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003562 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003563 }
3564}
3565
3566static void __init set_ftrace_early_filters(void)
3567{
3568 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003569 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003570 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003571 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003572#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3573 if (ftrace_graph_buf[0])
3574 set_ftrace_early_graph(ftrace_graph_buf);
3575#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003576}
3577
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003578int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003579{
3580 struct seq_file *m = (struct seq_file *)file->private_data;
3581 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003582 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003583 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003584 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003585 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003586
Steven Rostedt5072c592008-05-12 21:20:43 +02003587 if (file->f_mode & FMODE_READ) {
3588 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003589 seq_release(inode, file);
3590 } else
3591 iter = file->private_data;
3592
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003593 parser = &iter->parser;
3594 if (trace_parser_loaded(parser)) {
3595 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003596 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003597 }
3598
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003599 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003600
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003601 mutex_lock(&iter->ops->regex_lock);
3602
Steven Rostedt058e2972011-04-29 22:35:33 -04003603 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003604 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3605
3606 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003607 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003608 else
3609 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003610
Steven Rostedt058e2972011-04-29 22:35:33 -04003611 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003612 ret = ftrace_hash_move(iter->ops, filter_hash,
3613 orig_hash, iter->hash);
3614 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3615 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003616 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003617
Steven Rostedt058e2972011-04-29 22:35:33 -04003618 mutex_unlock(&ftrace_lock);
3619 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003620
3621 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003622 free_ftrace_hash(iter->hash);
3623 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003624
Steven Rostedt5072c592008-05-12 21:20:43 +02003625 return 0;
3626}
3627
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003628static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003629 .open = ftrace_avail_open,
3630 .read = seq_read,
3631 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003632 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003633};
3634
Steven Rostedt647bcd02011-05-03 14:39:21 -04003635static const struct file_operations ftrace_enabled_fops = {
3636 .open = ftrace_enabled_open,
3637 .read = seq_read,
3638 .llseek = seq_lseek,
3639 .release = seq_release_private,
3640};
3641
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003642static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003643 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003644 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003645 .write = ftrace_filter_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003646 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003647 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003648};
3649
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003650static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003651 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003652 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003653 .write = ftrace_notrace_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003654 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003655 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003656};
3657
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003658#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3659
3660static DEFINE_MUTEX(graph_lock);
3661
3662int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003663int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003664unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3665
3666static void *
Li Zefan85951842009-06-24 09:54:00 +08003667__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003668{
Li Zefan85951842009-06-24 09:54:00 +08003669 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003670 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003671 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003672}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003673
Li Zefan85951842009-06-24 09:54:00 +08003674static void *
3675g_next(struct seq_file *m, void *v, loff_t *pos)
3676{
3677 (*pos)++;
3678 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003679}
3680
3681static void *g_start(struct seq_file *m, loff_t *pos)
3682{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003683 mutex_lock(&graph_lock);
3684
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003685 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003686 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003687 return (void *)1;
3688
Li Zefan85951842009-06-24 09:54:00 +08003689 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003690}
3691
3692static void g_stop(struct seq_file *m, void *p)
3693{
3694 mutex_unlock(&graph_lock);
3695}
3696
3697static int g_show(struct seq_file *m, void *v)
3698{
3699 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003700
3701 if (!ptr)
3702 return 0;
3703
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003704 if (ptr == (unsigned long *)1) {
3705 seq_printf(m, "#### all functions enabled ####\n");
3706 return 0;
3707 }
3708
Steven Rostedtb375a112009-09-17 00:05:58 -04003709 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003710
3711 return 0;
3712}
3713
James Morris88e9d342009-09-22 16:43:43 -07003714static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003715 .start = g_start,
3716 .next = g_next,
3717 .stop = g_stop,
3718 .show = g_show,
3719};
3720
3721static int
3722ftrace_graph_open(struct inode *inode, struct file *file)
3723{
3724 int ret = 0;
3725
3726 if (unlikely(ftrace_disabled))
3727 return -ENODEV;
3728
3729 mutex_lock(&graph_lock);
3730 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003731 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003732 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003733 ftrace_graph_count = 0;
3734 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3735 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003736 mutex_unlock(&graph_lock);
3737
Li Zefana4ec5e02009-09-18 14:06:28 +08003738 if (file->f_mode & FMODE_READ)
3739 ret = seq_open(file, &ftrace_graph_seq_ops);
3740
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003741 return ret;
3742}
3743
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003744static int
Li Zefan87827112009-07-23 11:29:11 +08003745ftrace_graph_release(struct inode *inode, struct file *file)
3746{
3747 if (file->f_mode & FMODE_READ)
3748 seq_release(inode, file);
3749 return 0;
3750}
3751
3752static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003753ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003754{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003755 struct dyn_ftrace *rec;
3756 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003757 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003758 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003759 int type, not;
3760 char *search;
3761 bool exists;
3762 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003763
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003764 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003765 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003766 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3767 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003768
3769 search_len = strlen(search);
3770
Steven Rostedt52baf112009-02-14 01:15:39 -05003771 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003772
3773 if (unlikely(ftrace_disabled)) {
3774 mutex_unlock(&ftrace_lock);
3775 return -ENODEV;
3776 }
3777
Steven Rostedt265c8312009-02-13 12:43:56 -05003778 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003779
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003780 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003781 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003782 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003783 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003784 if (array[i] == rec->ip) {
3785 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003786 break;
3787 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003788 }
3789
3790 if (!not) {
3791 fail = 0;
3792 if (!exists) {
3793 array[(*idx)++] = rec->ip;
3794 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3795 goto out;
3796 }
3797 } else {
3798 if (exists) {
3799 array[i] = array[--(*idx)];
3800 array[*idx] = 0;
3801 fail = 0;
3802 }
3803 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003804 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003805 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003806out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003807 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003808
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003809 if (fail)
3810 return -EINVAL;
3811
Namhyung Kim9f50afc2013-04-11 16:01:38 +09003812 ftrace_graph_filter_enabled = !!(*idx);
3813
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003814 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003815}
3816
3817static ssize_t
3818ftrace_graph_write(struct file *file, const char __user *ubuf,
3819 size_t cnt, loff_t *ppos)
3820{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003821 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003822 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003823
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003824 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003825 return 0;
3826
3827 mutex_lock(&graph_lock);
3828
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003829 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3830 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003831 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003832 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003833
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003834 read = trace_get_user(&parser, ubuf, cnt, ppos);
3835
Li Zefan4ba79782009-09-22 13:52:20 +08003836 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003837 parser.buffer[parser.idx] = 0;
3838
3839 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003840 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003841 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003842 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003843 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003844 }
3845
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003846 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003847
3848out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003849 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003850out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003851 mutex_unlock(&graph_lock);
3852
3853 return ret;
3854}
3855
3856static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003857 .open = ftrace_graph_open,
3858 .read = seq_read,
3859 .write = ftrace_graph_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003860 .llseek = ftrace_filter_lseek,
Li Zefan87827112009-07-23 11:29:11 +08003861 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003862};
3863#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3864
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003865static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003866{
Steven Rostedt5072c592008-05-12 21:20:43 +02003867
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003868 trace_create_file("available_filter_functions", 0444,
3869 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003870
Steven Rostedt647bcd02011-05-03 14:39:21 -04003871 trace_create_file("enabled_functions", 0444,
3872 d_tracer, NULL, &ftrace_enabled_fops);
3873
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003874 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3875 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003876
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003877 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003878 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003879
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003880#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003881 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003882 NULL,
3883 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003884#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3885
Steven Rostedt5072c592008-05-12 21:20:43 +02003886 return 0;
3887}
3888
Steven Rostedt9fd49322012-04-24 22:32:06 -04003889static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003890{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003891 const unsigned long *ipa = a;
3892 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003893
Steven Rostedt9fd49322012-04-24 22:32:06 -04003894 if (*ipa > *ipb)
3895 return 1;
3896 if (*ipa < *ipb)
3897 return -1;
3898 return 0;
3899}
3900
3901static void ftrace_swap_ips(void *a, void *b, int size)
3902{
3903 unsigned long *ipa = a;
3904 unsigned long *ipb = b;
3905 unsigned long t;
3906
3907 t = *ipa;
3908 *ipa = *ipb;
3909 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003910}
3911
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003912static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003913 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003914 unsigned long *end)
3915{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003916 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003917 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003918 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003919 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003920 unsigned long *p;
3921 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003922 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003923 int ret = -ENOMEM;
3924
3925 count = end - start;
3926
3927 if (!count)
3928 return 0;
3929
Steven Rostedt9fd49322012-04-24 22:32:06 -04003930 sort(start, count, sizeof(*start),
3931 ftrace_cmp_ips, ftrace_swap_ips);
3932
Steven Rostedt706c81f2012-04-24 23:45:26 -04003933 start_pg = ftrace_allocate_pages(count);
3934 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05003935 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003936
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003937 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003938
Steven Rostedt32082302011-12-16 14:42:37 -05003939 /*
3940 * Core and each module needs their own pages, as
3941 * modules will free them when they are removed.
3942 * Force a new page to be allocated for modules.
3943 */
Steven Rostedta7900872011-12-16 16:23:44 -05003944 if (!mod) {
3945 WARN_ON(ftrace_pages || ftrace_pages_start);
3946 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003947 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003948 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003949 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003950 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003951
Steven Rostedta7900872011-12-16 16:23:44 -05003952 if (WARN_ON(ftrace_pages->next)) {
3953 /* Hmm, we have free pages? */
3954 while (ftrace_pages->next)
3955 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003956 }
Steven Rostedta7900872011-12-16 16:23:44 -05003957
Steven Rostedt706c81f2012-04-24 23:45:26 -04003958 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003959 }
3960
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003961 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003962 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003963 while (p < end) {
3964 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003965 /*
3966 * Some architecture linkers will pad between
3967 * the different mcount_loc sections of different
3968 * object files to satisfy alignments.
3969 * Skip any NULL pointers.
3970 */
3971 if (!addr)
3972 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003973
3974 if (pg->index == pg->size) {
3975 /* We should have allocated enough */
3976 if (WARN_ON(!pg->next))
3977 break;
3978 pg = pg->next;
3979 }
3980
3981 rec = &pg->records[pg->index++];
3982 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003983 }
3984
Steven Rostedt706c81f2012-04-24 23:45:26 -04003985 /* We should have used all pages */
3986 WARN_ON(pg->next);
3987
3988 /* Assign the last page to ftrace_pages */
3989 ftrace_pages = pg;
3990
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003991 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003992 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003993
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003994 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003995 * We only need to disable interrupts on start up
3996 * because we are modifying code that an interrupt
3997 * may execute, and the modification is not atomic.
3998 * But for modules, nothing runs the code we modify
3999 * until we are finished with it, and there's no
4000 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004001 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004002 if (!mod)
4003 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08004004 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004005 if (!mod)
4006 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004007 ret = 0;
4008 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004009 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004010
Steven Rostedta7900872011-12-16 16:23:44 -05004011 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004012}
4013
Steven Rostedt93eb6772009-04-15 13:24:06 -04004014#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004015
4016#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4017
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004018void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004019{
4020 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004021 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004022 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004023 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004024
Steven Rostedt93eb6772009-04-15 13:24:06 -04004025 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004026
4027 if (ftrace_disabled)
4028 goto out_unlock;
4029
Steven Rostedt32082302011-12-16 14:42:37 -05004030 /*
4031 * Each module has its own ftrace_pages, remove
4032 * them from the list.
4033 */
4034 last_pg = &ftrace_pages_start;
4035 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4036 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004037 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004038 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004039 * As core pages are first, the first
4040 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004041 */
Steven Rostedt32082302011-12-16 14:42:37 -05004042 if (WARN_ON(pg == ftrace_pages_start))
4043 goto out_unlock;
4044
4045 /* Check if we are deleting the last page */
4046 if (pg == ftrace_pages)
4047 ftrace_pages = next_to_ftrace_page(last_pg);
4048
4049 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004050 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4051 free_pages((unsigned long)pg->records, order);
4052 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004053 } else
4054 last_pg = &pg->next;
4055 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004056 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004057 mutex_unlock(&ftrace_lock);
4058}
4059
4060static void ftrace_init_module(struct module *mod,
4061 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004062{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004063 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004064 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004065 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004066}
4067
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004068static int ftrace_module_notify_enter(struct notifier_block *self,
4069 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004070{
4071 struct module *mod = data;
4072
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004073 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004074 ftrace_init_module(mod, mod->ftrace_callsites,
4075 mod->ftrace_callsites +
4076 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004077 return 0;
4078}
4079
4080static int ftrace_module_notify_exit(struct notifier_block *self,
4081 unsigned long val, void *data)
4082{
4083 struct module *mod = data;
4084
4085 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004086 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004087
4088 return 0;
4089}
4090#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004091static int ftrace_module_notify_enter(struct notifier_block *self,
4092 unsigned long val, void *data)
4093{
4094 return 0;
4095}
4096static int ftrace_module_notify_exit(struct notifier_block *self,
4097 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004098{
4099 return 0;
4100}
4101#endif /* CONFIG_MODULES */
4102
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004103struct notifier_block ftrace_module_enter_nb = {
4104 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004105 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004106};
4107
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004108struct notifier_block ftrace_module_exit_nb = {
4109 .notifier_call = ftrace_module_notify_exit,
4110 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4111};
4112
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004113extern unsigned long __start_mcount_loc[];
4114extern unsigned long __stop_mcount_loc[];
4115
4116void __init ftrace_init(void)
4117{
4118 unsigned long count, addr, flags;
4119 int ret;
4120
4121 /* Keep the ftrace pointer to the stub */
4122 addr = (unsigned long)ftrace_stub;
4123
4124 local_irq_save(flags);
4125 ftrace_dyn_arch_init(&addr);
4126 local_irq_restore(flags);
4127
4128 /* ftrace_dyn_arch_init places the return code in addr */
4129 if (addr)
4130 goto failed;
4131
4132 count = __stop_mcount_loc - __start_mcount_loc;
4133
4134 ret = ftrace_dyn_table_alloc(count);
4135 if (ret)
4136 goto failed;
4137
4138 last_ftrace_enabled = ftrace_enabled = 1;
4139
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004140 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004141 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004142 __stop_mcount_loc);
4143
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004144 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004145 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004146 pr_warning("Failed to register trace ftrace module enter notifier\n");
4147
4148 ret = register_module_notifier(&ftrace_module_exit_nb);
4149 if (ret)
4150 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004151
Steven Rostedt2af15d62009-05-28 13:37:24 -04004152 set_ftrace_early_filters();
4153
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004154 return;
4155 failed:
4156 ftrace_disabled = 1;
4157}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004158
Steven Rostedt3d083392008-05-12 21:20:42 +02004159#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004160
Steven Rostedt2b499382011-05-03 22:49:52 -04004161static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004162 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004163 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4164 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004165};
4166
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004167static int __init ftrace_nodyn_init(void)
4168{
4169 ftrace_enabled = 1;
4170 return 0;
4171}
Steven Rostedt6f415672012-10-05 12:13:07 -04004172core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004173
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004174static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4175static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004176/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004177# define ftrace_startup(ops, command) \
4178 ({ \
4179 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4180 0; \
4181 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04004182# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004183# define ftrace_startup_sysctl() do { } while (0)
4184# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004185
4186static inline int
4187ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4188{
4189 return 1;
4190}
4191
Steven Rostedt3d083392008-05-12 21:20:42 +02004192#endif /* CONFIG_DYNAMIC_FTRACE */
4193
Steven Rostedtb8489142011-05-04 09:27:52 -04004194static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004195ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004196 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004197{
Jiri Olsae2484912012-02-15 15:51:48 +01004198 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4199 return;
4200
4201 /*
4202 * Some of the ops may be dynamically allocated,
4203 * they must be freed after a synchronize_sched().
4204 */
4205 preempt_disable_notrace();
4206 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt0a016402012-11-02 17:03:03 -04004207 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004208 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4209 !ftrace_function_local_disabled(op) &&
Jiri Olsae2484912012-02-15 15:51:48 +01004210 ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004211 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004212 } while_for_each_ftrace_op(op);
Jiri Olsae2484912012-02-15 15:51:48 +01004213 trace_recursion_clear(TRACE_CONTROL_BIT);
4214 preempt_enable_notrace();
4215}
4216
4217static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004218 .func = ftrace_ops_control_func,
4219 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4220 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004221};
4222
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004223static inline void
4224__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004225 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004226{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004227 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004228 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004229
Steven Rostedtccf36722012-06-05 09:44:25 -04004230 if (function_trace_stop)
4231 return;
4232
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004233 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4234 if (bit < 0)
4235 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004236
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004237 /*
4238 * Some of the ops may be dynamically allocated,
4239 * they must be freed after a synchronize_sched().
4240 */
4241 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004242 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedtb8489142011-05-04 09:27:52 -04004243 if (ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004244 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004245 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004246 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004247 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004248}
4249
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004250/*
4251 * Some archs only support passing ip and parent_ip. Even though
4252 * the list function ignores the op parameter, we do not want any
4253 * C side effects, where a function is called without the caller
4254 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004255 * Archs are to support both the regs and ftrace_ops at the same time.
4256 * If they support ftrace_ops, it is assumed they support regs.
4257 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004258 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4259 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004260 * An architecture can pass partial regs with ftrace_ops and still
4261 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004262 */
4263#if ARCH_SUPPORTS_FTRACE_OPS
4264static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004265 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004266{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004267 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004268}
4269#else
4270static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4271{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004272 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004273}
4274#endif
4275
Steven Rostedte32d8952008-12-04 00:26:41 -05004276static void clear_ftrace_swapper(void)
4277{
4278 struct task_struct *p;
4279 int cpu;
4280
4281 get_online_cpus();
4282 for_each_online_cpu(cpu) {
4283 p = idle_task(cpu);
4284 clear_tsk_trace_trace(p);
4285 }
4286 put_online_cpus();
4287}
4288
4289static void set_ftrace_swapper(void)
4290{
4291 struct task_struct *p;
4292 int cpu;
4293
4294 get_online_cpus();
4295 for_each_online_cpu(cpu) {
4296 p = idle_task(cpu);
4297 set_tsk_trace_trace(p);
4298 }
4299 put_online_cpus();
4300}
4301
4302static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004303{
4304 struct task_struct *p;
4305
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004306 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004307 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004308 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004309 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004310 rcu_read_unlock();
4311
Steven Rostedte32d8952008-12-04 00:26:41 -05004312 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004313}
4314
Steven Rostedte32d8952008-12-04 00:26:41 -05004315static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004316{
4317 struct task_struct *p;
4318
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004319 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004320 do_each_pid_task(pid, PIDTYPE_PID, p) {
4321 set_tsk_trace_trace(p);
4322 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004323 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004324}
4325
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004326static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004327{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004328 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004329 clear_ftrace_swapper();
4330 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004331 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004332}
4333
4334static void set_ftrace_pid_task(struct pid *pid)
4335{
4336 if (pid == ftrace_swapper_pid)
4337 set_ftrace_swapper();
4338 else
4339 set_ftrace_pid(pid);
4340}
4341
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004342static int ftrace_pid_add(int p)
4343{
4344 struct pid *pid;
4345 struct ftrace_pid *fpid;
4346 int ret = -EINVAL;
4347
4348 mutex_lock(&ftrace_lock);
4349
4350 if (!p)
4351 pid = ftrace_swapper_pid;
4352 else
4353 pid = find_get_pid(p);
4354
4355 if (!pid)
4356 goto out;
4357
4358 ret = 0;
4359
4360 list_for_each_entry(fpid, &ftrace_pids, list)
4361 if (fpid->pid == pid)
4362 goto out_put;
4363
4364 ret = -ENOMEM;
4365
4366 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4367 if (!fpid)
4368 goto out_put;
4369
4370 list_add(&fpid->list, &ftrace_pids);
4371 fpid->pid = pid;
4372
4373 set_ftrace_pid_task(pid);
4374
4375 ftrace_update_pid_func();
4376 ftrace_startup_enable(0);
4377
4378 mutex_unlock(&ftrace_lock);
4379 return 0;
4380
4381out_put:
4382 if (pid != ftrace_swapper_pid)
4383 put_pid(pid);
4384
4385out:
4386 mutex_unlock(&ftrace_lock);
4387 return ret;
4388}
4389
4390static void ftrace_pid_reset(void)
4391{
4392 struct ftrace_pid *fpid, *safe;
4393
4394 mutex_lock(&ftrace_lock);
4395 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4396 struct pid *pid = fpid->pid;
4397
4398 clear_ftrace_pid_task(pid);
4399
4400 list_del(&fpid->list);
4401 kfree(fpid);
4402 }
4403
4404 ftrace_update_pid_func();
4405 ftrace_startup_enable(0);
4406
4407 mutex_unlock(&ftrace_lock);
4408}
4409
4410static void *fpid_start(struct seq_file *m, loff_t *pos)
4411{
4412 mutex_lock(&ftrace_lock);
4413
4414 if (list_empty(&ftrace_pids) && (!*pos))
4415 return (void *) 1;
4416
4417 return seq_list_start(&ftrace_pids, *pos);
4418}
4419
4420static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4421{
4422 if (v == (void *)1)
4423 return NULL;
4424
4425 return seq_list_next(v, &ftrace_pids, pos);
4426}
4427
4428static void fpid_stop(struct seq_file *m, void *p)
4429{
4430 mutex_unlock(&ftrace_lock);
4431}
4432
4433static int fpid_show(struct seq_file *m, void *v)
4434{
4435 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4436
4437 if (v == (void *)1) {
4438 seq_printf(m, "no pid\n");
4439 return 0;
4440 }
4441
4442 if (fpid->pid == ftrace_swapper_pid)
4443 seq_printf(m, "swapper tasks\n");
4444 else
4445 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4446
4447 return 0;
4448}
4449
4450static const struct seq_operations ftrace_pid_sops = {
4451 .start = fpid_start,
4452 .next = fpid_next,
4453 .stop = fpid_stop,
4454 .show = fpid_show,
4455};
4456
4457static int
4458ftrace_pid_open(struct inode *inode, struct file *file)
4459{
4460 int ret = 0;
4461
4462 if ((file->f_mode & FMODE_WRITE) &&
4463 (file->f_flags & O_TRUNC))
4464 ftrace_pid_reset();
4465
4466 if (file->f_mode & FMODE_READ)
4467 ret = seq_open(file, &ftrace_pid_sops);
4468
4469 return ret;
4470}
4471
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004472static ssize_t
4473ftrace_pid_write(struct file *filp, const char __user *ubuf,
4474 size_t cnt, loff_t *ppos)
4475{
Ingo Molnar457dc922009-11-23 11:03:28 +01004476 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004477 long val;
4478 int ret;
4479
4480 if (cnt >= sizeof(buf))
4481 return -EINVAL;
4482
4483 if (copy_from_user(&buf, ubuf, cnt))
4484 return -EFAULT;
4485
4486 buf[cnt] = 0;
4487
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004488 /*
4489 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4490 * to clean the filter quietly.
4491 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004492 tmp = strstrip(buf);
4493 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004494 return 1;
4495
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004496 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004497 if (ret < 0)
4498 return ret;
4499
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004500 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004501
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004502 return ret ? ret : cnt;
4503}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004504
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004505static int
4506ftrace_pid_release(struct inode *inode, struct file *file)
4507{
4508 if (file->f_mode & FMODE_READ)
4509 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004510
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004511 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004512}
4513
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004514static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004515 .open = ftrace_pid_open,
4516 .write = ftrace_pid_write,
4517 .read = seq_read,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09004518 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004519 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004520};
4521
4522static __init int ftrace_init_debugfs(void)
4523{
4524 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004525
4526 d_tracer = tracing_init_dentry();
4527 if (!d_tracer)
4528 return 0;
4529
4530 ftrace_init_dyn_debugfs(d_tracer);
4531
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004532 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4533 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004534
4535 ftrace_profile_debugfs(d_tracer);
4536
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004537 return 0;
4538}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004539fs_initcall(ftrace_init_debugfs);
4540
Steven Rostedt3d083392008-05-12 21:20:42 +02004541/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004542 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004543 *
4544 * This function should be used by panic code. It stops ftrace
4545 * but in a not so nice way. If you need to simply kill ftrace
4546 * from a non-atomic section, use ftrace_kill.
4547 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004548void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004549{
4550 ftrace_disabled = 1;
4551 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004552 clear_ftrace_function();
4553}
4554
4555/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004556 * Test if ftrace is dead or not.
4557 */
4558int ftrace_is_dead(void)
4559{
4560 return ftrace_disabled;
4561}
4562
4563/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004564 * register_ftrace_function - register a function for profiling
4565 * @ops - ops structure that holds the function for profiling.
4566 *
4567 * Register a function to be called by all functions in the
4568 * kernel.
4569 *
4570 * Note: @ops->func and all the functions it calls must be labeled
4571 * with "notrace", otherwise it will go into a
4572 * recursive loop.
4573 */
4574int register_ftrace_function(struct ftrace_ops *ops)
4575{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004576 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004577
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004578 ftrace_ops_init(ops);
4579
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004580 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004581
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004582 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004583 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004584 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004585
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004586 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004587
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004588 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004589}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004590EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004591
4592/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004593 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004594 * @ops - ops structure that holds the function to unregister
4595 *
4596 * Unregister a function that was added to be called by ftrace profiling.
4597 */
4598int unregister_ftrace_function(struct ftrace_ops *ops)
4599{
4600 int ret;
4601
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004602 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004603 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004604 if (!ret)
4605 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004606 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004607
4608 return ret;
4609}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004610EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004611
Ingo Molnare309b412008-05-12 21:20:51 +02004612int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004613ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004614 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004615 loff_t *ppos)
4616{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004617 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004618
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004619 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004620
Steven Rostedt45a4a232011-04-21 23:16:46 -04004621 if (unlikely(ftrace_disabled))
4622 goto out;
4623
4624 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004625
Li Zefana32c7762009-06-26 16:55:51 +08004626 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004627 goto out;
4628
Li Zefana32c7762009-06-26 16:55:51 +08004629 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004630
4631 if (ftrace_enabled) {
4632
4633 ftrace_startup_sysctl();
4634
4635 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004636 if (ftrace_ops_list != &ftrace_list_end)
4637 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004638
4639 } else {
4640 /* stopping ftrace calls (just send to ftrace_stub) */
4641 ftrace_trace_function = ftrace_stub;
4642
4643 ftrace_shutdown_sysctl();
4644 }
4645
4646 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004647 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004648 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004649}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004650
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004651#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004652
Steven Rostedt597af812009-04-03 15:24:12 -04004653static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004654static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004655
Steven Rostedte49dc192008-12-02 23:50:05 -05004656int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4657{
4658 return 0;
4659}
4660
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004661/* The callbacks that hook a function */
4662trace_func_graph_ret_t ftrace_graph_return =
4663 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004664trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004665
4666/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4667static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4668{
4669 int i;
4670 int ret = 0;
4671 unsigned long flags;
4672 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4673 struct task_struct *g, *t;
4674
4675 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4676 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4677 * sizeof(struct ftrace_ret_stack),
4678 GFP_KERNEL);
4679 if (!ret_stack_list[i]) {
4680 start = 0;
4681 end = i;
4682 ret = -ENOMEM;
4683 goto free;
4684 }
4685 }
4686
4687 read_lock_irqsave(&tasklist_lock, flags);
4688 do_each_thread(g, t) {
4689 if (start == end) {
4690 ret = -EAGAIN;
4691 goto unlock;
4692 }
4693
4694 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004695 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004696 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004697 t->curr_ret_stack = -1;
4698 /* Make sure the tasks see the -1 first: */
4699 smp_wmb();
4700 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004701 }
4702 } while_each_thread(g, t);
4703
4704unlock:
4705 read_unlock_irqrestore(&tasklist_lock, flags);
4706free:
4707 for (i = start; i < end; i++)
4708 kfree(ret_stack_list[i]);
4709 return ret;
4710}
4711
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004712static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004713ftrace_graph_probe_sched_switch(void *ignore,
4714 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004715{
4716 unsigned long long timestamp;
4717 int index;
4718
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004719 /*
4720 * Does the user want to count the time a function was asleep.
4721 * If so, do not update the time stamps.
4722 */
4723 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4724 return;
4725
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004726 timestamp = trace_clock_local();
4727
4728 prev->ftrace_timestamp = timestamp;
4729
4730 /* only process tasks that we timestamped */
4731 if (!next->ftrace_timestamp)
4732 return;
4733
4734 /*
4735 * Update all the counters in next to make up for the
4736 * time next was sleeping.
4737 */
4738 timestamp -= next->ftrace_timestamp;
4739
4740 for (index = next->curr_ret_stack; index >= 0; index--)
4741 next->ret_stack[index].calltime += timestamp;
4742}
4743
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004744/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004745static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004746{
4747 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004748 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004749
4750 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4751 sizeof(struct ftrace_ret_stack *),
4752 GFP_KERNEL);
4753
4754 if (!ret_stack_list)
4755 return -ENOMEM;
4756
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004757 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004758 for_each_online_cpu(cpu) {
4759 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004760 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004761 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004762
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004763 do {
4764 ret = alloc_retstack_tasklist(ret_stack_list);
4765 } while (ret == -EAGAIN);
4766
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004767 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004768 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004769 if (ret)
4770 pr_info("ftrace_graph: Couldn't activate tracepoint"
4771 " probe to kernel_sched_switch\n");
4772 }
4773
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004774 kfree(ret_stack_list);
4775 return ret;
4776}
4777
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004778/*
4779 * Hibernation protection.
4780 * The state of the current task is too much unstable during
4781 * suspend/restore to disk. We want to protect against that.
4782 */
4783static int
4784ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4785 void *unused)
4786{
4787 switch (state) {
4788 case PM_HIBERNATION_PREPARE:
4789 pause_graph_tracing();
4790 break;
4791
4792 case PM_POST_HIBERNATION:
4793 unpause_graph_tracing();
4794 break;
4795 }
4796 return NOTIFY_DONE;
4797}
4798
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004799int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4800 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004801{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004802 int ret = 0;
4803
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004804 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004805
Steven Rostedt05ce5812009-03-24 00:18:31 -04004806 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004807 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004808 ret = -EBUSY;
4809 goto out;
4810 }
4811
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004812 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4813 register_pm_notifier(&ftrace_suspend_notifier);
4814
Steven Rostedt597af812009-04-03 15:24:12 -04004815 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004816 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004817 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004818 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004819 goto out;
4820 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004821
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004822 ftrace_graph_return = retfunc;
4823 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004824
Steven Rostedta1cd6172011-05-23 15:24:25 -04004825 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004826
4827out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004828 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004829 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004830}
4831
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004832void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004833{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004834 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004835
Steven Rostedt597af812009-04-03 15:24:12 -04004836 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004837 goto out;
4838
Steven Rostedt597af812009-04-03 15:24:12 -04004839 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004840 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004841 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004842 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004843 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004844 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004845
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004846 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004847 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004848}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004849
Steven Rostedt868baf02011-02-10 21:26:13 -05004850static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4851
4852static void
4853graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4854{
4855 atomic_set(&t->tracing_graph_pause, 0);
4856 atomic_set(&t->trace_overrun, 0);
4857 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004858 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004859 smp_wmb();
4860 t->ret_stack = ret_stack;
4861}
4862
4863/*
4864 * Allocate a return stack for the idle task. May be the first
4865 * time through, or it may be done by CPU hotplug online.
4866 */
4867void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4868{
4869 t->curr_ret_stack = -1;
4870 /*
4871 * The idle task has no parent, it either has its own
4872 * stack or no stack at all.
4873 */
4874 if (t->ret_stack)
4875 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4876
4877 if (ftrace_graph_active) {
4878 struct ftrace_ret_stack *ret_stack;
4879
4880 ret_stack = per_cpu(idle_ret_stack, cpu);
4881 if (!ret_stack) {
4882 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4883 * sizeof(struct ftrace_ret_stack),
4884 GFP_KERNEL);
4885 if (!ret_stack)
4886 return;
4887 per_cpu(idle_ret_stack, cpu) = ret_stack;
4888 }
4889 graph_init_task(t, ret_stack);
4890 }
4891}
4892
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004893/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004894void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004895{
Steven Rostedt84047e32009-06-02 16:51:55 -04004896 /* Make sure we do not use the parent ret_stack */
4897 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004898 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004899
Steven Rostedt597af812009-04-03 15:24:12 -04004900 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004901 struct ftrace_ret_stack *ret_stack;
4902
4903 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004904 * sizeof(struct ftrace_ret_stack),
4905 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004906 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004907 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004908 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004909 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004910}
4911
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004912void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004913{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004914 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4915
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004916 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004917 /* NULL must become visible to IRQs before we free it: */
4918 barrier();
4919
4920 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004921}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004922
4923void ftrace_graph_stop(void)
4924{
4925 ftrace_stop();
4926}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004927#endif