blob: 92d3334de0c3dccf8b374819dc392845d4d62263 [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
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400123 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400124 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400125 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400126 * 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) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400131 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400132 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) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400138 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400139 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
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400416static void ftrace_sync(struct work_struct *work)
417{
418 /*
419 * This function is just a stub to implement a hard force
420 * of synchronize_sched(). This requires synchronizing
421 * tasks even in userspace and idle.
422 *
423 * Yes, function tracing is rude.
424 */
425}
426
Ingo Molnare309b412008-05-12 21:20:51 +0200427static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200428{
Steven Rostedt2b499382011-05-03 22:49:52 -0400429 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200430
Steven Rostedt2b499382011-05-03 22:49:52 -0400431 if (ftrace_disabled)
432 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200433
Steven Rostedtb8489142011-05-04 09:27:52 -0400434 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
435 return -EBUSY;
436
Steven Rostedt2b499382011-05-03 22:49:52 -0400437 if (FTRACE_WARN_ON(ops == &global_ops))
438 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200439
Steven Rostedtb8489142011-05-04 09:27:52 -0400440 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100441 ret = remove_ftrace_list_ops(&ftrace_global_list,
442 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400443 if (!ret)
444 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100445 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
446 ret = remove_ftrace_list_ops(&ftrace_control_list,
447 &control_ops, ops);
448 if (!ret) {
449 /*
450 * The ftrace_ops is now removed from the list,
451 * so there'll be no new users. We must ensure
452 * all current users are done before we free
453 * the control data.
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400454 * Note synchronize_sched() is not enough, as we
455 * use preempt_disable() to do RCU, but the function
456 * tracer can be called where RCU is not active
457 * (before user_exit()).
Jiri Olsae2484912012-02-15 15:51:48 +0100458 */
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400459 schedule_on_each_cpu(ftrace_sync);
Jiri Olsae2484912012-02-15 15:51:48 +0100460 control_ops_free(ops);
461 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400462 } else
463 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
464
Steven Rostedt2b499382011-05-03 22:49:52 -0400465 if (ret < 0)
466 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400467
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400468 if (ftrace_enabled)
469 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200470
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400471 /*
472 * Dynamic ops may be freed, we must make sure that all
473 * callers are done before leaving this function.
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400474 *
475 * Again, normal synchronize_sched() is not good enough.
476 * We need to do a hard force of sched synchronization.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400477 */
478 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400479 schedule_on_each_cpu(ftrace_sync);
480
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400481
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500482 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200483}
484
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500485static void ftrace_update_pid_func(void)
486{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400487 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500488 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900489 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500490
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400491 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500492}
493
Steven Rostedt493762f2009-03-23 17:12:36 -0400494#ifdef CONFIG_FUNCTION_PROFILER
495struct ftrace_profile {
496 struct hlist_node node;
497 unsigned long ip;
498 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400499#ifdef CONFIG_FUNCTION_GRAPH_TRACER
500 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400501 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400502#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400503};
504
505struct ftrace_profile_page {
506 struct ftrace_profile_page *next;
507 unsigned long index;
508 struct ftrace_profile records[];
509};
510
Steven Rostedtcafb1682009-03-24 20:50:39 -0400511struct ftrace_profile_stat {
512 atomic_t disabled;
513 struct hlist_head *hash;
514 struct ftrace_profile_page *pages;
515 struct ftrace_profile_page *start;
516 struct tracer_stat stat;
517};
518
Steven Rostedt493762f2009-03-23 17:12:36 -0400519#define PROFILE_RECORDS_SIZE \
520 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
521
522#define PROFILES_PER_PAGE \
523 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
524
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400525static int ftrace_profile_enabled __read_mostly;
526
527/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400528static DEFINE_MUTEX(ftrace_profile_lock);
529
Steven Rostedtcafb1682009-03-24 20:50:39 -0400530static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400531
Namhyung Kim20079eb2013-04-10 08:55:50 +0900532#define FTRACE_PROFILE_HASH_BITS 10
533#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400534
Steven Rostedt493762f2009-03-23 17:12:36 -0400535static void *
536function_stat_next(void *v, int idx)
537{
538 struct ftrace_profile *rec = v;
539 struct ftrace_profile_page *pg;
540
541 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
542
543 again:
Li Zefan0296e422009-06-26 11:15:37 +0800544 if (idx != 0)
545 rec++;
546
Steven Rostedt493762f2009-03-23 17:12:36 -0400547 if ((void *)rec >= (void *)&pg->records[pg->index]) {
548 pg = pg->next;
549 if (!pg)
550 return NULL;
551 rec = &pg->records[0];
552 if (!rec->counter)
553 goto again;
554 }
555
556 return rec;
557}
558
559static void *function_stat_start(struct tracer_stat *trace)
560{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400561 struct ftrace_profile_stat *stat =
562 container_of(trace, struct ftrace_profile_stat, stat);
563
564 if (!stat || !stat->start)
565 return NULL;
566
567 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400568}
569
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400570#ifdef CONFIG_FUNCTION_GRAPH_TRACER
571/* function graph compares on total time */
572static int function_stat_cmp(void *p1, void *p2)
573{
574 struct ftrace_profile *a = p1;
575 struct ftrace_profile *b = p2;
576
577 if (a->time < b->time)
578 return -1;
579 if (a->time > b->time)
580 return 1;
581 else
582 return 0;
583}
584#else
585/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400586static int function_stat_cmp(void *p1, void *p2)
587{
588 struct ftrace_profile *a = p1;
589 struct ftrace_profile *b = p2;
590
591 if (a->counter < b->counter)
592 return -1;
593 if (a->counter > b->counter)
594 return 1;
595 else
596 return 0;
597}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400598#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400599
600static int function_stat_headers(struct seq_file *m)
601{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400602#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400603 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400604 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400605 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400606 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400607#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400608 seq_printf(m, " Function Hit\n"
609 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400610#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400611 return 0;
612}
613
614static int function_stat_show(struct seq_file *m, void *v)
615{
616 struct ftrace_profile *rec = v;
617 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800618 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400619#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400620 static struct trace_seq s;
621 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400622 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400623#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800624 mutex_lock(&ftrace_profile_lock);
625
626 /* we raced with function_profile_reset() */
627 if (unlikely(rec->counter == 0)) {
628 ret = -EBUSY;
629 goto out;
630 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400631
632 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400633 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400634
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400635#ifdef CONFIG_FUNCTION_GRAPH_TRACER
636 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400637 avg = rec->time;
638 do_div(avg, rec->counter);
639
Chase Douglase330b3b2010-04-26 14:02:05 -0400640 /* Sample standard deviation (s^2) */
641 if (rec->counter <= 1)
642 stddev = 0;
643 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200644 /*
645 * Apply Welford's method:
646 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
647 */
648 stddev = rec->counter * rec->time_squared -
649 rec->time * rec->time;
650
Chase Douglase330b3b2010-04-26 14:02:05 -0400651 /*
652 * Divide only 1000 for ns^2 -> us^2 conversion.
653 * trace_print_graph_duration will divide 1000 again.
654 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200655 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400656 }
657
Steven Rostedt34886c82009-03-25 21:00:47 -0400658 trace_seq_init(&s);
659 trace_print_graph_duration(rec->time, &s);
660 trace_seq_puts(&s, " ");
661 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400662 trace_seq_puts(&s, " ");
663 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400664 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400665#endif
666 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800667out:
668 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400669
Li Zefan3aaba202010-08-23 16:50:12 +0800670 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400671}
672
Steven Rostedtcafb1682009-03-24 20:50:39 -0400673static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400674{
675 struct ftrace_profile_page *pg;
676
Steven Rostedtcafb1682009-03-24 20:50:39 -0400677 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400678
679 while (pg) {
680 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
681 pg->index = 0;
682 pg = pg->next;
683 }
684
Steven Rostedtcafb1682009-03-24 20:50:39 -0400685 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400686 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
687}
688
Steven Rostedtcafb1682009-03-24 20:50:39 -0400689int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400690{
691 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400692 int functions;
693 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400694 int i;
695
696 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400697 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400698 return 0;
699
Steven Rostedtcafb1682009-03-24 20:50:39 -0400700 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
701 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400702 return -ENOMEM;
703
Steven Rostedt318e0a72009-03-25 20:06:34 -0400704#ifdef CONFIG_DYNAMIC_FTRACE
705 functions = ftrace_update_tot_cnt;
706#else
707 /*
708 * We do not know the number of functions that exist because
709 * dynamic tracing is what counts them. With past experience
710 * we have around 20K functions. That should be more than enough.
711 * It is highly unlikely we will execute every function in
712 * the kernel.
713 */
714 functions = 20000;
715#endif
716
Steven Rostedtcafb1682009-03-24 20:50:39 -0400717 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400718
Steven Rostedt318e0a72009-03-25 20:06:34 -0400719 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
720
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900721 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400722 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400723 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400724 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400725 pg = pg->next;
726 }
727
728 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400729
730 out_free:
731 pg = stat->start;
732 while (pg) {
733 unsigned long tmp = (unsigned long)pg;
734
735 pg = pg->next;
736 free_page(tmp);
737 }
738
Steven Rostedt318e0a72009-03-25 20:06:34 -0400739 stat->pages = NULL;
740 stat->start = NULL;
741
742 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400743}
744
Steven Rostedtcafb1682009-03-24 20:50:39 -0400745static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400746{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400747 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400748 int size;
749
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750 stat = &per_cpu(ftrace_profile_stats, cpu);
751
752 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400753 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400754 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400755 return 0;
756 }
757
758 /*
759 * We are profiling all functions, but usually only a few thousand
760 * functions are hit. We'll make a hash of 1024 items.
761 */
762 size = FTRACE_PROFILE_HASH_SIZE;
763
Steven Rostedtcafb1682009-03-24 20:50:39 -0400764 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400765
Steven Rostedtcafb1682009-03-24 20:50:39 -0400766 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400767 return -ENOMEM;
768
Steven Rostedt318e0a72009-03-25 20:06:34 -0400769 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400770 if (ftrace_profile_pages_init(stat) < 0) {
771 kfree(stat->hash);
772 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400773 return -ENOMEM;
774 }
775
776 return 0;
777}
778
Steven Rostedtcafb1682009-03-24 20:50:39 -0400779static int ftrace_profile_init(void)
780{
781 int cpu;
782 int ret = 0;
783
784 for_each_online_cpu(cpu) {
785 ret = ftrace_profile_init_cpu(cpu);
786 if (ret)
787 break;
788 }
789
790 return ret;
791}
792
Steven Rostedt493762f2009-03-23 17:12:36 -0400793/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400794static struct ftrace_profile *
795ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400796{
797 struct ftrace_profile *rec;
798 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400799 unsigned long key;
800
Namhyung Kim20079eb2013-04-10 08:55:50 +0900801 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400802 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400803
804 if (hlist_empty(hhd))
805 return NULL;
806
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400807 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400808 if (rec->ip == ip)
809 return rec;
810 }
811
812 return NULL;
813}
814
Steven Rostedtcafb1682009-03-24 20:50:39 -0400815static void ftrace_add_profile(struct ftrace_profile_stat *stat,
816 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400817{
818 unsigned long key;
819
Namhyung Kim20079eb2013-04-10 08:55:50 +0900820 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400821 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400822}
823
Steven Rostedt318e0a72009-03-25 20:06:34 -0400824/*
825 * The memory is already allocated, this simply finds a new record to use.
826 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400827static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400828ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400829{
830 struct ftrace_profile *rec = NULL;
831
Steven Rostedt318e0a72009-03-25 20:06:34 -0400832 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400833 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400834 goto out;
835
Steven Rostedt493762f2009-03-23 17:12:36 -0400836 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400837 * Try to find the function again since an NMI
838 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400839 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400840 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400841 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400842 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400843
Steven Rostedtcafb1682009-03-24 20:50:39 -0400844 if (stat->pages->index == PROFILES_PER_PAGE) {
845 if (!stat->pages->next)
846 goto out;
847 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400848 }
849
Steven Rostedtcafb1682009-03-24 20:50:39 -0400850 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400851 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400852 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400853
Steven Rostedt493762f2009-03-23 17:12:36 -0400854 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400855 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400856
857 return rec;
858}
859
Steven Rostedt493762f2009-03-23 17:12:36 -0400860static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400861function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400862 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400863{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400864 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400865 struct ftrace_profile *rec;
866 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400867
868 if (!ftrace_profile_enabled)
869 return;
870
Steven Rostedt493762f2009-03-23 17:12:36 -0400871 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400872
873 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400874 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400875 goto out;
876
877 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400878 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400879 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400880 if (!rec)
881 goto out;
882 }
883
884 rec->counter++;
885 out:
886 local_irq_restore(flags);
887}
888
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400889#ifdef CONFIG_FUNCTION_GRAPH_TRACER
890static int profile_graph_entry(struct ftrace_graph_ent *trace)
891{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400892 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400893 return 1;
894}
895
896static void profile_graph_return(struct ftrace_graph_ret *trace)
897{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400898 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400899 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400900 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400901 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400902
903 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400904 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400905 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400906 goto out;
907
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400908 /* If the calltime was zero'd ignore it */
909 if (!trace->calltime)
910 goto out;
911
Steven Rostedta2a16d62009-03-24 23:17:58 -0400912 calltime = trace->rettime - trace->calltime;
913
914 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
915 int index;
916
917 index = trace->depth;
918
919 /* Append this call time to the parent time to subtract */
920 if (index)
921 current->ret_stack[index - 1].subtime += calltime;
922
923 if (current->ret_stack[index].subtime < calltime)
924 calltime -= current->ret_stack[index].subtime;
925 else
926 calltime = 0;
927 }
928
Steven Rostedtcafb1682009-03-24 20:50:39 -0400929 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400930 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400931 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400932 rec->time_squared += calltime * calltime;
933 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400934
Steven Rostedtcafb1682009-03-24 20:50:39 -0400935 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400936 local_irq_restore(flags);
937}
938
939static int register_ftrace_profiler(void)
940{
941 return register_ftrace_graph(&profile_graph_return,
942 &profile_graph_entry);
943}
944
945static void unregister_ftrace_profiler(void)
946{
947 unregister_ftrace_graph();
948}
949#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100950static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400951 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900952 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
953 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400954};
955
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400956static int register_ftrace_profiler(void)
957{
958 return register_ftrace_function(&ftrace_profile_ops);
959}
960
961static void unregister_ftrace_profiler(void)
962{
963 unregister_ftrace_function(&ftrace_profile_ops);
964}
965#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
966
Steven Rostedt493762f2009-03-23 17:12:36 -0400967static ssize_t
968ftrace_profile_write(struct file *filp, const char __user *ubuf,
969 size_t cnt, loff_t *ppos)
970{
971 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400972 int ret;
973
Peter Huewe22fe9b52011-06-07 21:58:27 +0200974 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
975 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400976 return ret;
977
978 val = !!val;
979
980 mutex_lock(&ftrace_profile_lock);
981 if (ftrace_profile_enabled ^ val) {
982 if (val) {
983 ret = ftrace_profile_init();
984 if (ret < 0) {
985 cnt = ret;
986 goto out;
987 }
988
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400989 ret = register_ftrace_profiler();
990 if (ret < 0) {
991 cnt = ret;
992 goto out;
993 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400994 ftrace_profile_enabled = 1;
995 } else {
996 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400997 /*
998 * unregister_ftrace_profiler calls stop_machine
999 * so this acts like an synchronize_sched.
1000 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001001 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -04001002 }
1003 }
1004 out:
1005 mutex_unlock(&ftrace_profile_lock);
1006
Jiri Olsacf8517c2009-10-23 19:36:16 -04001007 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001008
1009 return cnt;
1010}
1011
1012static ssize_t
1013ftrace_profile_read(struct file *filp, char __user *ubuf,
1014 size_t cnt, loff_t *ppos)
1015{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001016 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001017 int r;
1018
1019 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1020 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1021}
1022
1023static const struct file_operations ftrace_profile_fops = {
1024 .open = tracing_open_generic,
1025 .read = ftrace_profile_read,
1026 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001027 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001028};
1029
Steven Rostedtcafb1682009-03-24 20:50:39 -04001030/* used to initialize the real stat files */
1031static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001032 .name = "functions",
1033 .stat_start = function_stat_start,
1034 .stat_next = function_stat_next,
1035 .stat_cmp = function_stat_cmp,
1036 .stat_headers = function_stat_headers,
1037 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001038};
1039
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001040static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001041{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001042 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001043 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001044 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001045 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001046 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001047
Steven Rostedtcafb1682009-03-24 20:50:39 -04001048 for_each_possible_cpu(cpu) {
1049 stat = &per_cpu(ftrace_profile_stats, cpu);
1050
1051 /* allocate enough for function name + cpu number */
1052 name = kmalloc(32, GFP_KERNEL);
1053 if (!name) {
1054 /*
1055 * The files created are permanent, if something happens
1056 * we still do not free memory.
1057 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001058 WARN(1,
1059 "Could not allocate stat file for cpu %d\n",
1060 cpu);
1061 return;
1062 }
1063 stat->stat = function_stats;
1064 snprintf(name, 32, "function%d", cpu);
1065 stat->stat.name = name;
1066 ret = register_stat_tracer(&stat->stat);
1067 if (ret) {
1068 WARN(1,
1069 "Could not register function stat for cpu %d\n",
1070 cpu);
1071 kfree(name);
1072 return;
1073 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001074 }
1075
1076 entry = debugfs_create_file("function_profile_enabled", 0644,
1077 d_tracer, NULL, &ftrace_profile_fops);
1078 if (!entry)
1079 pr_warning("Could not create debugfs "
1080 "'function_profile_enabled' entry\n");
1081}
1082
1083#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001084static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001085{
1086}
1087#endif /* CONFIG_FUNCTION_PROFILER */
1088
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001089static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1090
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04001091loff_t
1092ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1093{
1094 loff_t ret;
1095
1096 if (file->f_mode & FMODE_READ)
1097 ret = seq_lseek(file, offset, whence);
1098 else
1099 file->f_pos = ret = 1;
1100
1101 return ret;
1102}
1103
Steven Rostedt3d083392008-05-12 21:20:42 +02001104#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001105
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001106#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001107# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001108#endif
1109
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001110static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1111
Steven Rostedtb6887d72009-02-17 12:32:04 -05001112struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001113 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001114 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001115 unsigned long flags;
1116 unsigned long ip;
1117 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001118 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001119};
1120
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001121struct ftrace_func_entry {
1122 struct hlist_node hlist;
1123 unsigned long ip;
1124};
1125
1126struct ftrace_hash {
1127 unsigned long size_bits;
1128 struct hlist_head *buckets;
1129 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001130 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001131};
1132
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001133/*
1134 * We make these constant because no one should touch them,
1135 * but they are used as the default "empty hash", to avoid allocating
1136 * it all the time. These are in a read only section such that if
1137 * anyone does try to modify it, it will cause an exception.
1138 */
1139static const struct hlist_head empty_buckets[1];
1140static const struct ftrace_hash empty_hash = {
1141 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001142};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001143#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001144
Steven Rostedt2b499382011-05-03 22:49:52 -04001145static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001146 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001147 .notrace_hash = EMPTY_HASH,
1148 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001149 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1150 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001151};
1152
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001153struct ftrace_page {
1154 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001155 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001156 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001157 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001158};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001159
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001160static struct ftrace_page *ftrace_new_pgs;
1161
Steven Rostedta7900872011-12-16 16:23:44 -05001162#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1163#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001164
1165/* estimate from running different kernels */
1166#define NR_TO_INIT 10000
1167
1168static struct ftrace_page *ftrace_pages_start;
1169static struct ftrace_page *ftrace_pages;
1170
Steven Rostedt06a51d92011-12-19 19:07:36 -05001171static bool ftrace_hash_empty(struct ftrace_hash *hash)
1172{
1173 return !hash || !hash->count;
1174}
1175
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001176static struct ftrace_func_entry *
1177ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1178{
1179 unsigned long key;
1180 struct ftrace_func_entry *entry;
1181 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001182
Steven Rostedt06a51d92011-12-19 19:07:36 -05001183 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001184 return NULL;
1185
1186 if (hash->size_bits > 0)
1187 key = hash_long(ip, hash->size_bits);
1188 else
1189 key = 0;
1190
1191 hhd = &hash->buckets[key];
1192
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001193 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001194 if (entry->ip == ip)
1195 return entry;
1196 }
1197 return NULL;
1198}
1199
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001200static void __add_hash_entry(struct ftrace_hash *hash,
1201 struct ftrace_func_entry *entry)
1202{
1203 struct hlist_head *hhd;
1204 unsigned long key;
1205
1206 if (hash->size_bits)
1207 key = hash_long(entry->ip, hash->size_bits);
1208 else
1209 key = 0;
1210
1211 hhd = &hash->buckets[key];
1212 hlist_add_head(&entry->hlist, hhd);
1213 hash->count++;
1214}
1215
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001216static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1217{
1218 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001219
1220 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1221 if (!entry)
1222 return -ENOMEM;
1223
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001224 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001225 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001226
1227 return 0;
1228}
1229
1230static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001231free_hash_entry(struct ftrace_hash *hash,
1232 struct ftrace_func_entry *entry)
1233{
1234 hlist_del(&entry->hlist);
1235 kfree(entry);
1236 hash->count--;
1237}
1238
1239static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001240remove_hash_entry(struct ftrace_hash *hash,
1241 struct ftrace_func_entry *entry)
1242{
1243 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001244 hash->count--;
1245}
1246
1247static void ftrace_hash_clear(struct ftrace_hash *hash)
1248{
1249 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001250 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001251 struct ftrace_func_entry *entry;
1252 int size = 1 << hash->size_bits;
1253 int i;
1254
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001255 if (!hash->count)
1256 return;
1257
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001258 for (i = 0; i < size; i++) {
1259 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001260 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001261 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001262 }
1263 FTRACE_WARN_ON(hash->count);
1264}
1265
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001266static void free_ftrace_hash(struct ftrace_hash *hash)
1267{
1268 if (!hash || hash == EMPTY_HASH)
1269 return;
1270 ftrace_hash_clear(hash);
1271 kfree(hash->buckets);
1272 kfree(hash);
1273}
1274
Steven Rostedt07fd5512011-05-05 18:03:47 -04001275static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1276{
1277 struct ftrace_hash *hash;
1278
1279 hash = container_of(rcu, struct ftrace_hash, rcu);
1280 free_ftrace_hash(hash);
1281}
1282
1283static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1284{
1285 if (!hash || hash == EMPTY_HASH)
1286 return;
1287 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1288}
1289
Jiri Olsa5500fa52012-02-15 15:51:54 +01001290void ftrace_free_filter(struct ftrace_ops *ops)
1291{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001292 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001293 free_ftrace_hash(ops->filter_hash);
1294 free_ftrace_hash(ops->notrace_hash);
1295}
1296
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001297static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1298{
1299 struct ftrace_hash *hash;
1300 int size;
1301
1302 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1303 if (!hash)
1304 return NULL;
1305
1306 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001307 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001308
1309 if (!hash->buckets) {
1310 kfree(hash);
1311 return NULL;
1312 }
1313
1314 hash->size_bits = size_bits;
1315
1316 return hash;
1317}
1318
1319static struct ftrace_hash *
1320alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1321{
1322 struct ftrace_func_entry *entry;
1323 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001324 int size;
1325 int ret;
1326 int i;
1327
1328 new_hash = alloc_ftrace_hash(size_bits);
1329 if (!new_hash)
1330 return NULL;
1331
1332 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001333 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001334 return new_hash;
1335
1336 size = 1 << hash->size_bits;
1337 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001338 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001339 ret = add_hash_entry(new_hash, entry->ip);
1340 if (ret < 0)
1341 goto free_hash;
1342 }
1343 }
1344
1345 FTRACE_WARN_ON(new_hash->count != hash->count);
1346
1347 return new_hash;
1348
1349 free_hash:
1350 free_ftrace_hash(new_hash);
1351 return NULL;
1352}
1353
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001354static void
1355ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1356static void
1357ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1358
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001359static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001360ftrace_hash_move(struct ftrace_ops *ops, int enable,
1361 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001362{
1363 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001364 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001365 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001366 struct ftrace_hash *old_hash;
1367 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001368 int size = src->count;
1369 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001370 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001371 int i;
1372
1373 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001374 * Remove the current set, update the hash and add
1375 * them back.
1376 */
1377 ftrace_hash_rec_disable(ops, enable);
1378
1379 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001380 * If the new source is empty, just free dst and assign it
1381 * the empty_hash.
1382 */
1383 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001384 free_ftrace_hash_rcu(*dst);
1385 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001386 /* still need to update the function records */
1387 ret = 0;
1388 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001389 }
1390
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001391 /*
1392 * Make the hash size about 1/2 the # found
1393 */
1394 for (size /= 2; size; size >>= 1)
1395 bits++;
1396
1397 /* Don't allocate too much */
1398 if (bits > FTRACE_HASH_MAX_BITS)
1399 bits = FTRACE_HASH_MAX_BITS;
1400
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001401 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001402 new_hash = alloc_ftrace_hash(bits);
1403 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001404 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001405
1406 size = 1 << src->size_bits;
1407 for (i = 0; i < size; i++) {
1408 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001409 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001410 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001411 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001412 }
1413 }
1414
Steven Rostedt07fd5512011-05-05 18:03:47 -04001415 old_hash = *dst;
1416 rcu_assign_pointer(*dst, new_hash);
1417 free_ftrace_hash_rcu(old_hash);
1418
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001419 ret = 0;
1420 out:
1421 /*
1422 * Enable regardless of ret:
1423 * On success, we enable the new hash.
1424 * On failure, we re-enable the original hash.
1425 */
1426 ftrace_hash_rec_enable(ops, enable);
1427
1428 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001429}
1430
Steven Rostedt265c8312009-02-13 12:43:56 -05001431/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001432 * Test the hashes for this ops to see if we want to call
1433 * the ops->func or not.
1434 *
1435 * It's a match if the ip is in the ops->filter_hash or
1436 * the filter_hash does not exist or is empty,
1437 * AND
1438 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001439 *
1440 * This needs to be called with preemption disabled as
1441 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001442 */
1443static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001444ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001445{
1446 struct ftrace_hash *filter_hash;
1447 struct ftrace_hash *notrace_hash;
1448 int ret;
1449
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001450#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1451 /*
1452 * There's a small race when adding ops that the ftrace handler
1453 * that wants regs, may be called without them. We can not
1454 * allow that handler to be called if regs is NULL.
1455 */
1456 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1457 return 0;
1458#endif
1459
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001460 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1461 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001462
Steven Rostedt06a51d92011-12-19 19:07:36 -05001463 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001464 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001465 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001466 !ftrace_lookup_ip(notrace_hash, ip)))
1467 ret = 1;
1468 else
1469 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001470
1471 return ret;
1472}
1473
1474/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001475 * This is a double for. Do not use 'break' to break out of the loop,
1476 * you must use a goto.
1477 */
1478#define do_for_each_ftrace_rec(pg, rec) \
1479 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1480 int _____i; \
1481 for (_____i = 0; _____i < pg->index; _____i++) { \
1482 rec = &pg->records[_____i];
1483
1484#define while_for_each_ftrace_rec() \
1485 } \
1486 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301487
Steven Rostedt5855fea2011-12-16 19:27:42 -05001488
1489static int ftrace_cmp_recs(const void *a, const void *b)
1490{
Steven Rostedta650e022012-04-25 13:48:13 -04001491 const struct dyn_ftrace *key = a;
1492 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001493
Steven Rostedta650e022012-04-25 13:48:13 -04001494 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001495 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001496 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1497 return 1;
1498 return 0;
1499}
1500
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001501static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001502{
1503 struct ftrace_page *pg;
1504 struct dyn_ftrace *rec;
1505 struct dyn_ftrace key;
1506
1507 key.ip = start;
1508 key.flags = end; /* overload flags, as it is unsigned long */
1509
1510 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1511 if (end < pg->records[0].ip ||
1512 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1513 continue;
1514 rec = bsearch(&key, pg->records, pg->index,
1515 sizeof(struct dyn_ftrace),
1516 ftrace_cmp_recs);
1517 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001518 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001519 }
1520
Steven Rostedt5855fea2011-12-16 19:27:42 -05001521 return 0;
1522}
1523
Steven Rostedtc88fd862011-08-16 09:53:39 -04001524/**
1525 * ftrace_location - return true if the ip giving is a traced location
1526 * @ip: the instruction pointer to check
1527 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001528 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001529 * That is, the instruction that is either a NOP or call to
1530 * the function tracer. It checks the ftrace internal tables to
1531 * determine if the address belongs or not.
1532 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001533unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001534{
Steven Rostedta650e022012-04-25 13:48:13 -04001535 return ftrace_location_range(ip, ip);
1536}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001537
Steven Rostedta650e022012-04-25 13:48:13 -04001538/**
1539 * ftrace_text_reserved - return true if range contains an ftrace location
1540 * @start: start of range to search
1541 * @end: end of range to search (inclusive). @end points to the last byte to check.
1542 *
1543 * Returns 1 if @start and @end contains a ftrace location.
1544 * That is, the instruction that is either a NOP or call to
1545 * the function tracer. It checks the ftrace internal tables to
1546 * determine if the address belongs or not.
1547 */
1548int ftrace_text_reserved(void *start, void *end)
1549{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001550 unsigned long ret;
1551
1552 ret = ftrace_location_range((unsigned long)start,
1553 (unsigned long)end);
1554
1555 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001556}
1557
Steven Rostedted926f92011-05-03 13:25:24 -04001558static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1559 int filter_hash,
1560 bool inc)
1561{
1562 struct ftrace_hash *hash;
1563 struct ftrace_hash *other_hash;
1564 struct ftrace_page *pg;
1565 struct dyn_ftrace *rec;
1566 int count = 0;
1567 int all = 0;
1568
1569 /* Only update if the ops has been registered */
1570 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1571 return;
1572
1573 /*
1574 * In the filter_hash case:
1575 * If the count is zero, we update all records.
1576 * Otherwise we just update the items in the hash.
1577 *
1578 * In the notrace_hash case:
1579 * We enable the update in the hash.
1580 * As disabling notrace means enabling the tracing,
1581 * and enabling notrace means disabling, the inc variable
1582 * gets inversed.
1583 */
1584 if (filter_hash) {
1585 hash = ops->filter_hash;
1586 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001587 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001588 all = 1;
1589 } else {
1590 inc = !inc;
1591 hash = ops->notrace_hash;
1592 other_hash = ops->filter_hash;
1593 /*
1594 * If the notrace hash has no items,
1595 * then there's nothing to do.
1596 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001597 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001598 return;
1599 }
1600
1601 do_for_each_ftrace_rec(pg, rec) {
1602 int in_other_hash = 0;
1603 int in_hash = 0;
1604 int match = 0;
1605
1606 if (all) {
1607 /*
1608 * Only the filter_hash affects all records.
1609 * Update if the record is not in the notrace hash.
1610 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001611 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001612 match = 1;
1613 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001614 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1615 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001616
1617 /*
1618 *
1619 */
1620 if (filter_hash && in_hash && !in_other_hash)
1621 match = 1;
1622 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001623 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001624 match = 1;
1625 }
1626 if (!match)
1627 continue;
1628
1629 if (inc) {
1630 rec->flags++;
1631 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1632 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001633 /*
1634 * If any ops wants regs saved for this function
1635 * then all ops will get saved regs.
1636 */
1637 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1638 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001639 } else {
1640 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1641 return;
1642 rec->flags--;
1643 }
1644 count++;
1645 /* Shortcut, if we handled all records, we are done. */
1646 if (!all && count == hash->count)
1647 return;
1648 } while_for_each_ftrace_rec();
1649}
1650
1651static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1652 int filter_hash)
1653{
1654 __ftrace_hash_rec_update(ops, filter_hash, 0);
1655}
1656
1657static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1658 int filter_hash)
1659{
1660 __ftrace_hash_rec_update(ops, filter_hash, 1);
1661}
1662
Steven Rostedt05736a42008-09-22 14:55:47 -07001663static void print_ip_ins(const char *fmt, unsigned char *p)
1664{
1665 int i;
1666
1667 printk(KERN_CONT "%s", fmt);
1668
1669 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1670 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1671}
1672
Steven Rostedtc88fd862011-08-16 09:53:39 -04001673/**
1674 * ftrace_bug - report and shutdown function tracer
1675 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1676 * @ip: The address that failed
1677 *
1678 * The arch code that enables or disables the function tracing
1679 * can call ftrace_bug() when it has detected a problem in
1680 * modifying the code. @failed should be one of either:
1681 * EFAULT - if the problem happens on reading the @ip address
1682 * EINVAL - if what is read at @ip is not what was expected
1683 * EPERM - if the problem happens on writting to the @ip address
1684 */
1685void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001686{
1687 switch (failed) {
1688 case -EFAULT:
1689 FTRACE_WARN_ON_ONCE(1);
1690 pr_info("ftrace faulted on modifying ");
1691 print_ip_sym(ip);
1692 break;
1693 case -EINVAL:
1694 FTRACE_WARN_ON_ONCE(1);
1695 pr_info("ftrace failed to modify ");
1696 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001697 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001698 printk(KERN_CONT "\n");
1699 break;
1700 case -EPERM:
1701 FTRACE_WARN_ON_ONCE(1);
1702 pr_info("ftrace faulted on writing ");
1703 print_ip_sym(ip);
1704 break;
1705 default:
1706 FTRACE_WARN_ON_ONCE(1);
1707 pr_info("ftrace faulted on unknown error ");
1708 print_ip_sym(ip);
1709 }
1710}
1711
Steven Rostedtc88fd862011-08-16 09:53:39 -04001712static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001713{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001714 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001715
Steven Rostedt982c3502008-11-15 16:31:41 -05001716 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001717 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001718 *
Steven Rostedted926f92011-05-03 13:25:24 -04001719 * If the record has a ref count, then we need to enable it
1720 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001721 *
Steven Rostedted926f92011-05-03 13:25:24 -04001722 * Otherwise we make sure its disabled.
1723 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001724 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001725 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001726 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001727 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001728 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001729
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001730 /*
1731 * If enabling and the REGS flag does not match the REGS_EN, then
1732 * do not ignore this record. Set flags to fail the compare against
1733 * ENABLED.
1734 */
1735 if (flag &&
1736 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1737 flag |= FTRACE_FL_REGS;
1738
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001739 /* If the state of this record hasn't changed, then do nothing */
1740 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001741 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001742
1743 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001744 /* Save off if rec is being enabled (for return value) */
1745 flag ^= rec->flags & FTRACE_FL_ENABLED;
1746
1747 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001748 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001749 if (flag & FTRACE_FL_REGS) {
1750 if (rec->flags & FTRACE_FL_REGS)
1751 rec->flags |= FTRACE_FL_REGS_EN;
1752 else
1753 rec->flags &= ~FTRACE_FL_REGS_EN;
1754 }
1755 }
1756
1757 /*
1758 * If this record is being updated from a nop, then
1759 * return UPDATE_MAKE_CALL.
1760 * Otherwise, if the EN flag is set, then return
1761 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1762 * from the non-save regs, to a save regs function.
1763 * Otherwise,
1764 * return UPDATE_MODIFY_CALL to tell the caller to convert
1765 * from the save regs, to a non-save regs function.
1766 */
1767 if (flag & FTRACE_FL_ENABLED)
1768 return FTRACE_UPDATE_MAKE_CALL;
1769 else if (rec->flags & FTRACE_FL_REGS_EN)
1770 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1771 else
1772 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001773 }
1774
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001775 if (update) {
1776 /* If there's no more users, clear all flags */
1777 if (!(rec->flags & ~FTRACE_FL_MASK))
1778 rec->flags = 0;
1779 else
1780 /* Just disable the record (keep REGS state) */
1781 rec->flags &= ~FTRACE_FL_ENABLED;
1782 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001783
1784 return FTRACE_UPDATE_MAKE_NOP;
1785}
1786
1787/**
1788 * ftrace_update_record, set a record that now is tracing or not
1789 * @rec: the record to update
1790 * @enable: set to 1 if the record is tracing, zero to force disable
1791 *
1792 * The records that represent all functions that can be traced need
1793 * to be updated when tracing has been enabled.
1794 */
1795int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1796{
1797 return ftrace_check_record(rec, enable, 1);
1798}
1799
1800/**
1801 * ftrace_test_record, check if the record has been enabled or not
1802 * @rec: the record to test
1803 * @enable: set to 1 to check if enabled, 0 if it is disabled
1804 *
1805 * The arch code may need to test if a record is already set to
1806 * tracing to determine how to modify the function code that it
1807 * represents.
1808 */
1809int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1810{
1811 return ftrace_check_record(rec, enable, 0);
1812}
1813
1814static int
1815__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1816{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001817 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001818 unsigned long ftrace_addr;
1819 int ret;
1820
Steven Rostedtc88fd862011-08-16 09:53:39 -04001821 ret = ftrace_update_record(rec, enable);
1822
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001823 if (rec->flags & FTRACE_FL_REGS)
1824 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1825 else
1826 ftrace_addr = (unsigned long)FTRACE_ADDR;
1827
Steven Rostedtc88fd862011-08-16 09:53:39 -04001828 switch (ret) {
1829 case FTRACE_UPDATE_IGNORE:
1830 return 0;
1831
1832 case FTRACE_UPDATE_MAKE_CALL:
1833 return ftrace_make_call(rec, ftrace_addr);
1834
1835 case FTRACE_UPDATE_MAKE_NOP:
1836 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001837
1838 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1839 case FTRACE_UPDATE_MODIFY_CALL:
1840 if (rec->flags & FTRACE_FL_REGS)
1841 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1842 else
1843 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1844
1845 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001846 }
1847
1848 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001849}
1850
Steven Rostedte4f5d542012-04-27 09:13:18 -04001851void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001852{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001853 struct dyn_ftrace *rec;
1854 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001855 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001856
Steven Rostedt45a4a232011-04-21 23:16:46 -04001857 if (unlikely(ftrace_disabled))
1858 return;
1859
Steven Rostedt265c8312009-02-13 12:43:56 -05001860 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001861 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001862 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001863 ftrace_bug(failed, rec->ip);
1864 /* Stop processing */
1865 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001866 }
1867 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001868}
1869
Steven Rostedtc88fd862011-08-16 09:53:39 -04001870struct ftrace_rec_iter {
1871 struct ftrace_page *pg;
1872 int index;
1873};
1874
1875/**
1876 * ftrace_rec_iter_start, start up iterating over traced functions
1877 *
1878 * Returns an iterator handle that is used to iterate over all
1879 * the records that represent address locations where functions
1880 * are traced.
1881 *
1882 * May return NULL if no records are available.
1883 */
1884struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1885{
1886 /*
1887 * We only use a single iterator.
1888 * Protected by the ftrace_lock mutex.
1889 */
1890 static struct ftrace_rec_iter ftrace_rec_iter;
1891 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1892
1893 iter->pg = ftrace_pages_start;
1894 iter->index = 0;
1895
1896 /* Could have empty pages */
1897 while (iter->pg && !iter->pg->index)
1898 iter->pg = iter->pg->next;
1899
1900 if (!iter->pg)
1901 return NULL;
1902
1903 return iter;
1904}
1905
1906/**
1907 * ftrace_rec_iter_next, get the next record to process.
1908 * @iter: The handle to the iterator.
1909 *
1910 * Returns the next iterator after the given iterator @iter.
1911 */
1912struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1913{
1914 iter->index++;
1915
1916 if (iter->index >= iter->pg->index) {
1917 iter->pg = iter->pg->next;
1918 iter->index = 0;
1919
1920 /* Could have empty pages */
1921 while (iter->pg && !iter->pg->index)
1922 iter->pg = iter->pg->next;
1923 }
1924
1925 if (!iter->pg)
1926 return NULL;
1927
1928 return iter;
1929}
1930
1931/**
1932 * ftrace_rec_iter_record, get the record at the iterator location
1933 * @iter: The current iterator location
1934 *
1935 * Returns the record that the current @iter is at.
1936 */
1937struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1938{
1939 return &iter->pg->records[iter->index];
1940}
1941
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301942static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001943ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001944{
1945 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001946 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001947
1948 ip = rec->ip;
1949
Steven Rostedt45a4a232011-04-21 23:16:46 -04001950 if (unlikely(ftrace_disabled))
1951 return 0;
1952
Shaohua Li25aac9d2009-01-09 11:29:40 +08001953 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001954 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001955 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301956 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001957 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301958 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001959}
1960
Steven Rostedt000ab692009-02-17 13:35:06 -05001961/*
1962 * archs can override this function if they must do something
1963 * before the modifying code is performed.
1964 */
1965int __weak ftrace_arch_code_modify_prepare(void)
1966{
1967 return 0;
1968}
1969
1970/*
1971 * archs can override this function if they must do something
1972 * after the modifying code is performed.
1973 */
1974int __weak ftrace_arch_code_modify_post_process(void)
1975{
1976 return 0;
1977}
1978
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001979void ftrace_modify_all_code(int command)
1980{
1981 if (command & FTRACE_UPDATE_CALLS)
1982 ftrace_replace_code(1);
1983 else if (command & FTRACE_DISABLE_CALLS)
1984 ftrace_replace_code(0);
1985
1986 if (command & FTRACE_UPDATE_TRACE_FUNC)
1987 ftrace_update_ftrace_func(ftrace_trace_function);
1988
1989 if (command & FTRACE_START_FUNC_RET)
1990 ftrace_enable_ftrace_graph_caller();
1991 else if (command & FTRACE_STOP_FUNC_RET)
1992 ftrace_disable_ftrace_graph_caller();
1993}
1994
Ingo Molnare309b412008-05-12 21:20:51 +02001995static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001996{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001997 int *command = data;
1998
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001999 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002000
Steven Rostedtc88fd862011-08-16 09:53:39 -04002001 return 0;
2002}
2003
2004/**
2005 * ftrace_run_stop_machine, go back to the stop machine method
2006 * @command: The command to tell ftrace what to do
2007 *
2008 * If an arch needs to fall back to the stop machine method, the
2009 * it can call this function.
2010 */
2011void ftrace_run_stop_machine(int command)
2012{
2013 stop_machine(__ftrace_modify_code, &command, NULL);
2014}
2015
2016/**
2017 * arch_ftrace_update_code, modify the code to trace or not trace
2018 * @command: The command that needs to be done
2019 *
2020 * Archs can override this function if it does not need to
2021 * run stop_machine() to modify code.
2022 */
2023void __weak arch_ftrace_update_code(int command)
2024{
2025 ftrace_run_stop_machine(command);
2026}
2027
2028static void ftrace_run_update_code(int command)
2029{
2030 int ret;
2031
2032 ret = ftrace_arch_code_modify_prepare();
2033 FTRACE_WARN_ON(ret);
2034 if (ret)
2035 return;
2036 /*
2037 * Do not call function tracer while we update the code.
2038 * We are in stop machine.
2039 */
2040 function_trace_stop++;
2041
2042 /*
2043 * By default we use stop_machine() to modify the code.
2044 * But archs can do what ever they want as long as it
2045 * is safe. The stop_machine() is the safest, but also
2046 * produces the most overhead.
2047 */
2048 arch_ftrace_update_code(command);
2049
Steven Rostedt6331c282011-07-13 15:11:02 -04002050 function_trace_stop--;
2051
Steven Rostedt000ab692009-02-17 13:35:06 -05002052 ret = ftrace_arch_code_modify_post_process();
2053 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002054}
2055
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002056static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002057static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002058static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002059
2060static void ftrace_startup_enable(int command)
2061{
2062 if (saved_ftrace_func != ftrace_trace_function) {
2063 saved_ftrace_func = ftrace_trace_function;
2064 command |= FTRACE_UPDATE_TRACE_FUNC;
2065 }
2066
2067 if (!command || !ftrace_enabled)
2068 return;
2069
2070 ftrace_run_update_code(command);
2071}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002072
Steven Rostedta1cd6172011-05-23 15:24:25 -04002073static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002074{
Steven Rostedtb8489142011-05-04 09:27:52 -04002075 bool hash_enable = true;
2076
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002077 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002078 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002079
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002080 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002081 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002082
Steven Rostedtb8489142011-05-04 09:27:52 -04002083 /* ops marked global share the filter hashes */
2084 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2085 ops = &global_ops;
2086 /* Don't update hash if global is already set */
2087 if (global_start_up)
2088 hash_enable = false;
2089 global_start_up++;
2090 }
2091
Steven Rostedted926f92011-05-03 13:25:24 -04002092 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002093 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002094 ftrace_hash_rec_enable(ops, 1);
2095
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002096 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002097
2098 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002099}
2100
Steven Rostedtbd69c302011-05-03 21:55:54 -04002101static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002102{
Steven Rostedtb8489142011-05-04 09:27:52 -04002103 bool hash_disable = true;
2104
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002105 if (unlikely(ftrace_disabled))
2106 return;
2107
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002108 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002109 /*
2110 * Just warn in case of unbalance, no need to kill ftrace, it's not
2111 * critical but the ftrace_call callers may be never nopped again after
2112 * further ftrace uses.
2113 */
2114 WARN_ON_ONCE(ftrace_start_up < 0);
2115
Steven Rostedtb8489142011-05-04 09:27:52 -04002116 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2117 ops = &global_ops;
2118 global_start_up--;
2119 WARN_ON_ONCE(global_start_up < 0);
2120 /* Don't update hash if global still has users */
2121 if (global_start_up) {
2122 WARN_ON_ONCE(!ftrace_start_up);
2123 hash_disable = false;
2124 }
2125 }
2126
2127 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002128 ftrace_hash_rec_disable(ops, 1);
2129
Steven Rostedtb8489142011-05-04 09:27:52 -04002130 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002131 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002132
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002133 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002134
2135 if (saved_ftrace_func != ftrace_trace_function) {
2136 saved_ftrace_func = ftrace_trace_function;
2137 command |= FTRACE_UPDATE_TRACE_FUNC;
2138 }
2139
2140 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002141 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002142
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002143 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002144}
2145
Ingo Molnare309b412008-05-12 21:20:51 +02002146static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002147{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002148 if (unlikely(ftrace_disabled))
2149 return;
2150
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002151 /* Force update next time */
2152 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002153 /* ftrace_start_up is true if we want ftrace running */
2154 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002155 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002156}
2157
Ingo Molnare309b412008-05-12 21:20:51 +02002158static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002159{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002160 if (unlikely(ftrace_disabled))
2161 return;
2162
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002163 /* ftrace_start_up is true if ftrace is running */
2164 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002165 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002166}
2167
Steven Rostedt3d083392008-05-12 21:20:42 +02002168static cycle_t ftrace_update_time;
2169static unsigned long ftrace_update_cnt;
2170unsigned long ftrace_update_tot_cnt;
2171
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002172static int ops_traces_mod(struct ftrace_ops *ops)
2173{
2174 struct ftrace_hash *hash;
2175
2176 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002177 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002178}
2179
Steven Rostedt31e88902008-11-14 16:21:19 -08002180static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002181{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002182 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002183 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302184 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002185 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002186 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002187
2188 /*
2189 * When adding a module, we need to check if tracers are
2190 * currently enabled and if they are set to trace all functions.
2191 * If they are, we need to enable the module functions as well
2192 * as update the reference counts for those function records.
2193 */
2194 if (mod) {
2195 struct ftrace_ops *ops;
2196
2197 for (ops = ftrace_ops_list;
2198 ops != &ftrace_list_end; ops = ops->next) {
2199 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2200 ops_traces_mod(ops))
2201 ref++;
2202 }
2203 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002204
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002205 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002206 ftrace_update_cnt = 0;
2207
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002208 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302209
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002210 for (i = 0; i < pg->index; i++) {
2211 /* If something went wrong, bail without enabling anything */
2212 if (unlikely(ftrace_disabled))
2213 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002214
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002215 p = &pg->records[i];
2216 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302217
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002218 /*
2219 * Do the initial record conversion from mcount jump
2220 * to the NOP instructions.
2221 */
2222 if (!ftrace_code_disable(mod, p))
2223 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002224
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002225 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002226
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002227 /*
2228 * If the tracing is enabled, go ahead and enable the record.
2229 *
2230 * The reason not to enable the record immediatelly is the
2231 * inherent check of ftrace_make_nop/ftrace_make_call for
2232 * correct previous instructions. Making first the NOP
2233 * conversion puts the module to the correct state, thus
2234 * passing the ftrace_make_call check.
2235 */
2236 if (ftrace_start_up && ref) {
2237 int failed = __ftrace_replace_code(p, 1);
2238 if (failed)
2239 ftrace_bug(failed, p->ip);
2240 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002241 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002242 }
2243
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002244 ftrace_new_pgs = NULL;
2245
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002246 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002247 ftrace_update_time = stop - start;
2248 ftrace_update_tot_cnt += ftrace_update_cnt;
2249
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002250 return 0;
2251}
2252
Steven Rostedta7900872011-12-16 16:23:44 -05002253static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002254{
Steven Rostedta7900872011-12-16 16:23:44 -05002255 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002256 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002257
Steven Rostedta7900872011-12-16 16:23:44 -05002258 if (WARN_ON(!count))
2259 return -EINVAL;
2260
2261 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002262
2263 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002264 * We want to fill as much as possible. No more than a page
2265 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002266 */
Steven Rostedta7900872011-12-16 16:23:44 -05002267 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2268 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002269
Steven Rostedta7900872011-12-16 16:23:44 -05002270 again:
2271 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2272
2273 if (!pg->records) {
2274 /* if we can't allocate this size, try something smaller */
2275 if (!order)
2276 return -ENOMEM;
2277 order >>= 1;
2278 goto again;
2279 }
2280
2281 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2282 pg->size = cnt;
2283
2284 if (cnt > count)
2285 cnt = count;
2286
2287 return cnt;
2288}
2289
2290static struct ftrace_page *
2291ftrace_allocate_pages(unsigned long num_to_init)
2292{
2293 struct ftrace_page *start_pg;
2294 struct ftrace_page *pg;
2295 int order;
2296 int cnt;
2297
2298 if (!num_to_init)
2299 return 0;
2300
2301 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2302 if (!pg)
2303 return NULL;
2304
2305 /*
2306 * Try to allocate as much as possible in one continues
2307 * location that fills in all of the space. We want to
2308 * waste as little space as possible.
2309 */
2310 for (;;) {
2311 cnt = ftrace_allocate_records(pg, num_to_init);
2312 if (cnt < 0)
2313 goto free_pages;
2314
2315 num_to_init -= cnt;
2316 if (!num_to_init)
2317 break;
2318
2319 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2320 if (!pg->next)
2321 goto free_pages;
2322
2323 pg = pg->next;
2324 }
2325
2326 return start_pg;
2327
2328 free_pages:
2329 while (start_pg) {
2330 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2331 free_pages((unsigned long)pg->records, order);
2332 start_pg = pg->next;
2333 kfree(pg);
2334 pg = start_pg;
2335 }
2336 pr_info("ftrace: FAILED to allocate memory for functions\n");
2337 return NULL;
2338}
2339
2340static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2341{
2342 int cnt;
2343
2344 if (!num_to_init) {
2345 pr_info("ftrace: No functions to be traced?\n");
2346 return -1;
2347 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002348
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002349 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002350 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002351 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002352
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002353 return 0;
2354}
2355
Steven Rostedt5072c592008-05-12 21:20:43 +02002356#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2357
2358struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002359 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002360 loff_t func_pos;
2361 struct ftrace_page *pg;
2362 struct dyn_ftrace *func;
2363 struct ftrace_func_probe *probe;
2364 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002365 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002366 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002367 int hidx;
2368 int idx;
2369 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002370};
2371
Ingo Molnare309b412008-05-12 21:20:51 +02002372static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002373t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002374{
2375 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002376 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002377 struct hlist_head *hhd;
2378
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002379 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002380 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002381
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002382 if (iter->probe)
2383 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002384 retry:
2385 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2386 return NULL;
2387
2388 hhd = &ftrace_func_hash[iter->hidx];
2389
2390 if (hlist_empty(hhd)) {
2391 iter->hidx++;
2392 hnd = NULL;
2393 goto retry;
2394 }
2395
2396 if (!hnd)
2397 hnd = hhd->first;
2398 else {
2399 hnd = hnd->next;
2400 if (!hnd) {
2401 iter->hidx++;
2402 goto retry;
2403 }
2404 }
2405
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002406 if (WARN_ON_ONCE(!hnd))
2407 return NULL;
2408
2409 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2410
2411 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002412}
2413
2414static void *t_hash_start(struct seq_file *m, loff_t *pos)
2415{
2416 struct ftrace_iterator *iter = m->private;
2417 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002418 loff_t l;
2419
Steven Rostedt69a30832011-12-19 15:21:16 -05002420 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2421 return NULL;
2422
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002423 if (iter->func_pos > *pos)
2424 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002425
Li Zefand82d6242009-06-24 09:54:54 +08002426 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002427 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002428 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002429 if (!p)
2430 break;
2431 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002432 if (!p)
2433 return NULL;
2434
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002435 /* Only set this if we have an item */
2436 iter->flags |= FTRACE_ITER_HASH;
2437
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002438 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002439}
2440
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002441static int
2442t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002443{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002444 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002445
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002446 rec = iter->probe;
2447 if (WARN_ON_ONCE(!rec))
2448 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002449
Steven Rostedt809dcf22009-02-16 23:06:01 -05002450 if (rec->ops->print)
2451 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2452
Steven Rostedtb375a112009-09-17 00:05:58 -04002453 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002454
2455 if (rec->data)
2456 seq_printf(m, ":%p", rec->data);
2457 seq_putc(m, '\n');
2458
2459 return 0;
2460}
2461
2462static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002463t_next(struct seq_file *m, void *v, loff_t *pos)
2464{
2465 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002466 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002467 struct dyn_ftrace *rec = NULL;
2468
Steven Rostedt45a4a232011-04-21 23:16:46 -04002469 if (unlikely(ftrace_disabled))
2470 return NULL;
2471
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002472 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002473 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002474
Steven Rostedt5072c592008-05-12 21:20:43 +02002475 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002476 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002477
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002478 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002479 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002480
Steven Rostedt5072c592008-05-12 21:20:43 +02002481 retry:
2482 if (iter->idx >= iter->pg->index) {
2483 if (iter->pg->next) {
2484 iter->pg = iter->pg->next;
2485 iter->idx = 0;
2486 goto retry;
2487 }
2488 } else {
2489 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002490 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002491 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002492
Steven Rostedt41c52c02008-05-22 11:46:33 -04002493 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002494 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2495
2496 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002497 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002498
Steven Rostedt5072c592008-05-12 21:20:43 +02002499 rec = NULL;
2500 goto retry;
2501 }
2502 }
2503
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002504 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002505 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002506
2507 iter->func = rec;
2508
2509 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002510}
2511
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002512static void reset_iter_read(struct ftrace_iterator *iter)
2513{
2514 iter->pos = 0;
2515 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002516 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002517}
2518
2519static void *t_start(struct seq_file *m, loff_t *pos)
2520{
2521 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002522 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002523 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002524 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002525
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002526 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002527
2528 if (unlikely(ftrace_disabled))
2529 return NULL;
2530
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002531 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002532 * If an lseek was done, then reset and start from beginning.
2533 */
2534 if (*pos < iter->pos)
2535 reset_iter_read(iter);
2536
2537 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002538 * For set_ftrace_filter reading, if we have the filter
2539 * off, we can short cut and just print out that all
2540 * functions are enabled.
2541 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002542 if (iter->flags & FTRACE_ITER_FILTER &&
2543 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002544 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002545 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002546 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002547 /* reset in case of seek/pread */
2548 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002549 return iter;
2550 }
2551
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002552 if (iter->flags & FTRACE_ITER_HASH)
2553 return t_hash_start(m, pos);
2554
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002555 /*
2556 * Unfortunately, we need to restart at ftrace_pages_start
2557 * every time we let go of the ftrace_mutex. This is because
2558 * those pointers can change without the lock.
2559 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002560 iter->pg = ftrace_pages_start;
2561 iter->idx = 0;
2562 for (l = 0; l <= *pos; ) {
2563 p = t_next(m, p, &l);
2564 if (!p)
2565 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002566 }
walimis5821e1b2008-11-15 15:19:06 +08002567
Steven Rostedt69a30832011-12-19 15:21:16 -05002568 if (!p)
2569 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002570
2571 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002572}
2573
2574static void t_stop(struct seq_file *m, void *p)
2575{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002576 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002577}
2578
2579static int t_show(struct seq_file *m, void *v)
2580{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002581 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002582 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002583
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002584 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002585 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002586
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002587 if (iter->flags & FTRACE_ITER_PRINTALL) {
2588 seq_printf(m, "#### all functions enabled ####\n");
2589 return 0;
2590 }
2591
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002592 rec = iter->func;
2593
Steven Rostedt5072c592008-05-12 21:20:43 +02002594 if (!rec)
2595 return 0;
2596
Steven Rostedt647bcd02011-05-03 14:39:21 -04002597 seq_printf(m, "%ps", (void *)rec->ip);
2598 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002599 seq_printf(m, " (%ld)%s",
2600 rec->flags & ~FTRACE_FL_MASK,
2601 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002602 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002603
2604 return 0;
2605}
2606
James Morris88e9d342009-09-22 16:43:43 -07002607static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002608 .start = t_start,
2609 .next = t_next,
2610 .stop = t_stop,
2611 .show = t_show,
2612};
2613
Ingo Molnare309b412008-05-12 21:20:51 +02002614static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002615ftrace_avail_open(struct inode *inode, struct file *file)
2616{
2617 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002618
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002619 if (unlikely(ftrace_disabled))
2620 return -ENODEV;
2621
Jiri Olsa50e18b92012-04-25 10:23:39 +02002622 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2623 if (iter) {
2624 iter->pg = ftrace_pages_start;
2625 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002626 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002627
Jiri Olsa50e18b92012-04-25 10:23:39 +02002628 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002629}
2630
Steven Rostedt647bcd02011-05-03 14:39:21 -04002631static int
2632ftrace_enabled_open(struct inode *inode, struct file *file)
2633{
2634 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002635
2636 if (unlikely(ftrace_disabled))
2637 return -ENODEV;
2638
Jiri Olsa50e18b92012-04-25 10:23:39 +02002639 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2640 if (iter) {
2641 iter->pg = ftrace_pages_start;
2642 iter->flags = FTRACE_ITER_ENABLED;
2643 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002644 }
2645
Jiri Olsa50e18b92012-04-25 10:23:39 +02002646 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002647}
2648
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002649static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002650{
Steven Rostedt52baf112009-02-14 01:15:39 -05002651 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002652 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002653 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002654}
2655
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002656/**
2657 * ftrace_regex_open - initialize function tracer filter files
2658 * @ops: The ftrace_ops that hold the hash filters
2659 * @flag: The type of filter to process
2660 * @inode: The inode, usually passed in to your open routine
2661 * @file: The file, usually passed in to your open routine
2662 *
2663 * ftrace_regex_open() initializes the filter files for the
2664 * @ops. Depending on @flag it may process the filter hash or
2665 * the notrace hash of @ops. With this called from the open
2666 * routine, you can use ftrace_filter_write() for the write
2667 * routine if @flag has FTRACE_ITER_FILTER set, or
2668 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04002669 * ftrace_filter_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002670 * release must call ftrace_regex_release().
2671 */
2672int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002673ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002674 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002675{
2676 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002677 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002678 int ret = 0;
2679
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002680 ftrace_ops_init(ops);
2681
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002682 if (unlikely(ftrace_disabled))
2683 return -ENODEV;
2684
Steven Rostedt5072c592008-05-12 21:20:43 +02002685 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2686 if (!iter)
2687 return -ENOMEM;
2688
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002689 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2690 kfree(iter);
2691 return -ENOMEM;
2692 }
2693
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002694 iter->ops = ops;
2695 iter->flags = flag;
2696
2697 mutex_lock(&ops->regex_lock);
2698
Steven Rostedtf45948e2011-05-02 12:29:25 -04002699 if (flag & FTRACE_ITER_NOTRACE)
2700 hash = ops->notrace_hash;
2701 else
2702 hash = ops->filter_hash;
2703
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002704 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002705 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002706 if (!iter->hash) {
2707 trace_parser_put(&iter->parser);
2708 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002709 ret = -ENOMEM;
2710 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002711 }
2712 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002713
Steven Rostedt5072c592008-05-12 21:20:43 +02002714 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002715 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002716 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002717
2718 if (file->f_mode & FMODE_READ) {
2719 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002720
2721 ret = seq_open(file, &show_ftrace_seq_ops);
2722 if (!ret) {
2723 struct seq_file *m = file->private_data;
2724 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002725 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002726 /* Failed */
2727 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002728 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002729 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002730 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002731 } else
2732 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002733
2734 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002735 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002736
2737 return ret;
2738}
2739
Steven Rostedt41c52c02008-05-22 11:46:33 -04002740static int
2741ftrace_filter_open(struct inode *inode, struct file *file)
2742{
Steven Rostedt69a30832011-12-19 15:21:16 -05002743 return ftrace_regex_open(&global_ops,
2744 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2745 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002746}
2747
2748static int
2749ftrace_notrace_open(struct inode *inode, struct file *file)
2750{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002751 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002752 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002753}
2754
Steven Rostedt64e7c442009-02-13 17:08:48 -05002755static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002756{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002757 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002758 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002759
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002760 switch (type) {
2761 case MATCH_FULL:
2762 if (strcmp(str, regex) == 0)
2763 matched = 1;
2764 break;
2765 case MATCH_FRONT_ONLY:
2766 if (strncmp(str, regex, len) == 0)
2767 matched = 1;
2768 break;
2769 case MATCH_MIDDLE_ONLY:
2770 if (strstr(str, regex))
2771 matched = 1;
2772 break;
2773 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002774 slen = strlen(str);
2775 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002776 matched = 1;
2777 break;
2778 }
2779
2780 return matched;
2781}
2782
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002783static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002784enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002785{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002786 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002787 int ret = 0;
2788
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002789 entry = ftrace_lookup_ip(hash, rec->ip);
2790 if (not) {
2791 /* Do nothing if it doesn't exist */
2792 if (!entry)
2793 return 0;
2794
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002795 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002796 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002797 /* Do nothing if it exists */
2798 if (entry)
2799 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002800
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002801 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002802 }
2803 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002804}
2805
Steven Rostedt64e7c442009-02-13 17:08:48 -05002806static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002807ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2808 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002809{
2810 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002811 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002812
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002813 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2814
2815 if (mod) {
2816 /* module lookup requires matching the module */
2817 if (!modname || strcmp(modname, mod))
2818 return 0;
2819
2820 /* blank search means to match all funcs in the mod */
2821 if (!len)
2822 return 1;
2823 }
2824
Steven Rostedt64e7c442009-02-13 17:08:48 -05002825 return ftrace_match(str, regex, len, type);
2826}
2827
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002828static int
2829match_records(struct ftrace_hash *hash, char *buff,
2830 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002831{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002832 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002833 struct ftrace_page *pg;
2834 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002835 int type = MATCH_FULL;
2836 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002837 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002838 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002839
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002840 if (len) {
2841 type = filter_parse_regex(buff, len, &search, &not);
2842 search_len = strlen(search);
2843 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002844
Steven Rostedt52baf112009-02-14 01:15:39 -05002845 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002846
2847 if (unlikely(ftrace_disabled))
2848 goto out_unlock;
2849
Steven Rostedt265c8312009-02-13 12:43:56 -05002850 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002851 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002852 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002853 if (ret < 0) {
2854 found = ret;
2855 goto out_unlock;
2856 }
Li Zefan311d16d2009-12-08 11:15:11 +08002857 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002858 }
2859 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002860 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002861 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002862
2863 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002864}
2865
Steven Rostedt64e7c442009-02-13 17:08:48 -05002866static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002867ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002868{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002869 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002870}
2871
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002872static int
2873ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002874{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002875 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002876
Steven Rostedt64e7c442009-02-13 17:08:48 -05002877 /* blank or '*' mean the same */
2878 if (strcmp(buff, "*") == 0)
2879 buff[0] = 0;
2880
2881 /* handle the case of 'dont filter this module' */
2882 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2883 buff[0] = 0;
2884 not = 1;
2885 }
2886
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002887 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002888}
2889
Steven Rostedtf6180772009-02-14 00:40:25 -05002890/*
2891 * We register the module command as a template to show others how
2892 * to register the a command as well.
2893 */
2894
2895static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002896ftrace_mod_callback(struct ftrace_hash *hash,
2897 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002898{
2899 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002900 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002901
2902 /*
2903 * cmd == 'mod' because we only registered this func
2904 * for the 'mod' ftrace_func_command.
2905 * But if you register one func with multiple commands,
2906 * you can tell which command was used by the cmd
2907 * parameter.
2908 */
2909
2910 /* we must have a module name */
2911 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002912 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002913
2914 mod = strsep(&param, ":");
2915 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002916 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002917
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002918 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002919 if (!ret)
2920 ret = -EINVAL;
2921 if (ret < 0)
2922 return ret;
2923
2924 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002925}
2926
2927static struct ftrace_func_command ftrace_mod_cmd = {
2928 .name = "mod",
2929 .func = ftrace_mod_callback,
2930};
2931
2932static int __init ftrace_mod_cmd_init(void)
2933{
2934 return register_ftrace_command(&ftrace_mod_cmd);
2935}
Steven Rostedt6f415672012-10-05 12:13:07 -04002936core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002937
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002938static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002939 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002940{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002941 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002942 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002943 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002944
2945 key = hash_long(ip, FTRACE_HASH_BITS);
2946
2947 hhd = &ftrace_func_hash[key];
2948
2949 if (hlist_empty(hhd))
2950 return;
2951
2952 /*
2953 * Disable preemption for these calls to prevent a RCU grace
2954 * period. This syncs the hash iteration and freeing of items
2955 * on the hash. rcu_read_lock is too dangerous here.
2956 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002957 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04002958 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002959 if (entry->ip == ip)
2960 entry->ops->func(ip, parent_ip, &entry->data);
2961 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002962 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002963}
2964
Steven Rostedtb6887d72009-02-17 12:32:04 -05002965static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002966{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002967 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002968 .flags = FTRACE_OPS_FL_INITIALIZED,
2969 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002970};
2971
Steven Rostedtb6887d72009-02-17 12:32:04 -05002972static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002973
Steven Rostedtb6887d72009-02-17 12:32:04 -05002974static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002975{
Steven Rostedtb8489142011-05-04 09:27:52 -04002976 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002977 int i;
2978
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04002979 if (ftrace_probe_registered) {
2980 /* still need to update the function call sites */
2981 if (ftrace_enabled)
2982 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002983 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04002984 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05002985
2986 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2987 struct hlist_head *hhd = &ftrace_func_hash[i];
2988 if (hhd->first)
2989 break;
2990 }
2991 /* Nothing registered? */
2992 if (i == FTRACE_FUNC_HASHSIZE)
2993 return;
2994
Steven Rostedtb8489142011-05-04 09:27:52 -04002995 ret = __register_ftrace_function(&trace_probe_ops);
2996 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002997 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002998
Steven Rostedtb6887d72009-02-17 12:32:04 -05002999 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003000}
3001
Steven Rostedtb6887d72009-02-17 12:32:04 -05003002static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003003{
Steven Rostedtb8489142011-05-04 09:27:52 -04003004 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003005 int i;
3006
Steven Rostedtb6887d72009-02-17 12:32:04 -05003007 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003008 return;
3009
3010 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3011 struct hlist_head *hhd = &ftrace_func_hash[i];
3012 if (hhd->first)
3013 return;
3014 }
3015
3016 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04003017 ret = __unregister_ftrace_function(&trace_probe_ops);
3018 if (!ret)
3019 ftrace_shutdown(&trace_probe_ops, 0);
3020
Steven Rostedtb6887d72009-02-17 12:32:04 -05003021 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003022}
3023
3024
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003025static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003026{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003027 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003028 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003029 kfree(entry);
3030}
3031
Steven Rostedt59df055f2009-02-14 15:29:06 -05003032int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003033register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003034 void *data)
3035{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003036 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003037 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3038 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003039 struct ftrace_page *pg;
3040 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003041 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003042 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003043 int count = 0;
3044 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003045 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003046
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003047 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003048 len = strlen(search);
3049
Steven Rostedtb6887d72009-02-17 12:32:04 -05003050 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003051 if (WARN_ON(not))
3052 return -EINVAL;
3053
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003054 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003055
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003056 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3057 if (!hash) {
3058 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003059 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003060 }
3061
3062 if (unlikely(ftrace_disabled)) {
3063 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003064 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003065 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003066
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003067 mutex_lock(&ftrace_lock);
3068
Steven Rostedt59df055f2009-02-14 15:29:06 -05003069 do_for_each_ftrace_rec(pg, rec) {
3070
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003071 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003072 continue;
3073
3074 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3075 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003076 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003077 if (!count)
3078 count = -ENOMEM;
3079 goto out_unlock;
3080 }
3081
3082 count++;
3083
3084 entry->data = data;
3085
3086 /*
3087 * The caller might want to do something special
3088 * for each function we find. We call the callback
3089 * to give the caller an opportunity to do so.
3090 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003091 if (ops->init) {
3092 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003093 /* caller does not like this func */
3094 kfree(entry);
3095 continue;
3096 }
3097 }
3098
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003099 ret = enter_record(hash, rec, 0);
3100 if (ret < 0) {
3101 kfree(entry);
3102 count = ret;
3103 goto out_unlock;
3104 }
3105
Steven Rostedt59df055f2009-02-14 15:29:06 -05003106 entry->ops = ops;
3107 entry->ip = rec->ip;
3108
3109 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3110 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3111
3112 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003113
3114 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3115 if (ret < 0)
3116 count = ret;
3117
Steven Rostedtb6887d72009-02-17 12:32:04 -05003118 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003119
3120 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003121 mutex_unlock(&ftrace_lock);
3122 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003123 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003124 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003125
3126 return count;
3127}
3128
3129enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003130 PROBE_TEST_FUNC = 1,
3131 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003132};
3133
3134static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003135__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003136 void *data, int flags)
3137{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003138 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003139 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003140 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003141 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003142 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003143 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003144 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003145 char str[KSYM_SYMBOL_LEN];
3146 int type = MATCH_FULL;
3147 int i, len = 0;
3148 char *search;
3149
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003150 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003151 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003152 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003153 int not;
3154
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003155 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003156 len = strlen(search);
3157
Steven Rostedtb6887d72009-02-17 12:32:04 -05003158 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003159 if (WARN_ON(not))
3160 return;
3161 }
3162
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003163 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003164
3165 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3166 if (!hash)
3167 /* Hmm, should report this somehow */
3168 goto out_unlock;
3169
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003170 INIT_LIST_HEAD(&free_list);
3171
Steven Rostedt59df055f2009-02-14 15:29:06 -05003172 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3173 struct hlist_head *hhd = &ftrace_func_hash[i];
3174
Sasha Levinb67bfe02013-02-27 17:06:00 -08003175 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003176
3177 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003178 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003179 continue;
3180
Steven Rostedtb6887d72009-02-17 12:32:04 -05003181 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003182 continue;
3183
3184 /* do this last, since it is the most expensive */
3185 if (glob) {
3186 kallsyms_lookup(entry->ip, NULL, NULL,
3187 NULL, str);
3188 if (!ftrace_match(str, glob, len, type))
3189 continue;
3190 }
3191
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003192 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3193 /* It is possible more than one entry had this ip */
3194 if (rec_entry)
3195 free_hash_entry(hash, rec_entry);
3196
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003197 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003198 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003199 }
3200 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003201 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003202 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003203 /*
3204 * Remove after the disable is called. Otherwise, if the last
3205 * probe is removed, a null hash means *all enabled*.
3206 */
3207 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003208 synchronize_sched();
3209 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3210 list_del(&entry->free_list);
3211 ftrace_free_entry(entry);
3212 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003213 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003214
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003215 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003216 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003217 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003218}
3219
3220void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003221unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003222 void *data)
3223{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003224 __unregister_ftrace_function_probe(glob, ops, data,
3225 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003226}
3227
3228void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003229unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003230{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003231 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003232}
3233
Steven Rostedtb6887d72009-02-17 12:32:04 -05003234void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003235{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003236 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003237}
3238
Steven Rostedtf6180772009-02-14 00:40:25 -05003239static LIST_HEAD(ftrace_commands);
3240static DEFINE_MUTEX(ftrace_cmd_mutex);
3241
3242int register_ftrace_command(struct ftrace_func_command *cmd)
3243{
3244 struct ftrace_func_command *p;
3245 int ret = 0;
3246
3247 mutex_lock(&ftrace_cmd_mutex);
3248 list_for_each_entry(p, &ftrace_commands, list) {
3249 if (strcmp(cmd->name, p->name) == 0) {
3250 ret = -EBUSY;
3251 goto out_unlock;
3252 }
3253 }
3254 list_add(&cmd->list, &ftrace_commands);
3255 out_unlock:
3256 mutex_unlock(&ftrace_cmd_mutex);
3257
3258 return ret;
3259}
3260
3261int unregister_ftrace_command(struct ftrace_func_command *cmd)
3262{
3263 struct ftrace_func_command *p, *n;
3264 int ret = -ENODEV;
3265
3266 mutex_lock(&ftrace_cmd_mutex);
3267 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3268 if (strcmp(cmd->name, p->name) == 0) {
3269 ret = 0;
3270 list_del_init(&p->list);
3271 goto out_unlock;
3272 }
3273 }
3274 out_unlock:
3275 mutex_unlock(&ftrace_cmd_mutex);
3276
3277 return ret;
3278}
3279
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003280static int ftrace_process_regex(struct ftrace_hash *hash,
3281 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003282{
Steven Rostedtf6180772009-02-14 00:40:25 -05003283 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003284 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003285 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003286
3287 func = strsep(&next, ":");
3288
3289 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003290 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003291 if (!ret)
3292 ret = -EINVAL;
3293 if (ret < 0)
3294 return ret;
3295 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003296 }
3297
Steven Rostedtf6180772009-02-14 00:40:25 -05003298 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003299
3300 command = strsep(&next, ":");
3301
Steven Rostedtf6180772009-02-14 00:40:25 -05003302 mutex_lock(&ftrace_cmd_mutex);
3303 list_for_each_entry(p, &ftrace_commands, list) {
3304 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003305 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003306 goto out_unlock;
3307 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003308 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003309 out_unlock:
3310 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003311
Steven Rostedtf6180772009-02-14 00:40:25 -05003312 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003313}
3314
Ingo Molnare309b412008-05-12 21:20:51 +02003315static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003316ftrace_regex_write(struct file *file, const char __user *ubuf,
3317 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003318{
3319 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003320 struct trace_parser *parser;
3321 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003322
Li Zefan4ba79782009-09-22 13:52:20 +08003323 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003324 return 0;
3325
Steven Rostedt5072c592008-05-12 21:20:43 +02003326 if (file->f_mode & FMODE_READ) {
3327 struct seq_file *m = file->private_data;
3328 iter = m->private;
3329 } else
3330 iter = file->private_data;
3331
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003332 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003333 return -ENODEV;
3334
3335 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003336
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003337 parser = &iter->parser;
3338 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003339
Li Zefan4ba79782009-09-22 13:52:20 +08003340 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003341 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003342 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003343 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003344 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003345 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003346 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003347 }
3348
Steven Rostedt5072c592008-05-12 21:20:43 +02003349 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003350 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003351 return ret;
3352}
3353
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003354ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003355ftrace_filter_write(struct file *file, const char __user *ubuf,
3356 size_t cnt, loff_t *ppos)
3357{
3358 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3359}
3360
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003361ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003362ftrace_notrace_write(struct file *file, const char __user *ubuf,
3363 size_t cnt, loff_t *ppos)
3364{
3365 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3366}
3367
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003368static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003369ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3370{
3371 struct ftrace_func_entry *entry;
3372
3373 if (!ftrace_location(ip))
3374 return -EINVAL;
3375
3376 if (remove) {
3377 entry = ftrace_lookup_ip(hash, ip);
3378 if (!entry)
3379 return -ENOENT;
3380 free_hash_entry(hash, entry);
3381 return 0;
3382 }
3383
3384 return add_hash_entry(hash, ip);
3385}
3386
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003387static void ftrace_ops_update_code(struct ftrace_ops *ops)
3388{
3389 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3390 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3391}
3392
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003393static int
3394ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3395 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003396{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003397 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003398 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003399 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003400
Steven Rostedt936e0742011-05-05 22:54:01 -04003401 /* All global ops uses the global ops filters */
3402 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3403 ops = &global_ops;
3404
Steven Rostedt41c52c02008-05-22 11:46:33 -04003405 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003406 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003407
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003408 mutex_lock(&ops->regex_lock);
3409
Steven Rostedtf45948e2011-05-02 12:29:25 -04003410 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003411 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003412 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003413 orig_hash = &ops->notrace_hash;
3414
3415 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003416 if (!hash) {
3417 ret = -ENOMEM;
3418 goto out_regex_unlock;
3419 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003420
Steven Rostedt41c52c02008-05-22 11:46:33 -04003421 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003422 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003423 if (buf && !ftrace_match_records(hash, buf, len)) {
3424 ret = -EINVAL;
3425 goto out_regex_unlock;
3426 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003427 if (ip) {
3428 ret = ftrace_match_addr(hash, ip, remove);
3429 if (ret < 0)
3430 goto out_regex_unlock;
3431 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003432
3433 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003434 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003435 if (!ret)
3436 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003437
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003438 mutex_unlock(&ftrace_lock);
3439
Jiri Olsaac483c42012-01-02 10:04:14 +01003440 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003441 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003442
3443 free_ftrace_hash(hash);
3444 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003445}
3446
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003447static int
3448ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3449 int reset, int enable)
3450{
3451 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3452}
3453
3454/**
3455 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3456 * @ops - the ops to set the filter with
3457 * @ip - the address to add to or remove from the filter.
3458 * @remove - non zero to remove the ip from the filter
3459 * @reset - non zero to reset all filters before applying this filter.
3460 *
3461 * Filters denote which functions should be enabled when tracing is enabled
3462 * If @ip is NULL, it failes to update filter.
3463 */
3464int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3465 int remove, int reset)
3466{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003467 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003468 return ftrace_set_addr(ops, ip, remove, reset, 1);
3469}
3470EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3471
3472static int
3473ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3474 int reset, int enable)
3475{
3476 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3477}
3478
Steven Rostedt77a2b372008-05-12 21:20:45 +02003479/**
3480 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003481 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003482 * @buf - the string that holds the function filter text.
3483 * @len - the length of the string.
3484 * @reset - non zero to reset all filters before applying this filter.
3485 *
3486 * Filters denote which functions should be enabled when tracing is enabled.
3487 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3488 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003489int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003490 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003491{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003492 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003493 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003494}
Steven Rostedt936e0742011-05-05 22:54:01 -04003495EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003496
Steven Rostedt41c52c02008-05-22 11:46:33 -04003497/**
3498 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003499 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003500 * @buf - the string that holds the function notrace text.
3501 * @len - the length of the string.
3502 * @reset - non zero to reset all filters before applying this filter.
3503 *
3504 * Notrace Filters denote which functions should not be enabled when tracing
3505 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3506 * for tracing.
3507 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003508int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003509 int len, int reset)
3510{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003511 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003512 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003513}
3514EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3515/**
3516 * ftrace_set_filter - set a function to filter on in ftrace
3517 * @ops - the ops to set the filter with
3518 * @buf - the string that holds the function filter text.
3519 * @len - the length of the string.
3520 * @reset - non zero to reset all filters before applying this filter.
3521 *
3522 * Filters denote which functions should be enabled when tracing is enabled.
3523 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3524 */
3525void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3526{
3527 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3528}
3529EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3530
3531/**
3532 * ftrace_set_notrace - set a function to not trace in ftrace
3533 * @ops - the ops to set the notrace filter with
3534 * @buf - the string that holds the function notrace text.
3535 * @len - the length of the string.
3536 * @reset - non zero to reset all filters before applying this filter.
3537 *
3538 * Notrace Filters denote which functions should not be enabled when tracing
3539 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3540 * for tracing.
3541 */
3542void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003543{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003544 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003545}
Steven Rostedt936e0742011-05-05 22:54:01 -04003546EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003547
Steven Rostedt2af15d62009-05-28 13:37:24 -04003548/*
3549 * command line interface to allow users to set filters on boot up.
3550 */
3551#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3552static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3553static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3554
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003555/* Used by function selftest to not test if filter is set */
3556bool ftrace_filter_param __initdata;
3557
Steven Rostedt2af15d62009-05-28 13:37:24 -04003558static int __init set_ftrace_notrace(char *str)
3559{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003560 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003561 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003562 return 1;
3563}
3564__setup("ftrace_notrace=", set_ftrace_notrace);
3565
3566static int __init set_ftrace_filter(char *str)
3567{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003568 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003569 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003570 return 1;
3571}
3572__setup("ftrace_filter=", set_ftrace_filter);
3573
Stefan Assmann369bc182009-10-12 22:17:21 +02003574#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003575static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003576static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3577
Stefan Assmann369bc182009-10-12 22:17:21 +02003578static int __init set_graph_function(char *str)
3579{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003580 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003581 return 1;
3582}
3583__setup("ftrace_graph_filter=", set_graph_function);
3584
3585static void __init set_ftrace_early_graph(char *buf)
3586{
3587 int ret;
3588 char *func;
3589
3590 while (buf) {
3591 func = strsep(&buf, ",");
3592 /* we allow only one expression at a time */
3593 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3594 func);
3595 if (ret)
3596 printk(KERN_DEBUG "ftrace: function %s not "
3597 "traceable\n", func);
3598 }
3599}
3600#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3601
Steven Rostedt2a85a372011-12-19 21:57:44 -05003602void __init
3603ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003604{
3605 char *func;
3606
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003607 ftrace_ops_init(ops);
3608
Steven Rostedt2af15d62009-05-28 13:37:24 -04003609 while (buf) {
3610 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003611 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003612 }
3613}
3614
3615static void __init set_ftrace_early_filters(void)
3616{
3617 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003618 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003619 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003620 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003621#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3622 if (ftrace_graph_buf[0])
3623 set_ftrace_early_graph(ftrace_graph_buf);
3624#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003625}
3626
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003627int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003628{
3629 struct seq_file *m = (struct seq_file *)file->private_data;
3630 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003631 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003632 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003633 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003634 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003635
Steven Rostedt5072c592008-05-12 21:20:43 +02003636 if (file->f_mode & FMODE_READ) {
3637 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003638 seq_release(inode, file);
3639 } else
3640 iter = file->private_data;
3641
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003642 parser = &iter->parser;
3643 if (trace_parser_loaded(parser)) {
3644 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003645 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003646 }
3647
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003648 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003649
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003650 mutex_lock(&iter->ops->regex_lock);
3651
Steven Rostedt058e2972011-04-29 22:35:33 -04003652 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003653 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3654
3655 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003656 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003657 else
3658 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003659
Steven Rostedt058e2972011-04-29 22:35:33 -04003660 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003661 ret = ftrace_hash_move(iter->ops, filter_hash,
3662 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003663 if (!ret)
3664 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003665
Steven Rostedt058e2972011-04-29 22:35:33 -04003666 mutex_unlock(&ftrace_lock);
3667 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003668
3669 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003670 free_ftrace_hash(iter->hash);
3671 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003672
Steven Rostedt5072c592008-05-12 21:20:43 +02003673 return 0;
3674}
3675
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003676static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003677 .open = ftrace_avail_open,
3678 .read = seq_read,
3679 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003680 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003681};
3682
Steven Rostedt647bcd02011-05-03 14:39:21 -04003683static const struct file_operations ftrace_enabled_fops = {
3684 .open = ftrace_enabled_open,
3685 .read = seq_read,
3686 .llseek = seq_lseek,
3687 .release = seq_release_private,
3688};
3689
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003690static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003691 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003692 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003693 .write = ftrace_filter_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003694 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003695 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003696};
3697
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003698static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003699 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003700 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003701 .write = ftrace_notrace_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003702 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003703 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003704};
3705
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003706#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3707
3708static DEFINE_MUTEX(graph_lock);
3709
3710int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003711int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003712unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3713
3714static void *
Li Zefan85951842009-06-24 09:54:00 +08003715__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003716{
Li Zefan85951842009-06-24 09:54:00 +08003717 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003718 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003719 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003720}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003721
Li Zefan85951842009-06-24 09:54:00 +08003722static void *
3723g_next(struct seq_file *m, void *v, loff_t *pos)
3724{
3725 (*pos)++;
3726 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003727}
3728
3729static void *g_start(struct seq_file *m, loff_t *pos)
3730{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003731 mutex_lock(&graph_lock);
3732
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003733 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003734 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003735 return (void *)1;
3736
Li Zefan85951842009-06-24 09:54:00 +08003737 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003738}
3739
3740static void g_stop(struct seq_file *m, void *p)
3741{
3742 mutex_unlock(&graph_lock);
3743}
3744
3745static int g_show(struct seq_file *m, void *v)
3746{
3747 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003748
3749 if (!ptr)
3750 return 0;
3751
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003752 if (ptr == (unsigned long *)1) {
3753 seq_printf(m, "#### all functions enabled ####\n");
3754 return 0;
3755 }
3756
Steven Rostedtb375a112009-09-17 00:05:58 -04003757 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003758
3759 return 0;
3760}
3761
James Morris88e9d342009-09-22 16:43:43 -07003762static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003763 .start = g_start,
3764 .next = g_next,
3765 .stop = g_stop,
3766 .show = g_show,
3767};
3768
3769static int
3770ftrace_graph_open(struct inode *inode, struct file *file)
3771{
3772 int ret = 0;
3773
3774 if (unlikely(ftrace_disabled))
3775 return -ENODEV;
3776
3777 mutex_lock(&graph_lock);
3778 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003779 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003780 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003781 ftrace_graph_count = 0;
3782 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3783 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003784 mutex_unlock(&graph_lock);
3785
Li Zefana4ec5e02009-09-18 14:06:28 +08003786 if (file->f_mode & FMODE_READ)
3787 ret = seq_open(file, &ftrace_graph_seq_ops);
3788
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003789 return ret;
3790}
3791
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003792static int
Li Zefan87827112009-07-23 11:29:11 +08003793ftrace_graph_release(struct inode *inode, struct file *file)
3794{
3795 if (file->f_mode & FMODE_READ)
3796 seq_release(inode, file);
3797 return 0;
3798}
3799
3800static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003801ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003802{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003803 struct dyn_ftrace *rec;
3804 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003805 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003806 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003807 int type, not;
3808 char *search;
3809 bool exists;
3810 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003811
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003812 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003813 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003814 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3815 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003816
3817 search_len = strlen(search);
3818
Steven Rostedt52baf112009-02-14 01:15:39 -05003819 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003820
3821 if (unlikely(ftrace_disabled)) {
3822 mutex_unlock(&ftrace_lock);
3823 return -ENODEV;
3824 }
3825
Steven Rostedt265c8312009-02-13 12:43:56 -05003826 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003827
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003828 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003829 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003830 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003831 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003832 if (array[i] == rec->ip) {
3833 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003834 break;
3835 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003836 }
3837
3838 if (!not) {
3839 fail = 0;
3840 if (!exists) {
3841 array[(*idx)++] = rec->ip;
3842 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3843 goto out;
3844 }
3845 } else {
3846 if (exists) {
3847 array[i] = array[--(*idx)];
3848 array[*idx] = 0;
3849 fail = 0;
3850 }
3851 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003852 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003853 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003854out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003855 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003856
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003857 if (fail)
3858 return -EINVAL;
3859
Namhyung Kim9f50afc2013-04-11 16:01:38 +09003860 ftrace_graph_filter_enabled = !!(*idx);
3861
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003862 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003863}
3864
3865static ssize_t
3866ftrace_graph_write(struct file *file, const char __user *ubuf,
3867 size_t cnt, loff_t *ppos)
3868{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003869 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003870 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003871
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003872 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003873 return 0;
3874
3875 mutex_lock(&graph_lock);
3876
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003877 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3878 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003879 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003880 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003881
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003882 read = trace_get_user(&parser, ubuf, cnt, ppos);
3883
Li Zefan4ba79782009-09-22 13:52:20 +08003884 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003885 parser.buffer[parser.idx] = 0;
3886
3887 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003888 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003889 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003890 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003891 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003892 }
3893
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003894 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003895
3896out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003897 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003898out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003899 mutex_unlock(&graph_lock);
3900
3901 return ret;
3902}
3903
3904static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003905 .open = ftrace_graph_open,
3906 .read = seq_read,
3907 .write = ftrace_graph_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003908 .llseek = ftrace_filter_lseek,
Li Zefan87827112009-07-23 11:29:11 +08003909 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003910};
3911#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3912
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003913static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003914{
Steven Rostedt5072c592008-05-12 21:20:43 +02003915
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003916 trace_create_file("available_filter_functions", 0444,
3917 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003918
Steven Rostedt647bcd02011-05-03 14:39:21 -04003919 trace_create_file("enabled_functions", 0444,
3920 d_tracer, NULL, &ftrace_enabled_fops);
3921
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003922 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3923 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003924
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003925 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003926 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003927
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003928#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003929 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003930 NULL,
3931 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003932#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3933
Steven Rostedt5072c592008-05-12 21:20:43 +02003934 return 0;
3935}
3936
Steven Rostedt9fd49322012-04-24 22:32:06 -04003937static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003938{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003939 const unsigned long *ipa = a;
3940 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003941
Steven Rostedt9fd49322012-04-24 22:32:06 -04003942 if (*ipa > *ipb)
3943 return 1;
3944 if (*ipa < *ipb)
3945 return -1;
3946 return 0;
3947}
3948
3949static void ftrace_swap_ips(void *a, void *b, int size)
3950{
3951 unsigned long *ipa = a;
3952 unsigned long *ipb = b;
3953 unsigned long t;
3954
3955 t = *ipa;
3956 *ipa = *ipb;
3957 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003958}
3959
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003960static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003961 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003962 unsigned long *end)
3963{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003964 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003965 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003966 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003967 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003968 unsigned long *p;
3969 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003970 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003971 int ret = -ENOMEM;
3972
3973 count = end - start;
3974
3975 if (!count)
3976 return 0;
3977
Steven Rostedt9fd49322012-04-24 22:32:06 -04003978 sort(start, count, sizeof(*start),
3979 ftrace_cmp_ips, ftrace_swap_ips);
3980
Steven Rostedt706c81f2012-04-24 23:45:26 -04003981 start_pg = ftrace_allocate_pages(count);
3982 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05003983 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003984
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003985 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003986
Steven Rostedt32082302011-12-16 14:42:37 -05003987 /*
3988 * Core and each module needs their own pages, as
3989 * modules will free them when they are removed.
3990 * Force a new page to be allocated for modules.
3991 */
Steven Rostedta7900872011-12-16 16:23:44 -05003992 if (!mod) {
3993 WARN_ON(ftrace_pages || ftrace_pages_start);
3994 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003995 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003996 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003997 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003998 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003999
Steven Rostedta7900872011-12-16 16:23:44 -05004000 if (WARN_ON(ftrace_pages->next)) {
4001 /* Hmm, we have free pages? */
4002 while (ftrace_pages->next)
4003 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004004 }
Steven Rostedta7900872011-12-16 16:23:44 -05004005
Steven Rostedt706c81f2012-04-24 23:45:26 -04004006 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004007 }
4008
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004009 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004010 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004011 while (p < end) {
4012 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004013 /*
4014 * Some architecture linkers will pad between
4015 * the different mcount_loc sections of different
4016 * object files to satisfy alignments.
4017 * Skip any NULL pointers.
4018 */
4019 if (!addr)
4020 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004021
4022 if (pg->index == pg->size) {
4023 /* We should have allocated enough */
4024 if (WARN_ON(!pg->next))
4025 break;
4026 pg = pg->next;
4027 }
4028
4029 rec = &pg->records[pg->index++];
4030 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004031 }
4032
Steven Rostedt706c81f2012-04-24 23:45:26 -04004033 /* We should have used all pages */
4034 WARN_ON(pg->next);
4035
4036 /* Assign the last page to ftrace_pages */
4037 ftrace_pages = pg;
4038
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004039 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004040 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004041
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004042 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004043 * We only need to disable interrupts on start up
4044 * because we are modifying code that an interrupt
4045 * may execute, and the modification is not atomic.
4046 * But for modules, nothing runs the code we modify
4047 * until we are finished with it, and there's no
4048 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004049 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004050 if (!mod)
4051 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08004052 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004053 if (!mod)
4054 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004055 ret = 0;
4056 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004057 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004058
Steven Rostedta7900872011-12-16 16:23:44 -05004059 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004060}
4061
Steven Rostedt93eb6772009-04-15 13:24:06 -04004062#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004063
4064#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4065
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004066void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004067{
4068 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004069 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004070 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004071 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004072
Steven Rostedt93eb6772009-04-15 13:24:06 -04004073 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004074
4075 if (ftrace_disabled)
4076 goto out_unlock;
4077
Steven Rostedt32082302011-12-16 14:42:37 -05004078 /*
4079 * Each module has its own ftrace_pages, remove
4080 * them from the list.
4081 */
4082 last_pg = &ftrace_pages_start;
4083 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4084 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004085 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004086 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004087 * As core pages are first, the first
4088 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004089 */
Steven Rostedt32082302011-12-16 14:42:37 -05004090 if (WARN_ON(pg == ftrace_pages_start))
4091 goto out_unlock;
4092
4093 /* Check if we are deleting the last page */
4094 if (pg == ftrace_pages)
4095 ftrace_pages = next_to_ftrace_page(last_pg);
4096
4097 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004098 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4099 free_pages((unsigned long)pg->records, order);
4100 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004101 } else
4102 last_pg = &pg->next;
4103 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004104 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004105 mutex_unlock(&ftrace_lock);
4106}
4107
4108static void ftrace_init_module(struct module *mod,
4109 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004110{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004111 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004112 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004113 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004114}
4115
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004116static int ftrace_module_notify_enter(struct notifier_block *self,
4117 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004118{
4119 struct module *mod = data;
4120
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004121 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004122 ftrace_init_module(mod, mod->ftrace_callsites,
4123 mod->ftrace_callsites +
4124 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004125 return 0;
4126}
4127
4128static int ftrace_module_notify_exit(struct notifier_block *self,
4129 unsigned long val, void *data)
4130{
4131 struct module *mod = data;
4132
4133 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004134 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004135
4136 return 0;
4137}
4138#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004139static int ftrace_module_notify_enter(struct notifier_block *self,
4140 unsigned long val, void *data)
4141{
4142 return 0;
4143}
4144static int ftrace_module_notify_exit(struct notifier_block *self,
4145 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004146{
4147 return 0;
4148}
4149#endif /* CONFIG_MODULES */
4150
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004151struct notifier_block ftrace_module_enter_nb = {
4152 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004153 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004154};
4155
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004156struct notifier_block ftrace_module_exit_nb = {
4157 .notifier_call = ftrace_module_notify_exit,
4158 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4159};
4160
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004161extern unsigned long __start_mcount_loc[];
4162extern unsigned long __stop_mcount_loc[];
4163
4164void __init ftrace_init(void)
4165{
4166 unsigned long count, addr, flags;
4167 int ret;
4168
4169 /* Keep the ftrace pointer to the stub */
4170 addr = (unsigned long)ftrace_stub;
4171
4172 local_irq_save(flags);
4173 ftrace_dyn_arch_init(&addr);
4174 local_irq_restore(flags);
4175
4176 /* ftrace_dyn_arch_init places the return code in addr */
4177 if (addr)
4178 goto failed;
4179
4180 count = __stop_mcount_loc - __start_mcount_loc;
4181
4182 ret = ftrace_dyn_table_alloc(count);
4183 if (ret)
4184 goto failed;
4185
4186 last_ftrace_enabled = ftrace_enabled = 1;
4187
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004188 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004189 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004190 __stop_mcount_loc);
4191
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004192 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004193 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004194 pr_warning("Failed to register trace ftrace module enter notifier\n");
4195
4196 ret = register_module_notifier(&ftrace_module_exit_nb);
4197 if (ret)
4198 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004199
Steven Rostedt2af15d62009-05-28 13:37:24 -04004200 set_ftrace_early_filters();
4201
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004202 return;
4203 failed:
4204 ftrace_disabled = 1;
4205}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004206
Steven Rostedt3d083392008-05-12 21:20:42 +02004207#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004208
Steven Rostedt2b499382011-05-03 22:49:52 -04004209static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004210 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004211 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4212 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004213};
4214
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004215static int __init ftrace_nodyn_init(void)
4216{
4217 ftrace_enabled = 1;
4218 return 0;
4219}
Steven Rostedt6f415672012-10-05 12:13:07 -04004220core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004221
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004222static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4223static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004224/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004225# define ftrace_startup(ops, command) \
4226 ({ \
4227 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4228 0; \
4229 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04004230# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004231# define ftrace_startup_sysctl() do { } while (0)
4232# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004233
4234static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004235ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004236{
4237 return 1;
4238}
4239
Steven Rostedt3d083392008-05-12 21:20:42 +02004240#endif /* CONFIG_DYNAMIC_FTRACE */
4241
Steven Rostedtb8489142011-05-04 09:27:52 -04004242static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004243ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004244 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004245{
Jiri Olsae2484912012-02-15 15:51:48 +01004246 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4247 return;
4248
4249 /*
4250 * Some of the ops may be dynamically allocated,
4251 * they must be freed after a synchronize_sched().
4252 */
4253 preempt_disable_notrace();
4254 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt0a016402012-11-02 17:03:03 -04004255 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004256 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4257 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004258 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004259 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004260 } while_for_each_ftrace_op(op);
Jiri Olsae2484912012-02-15 15:51:48 +01004261 trace_recursion_clear(TRACE_CONTROL_BIT);
4262 preempt_enable_notrace();
4263}
4264
4265static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004266 .func = ftrace_ops_control_func,
4267 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4268 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004269};
4270
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004271static inline void
4272__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004273 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004274{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004275 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004276 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004277
Steven Rostedtccf36722012-06-05 09:44:25 -04004278 if (function_trace_stop)
4279 return;
4280
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004281 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4282 if (bit < 0)
4283 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004284
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004285 /*
4286 * Some of the ops may be dynamically allocated,
4287 * they must be freed after a synchronize_sched().
4288 */
4289 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004290 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004291 if (ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004292 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004293 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004294 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004295 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004296}
4297
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004298/*
4299 * Some archs only support passing ip and parent_ip. Even though
4300 * the list function ignores the op parameter, we do not want any
4301 * C side effects, where a function is called without the caller
4302 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004303 * Archs are to support both the regs and ftrace_ops at the same time.
4304 * If they support ftrace_ops, it is assumed they support regs.
4305 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004306 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4307 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004308 * An architecture can pass partial regs with ftrace_ops and still
4309 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004310 */
4311#if ARCH_SUPPORTS_FTRACE_OPS
4312static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004313 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004314{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004315 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004316}
4317#else
4318static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4319{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004320 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004321}
4322#endif
4323
Steven Rostedte32d8952008-12-04 00:26:41 -05004324static void clear_ftrace_swapper(void)
4325{
4326 struct task_struct *p;
4327 int cpu;
4328
4329 get_online_cpus();
4330 for_each_online_cpu(cpu) {
4331 p = idle_task(cpu);
4332 clear_tsk_trace_trace(p);
4333 }
4334 put_online_cpus();
4335}
4336
4337static void set_ftrace_swapper(void)
4338{
4339 struct task_struct *p;
4340 int cpu;
4341
4342 get_online_cpus();
4343 for_each_online_cpu(cpu) {
4344 p = idle_task(cpu);
4345 set_tsk_trace_trace(p);
4346 }
4347 put_online_cpus();
4348}
4349
4350static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004351{
4352 struct task_struct *p;
4353
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004354 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004355 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004356 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004357 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004358 rcu_read_unlock();
4359
Steven Rostedte32d8952008-12-04 00:26:41 -05004360 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004361}
4362
Steven Rostedte32d8952008-12-04 00:26:41 -05004363static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004364{
4365 struct task_struct *p;
4366
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004367 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004368 do_each_pid_task(pid, PIDTYPE_PID, p) {
4369 set_tsk_trace_trace(p);
4370 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004371 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004372}
4373
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004374static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004375{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004376 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004377 clear_ftrace_swapper();
4378 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004379 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004380}
4381
4382static void set_ftrace_pid_task(struct pid *pid)
4383{
4384 if (pid == ftrace_swapper_pid)
4385 set_ftrace_swapper();
4386 else
4387 set_ftrace_pid(pid);
4388}
4389
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004390static int ftrace_pid_add(int p)
4391{
4392 struct pid *pid;
4393 struct ftrace_pid *fpid;
4394 int ret = -EINVAL;
4395
4396 mutex_lock(&ftrace_lock);
4397
4398 if (!p)
4399 pid = ftrace_swapper_pid;
4400 else
4401 pid = find_get_pid(p);
4402
4403 if (!pid)
4404 goto out;
4405
4406 ret = 0;
4407
4408 list_for_each_entry(fpid, &ftrace_pids, list)
4409 if (fpid->pid == pid)
4410 goto out_put;
4411
4412 ret = -ENOMEM;
4413
4414 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4415 if (!fpid)
4416 goto out_put;
4417
4418 list_add(&fpid->list, &ftrace_pids);
4419 fpid->pid = pid;
4420
4421 set_ftrace_pid_task(pid);
4422
4423 ftrace_update_pid_func();
4424 ftrace_startup_enable(0);
4425
4426 mutex_unlock(&ftrace_lock);
4427 return 0;
4428
4429out_put:
4430 if (pid != ftrace_swapper_pid)
4431 put_pid(pid);
4432
4433out:
4434 mutex_unlock(&ftrace_lock);
4435 return ret;
4436}
4437
4438static void ftrace_pid_reset(void)
4439{
4440 struct ftrace_pid *fpid, *safe;
4441
4442 mutex_lock(&ftrace_lock);
4443 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4444 struct pid *pid = fpid->pid;
4445
4446 clear_ftrace_pid_task(pid);
4447
4448 list_del(&fpid->list);
4449 kfree(fpid);
4450 }
4451
4452 ftrace_update_pid_func();
4453 ftrace_startup_enable(0);
4454
4455 mutex_unlock(&ftrace_lock);
4456}
4457
4458static void *fpid_start(struct seq_file *m, loff_t *pos)
4459{
4460 mutex_lock(&ftrace_lock);
4461
4462 if (list_empty(&ftrace_pids) && (!*pos))
4463 return (void *) 1;
4464
4465 return seq_list_start(&ftrace_pids, *pos);
4466}
4467
4468static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4469{
4470 if (v == (void *)1)
4471 return NULL;
4472
4473 return seq_list_next(v, &ftrace_pids, pos);
4474}
4475
4476static void fpid_stop(struct seq_file *m, void *p)
4477{
4478 mutex_unlock(&ftrace_lock);
4479}
4480
4481static int fpid_show(struct seq_file *m, void *v)
4482{
4483 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4484
4485 if (v == (void *)1) {
4486 seq_printf(m, "no pid\n");
4487 return 0;
4488 }
4489
4490 if (fpid->pid == ftrace_swapper_pid)
4491 seq_printf(m, "swapper tasks\n");
4492 else
4493 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4494
4495 return 0;
4496}
4497
4498static const struct seq_operations ftrace_pid_sops = {
4499 .start = fpid_start,
4500 .next = fpid_next,
4501 .stop = fpid_stop,
4502 .show = fpid_show,
4503};
4504
4505static int
4506ftrace_pid_open(struct inode *inode, struct file *file)
4507{
4508 int ret = 0;
4509
4510 if ((file->f_mode & FMODE_WRITE) &&
4511 (file->f_flags & O_TRUNC))
4512 ftrace_pid_reset();
4513
4514 if (file->f_mode & FMODE_READ)
4515 ret = seq_open(file, &ftrace_pid_sops);
4516
4517 return ret;
4518}
4519
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004520static ssize_t
4521ftrace_pid_write(struct file *filp, const char __user *ubuf,
4522 size_t cnt, loff_t *ppos)
4523{
Ingo Molnar457dc922009-11-23 11:03:28 +01004524 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004525 long val;
4526 int ret;
4527
4528 if (cnt >= sizeof(buf))
4529 return -EINVAL;
4530
4531 if (copy_from_user(&buf, ubuf, cnt))
4532 return -EFAULT;
4533
4534 buf[cnt] = 0;
4535
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004536 /*
4537 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4538 * to clean the filter quietly.
4539 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004540 tmp = strstrip(buf);
4541 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004542 return 1;
4543
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004544 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004545 if (ret < 0)
4546 return ret;
4547
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004548 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004549
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004550 return ret ? ret : cnt;
4551}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004552
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004553static int
4554ftrace_pid_release(struct inode *inode, struct file *file)
4555{
4556 if (file->f_mode & FMODE_READ)
4557 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004558
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004559 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004560}
4561
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004562static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004563 .open = ftrace_pid_open,
4564 .write = ftrace_pid_write,
4565 .read = seq_read,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09004566 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004567 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004568};
4569
4570static __init int ftrace_init_debugfs(void)
4571{
4572 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004573
4574 d_tracer = tracing_init_dentry();
4575 if (!d_tracer)
4576 return 0;
4577
4578 ftrace_init_dyn_debugfs(d_tracer);
4579
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004580 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4581 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004582
4583 ftrace_profile_debugfs(d_tracer);
4584
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004585 return 0;
4586}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004587fs_initcall(ftrace_init_debugfs);
4588
Steven Rostedt3d083392008-05-12 21:20:42 +02004589/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004590 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004591 *
4592 * This function should be used by panic code. It stops ftrace
4593 * but in a not so nice way. If you need to simply kill ftrace
4594 * from a non-atomic section, use ftrace_kill.
4595 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004596void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004597{
4598 ftrace_disabled = 1;
4599 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004600 clear_ftrace_function();
4601}
4602
4603/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004604 * Test if ftrace is dead or not.
4605 */
4606int ftrace_is_dead(void)
4607{
4608 return ftrace_disabled;
4609}
4610
4611/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004612 * register_ftrace_function - register a function for profiling
4613 * @ops - ops structure that holds the function for profiling.
4614 *
4615 * Register a function to be called by all functions in the
4616 * kernel.
4617 *
4618 * Note: @ops->func and all the functions it calls must be labeled
4619 * with "notrace", otherwise it will go into a
4620 * recursive loop.
4621 */
4622int register_ftrace_function(struct ftrace_ops *ops)
4623{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004624 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004625
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004626 ftrace_ops_init(ops);
4627
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004628 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004629
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004630 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004631 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004632 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004633
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004634 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004635
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004636 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004637}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004638EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004639
4640/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004641 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004642 * @ops - ops structure that holds the function to unregister
4643 *
4644 * Unregister a function that was added to be called by ftrace profiling.
4645 */
4646int unregister_ftrace_function(struct ftrace_ops *ops)
4647{
4648 int ret;
4649
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004650 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004651 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004652 if (!ret)
4653 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004654 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004655
4656 return ret;
4657}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004658EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004659
Ingo Molnare309b412008-05-12 21:20:51 +02004660int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004661ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004662 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004663 loff_t *ppos)
4664{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004665 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004666
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004667 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004668
Steven Rostedt45a4a232011-04-21 23:16:46 -04004669 if (unlikely(ftrace_disabled))
4670 goto out;
4671
4672 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004673
Li Zefana32c7762009-06-26 16:55:51 +08004674 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004675 goto out;
4676
Li Zefana32c7762009-06-26 16:55:51 +08004677 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004678
4679 if (ftrace_enabled) {
4680
4681 ftrace_startup_sysctl();
4682
4683 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004684 if (ftrace_ops_list != &ftrace_list_end)
4685 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004686
4687 } else {
4688 /* stopping ftrace calls (just send to ftrace_stub) */
4689 ftrace_trace_function = ftrace_stub;
4690
4691 ftrace_shutdown_sysctl();
4692 }
4693
4694 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004695 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004696 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004697}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004698
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004699#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004700
Steven Rostedt597af812009-04-03 15:24:12 -04004701static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004702static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004703
Steven Rostedte49dc192008-12-02 23:50:05 -05004704int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4705{
4706 return 0;
4707}
4708
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004709/* The callbacks that hook a function */
4710trace_func_graph_ret_t ftrace_graph_return =
4711 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004712trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004713
4714/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4715static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4716{
4717 int i;
4718 int ret = 0;
4719 unsigned long flags;
4720 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4721 struct task_struct *g, *t;
4722
4723 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4724 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4725 * sizeof(struct ftrace_ret_stack),
4726 GFP_KERNEL);
4727 if (!ret_stack_list[i]) {
4728 start = 0;
4729 end = i;
4730 ret = -ENOMEM;
4731 goto free;
4732 }
4733 }
4734
4735 read_lock_irqsave(&tasklist_lock, flags);
4736 do_each_thread(g, t) {
4737 if (start == end) {
4738 ret = -EAGAIN;
4739 goto unlock;
4740 }
4741
4742 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004743 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004744 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004745 t->curr_ret_stack = -1;
4746 /* Make sure the tasks see the -1 first: */
4747 smp_wmb();
4748 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004749 }
4750 } while_each_thread(g, t);
4751
4752unlock:
4753 read_unlock_irqrestore(&tasklist_lock, flags);
4754free:
4755 for (i = start; i < end; i++)
4756 kfree(ret_stack_list[i]);
4757 return ret;
4758}
4759
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004760static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004761ftrace_graph_probe_sched_switch(void *ignore,
4762 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004763{
4764 unsigned long long timestamp;
4765 int index;
4766
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004767 /*
4768 * Does the user want to count the time a function was asleep.
4769 * If so, do not update the time stamps.
4770 */
4771 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4772 return;
4773
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004774 timestamp = trace_clock_local();
4775
4776 prev->ftrace_timestamp = timestamp;
4777
4778 /* only process tasks that we timestamped */
4779 if (!next->ftrace_timestamp)
4780 return;
4781
4782 /*
4783 * Update all the counters in next to make up for the
4784 * time next was sleeping.
4785 */
4786 timestamp -= next->ftrace_timestamp;
4787
4788 for (index = next->curr_ret_stack; index >= 0; index--)
4789 next->ret_stack[index].calltime += timestamp;
4790}
4791
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004792/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004793static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004794{
4795 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004796 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004797
4798 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4799 sizeof(struct ftrace_ret_stack *),
4800 GFP_KERNEL);
4801
4802 if (!ret_stack_list)
4803 return -ENOMEM;
4804
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004805 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004806 for_each_online_cpu(cpu) {
4807 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004808 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004809 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004810
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004811 do {
4812 ret = alloc_retstack_tasklist(ret_stack_list);
4813 } while (ret == -EAGAIN);
4814
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004815 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004816 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004817 if (ret)
4818 pr_info("ftrace_graph: Couldn't activate tracepoint"
4819 " probe to kernel_sched_switch\n");
4820 }
4821
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004822 kfree(ret_stack_list);
4823 return ret;
4824}
4825
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004826/*
4827 * Hibernation protection.
4828 * The state of the current task is too much unstable during
4829 * suspend/restore to disk. We want to protect against that.
4830 */
4831static int
4832ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4833 void *unused)
4834{
4835 switch (state) {
4836 case PM_HIBERNATION_PREPARE:
4837 pause_graph_tracing();
4838 break;
4839
4840 case PM_POST_HIBERNATION:
4841 unpause_graph_tracing();
4842 break;
4843 }
4844 return NOTIFY_DONE;
4845}
4846
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004847int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4848 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004849{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004850 int ret = 0;
4851
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004852 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004853
Steven Rostedt05ce5812009-03-24 00:18:31 -04004854 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004855 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004856 ret = -EBUSY;
4857 goto out;
4858 }
4859
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004860 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4861 register_pm_notifier(&ftrace_suspend_notifier);
4862
Steven Rostedt597af812009-04-03 15:24:12 -04004863 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004864 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004865 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004866 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004867 goto out;
4868 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004869
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004870 ftrace_graph_return = retfunc;
4871 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004872
Steven Rostedta1cd6172011-05-23 15:24:25 -04004873 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004874
4875out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004876 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004877 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004878}
4879
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004880void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004881{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004882 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004883
Steven Rostedt597af812009-04-03 15:24:12 -04004884 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004885 goto out;
4886
Steven Rostedt597af812009-04-03 15:24:12 -04004887 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004888 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004889 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004890 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004891 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004892 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004893
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004894 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004895 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004896}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004897
Steven Rostedt868baf02011-02-10 21:26:13 -05004898static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4899
4900static void
4901graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4902{
4903 atomic_set(&t->tracing_graph_pause, 0);
4904 atomic_set(&t->trace_overrun, 0);
4905 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004906 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004907 smp_wmb();
4908 t->ret_stack = ret_stack;
4909}
4910
4911/*
4912 * Allocate a return stack for the idle task. May be the first
4913 * time through, or it may be done by CPU hotplug online.
4914 */
4915void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4916{
4917 t->curr_ret_stack = -1;
4918 /*
4919 * The idle task has no parent, it either has its own
4920 * stack or no stack at all.
4921 */
4922 if (t->ret_stack)
4923 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4924
4925 if (ftrace_graph_active) {
4926 struct ftrace_ret_stack *ret_stack;
4927
4928 ret_stack = per_cpu(idle_ret_stack, cpu);
4929 if (!ret_stack) {
4930 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4931 * sizeof(struct ftrace_ret_stack),
4932 GFP_KERNEL);
4933 if (!ret_stack)
4934 return;
4935 per_cpu(idle_ret_stack, cpu) = ret_stack;
4936 }
4937 graph_init_task(t, ret_stack);
4938 }
4939}
4940
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004941/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004942void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004943{
Steven Rostedt84047e32009-06-02 16:51:55 -04004944 /* Make sure we do not use the parent ret_stack */
4945 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004946 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004947
Steven Rostedt597af812009-04-03 15:24:12 -04004948 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004949 struct ftrace_ret_stack *ret_stack;
4950
4951 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004952 * sizeof(struct ftrace_ret_stack),
4953 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004954 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004955 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004956 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004957 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004958}
4959
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004960void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004961{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004962 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4963
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004964 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004965 /* NULL must become visible to IRQs before we free it: */
4966 barrier();
4967
4968 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004969}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004970
4971void ftrace_graph_stop(void)
4972{
4973 ftrace_stop();
4974}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004975#endif