blob: 800a8a2fbddb34ec06381a0cd4cf881af965f7e1 [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 {
644 stddev = rec->time_squared - rec->counter * avg * avg;
645 /*
646 * Divide only 1000 for ns^2 -> us^2 conversion.
647 * trace_print_graph_duration will divide 1000 again.
648 */
649 do_div(stddev, (rec->counter - 1) * 1000);
650 }
651
Steven Rostedt34886c82009-03-25 21:00:47 -0400652 trace_seq_init(&s);
653 trace_print_graph_duration(rec->time, &s);
654 trace_seq_puts(&s, " ");
655 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400656 trace_seq_puts(&s, " ");
657 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400658 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400659#endif
660 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800661out:
662 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400663
Li Zefan3aaba202010-08-23 16:50:12 +0800664 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400665}
666
Steven Rostedtcafb1682009-03-24 20:50:39 -0400667static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400668{
669 struct ftrace_profile_page *pg;
670
Steven Rostedtcafb1682009-03-24 20:50:39 -0400671 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400672
673 while (pg) {
674 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
675 pg->index = 0;
676 pg = pg->next;
677 }
678
Steven Rostedtcafb1682009-03-24 20:50:39 -0400679 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400680 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
681}
682
Steven Rostedtcafb1682009-03-24 20:50:39 -0400683int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400684{
685 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400686 int functions;
687 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400688 int i;
689
690 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400691 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400692 return 0;
693
Steven Rostedtcafb1682009-03-24 20:50:39 -0400694 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
695 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400696 return -ENOMEM;
697
Steven Rostedt318e0a72009-03-25 20:06:34 -0400698#ifdef CONFIG_DYNAMIC_FTRACE
699 functions = ftrace_update_tot_cnt;
700#else
701 /*
702 * We do not know the number of functions that exist because
703 * dynamic tracing is what counts them. With past experience
704 * we have around 20K functions. That should be more than enough.
705 * It is highly unlikely we will execute every function in
706 * the kernel.
707 */
708 functions = 20000;
709#endif
710
Steven Rostedtcafb1682009-03-24 20:50:39 -0400711 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400712
Steven Rostedt318e0a72009-03-25 20:06:34 -0400713 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
714
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900715 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400716 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400717 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400718 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400719 pg = pg->next;
720 }
721
722 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400723
724 out_free:
725 pg = stat->start;
726 while (pg) {
727 unsigned long tmp = (unsigned long)pg;
728
729 pg = pg->next;
730 free_page(tmp);
731 }
732
Steven Rostedt318e0a72009-03-25 20:06:34 -0400733 stat->pages = NULL;
734 stat->start = NULL;
735
736 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400737}
738
Steven Rostedtcafb1682009-03-24 20:50:39 -0400739static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400740{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400742 int size;
743
Steven Rostedtcafb1682009-03-24 20:50:39 -0400744 stat = &per_cpu(ftrace_profile_stats, cpu);
745
746 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400747 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 return 0;
750 }
751
752 /*
753 * We are profiling all functions, but usually only a few thousand
754 * functions are hit. We'll make a hash of 1024 items.
755 */
756 size = FTRACE_PROFILE_HASH_SIZE;
757
Steven Rostedtcafb1682009-03-24 20:50:39 -0400758 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400759
Steven Rostedtcafb1682009-03-24 20:50:39 -0400760 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400761 return -ENOMEM;
762
Steven Rostedt318e0a72009-03-25 20:06:34 -0400763 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400764 if (ftrace_profile_pages_init(stat) < 0) {
765 kfree(stat->hash);
766 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400767 return -ENOMEM;
768 }
769
770 return 0;
771}
772
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773static int ftrace_profile_init(void)
774{
775 int cpu;
776 int ret = 0;
777
778 for_each_online_cpu(cpu) {
779 ret = ftrace_profile_init_cpu(cpu);
780 if (ret)
781 break;
782 }
783
784 return ret;
785}
786
Steven Rostedt493762f2009-03-23 17:12:36 -0400787/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400788static struct ftrace_profile *
789ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400790{
791 struct ftrace_profile *rec;
792 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400793 unsigned long key;
794
Namhyung Kim20079eb2013-04-10 08:55:50 +0900795 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400796 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400797
798 if (hlist_empty(hhd))
799 return NULL;
800
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400801 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400802 if (rec->ip == ip)
803 return rec;
804 }
805
806 return NULL;
807}
808
Steven Rostedtcafb1682009-03-24 20:50:39 -0400809static void ftrace_add_profile(struct ftrace_profile_stat *stat,
810 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400811{
812 unsigned long key;
813
Namhyung Kim20079eb2013-04-10 08:55:50 +0900814 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400815 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400816}
817
Steven Rostedt318e0a72009-03-25 20:06:34 -0400818/*
819 * The memory is already allocated, this simply finds a new record to use.
820 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400821static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400822ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400823{
824 struct ftrace_profile *rec = NULL;
825
Steven Rostedt318e0a72009-03-25 20:06:34 -0400826 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400827 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400828 goto out;
829
Steven Rostedt493762f2009-03-23 17:12:36 -0400830 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400831 * Try to find the function again since an NMI
832 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400833 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400834 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400835 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400836 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400837
Steven Rostedtcafb1682009-03-24 20:50:39 -0400838 if (stat->pages->index == PROFILES_PER_PAGE) {
839 if (!stat->pages->next)
840 goto out;
841 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400842 }
843
Steven Rostedtcafb1682009-03-24 20:50:39 -0400844 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400845 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400846 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400847
Steven Rostedt493762f2009-03-23 17:12:36 -0400848 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400849 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400850
851 return rec;
852}
853
Steven Rostedt493762f2009-03-23 17:12:36 -0400854static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400855function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400856 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400857{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400858 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400859 struct ftrace_profile *rec;
860 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400861
862 if (!ftrace_profile_enabled)
863 return;
864
Steven Rostedt493762f2009-03-23 17:12:36 -0400865 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400866
867 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400868 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400869 goto out;
870
871 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400872 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400873 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400874 if (!rec)
875 goto out;
876 }
877
878 rec->counter++;
879 out:
880 local_irq_restore(flags);
881}
882
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400883#ifdef CONFIG_FUNCTION_GRAPH_TRACER
884static int profile_graph_entry(struct ftrace_graph_ent *trace)
885{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400886 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400887 return 1;
888}
889
890static void profile_graph_return(struct ftrace_graph_ret *trace)
891{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400892 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400893 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400894 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400895 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400896
897 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400898 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400899 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400900 goto out;
901
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400902 /* If the calltime was zero'd ignore it */
903 if (!trace->calltime)
904 goto out;
905
Steven Rostedta2a16d62009-03-24 23:17:58 -0400906 calltime = trace->rettime - trace->calltime;
907
908 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
909 int index;
910
911 index = trace->depth;
912
913 /* Append this call time to the parent time to subtract */
914 if (index)
915 current->ret_stack[index - 1].subtime += calltime;
916
917 if (current->ret_stack[index].subtime < calltime)
918 calltime -= current->ret_stack[index].subtime;
919 else
920 calltime = 0;
921 }
922
Steven Rostedtcafb1682009-03-24 20:50:39 -0400923 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400924 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400925 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400926 rec->time_squared += calltime * calltime;
927 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400928
Steven Rostedtcafb1682009-03-24 20:50:39 -0400929 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400930 local_irq_restore(flags);
931}
932
933static int register_ftrace_profiler(void)
934{
935 return register_ftrace_graph(&profile_graph_return,
936 &profile_graph_entry);
937}
938
939static void unregister_ftrace_profiler(void)
940{
941 unregister_ftrace_graph();
942}
943#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100944static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400945 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900946 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
947 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400948};
949
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400950static int register_ftrace_profiler(void)
951{
952 return register_ftrace_function(&ftrace_profile_ops);
953}
954
955static void unregister_ftrace_profiler(void)
956{
957 unregister_ftrace_function(&ftrace_profile_ops);
958}
959#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
960
Steven Rostedt493762f2009-03-23 17:12:36 -0400961static ssize_t
962ftrace_profile_write(struct file *filp, const char __user *ubuf,
963 size_t cnt, loff_t *ppos)
964{
965 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400966 int ret;
967
Peter Huewe22fe9b52011-06-07 21:58:27 +0200968 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
969 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400970 return ret;
971
972 val = !!val;
973
974 mutex_lock(&ftrace_profile_lock);
975 if (ftrace_profile_enabled ^ val) {
976 if (val) {
977 ret = ftrace_profile_init();
978 if (ret < 0) {
979 cnt = ret;
980 goto out;
981 }
982
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400983 ret = register_ftrace_profiler();
984 if (ret < 0) {
985 cnt = ret;
986 goto out;
987 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400988 ftrace_profile_enabled = 1;
989 } else {
990 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400991 /*
992 * unregister_ftrace_profiler calls stop_machine
993 * so this acts like an synchronize_sched.
994 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400995 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400996 }
997 }
998 out:
999 mutex_unlock(&ftrace_profile_lock);
1000
Jiri Olsacf8517c2009-10-23 19:36:16 -04001001 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001002
1003 return cnt;
1004}
1005
1006static ssize_t
1007ftrace_profile_read(struct file *filp, char __user *ubuf,
1008 size_t cnt, loff_t *ppos)
1009{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001010 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001011 int r;
1012
1013 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1014 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1015}
1016
1017static const struct file_operations ftrace_profile_fops = {
1018 .open = tracing_open_generic,
1019 .read = ftrace_profile_read,
1020 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001021 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001022};
1023
Steven Rostedtcafb1682009-03-24 20:50:39 -04001024/* used to initialize the real stat files */
1025static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001026 .name = "functions",
1027 .stat_start = function_stat_start,
1028 .stat_next = function_stat_next,
1029 .stat_cmp = function_stat_cmp,
1030 .stat_headers = function_stat_headers,
1031 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001032};
1033
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001034static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001035{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001036 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001037 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001038 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001039 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001040 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001041
Steven Rostedtcafb1682009-03-24 20:50:39 -04001042 for_each_possible_cpu(cpu) {
1043 stat = &per_cpu(ftrace_profile_stats, cpu);
1044
1045 /* allocate enough for function name + cpu number */
1046 name = kmalloc(32, GFP_KERNEL);
1047 if (!name) {
1048 /*
1049 * The files created are permanent, if something happens
1050 * we still do not free memory.
1051 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001052 WARN(1,
1053 "Could not allocate stat file for cpu %d\n",
1054 cpu);
1055 return;
1056 }
1057 stat->stat = function_stats;
1058 snprintf(name, 32, "function%d", cpu);
1059 stat->stat.name = name;
1060 ret = register_stat_tracer(&stat->stat);
1061 if (ret) {
1062 WARN(1,
1063 "Could not register function stat for cpu %d\n",
1064 cpu);
1065 kfree(name);
1066 return;
1067 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001068 }
1069
1070 entry = debugfs_create_file("function_profile_enabled", 0644,
1071 d_tracer, NULL, &ftrace_profile_fops);
1072 if (!entry)
1073 pr_warning("Could not create debugfs "
1074 "'function_profile_enabled' entry\n");
1075}
1076
1077#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001078static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001079{
1080}
1081#endif /* CONFIG_FUNCTION_PROFILER */
1082
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001083static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1084
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04001085loff_t
1086ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1087{
1088 loff_t ret;
1089
1090 if (file->f_mode & FMODE_READ)
1091 ret = seq_lseek(file, offset, whence);
1092 else
1093 file->f_pos = ret = 1;
1094
1095 return ret;
1096}
1097
Steven Rostedt3d083392008-05-12 21:20:42 +02001098#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001099
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001100#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001101# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001102#endif
1103
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001104static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1105
Steven Rostedtb6887d72009-02-17 12:32:04 -05001106struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001107 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001108 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001109 unsigned long flags;
1110 unsigned long ip;
1111 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001112 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001113};
1114
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001115struct ftrace_func_entry {
1116 struct hlist_node hlist;
1117 unsigned long ip;
1118};
1119
1120struct ftrace_hash {
1121 unsigned long size_bits;
1122 struct hlist_head *buckets;
1123 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001124 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001125};
1126
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001127/*
1128 * We make these constant because no one should touch them,
1129 * but they are used as the default "empty hash", to avoid allocating
1130 * it all the time. These are in a read only section such that if
1131 * anyone does try to modify it, it will cause an exception.
1132 */
1133static const struct hlist_head empty_buckets[1];
1134static const struct ftrace_hash empty_hash = {
1135 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001136};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001137#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001138
Steven Rostedt2b499382011-05-03 22:49:52 -04001139static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001140 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001141 .notrace_hash = EMPTY_HASH,
1142 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001143 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1144 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001145};
1146
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001147struct ftrace_page {
1148 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001149 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001150 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001151 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001152};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001153
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001154static struct ftrace_page *ftrace_new_pgs;
1155
Steven Rostedta7900872011-12-16 16:23:44 -05001156#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1157#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001158
1159/* estimate from running different kernels */
1160#define NR_TO_INIT 10000
1161
1162static struct ftrace_page *ftrace_pages_start;
1163static struct ftrace_page *ftrace_pages;
1164
Steven Rostedt06a51d92011-12-19 19:07:36 -05001165static bool ftrace_hash_empty(struct ftrace_hash *hash)
1166{
1167 return !hash || !hash->count;
1168}
1169
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001170static struct ftrace_func_entry *
1171ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1172{
1173 unsigned long key;
1174 struct ftrace_func_entry *entry;
1175 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001176
Steven Rostedt06a51d92011-12-19 19:07:36 -05001177 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001178 return NULL;
1179
1180 if (hash->size_bits > 0)
1181 key = hash_long(ip, hash->size_bits);
1182 else
1183 key = 0;
1184
1185 hhd = &hash->buckets[key];
1186
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001187 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001188 if (entry->ip == ip)
1189 return entry;
1190 }
1191 return NULL;
1192}
1193
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001194static void __add_hash_entry(struct ftrace_hash *hash,
1195 struct ftrace_func_entry *entry)
1196{
1197 struct hlist_head *hhd;
1198 unsigned long key;
1199
1200 if (hash->size_bits)
1201 key = hash_long(entry->ip, hash->size_bits);
1202 else
1203 key = 0;
1204
1205 hhd = &hash->buckets[key];
1206 hlist_add_head(&entry->hlist, hhd);
1207 hash->count++;
1208}
1209
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001210static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1211{
1212 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001213
1214 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1215 if (!entry)
1216 return -ENOMEM;
1217
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001218 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001219 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001220
1221 return 0;
1222}
1223
1224static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001225free_hash_entry(struct ftrace_hash *hash,
1226 struct ftrace_func_entry *entry)
1227{
1228 hlist_del(&entry->hlist);
1229 kfree(entry);
1230 hash->count--;
1231}
1232
1233static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001234remove_hash_entry(struct ftrace_hash *hash,
1235 struct ftrace_func_entry *entry)
1236{
1237 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001238 hash->count--;
1239}
1240
1241static void ftrace_hash_clear(struct ftrace_hash *hash)
1242{
1243 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001244 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001245 struct ftrace_func_entry *entry;
1246 int size = 1 << hash->size_bits;
1247 int i;
1248
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001249 if (!hash->count)
1250 return;
1251
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001252 for (i = 0; i < size; i++) {
1253 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001254 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001255 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001256 }
1257 FTRACE_WARN_ON(hash->count);
1258}
1259
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001260static void free_ftrace_hash(struct ftrace_hash *hash)
1261{
1262 if (!hash || hash == EMPTY_HASH)
1263 return;
1264 ftrace_hash_clear(hash);
1265 kfree(hash->buckets);
1266 kfree(hash);
1267}
1268
Steven Rostedt07fd5512011-05-05 18:03:47 -04001269static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1270{
1271 struct ftrace_hash *hash;
1272
1273 hash = container_of(rcu, struct ftrace_hash, rcu);
1274 free_ftrace_hash(hash);
1275}
1276
1277static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1278{
1279 if (!hash || hash == EMPTY_HASH)
1280 return;
1281 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1282}
1283
Jiri Olsa5500fa52012-02-15 15:51:54 +01001284void ftrace_free_filter(struct ftrace_ops *ops)
1285{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001286 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001287 free_ftrace_hash(ops->filter_hash);
1288 free_ftrace_hash(ops->notrace_hash);
1289}
1290
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001291static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1292{
1293 struct ftrace_hash *hash;
1294 int size;
1295
1296 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1297 if (!hash)
1298 return NULL;
1299
1300 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001301 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001302
1303 if (!hash->buckets) {
1304 kfree(hash);
1305 return NULL;
1306 }
1307
1308 hash->size_bits = size_bits;
1309
1310 return hash;
1311}
1312
1313static struct ftrace_hash *
1314alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1315{
1316 struct ftrace_func_entry *entry;
1317 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001318 int size;
1319 int ret;
1320 int i;
1321
1322 new_hash = alloc_ftrace_hash(size_bits);
1323 if (!new_hash)
1324 return NULL;
1325
1326 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001327 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328 return new_hash;
1329
1330 size = 1 << hash->size_bits;
1331 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001332 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001333 ret = add_hash_entry(new_hash, entry->ip);
1334 if (ret < 0)
1335 goto free_hash;
1336 }
1337 }
1338
1339 FTRACE_WARN_ON(new_hash->count != hash->count);
1340
1341 return new_hash;
1342
1343 free_hash:
1344 free_ftrace_hash(new_hash);
1345 return NULL;
1346}
1347
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001348static void
1349ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1350static void
1351ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1352
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001353static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001354ftrace_hash_move(struct ftrace_ops *ops, int enable,
1355 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001356{
1357 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001358 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001359 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001360 struct ftrace_hash *old_hash;
1361 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001362 int size = src->count;
1363 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001364 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001365 int i;
1366
1367 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001368 * Remove the current set, update the hash and add
1369 * them back.
1370 */
1371 ftrace_hash_rec_disable(ops, enable);
1372
1373 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001374 * If the new source is empty, just free dst and assign it
1375 * the empty_hash.
1376 */
1377 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001378 free_ftrace_hash_rcu(*dst);
1379 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001380 /* still need to update the function records */
1381 ret = 0;
1382 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001383 }
1384
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001385 /*
1386 * Make the hash size about 1/2 the # found
1387 */
1388 for (size /= 2; size; size >>= 1)
1389 bits++;
1390
1391 /* Don't allocate too much */
1392 if (bits > FTRACE_HASH_MAX_BITS)
1393 bits = FTRACE_HASH_MAX_BITS;
1394
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001395 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001396 new_hash = alloc_ftrace_hash(bits);
1397 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001398 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001399
1400 size = 1 << src->size_bits;
1401 for (i = 0; i < size; i++) {
1402 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001403 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001404 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001405 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001406 }
1407 }
1408
Steven Rostedt07fd5512011-05-05 18:03:47 -04001409 old_hash = *dst;
1410 rcu_assign_pointer(*dst, new_hash);
1411 free_ftrace_hash_rcu(old_hash);
1412
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001413 ret = 0;
1414 out:
1415 /*
1416 * Enable regardless of ret:
1417 * On success, we enable the new hash.
1418 * On failure, we re-enable the original hash.
1419 */
1420 ftrace_hash_rec_enable(ops, enable);
1421
1422 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001423}
1424
Steven Rostedt265c8312009-02-13 12:43:56 -05001425/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001426 * Test the hashes for this ops to see if we want to call
1427 * the ops->func or not.
1428 *
1429 * It's a match if the ip is in the ops->filter_hash or
1430 * the filter_hash does not exist or is empty,
1431 * AND
1432 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001433 *
1434 * This needs to be called with preemption disabled as
1435 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001436 */
1437static int
1438ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1439{
1440 struct ftrace_hash *filter_hash;
1441 struct ftrace_hash *notrace_hash;
1442 int ret;
1443
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001444 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1445 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001446
Steven Rostedt06a51d92011-12-19 19:07:36 -05001447 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001448 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001449 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001450 !ftrace_lookup_ip(notrace_hash, ip)))
1451 ret = 1;
1452 else
1453 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001454
1455 return ret;
1456}
1457
1458/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001459 * This is a double for. Do not use 'break' to break out of the loop,
1460 * you must use a goto.
1461 */
1462#define do_for_each_ftrace_rec(pg, rec) \
1463 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1464 int _____i; \
1465 for (_____i = 0; _____i < pg->index; _____i++) { \
1466 rec = &pg->records[_____i];
1467
1468#define while_for_each_ftrace_rec() \
1469 } \
1470 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301471
Steven Rostedt5855fea2011-12-16 19:27:42 -05001472
1473static int ftrace_cmp_recs(const void *a, const void *b)
1474{
Steven Rostedta650e022012-04-25 13:48:13 -04001475 const struct dyn_ftrace *key = a;
1476 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001477
Steven Rostedta650e022012-04-25 13:48:13 -04001478 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001479 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001480 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1481 return 1;
1482 return 0;
1483}
1484
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001485static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001486{
1487 struct ftrace_page *pg;
1488 struct dyn_ftrace *rec;
1489 struct dyn_ftrace key;
1490
1491 key.ip = start;
1492 key.flags = end; /* overload flags, as it is unsigned long */
1493
1494 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1495 if (end < pg->records[0].ip ||
1496 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1497 continue;
1498 rec = bsearch(&key, pg->records, pg->index,
1499 sizeof(struct dyn_ftrace),
1500 ftrace_cmp_recs);
1501 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001502 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001503 }
1504
Steven Rostedt5855fea2011-12-16 19:27:42 -05001505 return 0;
1506}
1507
Steven Rostedtc88fd862011-08-16 09:53:39 -04001508/**
1509 * ftrace_location - return true if the ip giving is a traced location
1510 * @ip: the instruction pointer to check
1511 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001512 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001513 * That is, the instruction that is either a NOP or call to
1514 * the function tracer. It checks the ftrace internal tables to
1515 * determine if the address belongs or not.
1516 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001517unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001518{
Steven Rostedta650e022012-04-25 13:48:13 -04001519 return ftrace_location_range(ip, ip);
1520}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001521
Steven Rostedta650e022012-04-25 13:48:13 -04001522/**
1523 * ftrace_text_reserved - return true if range contains an ftrace location
1524 * @start: start of range to search
1525 * @end: end of range to search (inclusive). @end points to the last byte to check.
1526 *
1527 * Returns 1 if @start and @end contains a ftrace location.
1528 * That is, the instruction that is either a NOP or call to
1529 * the function tracer. It checks the ftrace internal tables to
1530 * determine if the address belongs or not.
1531 */
1532int ftrace_text_reserved(void *start, void *end)
1533{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001534 unsigned long ret;
1535
1536 ret = ftrace_location_range((unsigned long)start,
1537 (unsigned long)end);
1538
1539 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001540}
1541
Steven Rostedted926f92011-05-03 13:25:24 -04001542static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1543 int filter_hash,
1544 bool inc)
1545{
1546 struct ftrace_hash *hash;
1547 struct ftrace_hash *other_hash;
1548 struct ftrace_page *pg;
1549 struct dyn_ftrace *rec;
1550 int count = 0;
1551 int all = 0;
1552
1553 /* Only update if the ops has been registered */
1554 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1555 return;
1556
1557 /*
1558 * In the filter_hash case:
1559 * If the count is zero, we update all records.
1560 * Otherwise we just update the items in the hash.
1561 *
1562 * In the notrace_hash case:
1563 * We enable the update in the hash.
1564 * As disabling notrace means enabling the tracing,
1565 * and enabling notrace means disabling, the inc variable
1566 * gets inversed.
1567 */
1568 if (filter_hash) {
1569 hash = ops->filter_hash;
1570 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001571 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001572 all = 1;
1573 } else {
1574 inc = !inc;
1575 hash = ops->notrace_hash;
1576 other_hash = ops->filter_hash;
1577 /*
1578 * If the notrace hash has no items,
1579 * then there's nothing to do.
1580 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001581 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001582 return;
1583 }
1584
1585 do_for_each_ftrace_rec(pg, rec) {
1586 int in_other_hash = 0;
1587 int in_hash = 0;
1588 int match = 0;
1589
1590 if (all) {
1591 /*
1592 * Only the filter_hash affects all records.
1593 * Update if the record is not in the notrace hash.
1594 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001595 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001596 match = 1;
1597 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001598 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1599 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001600
1601 /*
1602 *
1603 */
1604 if (filter_hash && in_hash && !in_other_hash)
1605 match = 1;
1606 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001607 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001608 match = 1;
1609 }
1610 if (!match)
1611 continue;
1612
1613 if (inc) {
1614 rec->flags++;
1615 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1616 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001617 /*
1618 * If any ops wants regs saved for this function
1619 * then all ops will get saved regs.
1620 */
1621 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1622 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001623 } else {
1624 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1625 return;
1626 rec->flags--;
1627 }
1628 count++;
1629 /* Shortcut, if we handled all records, we are done. */
1630 if (!all && count == hash->count)
1631 return;
1632 } while_for_each_ftrace_rec();
1633}
1634
1635static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1636 int filter_hash)
1637{
1638 __ftrace_hash_rec_update(ops, filter_hash, 0);
1639}
1640
1641static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1642 int filter_hash)
1643{
1644 __ftrace_hash_rec_update(ops, filter_hash, 1);
1645}
1646
Steven Rostedt05736a42008-09-22 14:55:47 -07001647static void print_ip_ins(const char *fmt, unsigned char *p)
1648{
1649 int i;
1650
1651 printk(KERN_CONT "%s", fmt);
1652
1653 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1654 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1655}
1656
Steven Rostedtc88fd862011-08-16 09:53:39 -04001657/**
1658 * ftrace_bug - report and shutdown function tracer
1659 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1660 * @ip: The address that failed
1661 *
1662 * The arch code that enables or disables the function tracing
1663 * can call ftrace_bug() when it has detected a problem in
1664 * modifying the code. @failed should be one of either:
1665 * EFAULT - if the problem happens on reading the @ip address
1666 * EINVAL - if what is read at @ip is not what was expected
1667 * EPERM - if the problem happens on writting to the @ip address
1668 */
1669void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001670{
1671 switch (failed) {
1672 case -EFAULT:
1673 FTRACE_WARN_ON_ONCE(1);
1674 pr_info("ftrace faulted on modifying ");
1675 print_ip_sym(ip);
1676 break;
1677 case -EINVAL:
1678 FTRACE_WARN_ON_ONCE(1);
1679 pr_info("ftrace failed to modify ");
1680 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001681 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001682 printk(KERN_CONT "\n");
1683 break;
1684 case -EPERM:
1685 FTRACE_WARN_ON_ONCE(1);
1686 pr_info("ftrace faulted on writing ");
1687 print_ip_sym(ip);
1688 break;
1689 default:
1690 FTRACE_WARN_ON_ONCE(1);
1691 pr_info("ftrace faulted on unknown error ");
1692 print_ip_sym(ip);
1693 }
1694}
1695
Steven Rostedtc88fd862011-08-16 09:53:39 -04001696static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001697{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001698 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001699
Steven Rostedt982c3502008-11-15 16:31:41 -05001700 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001701 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001702 *
Steven Rostedted926f92011-05-03 13:25:24 -04001703 * If the record has a ref count, then we need to enable it
1704 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001705 *
Steven Rostedted926f92011-05-03 13:25:24 -04001706 * Otherwise we make sure its disabled.
1707 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001708 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001709 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001710 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001711 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001712 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001713
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001714 /*
1715 * If enabling and the REGS flag does not match the REGS_EN, then
1716 * do not ignore this record. Set flags to fail the compare against
1717 * ENABLED.
1718 */
1719 if (flag &&
1720 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1721 flag |= FTRACE_FL_REGS;
1722
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001723 /* If the state of this record hasn't changed, then do nothing */
1724 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001725 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001726
1727 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001728 /* Save off if rec is being enabled (for return value) */
1729 flag ^= rec->flags & FTRACE_FL_ENABLED;
1730
1731 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001732 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001733 if (flag & FTRACE_FL_REGS) {
1734 if (rec->flags & FTRACE_FL_REGS)
1735 rec->flags |= FTRACE_FL_REGS_EN;
1736 else
1737 rec->flags &= ~FTRACE_FL_REGS_EN;
1738 }
1739 }
1740
1741 /*
1742 * If this record is being updated from a nop, then
1743 * return UPDATE_MAKE_CALL.
1744 * Otherwise, if the EN flag is set, then return
1745 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1746 * from the non-save regs, to a save regs function.
1747 * Otherwise,
1748 * return UPDATE_MODIFY_CALL to tell the caller to convert
1749 * from the save regs, to a non-save regs function.
1750 */
1751 if (flag & FTRACE_FL_ENABLED)
1752 return FTRACE_UPDATE_MAKE_CALL;
1753 else if (rec->flags & FTRACE_FL_REGS_EN)
1754 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1755 else
1756 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001757 }
1758
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001759 if (update) {
1760 /* If there's no more users, clear all flags */
1761 if (!(rec->flags & ~FTRACE_FL_MASK))
1762 rec->flags = 0;
1763 else
1764 /* Just disable the record (keep REGS state) */
1765 rec->flags &= ~FTRACE_FL_ENABLED;
1766 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001767
1768 return FTRACE_UPDATE_MAKE_NOP;
1769}
1770
1771/**
1772 * ftrace_update_record, set a record that now is tracing or not
1773 * @rec: the record to update
1774 * @enable: set to 1 if the record is tracing, zero to force disable
1775 *
1776 * The records that represent all functions that can be traced need
1777 * to be updated when tracing has been enabled.
1778 */
1779int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1780{
1781 return ftrace_check_record(rec, enable, 1);
1782}
1783
1784/**
1785 * ftrace_test_record, check if the record has been enabled or not
1786 * @rec: the record to test
1787 * @enable: set to 1 to check if enabled, 0 if it is disabled
1788 *
1789 * The arch code may need to test if a record is already set to
1790 * tracing to determine how to modify the function code that it
1791 * represents.
1792 */
1793int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1794{
1795 return ftrace_check_record(rec, enable, 0);
1796}
1797
1798static int
1799__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1800{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001801 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001802 unsigned long ftrace_addr;
1803 int ret;
1804
Steven Rostedtc88fd862011-08-16 09:53:39 -04001805 ret = ftrace_update_record(rec, enable);
1806
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001807 if (rec->flags & FTRACE_FL_REGS)
1808 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1809 else
1810 ftrace_addr = (unsigned long)FTRACE_ADDR;
1811
Steven Rostedtc88fd862011-08-16 09:53:39 -04001812 switch (ret) {
1813 case FTRACE_UPDATE_IGNORE:
1814 return 0;
1815
1816 case FTRACE_UPDATE_MAKE_CALL:
1817 return ftrace_make_call(rec, ftrace_addr);
1818
1819 case FTRACE_UPDATE_MAKE_NOP:
1820 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001821
1822 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1823 case FTRACE_UPDATE_MODIFY_CALL:
1824 if (rec->flags & FTRACE_FL_REGS)
1825 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1826 else
1827 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1828
1829 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001830 }
1831
1832 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001833}
1834
Steven Rostedte4f5d542012-04-27 09:13:18 -04001835void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001836{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001837 struct dyn_ftrace *rec;
1838 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001839 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001840
Steven Rostedt45a4a232011-04-21 23:16:46 -04001841 if (unlikely(ftrace_disabled))
1842 return;
1843
Steven Rostedt265c8312009-02-13 12:43:56 -05001844 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001845 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001846 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001847 ftrace_bug(failed, rec->ip);
1848 /* Stop processing */
1849 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001850 }
1851 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001852}
1853
Steven Rostedtc88fd862011-08-16 09:53:39 -04001854struct ftrace_rec_iter {
1855 struct ftrace_page *pg;
1856 int index;
1857};
1858
1859/**
1860 * ftrace_rec_iter_start, start up iterating over traced functions
1861 *
1862 * Returns an iterator handle that is used to iterate over all
1863 * the records that represent address locations where functions
1864 * are traced.
1865 *
1866 * May return NULL if no records are available.
1867 */
1868struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1869{
1870 /*
1871 * We only use a single iterator.
1872 * Protected by the ftrace_lock mutex.
1873 */
1874 static struct ftrace_rec_iter ftrace_rec_iter;
1875 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1876
1877 iter->pg = ftrace_pages_start;
1878 iter->index = 0;
1879
1880 /* Could have empty pages */
1881 while (iter->pg && !iter->pg->index)
1882 iter->pg = iter->pg->next;
1883
1884 if (!iter->pg)
1885 return NULL;
1886
1887 return iter;
1888}
1889
1890/**
1891 * ftrace_rec_iter_next, get the next record to process.
1892 * @iter: The handle to the iterator.
1893 *
1894 * Returns the next iterator after the given iterator @iter.
1895 */
1896struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1897{
1898 iter->index++;
1899
1900 if (iter->index >= iter->pg->index) {
1901 iter->pg = iter->pg->next;
1902 iter->index = 0;
1903
1904 /* Could have empty pages */
1905 while (iter->pg && !iter->pg->index)
1906 iter->pg = iter->pg->next;
1907 }
1908
1909 if (!iter->pg)
1910 return NULL;
1911
1912 return iter;
1913}
1914
1915/**
1916 * ftrace_rec_iter_record, get the record at the iterator location
1917 * @iter: The current iterator location
1918 *
1919 * Returns the record that the current @iter is at.
1920 */
1921struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1922{
1923 return &iter->pg->records[iter->index];
1924}
1925
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301926static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001927ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001928{
1929 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001930 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001931
1932 ip = rec->ip;
1933
Steven Rostedt45a4a232011-04-21 23:16:46 -04001934 if (unlikely(ftrace_disabled))
1935 return 0;
1936
Shaohua Li25aac9d2009-01-09 11:29:40 +08001937 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001938 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001939 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301940 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001941 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301942 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001943}
1944
Steven Rostedt000ab692009-02-17 13:35:06 -05001945/*
1946 * archs can override this function if they must do something
1947 * before the modifying code is performed.
1948 */
1949int __weak ftrace_arch_code_modify_prepare(void)
1950{
1951 return 0;
1952}
1953
1954/*
1955 * archs can override this function if they must do something
1956 * after the modifying code is performed.
1957 */
1958int __weak ftrace_arch_code_modify_post_process(void)
1959{
1960 return 0;
1961}
1962
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001963void ftrace_modify_all_code(int command)
1964{
1965 if (command & FTRACE_UPDATE_CALLS)
1966 ftrace_replace_code(1);
1967 else if (command & FTRACE_DISABLE_CALLS)
1968 ftrace_replace_code(0);
1969
1970 if (command & FTRACE_UPDATE_TRACE_FUNC)
1971 ftrace_update_ftrace_func(ftrace_trace_function);
1972
1973 if (command & FTRACE_START_FUNC_RET)
1974 ftrace_enable_ftrace_graph_caller();
1975 else if (command & FTRACE_STOP_FUNC_RET)
1976 ftrace_disable_ftrace_graph_caller();
1977}
1978
Ingo Molnare309b412008-05-12 21:20:51 +02001979static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001980{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001981 int *command = data;
1982
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001983 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001984
Steven Rostedtc88fd862011-08-16 09:53:39 -04001985 return 0;
1986}
1987
1988/**
1989 * ftrace_run_stop_machine, go back to the stop machine method
1990 * @command: The command to tell ftrace what to do
1991 *
1992 * If an arch needs to fall back to the stop machine method, the
1993 * it can call this function.
1994 */
1995void ftrace_run_stop_machine(int command)
1996{
1997 stop_machine(__ftrace_modify_code, &command, NULL);
1998}
1999
2000/**
2001 * arch_ftrace_update_code, modify the code to trace or not trace
2002 * @command: The command that needs to be done
2003 *
2004 * Archs can override this function if it does not need to
2005 * run stop_machine() to modify code.
2006 */
2007void __weak arch_ftrace_update_code(int command)
2008{
2009 ftrace_run_stop_machine(command);
2010}
2011
2012static void ftrace_run_update_code(int command)
2013{
2014 int ret;
2015
2016 ret = ftrace_arch_code_modify_prepare();
2017 FTRACE_WARN_ON(ret);
2018 if (ret)
2019 return;
2020 /*
2021 * Do not call function tracer while we update the code.
2022 * We are in stop machine.
2023 */
2024 function_trace_stop++;
2025
2026 /*
2027 * By default we use stop_machine() to modify the code.
2028 * But archs can do what ever they want as long as it
2029 * is safe. The stop_machine() is the safest, but also
2030 * produces the most overhead.
2031 */
2032 arch_ftrace_update_code(command);
2033
Steven Rostedt6331c282011-07-13 15:11:02 -04002034 function_trace_stop--;
2035
Steven Rostedt000ab692009-02-17 13:35:06 -05002036 ret = ftrace_arch_code_modify_post_process();
2037 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002038}
2039
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002040static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002041static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002042static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002043
2044static void ftrace_startup_enable(int command)
2045{
2046 if (saved_ftrace_func != ftrace_trace_function) {
2047 saved_ftrace_func = ftrace_trace_function;
2048 command |= FTRACE_UPDATE_TRACE_FUNC;
2049 }
2050
2051 if (!command || !ftrace_enabled)
2052 return;
2053
2054 ftrace_run_update_code(command);
2055}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002056
Steven Rostedta1cd6172011-05-23 15:24:25 -04002057static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002058{
Steven Rostedtb8489142011-05-04 09:27:52 -04002059 bool hash_enable = true;
2060
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002061 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002062 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002063
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002064 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002065 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002066
Steven Rostedtb8489142011-05-04 09:27:52 -04002067 /* ops marked global share the filter hashes */
2068 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2069 ops = &global_ops;
2070 /* Don't update hash if global is already set */
2071 if (global_start_up)
2072 hash_enable = false;
2073 global_start_up++;
2074 }
2075
Steven Rostedted926f92011-05-03 13:25:24 -04002076 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002077 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002078 ftrace_hash_rec_enable(ops, 1);
2079
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002080 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002081
2082 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002083}
2084
Steven Rostedtbd69c302011-05-03 21:55:54 -04002085static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002086{
Steven Rostedtb8489142011-05-04 09:27:52 -04002087 bool hash_disable = true;
2088
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002089 if (unlikely(ftrace_disabled))
2090 return;
2091
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002092 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002093 /*
2094 * Just warn in case of unbalance, no need to kill ftrace, it's not
2095 * critical but the ftrace_call callers may be never nopped again after
2096 * further ftrace uses.
2097 */
2098 WARN_ON_ONCE(ftrace_start_up < 0);
2099
Steven Rostedtb8489142011-05-04 09:27:52 -04002100 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2101 ops = &global_ops;
2102 global_start_up--;
2103 WARN_ON_ONCE(global_start_up < 0);
2104 /* Don't update hash if global still has users */
2105 if (global_start_up) {
2106 WARN_ON_ONCE(!ftrace_start_up);
2107 hash_disable = false;
2108 }
2109 }
2110
2111 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002112 ftrace_hash_rec_disable(ops, 1);
2113
Steven Rostedtb8489142011-05-04 09:27:52 -04002114 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002115 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002116
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002117 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002118
2119 if (saved_ftrace_func != ftrace_trace_function) {
2120 saved_ftrace_func = ftrace_trace_function;
2121 command |= FTRACE_UPDATE_TRACE_FUNC;
2122 }
2123
2124 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002125 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002126
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002127 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002128}
2129
Ingo Molnare309b412008-05-12 21:20:51 +02002130static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002131{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002132 if (unlikely(ftrace_disabled))
2133 return;
2134
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002135 /* Force update next time */
2136 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002137 /* ftrace_start_up is true if we want ftrace running */
2138 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002139 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002140}
2141
Ingo Molnare309b412008-05-12 21:20:51 +02002142static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002143{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002144 if (unlikely(ftrace_disabled))
2145 return;
2146
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002147 /* ftrace_start_up is true if ftrace is running */
2148 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002149 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002150}
2151
Steven Rostedt3d083392008-05-12 21:20:42 +02002152static cycle_t ftrace_update_time;
2153static unsigned long ftrace_update_cnt;
2154unsigned long ftrace_update_tot_cnt;
2155
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002156static int ops_traces_mod(struct ftrace_ops *ops)
2157{
2158 struct ftrace_hash *hash;
2159
2160 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002161 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002162}
2163
Steven Rostedt31e88902008-11-14 16:21:19 -08002164static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002165{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002166 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002167 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302168 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002169 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002170 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002171
2172 /*
2173 * When adding a module, we need to check if tracers are
2174 * currently enabled and if they are set to trace all functions.
2175 * If they are, we need to enable the module functions as well
2176 * as update the reference counts for those function records.
2177 */
2178 if (mod) {
2179 struct ftrace_ops *ops;
2180
2181 for (ops = ftrace_ops_list;
2182 ops != &ftrace_list_end; ops = ops->next) {
2183 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2184 ops_traces_mod(ops))
2185 ref++;
2186 }
2187 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002188
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002189 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002190 ftrace_update_cnt = 0;
2191
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002192 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302193
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002194 for (i = 0; i < pg->index; i++) {
2195 /* If something went wrong, bail without enabling anything */
2196 if (unlikely(ftrace_disabled))
2197 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002198
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002199 p = &pg->records[i];
2200 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302201
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002202 /*
2203 * Do the initial record conversion from mcount jump
2204 * to the NOP instructions.
2205 */
2206 if (!ftrace_code_disable(mod, p))
2207 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002208
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002209 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002210
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002211 /*
2212 * If the tracing is enabled, go ahead and enable the record.
2213 *
2214 * The reason not to enable the record immediatelly is the
2215 * inherent check of ftrace_make_nop/ftrace_make_call for
2216 * correct previous instructions. Making first the NOP
2217 * conversion puts the module to the correct state, thus
2218 * passing the ftrace_make_call check.
2219 */
2220 if (ftrace_start_up && ref) {
2221 int failed = __ftrace_replace_code(p, 1);
2222 if (failed)
2223 ftrace_bug(failed, p->ip);
2224 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002225 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002226 }
2227
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002228 ftrace_new_pgs = NULL;
2229
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002230 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002231 ftrace_update_time = stop - start;
2232 ftrace_update_tot_cnt += ftrace_update_cnt;
2233
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002234 return 0;
2235}
2236
Steven Rostedta7900872011-12-16 16:23:44 -05002237static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002238{
Steven Rostedta7900872011-12-16 16:23:44 -05002239 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002240 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002241
Steven Rostedta7900872011-12-16 16:23:44 -05002242 if (WARN_ON(!count))
2243 return -EINVAL;
2244
2245 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002246
2247 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002248 * We want to fill as much as possible. No more than a page
2249 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002250 */
Steven Rostedta7900872011-12-16 16:23:44 -05002251 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2252 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002253
Steven Rostedta7900872011-12-16 16:23:44 -05002254 again:
2255 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2256
2257 if (!pg->records) {
2258 /* if we can't allocate this size, try something smaller */
2259 if (!order)
2260 return -ENOMEM;
2261 order >>= 1;
2262 goto again;
2263 }
2264
2265 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2266 pg->size = cnt;
2267
2268 if (cnt > count)
2269 cnt = count;
2270
2271 return cnt;
2272}
2273
2274static struct ftrace_page *
2275ftrace_allocate_pages(unsigned long num_to_init)
2276{
2277 struct ftrace_page *start_pg;
2278 struct ftrace_page *pg;
2279 int order;
2280 int cnt;
2281
2282 if (!num_to_init)
2283 return 0;
2284
2285 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2286 if (!pg)
2287 return NULL;
2288
2289 /*
2290 * Try to allocate as much as possible in one continues
2291 * location that fills in all of the space. We want to
2292 * waste as little space as possible.
2293 */
2294 for (;;) {
2295 cnt = ftrace_allocate_records(pg, num_to_init);
2296 if (cnt < 0)
2297 goto free_pages;
2298
2299 num_to_init -= cnt;
2300 if (!num_to_init)
2301 break;
2302
2303 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2304 if (!pg->next)
2305 goto free_pages;
2306
2307 pg = pg->next;
2308 }
2309
2310 return start_pg;
2311
2312 free_pages:
2313 while (start_pg) {
2314 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2315 free_pages((unsigned long)pg->records, order);
2316 start_pg = pg->next;
2317 kfree(pg);
2318 pg = start_pg;
2319 }
2320 pr_info("ftrace: FAILED to allocate memory for functions\n");
2321 return NULL;
2322}
2323
2324static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2325{
2326 int cnt;
2327
2328 if (!num_to_init) {
2329 pr_info("ftrace: No functions to be traced?\n");
2330 return -1;
2331 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002332
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002333 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002334 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002335 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002336
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002337 return 0;
2338}
2339
Steven Rostedt5072c592008-05-12 21:20:43 +02002340#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2341
2342struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002343 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002344 loff_t func_pos;
2345 struct ftrace_page *pg;
2346 struct dyn_ftrace *func;
2347 struct ftrace_func_probe *probe;
2348 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002349 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002350 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002351 int hidx;
2352 int idx;
2353 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002354};
2355
Ingo Molnare309b412008-05-12 21:20:51 +02002356static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002357t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002358{
2359 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002360 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002361 struct hlist_head *hhd;
2362
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002363 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002364 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002365
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002366 if (iter->probe)
2367 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002368 retry:
2369 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2370 return NULL;
2371
2372 hhd = &ftrace_func_hash[iter->hidx];
2373
2374 if (hlist_empty(hhd)) {
2375 iter->hidx++;
2376 hnd = NULL;
2377 goto retry;
2378 }
2379
2380 if (!hnd)
2381 hnd = hhd->first;
2382 else {
2383 hnd = hnd->next;
2384 if (!hnd) {
2385 iter->hidx++;
2386 goto retry;
2387 }
2388 }
2389
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002390 if (WARN_ON_ONCE(!hnd))
2391 return NULL;
2392
2393 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2394
2395 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002396}
2397
2398static void *t_hash_start(struct seq_file *m, loff_t *pos)
2399{
2400 struct ftrace_iterator *iter = m->private;
2401 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002402 loff_t l;
2403
Steven Rostedt69a30832011-12-19 15:21:16 -05002404 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2405 return NULL;
2406
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002407 if (iter->func_pos > *pos)
2408 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002409
Li Zefand82d6242009-06-24 09:54:54 +08002410 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002411 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002412 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002413 if (!p)
2414 break;
2415 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002416 if (!p)
2417 return NULL;
2418
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002419 /* Only set this if we have an item */
2420 iter->flags |= FTRACE_ITER_HASH;
2421
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002422 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002423}
2424
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002425static int
2426t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002427{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002428 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002429
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002430 rec = iter->probe;
2431 if (WARN_ON_ONCE(!rec))
2432 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002433
Steven Rostedt809dcf22009-02-16 23:06:01 -05002434 if (rec->ops->print)
2435 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2436
Steven Rostedtb375a112009-09-17 00:05:58 -04002437 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002438
2439 if (rec->data)
2440 seq_printf(m, ":%p", rec->data);
2441 seq_putc(m, '\n');
2442
2443 return 0;
2444}
2445
2446static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002447t_next(struct seq_file *m, void *v, loff_t *pos)
2448{
2449 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002450 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002451 struct dyn_ftrace *rec = NULL;
2452
Steven Rostedt45a4a232011-04-21 23:16:46 -04002453 if (unlikely(ftrace_disabled))
2454 return NULL;
2455
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002456 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002457 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002458
Steven Rostedt5072c592008-05-12 21:20:43 +02002459 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002460 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002461
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002462 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002463 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002464
Steven Rostedt5072c592008-05-12 21:20:43 +02002465 retry:
2466 if (iter->idx >= iter->pg->index) {
2467 if (iter->pg->next) {
2468 iter->pg = iter->pg->next;
2469 iter->idx = 0;
2470 goto retry;
2471 }
2472 } else {
2473 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002474 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002475 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002476
Steven Rostedt41c52c02008-05-22 11:46:33 -04002477 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002478 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2479
2480 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002481 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002482
Steven Rostedt5072c592008-05-12 21:20:43 +02002483 rec = NULL;
2484 goto retry;
2485 }
2486 }
2487
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002488 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002489 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002490
2491 iter->func = rec;
2492
2493 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002494}
2495
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002496static void reset_iter_read(struct ftrace_iterator *iter)
2497{
2498 iter->pos = 0;
2499 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002500 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002501}
2502
2503static void *t_start(struct seq_file *m, loff_t *pos)
2504{
2505 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002506 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002507 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002508 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002509
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002510 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002511
2512 if (unlikely(ftrace_disabled))
2513 return NULL;
2514
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002515 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002516 * If an lseek was done, then reset and start from beginning.
2517 */
2518 if (*pos < iter->pos)
2519 reset_iter_read(iter);
2520
2521 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002522 * For set_ftrace_filter reading, if we have the filter
2523 * off, we can short cut and just print out that all
2524 * functions are enabled.
2525 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002526 if (iter->flags & FTRACE_ITER_FILTER &&
2527 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002528 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002529 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002530 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002531 /* reset in case of seek/pread */
2532 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002533 return iter;
2534 }
2535
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002536 if (iter->flags & FTRACE_ITER_HASH)
2537 return t_hash_start(m, pos);
2538
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002539 /*
2540 * Unfortunately, we need to restart at ftrace_pages_start
2541 * every time we let go of the ftrace_mutex. This is because
2542 * those pointers can change without the lock.
2543 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002544 iter->pg = ftrace_pages_start;
2545 iter->idx = 0;
2546 for (l = 0; l <= *pos; ) {
2547 p = t_next(m, p, &l);
2548 if (!p)
2549 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002550 }
walimis5821e1b2008-11-15 15:19:06 +08002551
Steven Rostedt69a30832011-12-19 15:21:16 -05002552 if (!p)
2553 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002554
2555 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002556}
2557
2558static void t_stop(struct seq_file *m, void *p)
2559{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002560 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002561}
2562
2563static int t_show(struct seq_file *m, void *v)
2564{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002565 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002566 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002567
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002568 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002569 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002570
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002571 if (iter->flags & FTRACE_ITER_PRINTALL) {
2572 seq_printf(m, "#### all functions enabled ####\n");
2573 return 0;
2574 }
2575
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002576 rec = iter->func;
2577
Steven Rostedt5072c592008-05-12 21:20:43 +02002578 if (!rec)
2579 return 0;
2580
Steven Rostedt647bcd02011-05-03 14:39:21 -04002581 seq_printf(m, "%ps", (void *)rec->ip);
2582 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002583 seq_printf(m, " (%ld)%s",
2584 rec->flags & ~FTRACE_FL_MASK,
2585 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002586 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002587
2588 return 0;
2589}
2590
James Morris88e9d342009-09-22 16:43:43 -07002591static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002592 .start = t_start,
2593 .next = t_next,
2594 .stop = t_stop,
2595 .show = t_show,
2596};
2597
Ingo Molnare309b412008-05-12 21:20:51 +02002598static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002599ftrace_avail_open(struct inode *inode, struct file *file)
2600{
2601 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002602
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002603 if (unlikely(ftrace_disabled))
2604 return -ENODEV;
2605
Jiri Olsa50e18b92012-04-25 10:23:39 +02002606 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2607 if (iter) {
2608 iter->pg = ftrace_pages_start;
2609 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002610 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002611
Jiri Olsa50e18b92012-04-25 10:23:39 +02002612 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002613}
2614
Steven Rostedt647bcd02011-05-03 14:39:21 -04002615static int
2616ftrace_enabled_open(struct inode *inode, struct file *file)
2617{
2618 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002619
2620 if (unlikely(ftrace_disabled))
2621 return -ENODEV;
2622
Jiri Olsa50e18b92012-04-25 10:23:39 +02002623 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2624 if (iter) {
2625 iter->pg = ftrace_pages_start;
2626 iter->flags = FTRACE_ITER_ENABLED;
2627 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002628 }
2629
Jiri Olsa50e18b92012-04-25 10:23:39 +02002630 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002631}
2632
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002633static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002634{
Steven Rostedt52baf112009-02-14 01:15:39 -05002635 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002636 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002637 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002638}
2639
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002640/**
2641 * ftrace_regex_open - initialize function tracer filter files
2642 * @ops: The ftrace_ops that hold the hash filters
2643 * @flag: The type of filter to process
2644 * @inode: The inode, usually passed in to your open routine
2645 * @file: The file, usually passed in to your open routine
2646 *
2647 * ftrace_regex_open() initializes the filter files for the
2648 * @ops. Depending on @flag it may process the filter hash or
2649 * the notrace hash of @ops. With this called from the open
2650 * routine, you can use ftrace_filter_write() for the write
2651 * routine if @flag has FTRACE_ITER_FILTER set, or
2652 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04002653 * ftrace_filter_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002654 * release must call ftrace_regex_release().
2655 */
2656int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002657ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002658 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002659{
2660 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002661 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002662 int ret = 0;
2663
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002664 ftrace_ops_init(ops);
2665
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002666 if (unlikely(ftrace_disabled))
2667 return -ENODEV;
2668
Steven Rostedt5072c592008-05-12 21:20:43 +02002669 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2670 if (!iter)
2671 return -ENOMEM;
2672
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002673 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2674 kfree(iter);
2675 return -ENOMEM;
2676 }
2677
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002678 iter->ops = ops;
2679 iter->flags = flag;
2680
2681 mutex_lock(&ops->regex_lock);
2682
Steven Rostedtf45948e2011-05-02 12:29:25 -04002683 if (flag & FTRACE_ITER_NOTRACE)
2684 hash = ops->notrace_hash;
2685 else
2686 hash = ops->filter_hash;
2687
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002688 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002689 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002690 if (!iter->hash) {
2691 trace_parser_put(&iter->parser);
2692 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002693 ret = -ENOMEM;
2694 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002695 }
2696 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002697
Steven Rostedt5072c592008-05-12 21:20:43 +02002698 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002699 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002700 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002701
2702 if (file->f_mode & FMODE_READ) {
2703 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002704
2705 ret = seq_open(file, &show_ftrace_seq_ops);
2706 if (!ret) {
2707 struct seq_file *m = file->private_data;
2708 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002709 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002710 /* Failed */
2711 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002712 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002713 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002714 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002715 } else
2716 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002717
2718 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002719 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002720
2721 return ret;
2722}
2723
Steven Rostedt41c52c02008-05-22 11:46:33 -04002724static int
2725ftrace_filter_open(struct inode *inode, struct file *file)
2726{
Steven Rostedt69a30832011-12-19 15:21:16 -05002727 return ftrace_regex_open(&global_ops,
2728 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2729 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002730}
2731
2732static int
2733ftrace_notrace_open(struct inode *inode, struct file *file)
2734{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002735 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002736 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002737}
2738
Steven Rostedt64e7c442009-02-13 17:08:48 -05002739static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002740{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002741 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002742 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002743
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002744 switch (type) {
2745 case MATCH_FULL:
2746 if (strcmp(str, regex) == 0)
2747 matched = 1;
2748 break;
2749 case MATCH_FRONT_ONLY:
2750 if (strncmp(str, regex, len) == 0)
2751 matched = 1;
2752 break;
2753 case MATCH_MIDDLE_ONLY:
2754 if (strstr(str, regex))
2755 matched = 1;
2756 break;
2757 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002758 slen = strlen(str);
2759 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002760 matched = 1;
2761 break;
2762 }
2763
2764 return matched;
2765}
2766
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002767static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002768enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002769{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002770 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002771 int ret = 0;
2772
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002773 entry = ftrace_lookup_ip(hash, rec->ip);
2774 if (not) {
2775 /* Do nothing if it doesn't exist */
2776 if (!entry)
2777 return 0;
2778
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002779 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002780 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002781 /* Do nothing if it exists */
2782 if (entry)
2783 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002784
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002785 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002786 }
2787 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002788}
2789
Steven Rostedt64e7c442009-02-13 17:08:48 -05002790static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002791ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2792 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002793{
2794 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002795 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002796
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002797 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2798
2799 if (mod) {
2800 /* module lookup requires matching the module */
2801 if (!modname || strcmp(modname, mod))
2802 return 0;
2803
2804 /* blank search means to match all funcs in the mod */
2805 if (!len)
2806 return 1;
2807 }
2808
Steven Rostedt64e7c442009-02-13 17:08:48 -05002809 return ftrace_match(str, regex, len, type);
2810}
2811
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002812static int
2813match_records(struct ftrace_hash *hash, char *buff,
2814 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002815{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002816 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002817 struct ftrace_page *pg;
2818 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002819 int type = MATCH_FULL;
2820 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002821 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002822 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002823
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002824 if (len) {
2825 type = filter_parse_regex(buff, len, &search, &not);
2826 search_len = strlen(search);
2827 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002828
Steven Rostedt52baf112009-02-14 01:15:39 -05002829 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002830
2831 if (unlikely(ftrace_disabled))
2832 goto out_unlock;
2833
Steven Rostedt265c8312009-02-13 12:43:56 -05002834 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002835 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002836 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002837 if (ret < 0) {
2838 found = ret;
2839 goto out_unlock;
2840 }
Li Zefan311d16d2009-12-08 11:15:11 +08002841 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002842 }
2843 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002844 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002845 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002846
2847 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002848}
2849
Steven Rostedt64e7c442009-02-13 17:08:48 -05002850static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002851ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002852{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002853 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002854}
2855
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002856static int
2857ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002858{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002859 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002860
Steven Rostedt64e7c442009-02-13 17:08:48 -05002861 /* blank or '*' mean the same */
2862 if (strcmp(buff, "*") == 0)
2863 buff[0] = 0;
2864
2865 /* handle the case of 'dont filter this module' */
2866 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2867 buff[0] = 0;
2868 not = 1;
2869 }
2870
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002871 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002872}
2873
Steven Rostedtf6180772009-02-14 00:40:25 -05002874/*
2875 * We register the module command as a template to show others how
2876 * to register the a command as well.
2877 */
2878
2879static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002880ftrace_mod_callback(struct ftrace_hash *hash,
2881 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002882{
2883 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002884 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002885
2886 /*
2887 * cmd == 'mod' because we only registered this func
2888 * for the 'mod' ftrace_func_command.
2889 * But if you register one func with multiple commands,
2890 * you can tell which command was used by the cmd
2891 * parameter.
2892 */
2893
2894 /* we must have a module name */
2895 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002896 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002897
2898 mod = strsep(&param, ":");
2899 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002900 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002901
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002902 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002903 if (!ret)
2904 ret = -EINVAL;
2905 if (ret < 0)
2906 return ret;
2907
2908 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002909}
2910
2911static struct ftrace_func_command ftrace_mod_cmd = {
2912 .name = "mod",
2913 .func = ftrace_mod_callback,
2914};
2915
2916static int __init ftrace_mod_cmd_init(void)
2917{
2918 return register_ftrace_command(&ftrace_mod_cmd);
2919}
Steven Rostedt6f415672012-10-05 12:13:07 -04002920core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002921
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002922static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002923 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002924{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002925 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002926 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002927 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002928
2929 key = hash_long(ip, FTRACE_HASH_BITS);
2930
2931 hhd = &ftrace_func_hash[key];
2932
2933 if (hlist_empty(hhd))
2934 return;
2935
2936 /*
2937 * Disable preemption for these calls to prevent a RCU grace
2938 * period. This syncs the hash iteration and freeing of items
2939 * on the hash. rcu_read_lock is too dangerous here.
2940 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002941 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04002942 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002943 if (entry->ip == ip)
2944 entry->ops->func(ip, parent_ip, &entry->data);
2945 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002946 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002947}
2948
Steven Rostedtb6887d72009-02-17 12:32:04 -05002949static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002950{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002951 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002952 .flags = FTRACE_OPS_FL_INITIALIZED,
2953 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002954};
2955
Steven Rostedtb6887d72009-02-17 12:32:04 -05002956static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002957
Steven Rostedtb6887d72009-02-17 12:32:04 -05002958static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002959{
Steven Rostedtb8489142011-05-04 09:27:52 -04002960 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002961 int i;
2962
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04002963 if (ftrace_probe_registered) {
2964 /* still need to update the function call sites */
2965 if (ftrace_enabled)
2966 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002967 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04002968 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05002969
2970 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2971 struct hlist_head *hhd = &ftrace_func_hash[i];
2972 if (hhd->first)
2973 break;
2974 }
2975 /* Nothing registered? */
2976 if (i == FTRACE_FUNC_HASHSIZE)
2977 return;
2978
Steven Rostedtb8489142011-05-04 09:27:52 -04002979 ret = __register_ftrace_function(&trace_probe_ops);
2980 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002981 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002982
Steven Rostedtb6887d72009-02-17 12:32:04 -05002983 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002984}
2985
Steven Rostedtb6887d72009-02-17 12:32:04 -05002986static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002987{
Steven Rostedtb8489142011-05-04 09:27:52 -04002988 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002989 int i;
2990
Steven Rostedtb6887d72009-02-17 12:32:04 -05002991 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002992 return;
2993
2994 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2995 struct hlist_head *hhd = &ftrace_func_hash[i];
2996 if (hhd->first)
2997 return;
2998 }
2999
3000 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04003001 ret = __unregister_ftrace_function(&trace_probe_ops);
3002 if (!ret)
3003 ftrace_shutdown(&trace_probe_ops, 0);
3004
Steven Rostedtb6887d72009-02-17 12:32:04 -05003005 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003006}
3007
3008
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003009static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003010{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003011 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003012 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003013 kfree(entry);
3014}
3015
Steven Rostedt59df055f2009-02-14 15:29:06 -05003016int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003017register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003018 void *data)
3019{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003020 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003021 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3022 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003023 struct ftrace_page *pg;
3024 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003025 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003026 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003027 int count = 0;
3028 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003029 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003030
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003031 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003032 len = strlen(search);
3033
Steven Rostedtb6887d72009-02-17 12:32:04 -05003034 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003035 if (WARN_ON(not))
3036 return -EINVAL;
3037
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003038 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003039
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003040 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3041 if (!hash) {
3042 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003043 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003044 }
3045
3046 if (unlikely(ftrace_disabled)) {
3047 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003048 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003049 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003050
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003051 mutex_lock(&ftrace_lock);
3052
Steven Rostedt59df055f2009-02-14 15:29:06 -05003053 do_for_each_ftrace_rec(pg, rec) {
3054
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003055 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003056 continue;
3057
3058 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3059 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003060 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003061 if (!count)
3062 count = -ENOMEM;
3063 goto out_unlock;
3064 }
3065
3066 count++;
3067
3068 entry->data = data;
3069
3070 /*
3071 * The caller might want to do something special
3072 * for each function we find. We call the callback
3073 * to give the caller an opportunity to do so.
3074 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003075 if (ops->init) {
3076 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003077 /* caller does not like this func */
3078 kfree(entry);
3079 continue;
3080 }
3081 }
3082
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003083 ret = enter_record(hash, rec, 0);
3084 if (ret < 0) {
3085 kfree(entry);
3086 count = ret;
3087 goto out_unlock;
3088 }
3089
Steven Rostedt59df055f2009-02-14 15:29:06 -05003090 entry->ops = ops;
3091 entry->ip = rec->ip;
3092
3093 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3094 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3095
3096 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003097
3098 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3099 if (ret < 0)
3100 count = ret;
3101
Steven Rostedtb6887d72009-02-17 12:32:04 -05003102 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003103
3104 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003105 mutex_unlock(&ftrace_lock);
3106 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003107 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003108 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003109
3110 return count;
3111}
3112
3113enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003114 PROBE_TEST_FUNC = 1,
3115 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003116};
3117
3118static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003119__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003120 void *data, int flags)
3121{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003122 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003123 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003124 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003125 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003126 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003127 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003128 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003129 char str[KSYM_SYMBOL_LEN];
3130 int type = MATCH_FULL;
3131 int i, len = 0;
3132 char *search;
3133
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003134 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003135 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003136 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003137 int not;
3138
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003139 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003140 len = strlen(search);
3141
Steven Rostedtb6887d72009-02-17 12:32:04 -05003142 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003143 if (WARN_ON(not))
3144 return;
3145 }
3146
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003147 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003148
3149 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3150 if (!hash)
3151 /* Hmm, should report this somehow */
3152 goto out_unlock;
3153
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003154 INIT_LIST_HEAD(&free_list);
3155
Steven Rostedt59df055f2009-02-14 15:29:06 -05003156 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3157 struct hlist_head *hhd = &ftrace_func_hash[i];
3158
Sasha Levinb67bfe02013-02-27 17:06:00 -08003159 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003160
3161 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003162 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003163 continue;
3164
Steven Rostedtb6887d72009-02-17 12:32:04 -05003165 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003166 continue;
3167
3168 /* do this last, since it is the most expensive */
3169 if (glob) {
3170 kallsyms_lookup(entry->ip, NULL, NULL,
3171 NULL, str);
3172 if (!ftrace_match(str, glob, len, type))
3173 continue;
3174 }
3175
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003176 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3177 /* It is possible more than one entry had this ip */
3178 if (rec_entry)
3179 free_hash_entry(hash, rec_entry);
3180
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003181 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003182 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003183 }
3184 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003185 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003186 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003187 /*
3188 * Remove after the disable is called. Otherwise, if the last
3189 * probe is removed, a null hash means *all enabled*.
3190 */
3191 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003192 synchronize_sched();
3193 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3194 list_del(&entry->free_list);
3195 ftrace_free_entry(entry);
3196 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003197 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003198
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003199 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003200 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003201 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003202}
3203
3204void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003205unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003206 void *data)
3207{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003208 __unregister_ftrace_function_probe(glob, ops, data,
3209 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003210}
3211
3212void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003213unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003214{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003215 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003216}
3217
Steven Rostedtb6887d72009-02-17 12:32:04 -05003218void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003219{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003220 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003221}
3222
Steven Rostedtf6180772009-02-14 00:40:25 -05003223static LIST_HEAD(ftrace_commands);
3224static DEFINE_MUTEX(ftrace_cmd_mutex);
3225
3226int register_ftrace_command(struct ftrace_func_command *cmd)
3227{
3228 struct ftrace_func_command *p;
3229 int ret = 0;
3230
3231 mutex_lock(&ftrace_cmd_mutex);
3232 list_for_each_entry(p, &ftrace_commands, list) {
3233 if (strcmp(cmd->name, p->name) == 0) {
3234 ret = -EBUSY;
3235 goto out_unlock;
3236 }
3237 }
3238 list_add(&cmd->list, &ftrace_commands);
3239 out_unlock:
3240 mutex_unlock(&ftrace_cmd_mutex);
3241
3242 return ret;
3243}
3244
3245int unregister_ftrace_command(struct ftrace_func_command *cmd)
3246{
3247 struct ftrace_func_command *p, *n;
3248 int ret = -ENODEV;
3249
3250 mutex_lock(&ftrace_cmd_mutex);
3251 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3252 if (strcmp(cmd->name, p->name) == 0) {
3253 ret = 0;
3254 list_del_init(&p->list);
3255 goto out_unlock;
3256 }
3257 }
3258 out_unlock:
3259 mutex_unlock(&ftrace_cmd_mutex);
3260
3261 return ret;
3262}
3263
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003264static int ftrace_process_regex(struct ftrace_hash *hash,
3265 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003266{
Steven Rostedtf6180772009-02-14 00:40:25 -05003267 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003268 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003269 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003270
3271 func = strsep(&next, ":");
3272
3273 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003274 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003275 if (!ret)
3276 ret = -EINVAL;
3277 if (ret < 0)
3278 return ret;
3279 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003280 }
3281
Steven Rostedtf6180772009-02-14 00:40:25 -05003282 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003283
3284 command = strsep(&next, ":");
3285
Steven Rostedtf6180772009-02-14 00:40:25 -05003286 mutex_lock(&ftrace_cmd_mutex);
3287 list_for_each_entry(p, &ftrace_commands, list) {
3288 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003289 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003290 goto out_unlock;
3291 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003292 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003293 out_unlock:
3294 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003295
Steven Rostedtf6180772009-02-14 00:40:25 -05003296 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003297}
3298
Ingo Molnare309b412008-05-12 21:20:51 +02003299static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003300ftrace_regex_write(struct file *file, const char __user *ubuf,
3301 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003302{
3303 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003304 struct trace_parser *parser;
3305 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003306
Li Zefan4ba79782009-09-22 13:52:20 +08003307 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003308 return 0;
3309
Steven Rostedt5072c592008-05-12 21:20:43 +02003310 if (file->f_mode & FMODE_READ) {
3311 struct seq_file *m = file->private_data;
3312 iter = m->private;
3313 } else
3314 iter = file->private_data;
3315
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003316 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003317 return -ENODEV;
3318
3319 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003320
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003321 parser = &iter->parser;
3322 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003323
Li Zefan4ba79782009-09-22 13:52:20 +08003324 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003325 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003326 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003327 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003328 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003329 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003330 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003331 }
3332
Steven Rostedt5072c592008-05-12 21:20:43 +02003333 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003334 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003335 return ret;
3336}
3337
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003338ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003339ftrace_filter_write(struct file *file, const char __user *ubuf,
3340 size_t cnt, loff_t *ppos)
3341{
3342 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3343}
3344
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003345ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003346ftrace_notrace_write(struct file *file, const char __user *ubuf,
3347 size_t cnt, loff_t *ppos)
3348{
3349 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3350}
3351
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003352static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003353ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3354{
3355 struct ftrace_func_entry *entry;
3356
3357 if (!ftrace_location(ip))
3358 return -EINVAL;
3359
3360 if (remove) {
3361 entry = ftrace_lookup_ip(hash, ip);
3362 if (!entry)
3363 return -ENOENT;
3364 free_hash_entry(hash, entry);
3365 return 0;
3366 }
3367
3368 return add_hash_entry(hash, ip);
3369}
3370
3371static int
3372ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3373 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003374{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003375 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003376 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003377 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003378
Steven Rostedt936e0742011-05-05 22:54:01 -04003379 /* All global ops uses the global ops filters */
3380 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3381 ops = &global_ops;
3382
Steven Rostedt41c52c02008-05-22 11:46:33 -04003383 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003384 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003385
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003386 mutex_lock(&ops->regex_lock);
3387
Steven Rostedtf45948e2011-05-02 12:29:25 -04003388 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003389 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003390 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003391 orig_hash = &ops->notrace_hash;
3392
3393 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003394 if (!hash) {
3395 ret = -ENOMEM;
3396 goto out_regex_unlock;
3397 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003398
Steven Rostedt41c52c02008-05-22 11:46:33 -04003399 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003400 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003401 if (buf && !ftrace_match_records(hash, buf, len)) {
3402 ret = -EINVAL;
3403 goto out_regex_unlock;
3404 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003405 if (ip) {
3406 ret = ftrace_match_addr(hash, ip, remove);
3407 if (ret < 0)
3408 goto out_regex_unlock;
3409 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003410
3411 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003412 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003413 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3414 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003415 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003416
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003417 mutex_unlock(&ftrace_lock);
3418
Jiri Olsaac483c42012-01-02 10:04:14 +01003419 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003420 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003421
3422 free_ftrace_hash(hash);
3423 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003424}
3425
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003426static int
3427ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3428 int reset, int enable)
3429{
3430 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3431}
3432
3433/**
3434 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3435 * @ops - the ops to set the filter with
3436 * @ip - the address to add to or remove from the filter.
3437 * @remove - non zero to remove the ip from the filter
3438 * @reset - non zero to reset all filters before applying this filter.
3439 *
3440 * Filters denote which functions should be enabled when tracing is enabled
3441 * If @ip is NULL, it failes to update filter.
3442 */
3443int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3444 int remove, int reset)
3445{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003446 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003447 return ftrace_set_addr(ops, ip, remove, reset, 1);
3448}
3449EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3450
3451static int
3452ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3453 int reset, int enable)
3454{
3455 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3456}
3457
Steven Rostedt77a2b372008-05-12 21:20:45 +02003458/**
3459 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003460 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003461 * @buf - the string that holds the function filter text.
3462 * @len - the length of the string.
3463 * @reset - non zero to reset all filters before applying this filter.
3464 *
3465 * Filters denote which functions should be enabled when tracing is enabled.
3466 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3467 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003468int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003469 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003470{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003471 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003472 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003473}
Steven Rostedt936e0742011-05-05 22:54:01 -04003474EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003475
Steven Rostedt41c52c02008-05-22 11:46:33 -04003476/**
3477 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003478 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003479 * @buf - the string that holds the function notrace text.
3480 * @len - the length of the string.
3481 * @reset - non zero to reset all filters before applying this filter.
3482 *
3483 * Notrace Filters denote which functions should not be enabled when tracing
3484 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3485 * for tracing.
3486 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003487int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003488 int len, int reset)
3489{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003490 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003491 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003492}
3493EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3494/**
3495 * ftrace_set_filter - set a function to filter on in ftrace
3496 * @ops - the ops to set the filter with
3497 * @buf - the string that holds the function filter text.
3498 * @len - the length of the string.
3499 * @reset - non zero to reset all filters before applying this filter.
3500 *
3501 * Filters denote which functions should be enabled when tracing is enabled.
3502 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3503 */
3504void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3505{
3506 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3507}
3508EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3509
3510/**
3511 * ftrace_set_notrace - set a function to not trace in ftrace
3512 * @ops - the ops to set the notrace filter with
3513 * @buf - the string that holds the function notrace text.
3514 * @len - the length of the string.
3515 * @reset - non zero to reset all filters before applying this filter.
3516 *
3517 * Notrace Filters denote which functions should not be enabled when tracing
3518 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3519 * for tracing.
3520 */
3521void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003522{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003523 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003524}
Steven Rostedt936e0742011-05-05 22:54:01 -04003525EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003526
Steven Rostedt2af15d62009-05-28 13:37:24 -04003527/*
3528 * command line interface to allow users to set filters on boot up.
3529 */
3530#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3531static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3532static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3533
3534static int __init set_ftrace_notrace(char *str)
3535{
Chen Gang75761cc2013-04-08 12:12:39 +08003536 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003537 return 1;
3538}
3539__setup("ftrace_notrace=", set_ftrace_notrace);
3540
3541static int __init set_ftrace_filter(char *str)
3542{
Chen Gang75761cc2013-04-08 12:12:39 +08003543 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003544 return 1;
3545}
3546__setup("ftrace_filter=", set_ftrace_filter);
3547
Stefan Assmann369bc182009-10-12 22:17:21 +02003548#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003549static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003550static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3551
Stefan Assmann369bc182009-10-12 22:17:21 +02003552static int __init set_graph_function(char *str)
3553{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003554 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003555 return 1;
3556}
3557__setup("ftrace_graph_filter=", set_graph_function);
3558
3559static void __init set_ftrace_early_graph(char *buf)
3560{
3561 int ret;
3562 char *func;
3563
3564 while (buf) {
3565 func = strsep(&buf, ",");
3566 /* we allow only one expression at a time */
3567 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3568 func);
3569 if (ret)
3570 printk(KERN_DEBUG "ftrace: function %s not "
3571 "traceable\n", func);
3572 }
3573}
3574#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3575
Steven Rostedt2a85a372011-12-19 21:57:44 -05003576void __init
3577ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003578{
3579 char *func;
3580
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003581 ftrace_ops_init(ops);
3582
Steven Rostedt2af15d62009-05-28 13:37:24 -04003583 while (buf) {
3584 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003585 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003586 }
3587}
3588
3589static void __init set_ftrace_early_filters(void)
3590{
3591 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003592 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003593 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003594 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003595#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3596 if (ftrace_graph_buf[0])
3597 set_ftrace_early_graph(ftrace_graph_buf);
3598#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003599}
3600
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003601int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003602{
3603 struct seq_file *m = (struct seq_file *)file->private_data;
3604 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003605 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003606 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003607 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003608 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003609
Steven Rostedt5072c592008-05-12 21:20:43 +02003610 if (file->f_mode & FMODE_READ) {
3611 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003612 seq_release(inode, file);
3613 } else
3614 iter = file->private_data;
3615
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003616 parser = &iter->parser;
3617 if (trace_parser_loaded(parser)) {
3618 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003619 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003620 }
3621
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003622 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003623
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003624 mutex_lock(&iter->ops->regex_lock);
3625
Steven Rostedt058e2972011-04-29 22:35:33 -04003626 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003627 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3628
3629 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003630 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003631 else
3632 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003633
Steven Rostedt058e2972011-04-29 22:35:33 -04003634 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003635 ret = ftrace_hash_move(iter->ops, filter_hash,
3636 orig_hash, iter->hash);
3637 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3638 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003639 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003640
Steven Rostedt058e2972011-04-29 22:35:33 -04003641 mutex_unlock(&ftrace_lock);
3642 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003643
3644 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003645 free_ftrace_hash(iter->hash);
3646 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003647
Steven Rostedt5072c592008-05-12 21:20:43 +02003648 return 0;
3649}
3650
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003651static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003652 .open = ftrace_avail_open,
3653 .read = seq_read,
3654 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003655 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003656};
3657
Steven Rostedt647bcd02011-05-03 14:39:21 -04003658static const struct file_operations ftrace_enabled_fops = {
3659 .open = ftrace_enabled_open,
3660 .read = seq_read,
3661 .llseek = seq_lseek,
3662 .release = seq_release_private,
3663};
3664
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003665static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003666 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003667 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003668 .write = ftrace_filter_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003669 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003670 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003671};
3672
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003673static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003674 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003675 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003676 .write = ftrace_notrace_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003677 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003678 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003679};
3680
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003681#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3682
3683static DEFINE_MUTEX(graph_lock);
3684
3685int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003686int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003687unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3688
3689static void *
Li Zefan85951842009-06-24 09:54:00 +08003690__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003691{
Li Zefan85951842009-06-24 09:54:00 +08003692 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003693 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003694 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003695}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003696
Li Zefan85951842009-06-24 09:54:00 +08003697static void *
3698g_next(struct seq_file *m, void *v, loff_t *pos)
3699{
3700 (*pos)++;
3701 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003702}
3703
3704static void *g_start(struct seq_file *m, loff_t *pos)
3705{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003706 mutex_lock(&graph_lock);
3707
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003708 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003709 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003710 return (void *)1;
3711
Li Zefan85951842009-06-24 09:54:00 +08003712 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003713}
3714
3715static void g_stop(struct seq_file *m, void *p)
3716{
3717 mutex_unlock(&graph_lock);
3718}
3719
3720static int g_show(struct seq_file *m, void *v)
3721{
3722 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003723
3724 if (!ptr)
3725 return 0;
3726
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003727 if (ptr == (unsigned long *)1) {
3728 seq_printf(m, "#### all functions enabled ####\n");
3729 return 0;
3730 }
3731
Steven Rostedtb375a112009-09-17 00:05:58 -04003732 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003733
3734 return 0;
3735}
3736
James Morris88e9d342009-09-22 16:43:43 -07003737static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003738 .start = g_start,
3739 .next = g_next,
3740 .stop = g_stop,
3741 .show = g_show,
3742};
3743
3744static int
3745ftrace_graph_open(struct inode *inode, struct file *file)
3746{
3747 int ret = 0;
3748
3749 if (unlikely(ftrace_disabled))
3750 return -ENODEV;
3751
3752 mutex_lock(&graph_lock);
3753 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003754 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003755 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003756 ftrace_graph_count = 0;
3757 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3758 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003759 mutex_unlock(&graph_lock);
3760
Li Zefana4ec5e02009-09-18 14:06:28 +08003761 if (file->f_mode & FMODE_READ)
3762 ret = seq_open(file, &ftrace_graph_seq_ops);
3763
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003764 return ret;
3765}
3766
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003767static int
Li Zefan87827112009-07-23 11:29:11 +08003768ftrace_graph_release(struct inode *inode, struct file *file)
3769{
3770 if (file->f_mode & FMODE_READ)
3771 seq_release(inode, file);
3772 return 0;
3773}
3774
3775static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003776ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003777{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003778 struct dyn_ftrace *rec;
3779 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003780 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003781 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003782 int type, not;
3783 char *search;
3784 bool exists;
3785 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003786
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003787 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003788 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003789 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3790 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003791
3792 search_len = strlen(search);
3793
Steven Rostedt52baf112009-02-14 01:15:39 -05003794 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003795
3796 if (unlikely(ftrace_disabled)) {
3797 mutex_unlock(&ftrace_lock);
3798 return -ENODEV;
3799 }
3800
Steven Rostedt265c8312009-02-13 12:43:56 -05003801 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003802
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003803 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003804 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003805 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003806 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003807 if (array[i] == rec->ip) {
3808 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003809 break;
3810 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003811 }
3812
3813 if (!not) {
3814 fail = 0;
3815 if (!exists) {
3816 array[(*idx)++] = rec->ip;
3817 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3818 goto out;
3819 }
3820 } else {
3821 if (exists) {
3822 array[i] = array[--(*idx)];
3823 array[*idx] = 0;
3824 fail = 0;
3825 }
3826 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003827 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003828 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003829out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003830 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003831
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003832 if (fail)
3833 return -EINVAL;
3834
Namhyung Kim9f50afc2013-04-11 16:01:38 +09003835 ftrace_graph_filter_enabled = !!(*idx);
3836
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003837 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003838}
3839
3840static ssize_t
3841ftrace_graph_write(struct file *file, const char __user *ubuf,
3842 size_t cnt, loff_t *ppos)
3843{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003844 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003845 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003846
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003847 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003848 return 0;
3849
3850 mutex_lock(&graph_lock);
3851
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003852 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3853 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003854 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003855 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003856
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003857 read = trace_get_user(&parser, ubuf, cnt, ppos);
3858
Li Zefan4ba79782009-09-22 13:52:20 +08003859 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003860 parser.buffer[parser.idx] = 0;
3861
3862 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003863 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003864 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003865 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003866 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003867 }
3868
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003869 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003870
3871out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003872 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003873out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003874 mutex_unlock(&graph_lock);
3875
3876 return ret;
3877}
3878
3879static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003880 .open = ftrace_graph_open,
3881 .read = seq_read,
3882 .write = ftrace_graph_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003883 .llseek = ftrace_filter_lseek,
Li Zefan87827112009-07-23 11:29:11 +08003884 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003885};
3886#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3887
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003888static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003889{
Steven Rostedt5072c592008-05-12 21:20:43 +02003890
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003891 trace_create_file("available_filter_functions", 0444,
3892 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003893
Steven Rostedt647bcd02011-05-03 14:39:21 -04003894 trace_create_file("enabled_functions", 0444,
3895 d_tracer, NULL, &ftrace_enabled_fops);
3896
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003897 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3898 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003899
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003900 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003901 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003902
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003903#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003904 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003905 NULL,
3906 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003907#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3908
Steven Rostedt5072c592008-05-12 21:20:43 +02003909 return 0;
3910}
3911
Steven Rostedt9fd49322012-04-24 22:32:06 -04003912static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003913{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003914 const unsigned long *ipa = a;
3915 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003916
Steven Rostedt9fd49322012-04-24 22:32:06 -04003917 if (*ipa > *ipb)
3918 return 1;
3919 if (*ipa < *ipb)
3920 return -1;
3921 return 0;
3922}
3923
3924static void ftrace_swap_ips(void *a, void *b, int size)
3925{
3926 unsigned long *ipa = a;
3927 unsigned long *ipb = b;
3928 unsigned long t;
3929
3930 t = *ipa;
3931 *ipa = *ipb;
3932 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003933}
3934
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003935static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003936 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003937 unsigned long *end)
3938{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003939 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003940 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003941 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003942 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003943 unsigned long *p;
3944 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003945 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003946 int ret = -ENOMEM;
3947
3948 count = end - start;
3949
3950 if (!count)
3951 return 0;
3952
Steven Rostedt9fd49322012-04-24 22:32:06 -04003953 sort(start, count, sizeof(*start),
3954 ftrace_cmp_ips, ftrace_swap_ips);
3955
Steven Rostedt706c81f2012-04-24 23:45:26 -04003956 start_pg = ftrace_allocate_pages(count);
3957 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05003958 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003959
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003960 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003961
Steven Rostedt32082302011-12-16 14:42:37 -05003962 /*
3963 * Core and each module needs their own pages, as
3964 * modules will free them when they are removed.
3965 * Force a new page to be allocated for modules.
3966 */
Steven Rostedta7900872011-12-16 16:23:44 -05003967 if (!mod) {
3968 WARN_ON(ftrace_pages || ftrace_pages_start);
3969 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003970 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003971 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003972 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003973 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003974
Steven Rostedta7900872011-12-16 16:23:44 -05003975 if (WARN_ON(ftrace_pages->next)) {
3976 /* Hmm, we have free pages? */
3977 while (ftrace_pages->next)
3978 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003979 }
Steven Rostedta7900872011-12-16 16:23:44 -05003980
Steven Rostedt706c81f2012-04-24 23:45:26 -04003981 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003982 }
3983
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003984 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003985 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003986 while (p < end) {
3987 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003988 /*
3989 * Some architecture linkers will pad between
3990 * the different mcount_loc sections of different
3991 * object files to satisfy alignments.
3992 * Skip any NULL pointers.
3993 */
3994 if (!addr)
3995 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003996
3997 if (pg->index == pg->size) {
3998 /* We should have allocated enough */
3999 if (WARN_ON(!pg->next))
4000 break;
4001 pg = pg->next;
4002 }
4003
4004 rec = &pg->records[pg->index++];
4005 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004006 }
4007
Steven Rostedt706c81f2012-04-24 23:45:26 -04004008 /* We should have used all pages */
4009 WARN_ON(pg->next);
4010
4011 /* Assign the last page to ftrace_pages */
4012 ftrace_pages = pg;
4013
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004014 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004015 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004016
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004017 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004018 * We only need to disable interrupts on start up
4019 * because we are modifying code that an interrupt
4020 * may execute, and the modification is not atomic.
4021 * But for modules, nothing runs the code we modify
4022 * until we are finished with it, and there's no
4023 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004024 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004025 if (!mod)
4026 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08004027 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004028 if (!mod)
4029 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004030 ret = 0;
4031 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004032 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004033
Steven Rostedta7900872011-12-16 16:23:44 -05004034 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004035}
4036
Steven Rostedt93eb6772009-04-15 13:24:06 -04004037#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004038
4039#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4040
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004041void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004042{
4043 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004044 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004045 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004046 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004047
Steven Rostedt93eb6772009-04-15 13:24:06 -04004048 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004049
4050 if (ftrace_disabled)
4051 goto out_unlock;
4052
Steven Rostedt32082302011-12-16 14:42:37 -05004053 /*
4054 * Each module has its own ftrace_pages, remove
4055 * them from the list.
4056 */
4057 last_pg = &ftrace_pages_start;
4058 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4059 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004060 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004061 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004062 * As core pages are first, the first
4063 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004064 */
Steven Rostedt32082302011-12-16 14:42:37 -05004065 if (WARN_ON(pg == ftrace_pages_start))
4066 goto out_unlock;
4067
4068 /* Check if we are deleting the last page */
4069 if (pg == ftrace_pages)
4070 ftrace_pages = next_to_ftrace_page(last_pg);
4071
4072 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004073 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4074 free_pages((unsigned long)pg->records, order);
4075 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004076 } else
4077 last_pg = &pg->next;
4078 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004079 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004080 mutex_unlock(&ftrace_lock);
4081}
4082
4083static void ftrace_init_module(struct module *mod,
4084 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004085{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004086 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004087 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004088 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004089}
4090
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004091static int ftrace_module_notify_enter(struct notifier_block *self,
4092 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004093{
4094 struct module *mod = data;
4095
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004096 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004097 ftrace_init_module(mod, mod->ftrace_callsites,
4098 mod->ftrace_callsites +
4099 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004100 return 0;
4101}
4102
4103static int ftrace_module_notify_exit(struct notifier_block *self,
4104 unsigned long val, void *data)
4105{
4106 struct module *mod = data;
4107
4108 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004109 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004110
4111 return 0;
4112}
4113#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004114static int ftrace_module_notify_enter(struct notifier_block *self,
4115 unsigned long val, void *data)
4116{
4117 return 0;
4118}
4119static int ftrace_module_notify_exit(struct notifier_block *self,
4120 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004121{
4122 return 0;
4123}
4124#endif /* CONFIG_MODULES */
4125
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004126struct notifier_block ftrace_module_enter_nb = {
4127 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004128 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004129};
4130
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004131struct notifier_block ftrace_module_exit_nb = {
4132 .notifier_call = ftrace_module_notify_exit,
4133 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4134};
4135
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004136extern unsigned long __start_mcount_loc[];
4137extern unsigned long __stop_mcount_loc[];
4138
4139void __init ftrace_init(void)
4140{
4141 unsigned long count, addr, flags;
4142 int ret;
4143
4144 /* Keep the ftrace pointer to the stub */
4145 addr = (unsigned long)ftrace_stub;
4146
4147 local_irq_save(flags);
4148 ftrace_dyn_arch_init(&addr);
4149 local_irq_restore(flags);
4150
4151 /* ftrace_dyn_arch_init places the return code in addr */
4152 if (addr)
4153 goto failed;
4154
4155 count = __stop_mcount_loc - __start_mcount_loc;
4156
4157 ret = ftrace_dyn_table_alloc(count);
4158 if (ret)
4159 goto failed;
4160
4161 last_ftrace_enabled = ftrace_enabled = 1;
4162
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004163 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004164 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004165 __stop_mcount_loc);
4166
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004167 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004168 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004169 pr_warning("Failed to register trace ftrace module enter notifier\n");
4170
4171 ret = register_module_notifier(&ftrace_module_exit_nb);
4172 if (ret)
4173 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004174
Steven Rostedt2af15d62009-05-28 13:37:24 -04004175 set_ftrace_early_filters();
4176
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004177 return;
4178 failed:
4179 ftrace_disabled = 1;
4180}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004181
Steven Rostedt3d083392008-05-12 21:20:42 +02004182#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004183
Steven Rostedt2b499382011-05-03 22:49:52 -04004184static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004185 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004186 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4187 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004188};
4189
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004190static int __init ftrace_nodyn_init(void)
4191{
4192 ftrace_enabled = 1;
4193 return 0;
4194}
Steven Rostedt6f415672012-10-05 12:13:07 -04004195core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004196
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004197static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4198static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004199/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004200# define ftrace_startup(ops, command) \
4201 ({ \
4202 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4203 0; \
4204 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04004205# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004206# define ftrace_startup_sysctl() do { } while (0)
4207# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004208
4209static inline int
4210ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4211{
4212 return 1;
4213}
4214
Steven Rostedt3d083392008-05-12 21:20:42 +02004215#endif /* CONFIG_DYNAMIC_FTRACE */
4216
Steven Rostedtb8489142011-05-04 09:27:52 -04004217static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004218ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004219 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004220{
Jiri Olsae2484912012-02-15 15:51:48 +01004221 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4222 return;
4223
4224 /*
4225 * Some of the ops may be dynamically allocated,
4226 * they must be freed after a synchronize_sched().
4227 */
4228 preempt_disable_notrace();
4229 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt0a016402012-11-02 17:03:03 -04004230 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004231 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4232 !ftrace_function_local_disabled(op) &&
Jiri Olsae2484912012-02-15 15:51:48 +01004233 ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004234 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004235 } while_for_each_ftrace_op(op);
Jiri Olsae2484912012-02-15 15:51:48 +01004236 trace_recursion_clear(TRACE_CONTROL_BIT);
4237 preempt_enable_notrace();
4238}
4239
4240static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004241 .func = ftrace_ops_control_func,
4242 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4243 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004244};
4245
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004246static inline void
4247__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004248 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004249{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004250 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004251 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004252
Steven Rostedtccf36722012-06-05 09:44:25 -04004253 if (function_trace_stop)
4254 return;
4255
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004256 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4257 if (bit < 0)
4258 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004259
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004260 /*
4261 * Some of the ops may be dynamically allocated,
4262 * they must be freed after a synchronize_sched().
4263 */
4264 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004265 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedtb8489142011-05-04 09:27:52 -04004266 if (ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004267 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004268 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004269 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004270 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004271}
4272
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004273/*
4274 * Some archs only support passing ip and parent_ip. Even though
4275 * the list function ignores the op parameter, we do not want any
4276 * C side effects, where a function is called without the caller
4277 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004278 * Archs are to support both the regs and ftrace_ops at the same time.
4279 * If they support ftrace_ops, it is assumed they support regs.
4280 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004281 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4282 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004283 * An architecture can pass partial regs with ftrace_ops and still
4284 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004285 */
4286#if ARCH_SUPPORTS_FTRACE_OPS
4287static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004288 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004289{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004290 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004291}
4292#else
4293static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4294{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004295 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004296}
4297#endif
4298
Steven Rostedte32d8952008-12-04 00:26:41 -05004299static void clear_ftrace_swapper(void)
4300{
4301 struct task_struct *p;
4302 int cpu;
4303
4304 get_online_cpus();
4305 for_each_online_cpu(cpu) {
4306 p = idle_task(cpu);
4307 clear_tsk_trace_trace(p);
4308 }
4309 put_online_cpus();
4310}
4311
4312static void set_ftrace_swapper(void)
4313{
4314 struct task_struct *p;
4315 int cpu;
4316
4317 get_online_cpus();
4318 for_each_online_cpu(cpu) {
4319 p = idle_task(cpu);
4320 set_tsk_trace_trace(p);
4321 }
4322 put_online_cpus();
4323}
4324
4325static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004326{
4327 struct task_struct *p;
4328
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004329 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004330 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004331 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004332 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004333 rcu_read_unlock();
4334
Steven Rostedte32d8952008-12-04 00:26:41 -05004335 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004336}
4337
Steven Rostedte32d8952008-12-04 00:26:41 -05004338static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004339{
4340 struct task_struct *p;
4341
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004342 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004343 do_each_pid_task(pid, PIDTYPE_PID, p) {
4344 set_tsk_trace_trace(p);
4345 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004346 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004347}
4348
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004349static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004350{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004351 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004352 clear_ftrace_swapper();
4353 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004354 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004355}
4356
4357static void set_ftrace_pid_task(struct pid *pid)
4358{
4359 if (pid == ftrace_swapper_pid)
4360 set_ftrace_swapper();
4361 else
4362 set_ftrace_pid(pid);
4363}
4364
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004365static int ftrace_pid_add(int p)
4366{
4367 struct pid *pid;
4368 struct ftrace_pid *fpid;
4369 int ret = -EINVAL;
4370
4371 mutex_lock(&ftrace_lock);
4372
4373 if (!p)
4374 pid = ftrace_swapper_pid;
4375 else
4376 pid = find_get_pid(p);
4377
4378 if (!pid)
4379 goto out;
4380
4381 ret = 0;
4382
4383 list_for_each_entry(fpid, &ftrace_pids, list)
4384 if (fpid->pid == pid)
4385 goto out_put;
4386
4387 ret = -ENOMEM;
4388
4389 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4390 if (!fpid)
4391 goto out_put;
4392
4393 list_add(&fpid->list, &ftrace_pids);
4394 fpid->pid = pid;
4395
4396 set_ftrace_pid_task(pid);
4397
4398 ftrace_update_pid_func();
4399 ftrace_startup_enable(0);
4400
4401 mutex_unlock(&ftrace_lock);
4402 return 0;
4403
4404out_put:
4405 if (pid != ftrace_swapper_pid)
4406 put_pid(pid);
4407
4408out:
4409 mutex_unlock(&ftrace_lock);
4410 return ret;
4411}
4412
4413static void ftrace_pid_reset(void)
4414{
4415 struct ftrace_pid *fpid, *safe;
4416
4417 mutex_lock(&ftrace_lock);
4418 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4419 struct pid *pid = fpid->pid;
4420
4421 clear_ftrace_pid_task(pid);
4422
4423 list_del(&fpid->list);
4424 kfree(fpid);
4425 }
4426
4427 ftrace_update_pid_func();
4428 ftrace_startup_enable(0);
4429
4430 mutex_unlock(&ftrace_lock);
4431}
4432
4433static void *fpid_start(struct seq_file *m, loff_t *pos)
4434{
4435 mutex_lock(&ftrace_lock);
4436
4437 if (list_empty(&ftrace_pids) && (!*pos))
4438 return (void *) 1;
4439
4440 return seq_list_start(&ftrace_pids, *pos);
4441}
4442
4443static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4444{
4445 if (v == (void *)1)
4446 return NULL;
4447
4448 return seq_list_next(v, &ftrace_pids, pos);
4449}
4450
4451static void fpid_stop(struct seq_file *m, void *p)
4452{
4453 mutex_unlock(&ftrace_lock);
4454}
4455
4456static int fpid_show(struct seq_file *m, void *v)
4457{
4458 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4459
4460 if (v == (void *)1) {
4461 seq_printf(m, "no pid\n");
4462 return 0;
4463 }
4464
4465 if (fpid->pid == ftrace_swapper_pid)
4466 seq_printf(m, "swapper tasks\n");
4467 else
4468 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4469
4470 return 0;
4471}
4472
4473static const struct seq_operations ftrace_pid_sops = {
4474 .start = fpid_start,
4475 .next = fpid_next,
4476 .stop = fpid_stop,
4477 .show = fpid_show,
4478};
4479
4480static int
4481ftrace_pid_open(struct inode *inode, struct file *file)
4482{
4483 int ret = 0;
4484
4485 if ((file->f_mode & FMODE_WRITE) &&
4486 (file->f_flags & O_TRUNC))
4487 ftrace_pid_reset();
4488
4489 if (file->f_mode & FMODE_READ)
4490 ret = seq_open(file, &ftrace_pid_sops);
4491
4492 return ret;
4493}
4494
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004495static ssize_t
4496ftrace_pid_write(struct file *filp, const char __user *ubuf,
4497 size_t cnt, loff_t *ppos)
4498{
Ingo Molnar457dc922009-11-23 11:03:28 +01004499 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004500 long val;
4501 int ret;
4502
4503 if (cnt >= sizeof(buf))
4504 return -EINVAL;
4505
4506 if (copy_from_user(&buf, ubuf, cnt))
4507 return -EFAULT;
4508
4509 buf[cnt] = 0;
4510
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004511 /*
4512 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4513 * to clean the filter quietly.
4514 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004515 tmp = strstrip(buf);
4516 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004517 return 1;
4518
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004519 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004520 if (ret < 0)
4521 return ret;
4522
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004523 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004524
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004525 return ret ? ret : cnt;
4526}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004527
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004528static int
4529ftrace_pid_release(struct inode *inode, struct file *file)
4530{
4531 if (file->f_mode & FMODE_READ)
4532 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004533
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004534 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004535}
4536
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004537static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004538 .open = ftrace_pid_open,
4539 .write = ftrace_pid_write,
4540 .read = seq_read,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09004541 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004542 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004543};
4544
4545static __init int ftrace_init_debugfs(void)
4546{
4547 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004548
4549 d_tracer = tracing_init_dentry();
4550 if (!d_tracer)
4551 return 0;
4552
4553 ftrace_init_dyn_debugfs(d_tracer);
4554
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004555 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4556 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004557
4558 ftrace_profile_debugfs(d_tracer);
4559
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004560 return 0;
4561}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004562fs_initcall(ftrace_init_debugfs);
4563
Steven Rostedt3d083392008-05-12 21:20:42 +02004564/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004565 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004566 *
4567 * This function should be used by panic code. It stops ftrace
4568 * but in a not so nice way. If you need to simply kill ftrace
4569 * from a non-atomic section, use ftrace_kill.
4570 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004571void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004572{
4573 ftrace_disabled = 1;
4574 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004575 clear_ftrace_function();
4576}
4577
4578/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004579 * Test if ftrace is dead or not.
4580 */
4581int ftrace_is_dead(void)
4582{
4583 return ftrace_disabled;
4584}
4585
4586/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004587 * register_ftrace_function - register a function for profiling
4588 * @ops - ops structure that holds the function for profiling.
4589 *
4590 * Register a function to be called by all functions in the
4591 * kernel.
4592 *
4593 * Note: @ops->func and all the functions it calls must be labeled
4594 * with "notrace", otherwise it will go into a
4595 * recursive loop.
4596 */
4597int register_ftrace_function(struct ftrace_ops *ops)
4598{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004599 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004600
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004601 ftrace_ops_init(ops);
4602
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004603 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004604
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004605 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004606 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004607 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004608
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004609 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004610
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004611 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004612}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004613EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004614
4615/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004616 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004617 * @ops - ops structure that holds the function to unregister
4618 *
4619 * Unregister a function that was added to be called by ftrace profiling.
4620 */
4621int unregister_ftrace_function(struct ftrace_ops *ops)
4622{
4623 int ret;
4624
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004625 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004626 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004627 if (!ret)
4628 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004629 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004630
4631 return ret;
4632}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004633EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004634
Ingo Molnare309b412008-05-12 21:20:51 +02004635int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004636ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004637 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004638 loff_t *ppos)
4639{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004640 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004641
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004642 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004643
Steven Rostedt45a4a232011-04-21 23:16:46 -04004644 if (unlikely(ftrace_disabled))
4645 goto out;
4646
4647 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004648
Li Zefana32c7762009-06-26 16:55:51 +08004649 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004650 goto out;
4651
Li Zefana32c7762009-06-26 16:55:51 +08004652 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004653
4654 if (ftrace_enabled) {
4655
4656 ftrace_startup_sysctl();
4657
4658 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004659 if (ftrace_ops_list != &ftrace_list_end)
4660 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004661
4662 } else {
4663 /* stopping ftrace calls (just send to ftrace_stub) */
4664 ftrace_trace_function = ftrace_stub;
4665
4666 ftrace_shutdown_sysctl();
4667 }
4668
4669 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004670 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004671 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004672}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004673
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004674#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004675
Steven Rostedt597af812009-04-03 15:24:12 -04004676static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004677static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004678
Steven Rostedte49dc192008-12-02 23:50:05 -05004679int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4680{
4681 return 0;
4682}
4683
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004684/* The callbacks that hook a function */
4685trace_func_graph_ret_t ftrace_graph_return =
4686 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004687trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004688
4689/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4690static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4691{
4692 int i;
4693 int ret = 0;
4694 unsigned long flags;
4695 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4696 struct task_struct *g, *t;
4697
4698 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4699 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4700 * sizeof(struct ftrace_ret_stack),
4701 GFP_KERNEL);
4702 if (!ret_stack_list[i]) {
4703 start = 0;
4704 end = i;
4705 ret = -ENOMEM;
4706 goto free;
4707 }
4708 }
4709
4710 read_lock_irqsave(&tasklist_lock, flags);
4711 do_each_thread(g, t) {
4712 if (start == end) {
4713 ret = -EAGAIN;
4714 goto unlock;
4715 }
4716
4717 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004718 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004719 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004720 t->curr_ret_stack = -1;
4721 /* Make sure the tasks see the -1 first: */
4722 smp_wmb();
4723 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004724 }
4725 } while_each_thread(g, t);
4726
4727unlock:
4728 read_unlock_irqrestore(&tasklist_lock, flags);
4729free:
4730 for (i = start; i < end; i++)
4731 kfree(ret_stack_list[i]);
4732 return ret;
4733}
4734
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004735static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004736ftrace_graph_probe_sched_switch(void *ignore,
4737 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004738{
4739 unsigned long long timestamp;
4740 int index;
4741
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004742 /*
4743 * Does the user want to count the time a function was asleep.
4744 * If so, do not update the time stamps.
4745 */
4746 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4747 return;
4748
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004749 timestamp = trace_clock_local();
4750
4751 prev->ftrace_timestamp = timestamp;
4752
4753 /* only process tasks that we timestamped */
4754 if (!next->ftrace_timestamp)
4755 return;
4756
4757 /*
4758 * Update all the counters in next to make up for the
4759 * time next was sleeping.
4760 */
4761 timestamp -= next->ftrace_timestamp;
4762
4763 for (index = next->curr_ret_stack; index >= 0; index--)
4764 next->ret_stack[index].calltime += timestamp;
4765}
4766
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004767/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004768static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004769{
4770 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004771 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004772
4773 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4774 sizeof(struct ftrace_ret_stack *),
4775 GFP_KERNEL);
4776
4777 if (!ret_stack_list)
4778 return -ENOMEM;
4779
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004780 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004781 for_each_online_cpu(cpu) {
4782 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004783 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004784 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004785
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004786 do {
4787 ret = alloc_retstack_tasklist(ret_stack_list);
4788 } while (ret == -EAGAIN);
4789
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004790 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004791 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004792 if (ret)
4793 pr_info("ftrace_graph: Couldn't activate tracepoint"
4794 " probe to kernel_sched_switch\n");
4795 }
4796
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004797 kfree(ret_stack_list);
4798 return ret;
4799}
4800
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004801/*
4802 * Hibernation protection.
4803 * The state of the current task is too much unstable during
4804 * suspend/restore to disk. We want to protect against that.
4805 */
4806static int
4807ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4808 void *unused)
4809{
4810 switch (state) {
4811 case PM_HIBERNATION_PREPARE:
4812 pause_graph_tracing();
4813 break;
4814
4815 case PM_POST_HIBERNATION:
4816 unpause_graph_tracing();
4817 break;
4818 }
4819 return NOTIFY_DONE;
4820}
4821
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004822int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4823 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004824{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004825 int ret = 0;
4826
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004827 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004828
Steven Rostedt05ce5812009-03-24 00:18:31 -04004829 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004830 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004831 ret = -EBUSY;
4832 goto out;
4833 }
4834
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004835 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4836 register_pm_notifier(&ftrace_suspend_notifier);
4837
Steven Rostedt597af812009-04-03 15:24:12 -04004838 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004839 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004840 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004841 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004842 goto out;
4843 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004844
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004845 ftrace_graph_return = retfunc;
4846 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004847
Steven Rostedta1cd6172011-05-23 15:24:25 -04004848 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004849
4850out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004851 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004852 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004853}
4854
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004855void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004856{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004857 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004858
Steven Rostedt597af812009-04-03 15:24:12 -04004859 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004860 goto out;
4861
Steven Rostedt597af812009-04-03 15:24:12 -04004862 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004863 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004864 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004865 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004866 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004867 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004868
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004869 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004870 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004871}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004872
Steven Rostedt868baf02011-02-10 21:26:13 -05004873static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4874
4875static void
4876graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4877{
4878 atomic_set(&t->tracing_graph_pause, 0);
4879 atomic_set(&t->trace_overrun, 0);
4880 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004881 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004882 smp_wmb();
4883 t->ret_stack = ret_stack;
4884}
4885
4886/*
4887 * Allocate a return stack for the idle task. May be the first
4888 * time through, or it may be done by CPU hotplug online.
4889 */
4890void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4891{
4892 t->curr_ret_stack = -1;
4893 /*
4894 * The idle task has no parent, it either has its own
4895 * stack or no stack at all.
4896 */
4897 if (t->ret_stack)
4898 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4899
4900 if (ftrace_graph_active) {
4901 struct ftrace_ret_stack *ret_stack;
4902
4903 ret_stack = per_cpu(idle_ret_stack, cpu);
4904 if (!ret_stack) {
4905 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4906 * sizeof(struct ftrace_ret_stack),
4907 GFP_KERNEL);
4908 if (!ret_stack)
4909 return;
4910 per_cpu(idle_ret_stack, cpu) = ret_stack;
4911 }
4912 graph_init_task(t, ret_stack);
4913 }
4914}
4915
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004916/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004917void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004918{
Steven Rostedt84047e32009-06-02 16:51:55 -04004919 /* Make sure we do not use the parent ret_stack */
4920 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004921 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004922
Steven Rostedt597af812009-04-03 15:24:12 -04004923 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004924 struct ftrace_ret_stack *ret_stack;
4925
4926 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004927 * sizeof(struct ftrace_ret_stack),
4928 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004929 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004930 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004931 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004932 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004933}
4934
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004935void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004936{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004937 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4938
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004939 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004940 /* NULL must become visible to IRQs before we free it: */
4941 barrier();
4942
4943 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004944}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004945
4946void ftrace_graph_stop(void)
4947{
4948 ftrace_stop();
4949}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004950#endif