blob: d0c5c3f0d9397997919bd328fea452674d1d6286 [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{
Steven Rostedt2b499382011-05-03 22:49:52 -0400370 if (FTRACE_WARN_ON(ops == &global_ops))
371 return -EINVAL;
372
Steven Rostedtb8489142011-05-04 09:27:52 -0400373 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
374 return -EBUSY;
375
Jiri Olsae2484912012-02-15 15:51:48 +0100376 /* We don't support both control and global flags set. */
377 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
378 return -EINVAL;
379
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900380#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400381 /*
382 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
383 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
384 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
385 */
386 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
387 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
388 return -EINVAL;
389
390 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
391 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
392#endif
393
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400394 if (!core_kernel_data((unsigned long)ops))
395 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
396
Steven Rostedtb8489142011-05-04 09:27:52 -0400397 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100398 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400399 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100400 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
401 if (control_ops_alloc(ops))
402 return -ENOMEM;
403 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400404 } else
405 add_ftrace_ops(&ftrace_ops_list, ops);
406
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400407 if (ftrace_enabled)
408 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200409
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200410 return 0;
411}
412
Ingo Molnare309b412008-05-12 21:20:51 +0200413static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200414{
Steven Rostedt2b499382011-05-03 22:49:52 -0400415 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200416
Steven Rostedtb8489142011-05-04 09:27:52 -0400417 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
418 return -EBUSY;
419
Steven Rostedt2b499382011-05-03 22:49:52 -0400420 if (FTRACE_WARN_ON(ops == &global_ops))
421 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200422
Steven Rostedtb8489142011-05-04 09:27:52 -0400423 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100424 ret = remove_ftrace_list_ops(&ftrace_global_list,
425 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400426 if (!ret)
427 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100428 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
429 ret = remove_ftrace_list_ops(&ftrace_control_list,
430 &control_ops, ops);
431 if (!ret) {
432 /*
433 * The ftrace_ops is now removed from the list,
434 * so there'll be no new users. We must ensure
435 * all current users are done before we free
436 * the control data.
437 */
438 synchronize_sched();
439 control_ops_free(ops);
440 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400441 } else
442 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
443
Steven Rostedt2b499382011-05-03 22:49:52 -0400444 if (ret < 0)
445 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400446
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400447 if (ftrace_enabled)
448 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200449
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400450 /*
451 * Dynamic ops may be freed, we must make sure that all
452 * callers are done before leaving this function.
453 */
454 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
455 synchronize_sched();
456
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500457 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200458}
459
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500460static void ftrace_update_pid_func(void)
461{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400462 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500463 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900464 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500465
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400466 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500467}
468
Steven Rostedt493762f2009-03-23 17:12:36 -0400469#ifdef CONFIG_FUNCTION_PROFILER
470struct ftrace_profile {
471 struct hlist_node node;
472 unsigned long ip;
473 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400474#ifdef CONFIG_FUNCTION_GRAPH_TRACER
475 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400476 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400477#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400478};
479
480struct ftrace_profile_page {
481 struct ftrace_profile_page *next;
482 unsigned long index;
483 struct ftrace_profile records[];
484};
485
Steven Rostedtcafb1682009-03-24 20:50:39 -0400486struct ftrace_profile_stat {
487 atomic_t disabled;
488 struct hlist_head *hash;
489 struct ftrace_profile_page *pages;
490 struct ftrace_profile_page *start;
491 struct tracer_stat stat;
492};
493
Steven Rostedt493762f2009-03-23 17:12:36 -0400494#define PROFILE_RECORDS_SIZE \
495 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
496
497#define PROFILES_PER_PAGE \
498 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
499
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400500static int ftrace_profile_enabled __read_mostly;
501
502/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400503static DEFINE_MUTEX(ftrace_profile_lock);
504
Steven Rostedtcafb1682009-03-24 20:50:39 -0400505static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400506
Namhyung Kim20079eb2013-04-10 08:55:50 +0900507#define FTRACE_PROFILE_HASH_BITS 10
508#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400509
Steven Rostedt493762f2009-03-23 17:12:36 -0400510static void *
511function_stat_next(void *v, int idx)
512{
513 struct ftrace_profile *rec = v;
514 struct ftrace_profile_page *pg;
515
516 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
517
518 again:
Li Zefan0296e422009-06-26 11:15:37 +0800519 if (idx != 0)
520 rec++;
521
Steven Rostedt493762f2009-03-23 17:12:36 -0400522 if ((void *)rec >= (void *)&pg->records[pg->index]) {
523 pg = pg->next;
524 if (!pg)
525 return NULL;
526 rec = &pg->records[0];
527 if (!rec->counter)
528 goto again;
529 }
530
531 return rec;
532}
533
534static void *function_stat_start(struct tracer_stat *trace)
535{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400536 struct ftrace_profile_stat *stat =
537 container_of(trace, struct ftrace_profile_stat, stat);
538
539 if (!stat || !stat->start)
540 return NULL;
541
542 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400543}
544
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400545#ifdef CONFIG_FUNCTION_GRAPH_TRACER
546/* function graph compares on total time */
547static int function_stat_cmp(void *p1, void *p2)
548{
549 struct ftrace_profile *a = p1;
550 struct ftrace_profile *b = p2;
551
552 if (a->time < b->time)
553 return -1;
554 if (a->time > b->time)
555 return 1;
556 else
557 return 0;
558}
559#else
560/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400561static int function_stat_cmp(void *p1, void *p2)
562{
563 struct ftrace_profile *a = p1;
564 struct ftrace_profile *b = p2;
565
566 if (a->counter < b->counter)
567 return -1;
568 if (a->counter > b->counter)
569 return 1;
570 else
571 return 0;
572}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400573#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400574
575static int function_stat_headers(struct seq_file *m)
576{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400577#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400578 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400579 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400580 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400581 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400582#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400583 seq_printf(m, " Function Hit\n"
584 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400585#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400586 return 0;
587}
588
589static int function_stat_show(struct seq_file *m, void *v)
590{
591 struct ftrace_profile *rec = v;
592 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800593 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400594#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400595 static struct trace_seq s;
596 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400597 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400598#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800599 mutex_lock(&ftrace_profile_lock);
600
601 /* we raced with function_profile_reset() */
602 if (unlikely(rec->counter == 0)) {
603 ret = -EBUSY;
604 goto out;
605 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400606
607 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400608 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400609
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400610#ifdef CONFIG_FUNCTION_GRAPH_TRACER
611 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400612 avg = rec->time;
613 do_div(avg, rec->counter);
614
Chase Douglase330b3b2010-04-26 14:02:05 -0400615 /* Sample standard deviation (s^2) */
616 if (rec->counter <= 1)
617 stddev = 0;
618 else {
619 stddev = rec->time_squared - rec->counter * avg * avg;
620 /*
621 * Divide only 1000 for ns^2 -> us^2 conversion.
622 * trace_print_graph_duration will divide 1000 again.
623 */
624 do_div(stddev, (rec->counter - 1) * 1000);
625 }
626
Steven Rostedt34886c82009-03-25 21:00:47 -0400627 trace_seq_init(&s);
628 trace_print_graph_duration(rec->time, &s);
629 trace_seq_puts(&s, " ");
630 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400631 trace_seq_puts(&s, " ");
632 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400633 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400634#endif
635 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800636out:
637 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400638
Li Zefan3aaba202010-08-23 16:50:12 +0800639 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400640}
641
Steven Rostedtcafb1682009-03-24 20:50:39 -0400642static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400643{
644 struct ftrace_profile_page *pg;
645
Steven Rostedtcafb1682009-03-24 20:50:39 -0400646 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400647
648 while (pg) {
649 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
650 pg->index = 0;
651 pg = pg->next;
652 }
653
Steven Rostedtcafb1682009-03-24 20:50:39 -0400654 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400655 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
656}
657
Steven Rostedtcafb1682009-03-24 20:50:39 -0400658int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400659{
660 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400661 int functions;
662 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400663 int i;
664
665 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400666 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400667 return 0;
668
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
670 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400671 return -ENOMEM;
672
Steven Rostedt318e0a72009-03-25 20:06:34 -0400673#ifdef CONFIG_DYNAMIC_FTRACE
674 functions = ftrace_update_tot_cnt;
675#else
676 /*
677 * We do not know the number of functions that exist because
678 * dynamic tracing is what counts them. With past experience
679 * we have around 20K functions. That should be more than enough.
680 * It is highly unlikely we will execute every function in
681 * the kernel.
682 */
683 functions = 20000;
684#endif
685
Steven Rostedtcafb1682009-03-24 20:50:39 -0400686 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400687
Steven Rostedt318e0a72009-03-25 20:06:34 -0400688 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
689
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900690 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400691 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400692 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400693 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400694 pg = pg->next;
695 }
696
697 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400698
699 out_free:
700 pg = stat->start;
701 while (pg) {
702 unsigned long tmp = (unsigned long)pg;
703
704 pg = pg->next;
705 free_page(tmp);
706 }
707
Steven Rostedt318e0a72009-03-25 20:06:34 -0400708 stat->pages = NULL;
709 stat->start = NULL;
710
711 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400712}
713
Steven Rostedtcafb1682009-03-24 20:50:39 -0400714static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400715{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400717 int size;
718
Steven Rostedtcafb1682009-03-24 20:50:39 -0400719 stat = &per_cpu(ftrace_profile_stats, cpu);
720
721 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400722 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400723 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400724 return 0;
725 }
726
727 /*
728 * We are profiling all functions, but usually only a few thousand
729 * functions are hit. We'll make a hash of 1024 items.
730 */
731 size = FTRACE_PROFILE_HASH_SIZE;
732
Steven Rostedtcafb1682009-03-24 20:50:39 -0400733 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400734
Steven Rostedtcafb1682009-03-24 20:50:39 -0400735 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400736 return -ENOMEM;
737
Steven Rostedt318e0a72009-03-25 20:06:34 -0400738 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400739 if (ftrace_profile_pages_init(stat) < 0) {
740 kfree(stat->hash);
741 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400742 return -ENOMEM;
743 }
744
745 return 0;
746}
747
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748static int ftrace_profile_init(void)
749{
750 int cpu;
751 int ret = 0;
752
Miao Xie885154c2013-12-16 15:20:01 +0800753 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400754 ret = ftrace_profile_init_cpu(cpu);
755 if (ret)
756 break;
757 }
758
759 return ret;
760}
761
Steven Rostedt493762f2009-03-23 17:12:36 -0400762/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400763static struct ftrace_profile *
764ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400765{
766 struct ftrace_profile *rec;
767 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400768 unsigned long key;
769
Namhyung Kim20079eb2013-04-10 08:55:50 +0900770 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400771 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400772
773 if (hlist_empty(hhd))
774 return NULL;
775
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400776 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400777 if (rec->ip == ip)
778 return rec;
779 }
780
781 return NULL;
782}
783
Steven Rostedtcafb1682009-03-24 20:50:39 -0400784static void ftrace_add_profile(struct ftrace_profile_stat *stat,
785 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400786{
787 unsigned long key;
788
Namhyung Kim20079eb2013-04-10 08:55:50 +0900789 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400790 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400791}
792
Steven Rostedt318e0a72009-03-25 20:06:34 -0400793/*
794 * The memory is already allocated, this simply finds a new record to use.
795 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400796static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400797ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400798{
799 struct ftrace_profile *rec = NULL;
800
Steven Rostedt318e0a72009-03-25 20:06:34 -0400801 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400802 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400803 goto out;
804
Steven Rostedt493762f2009-03-23 17:12:36 -0400805 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400806 * Try to find the function again since an NMI
807 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400808 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400809 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400810 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400811 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400812
Steven Rostedtcafb1682009-03-24 20:50:39 -0400813 if (stat->pages->index == PROFILES_PER_PAGE) {
814 if (!stat->pages->next)
815 goto out;
816 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400817 }
818
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400820 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400821 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400822
Steven Rostedt493762f2009-03-23 17:12:36 -0400823 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400825
826 return rec;
827}
828
Steven Rostedt493762f2009-03-23 17:12:36 -0400829static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400830function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400831 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400832{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400833 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400834 struct ftrace_profile *rec;
835 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400836
837 if (!ftrace_profile_enabled)
838 return;
839
Steven Rostedt493762f2009-03-23 17:12:36 -0400840 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400841
842 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400843 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400844 goto out;
845
846 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400847 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400848 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400849 if (!rec)
850 goto out;
851 }
852
853 rec->counter++;
854 out:
855 local_irq_restore(flags);
856}
857
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400858#ifdef CONFIG_FUNCTION_GRAPH_TRACER
859static int profile_graph_entry(struct ftrace_graph_ent *trace)
860{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400861 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400862 return 1;
863}
864
865static void profile_graph_return(struct ftrace_graph_ret *trace)
866{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400867 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400868 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400869 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400870 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400871
872 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400873 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400874 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400875 goto out;
876
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400877 /* If the calltime was zero'd ignore it */
878 if (!trace->calltime)
879 goto out;
880
Steven Rostedta2a16d62009-03-24 23:17:58 -0400881 calltime = trace->rettime - trace->calltime;
882
883 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
884 int index;
885
886 index = trace->depth;
887
888 /* Append this call time to the parent time to subtract */
889 if (index)
890 current->ret_stack[index - 1].subtime += calltime;
891
892 if (current->ret_stack[index].subtime < calltime)
893 calltime -= current->ret_stack[index].subtime;
894 else
895 calltime = 0;
896 }
897
Steven Rostedtcafb1682009-03-24 20:50:39 -0400898 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400899 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400900 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400901 rec->time_squared += calltime * calltime;
902 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400903
Steven Rostedtcafb1682009-03-24 20:50:39 -0400904 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400905 local_irq_restore(flags);
906}
907
908static int register_ftrace_profiler(void)
909{
910 return register_ftrace_graph(&profile_graph_return,
911 &profile_graph_entry);
912}
913
914static void unregister_ftrace_profiler(void)
915{
916 unregister_ftrace_graph();
917}
918#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100919static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400920 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900921 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
922 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400923};
924
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400925static int register_ftrace_profiler(void)
926{
927 return register_ftrace_function(&ftrace_profile_ops);
928}
929
930static void unregister_ftrace_profiler(void)
931{
932 unregister_ftrace_function(&ftrace_profile_ops);
933}
934#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
935
Steven Rostedt493762f2009-03-23 17:12:36 -0400936static ssize_t
937ftrace_profile_write(struct file *filp, const char __user *ubuf,
938 size_t cnt, loff_t *ppos)
939{
940 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400941 int ret;
942
Peter Huewe22fe9b52011-06-07 21:58:27 +0200943 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
944 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400945 return ret;
946
947 val = !!val;
948
949 mutex_lock(&ftrace_profile_lock);
950 if (ftrace_profile_enabled ^ val) {
951 if (val) {
952 ret = ftrace_profile_init();
953 if (ret < 0) {
954 cnt = ret;
955 goto out;
956 }
957
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400958 ret = register_ftrace_profiler();
959 if (ret < 0) {
960 cnt = ret;
961 goto out;
962 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400963 ftrace_profile_enabled = 1;
964 } else {
965 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400966 /*
967 * unregister_ftrace_profiler calls stop_machine
968 * so this acts like an synchronize_sched.
969 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400970 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400971 }
972 }
973 out:
974 mutex_unlock(&ftrace_profile_lock);
975
Jiri Olsacf8517c2009-10-23 19:36:16 -0400976 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400977
978 return cnt;
979}
980
981static ssize_t
982ftrace_profile_read(struct file *filp, char __user *ubuf,
983 size_t cnt, loff_t *ppos)
984{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400985 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400986 int r;
987
988 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
989 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
990}
991
992static const struct file_operations ftrace_profile_fops = {
993 .open = tracing_open_generic,
994 .read = ftrace_profile_read,
995 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200996 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400997};
998
Steven Rostedtcafb1682009-03-24 20:50:39 -0400999/* used to initialize the real stat files */
1000static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001001 .name = "functions",
1002 .stat_start = function_stat_start,
1003 .stat_next = function_stat_next,
1004 .stat_cmp = function_stat_cmp,
1005 .stat_headers = function_stat_headers,
1006 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001007};
1008
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001009static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001010{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001011 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001012 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001013 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001014 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001015 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001016
Steven Rostedtcafb1682009-03-24 20:50:39 -04001017 for_each_possible_cpu(cpu) {
1018 stat = &per_cpu(ftrace_profile_stats, cpu);
1019
1020 /* allocate enough for function name + cpu number */
1021 name = kmalloc(32, GFP_KERNEL);
1022 if (!name) {
1023 /*
1024 * The files created are permanent, if something happens
1025 * we still do not free memory.
1026 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001027 WARN(1,
1028 "Could not allocate stat file for cpu %d\n",
1029 cpu);
1030 return;
1031 }
1032 stat->stat = function_stats;
1033 snprintf(name, 32, "function%d", cpu);
1034 stat->stat.name = name;
1035 ret = register_stat_tracer(&stat->stat);
1036 if (ret) {
1037 WARN(1,
1038 "Could not register function stat for cpu %d\n",
1039 cpu);
1040 kfree(name);
1041 return;
1042 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001043 }
1044
1045 entry = debugfs_create_file("function_profile_enabled", 0644,
1046 d_tracer, NULL, &ftrace_profile_fops);
1047 if (!entry)
1048 pr_warning("Could not create debugfs "
1049 "'function_profile_enabled' entry\n");
1050}
1051
1052#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001053static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001054{
1055}
1056#endif /* CONFIG_FUNCTION_PROFILER */
1057
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001058static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1059
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04001060loff_t
1061ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1062{
1063 loff_t ret;
1064
1065 if (file->f_mode & FMODE_READ)
1066 ret = seq_lseek(file, offset, whence);
1067 else
1068 file->f_pos = ret = 1;
1069
1070 return ret;
1071}
1072
Steven Rostedt3d083392008-05-12 21:20:42 +02001073#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001074
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001075#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001076# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001077#endif
1078
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001079static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1080
Steven Rostedtb6887d72009-02-17 12:32:04 -05001081struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001082 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001083 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001084 unsigned long flags;
1085 unsigned long ip;
1086 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001087 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001088};
1089
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001090struct ftrace_func_entry {
1091 struct hlist_node hlist;
1092 unsigned long ip;
1093};
1094
1095struct ftrace_hash {
1096 unsigned long size_bits;
1097 struct hlist_head *buckets;
1098 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001099 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001100};
1101
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001102/*
1103 * We make these constant because no one should touch them,
1104 * but they are used as the default "empty hash", to avoid allocating
1105 * it all the time. These are in a read only section such that if
1106 * anyone does try to modify it, it will cause an exception.
1107 */
1108static const struct hlist_head empty_buckets[1];
1109static const struct ftrace_hash empty_hash = {
1110 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001111};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001112#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001113
Steven Rostedt2b499382011-05-03 22:49:52 -04001114static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001115 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001116 .notrace_hash = EMPTY_HASH,
1117 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001118 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1119 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001120};
1121
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001122struct ftrace_page {
1123 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001124 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001125 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001126 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001127};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001128
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001129static struct ftrace_page *ftrace_new_pgs;
1130
Steven Rostedta7900872011-12-16 16:23:44 -05001131#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1132#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001133
1134/* estimate from running different kernels */
1135#define NR_TO_INIT 10000
1136
1137static struct ftrace_page *ftrace_pages_start;
1138static struct ftrace_page *ftrace_pages;
1139
Steven Rostedt06a51d92011-12-19 19:07:36 -05001140static bool ftrace_hash_empty(struct ftrace_hash *hash)
1141{
1142 return !hash || !hash->count;
1143}
1144
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001145static struct ftrace_func_entry *
1146ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1147{
1148 unsigned long key;
1149 struct ftrace_func_entry *entry;
1150 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001151
Steven Rostedt06a51d92011-12-19 19:07:36 -05001152 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001153 return NULL;
1154
1155 if (hash->size_bits > 0)
1156 key = hash_long(ip, hash->size_bits);
1157 else
1158 key = 0;
1159
1160 hhd = &hash->buckets[key];
1161
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001162 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001163 if (entry->ip == ip)
1164 return entry;
1165 }
1166 return NULL;
1167}
1168
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001169static void __add_hash_entry(struct ftrace_hash *hash,
1170 struct ftrace_func_entry *entry)
1171{
1172 struct hlist_head *hhd;
1173 unsigned long key;
1174
1175 if (hash->size_bits)
1176 key = hash_long(entry->ip, hash->size_bits);
1177 else
1178 key = 0;
1179
1180 hhd = &hash->buckets[key];
1181 hlist_add_head(&entry->hlist, hhd);
1182 hash->count++;
1183}
1184
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001185static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1186{
1187 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001188
1189 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1190 if (!entry)
1191 return -ENOMEM;
1192
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001193 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001194 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001195
1196 return 0;
1197}
1198
1199static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001200free_hash_entry(struct ftrace_hash *hash,
1201 struct ftrace_func_entry *entry)
1202{
1203 hlist_del(&entry->hlist);
1204 kfree(entry);
1205 hash->count--;
1206}
1207
1208static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001209remove_hash_entry(struct ftrace_hash *hash,
1210 struct ftrace_func_entry *entry)
1211{
1212 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001213 hash->count--;
1214}
1215
1216static void ftrace_hash_clear(struct ftrace_hash *hash)
1217{
1218 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001219 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001220 struct ftrace_func_entry *entry;
1221 int size = 1 << hash->size_bits;
1222 int i;
1223
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001224 if (!hash->count)
1225 return;
1226
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001227 for (i = 0; i < size; i++) {
1228 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001229 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001230 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001231 }
1232 FTRACE_WARN_ON(hash->count);
1233}
1234
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001235static void free_ftrace_hash(struct ftrace_hash *hash)
1236{
1237 if (!hash || hash == EMPTY_HASH)
1238 return;
1239 ftrace_hash_clear(hash);
1240 kfree(hash->buckets);
1241 kfree(hash);
1242}
1243
Steven Rostedt07fd5512011-05-05 18:03:47 -04001244static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1245{
1246 struct ftrace_hash *hash;
1247
1248 hash = container_of(rcu, struct ftrace_hash, rcu);
1249 free_ftrace_hash(hash);
1250}
1251
1252static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1253{
1254 if (!hash || hash == EMPTY_HASH)
1255 return;
1256 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1257}
1258
Jiri Olsa5500fa52012-02-15 15:51:54 +01001259void ftrace_free_filter(struct ftrace_ops *ops)
1260{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001261 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001262 free_ftrace_hash(ops->filter_hash);
1263 free_ftrace_hash(ops->notrace_hash);
1264}
1265
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001266static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1267{
1268 struct ftrace_hash *hash;
1269 int size;
1270
1271 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1272 if (!hash)
1273 return NULL;
1274
1275 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001276 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001277
1278 if (!hash->buckets) {
1279 kfree(hash);
1280 return NULL;
1281 }
1282
1283 hash->size_bits = size_bits;
1284
1285 return hash;
1286}
1287
1288static struct ftrace_hash *
1289alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1290{
1291 struct ftrace_func_entry *entry;
1292 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001293 int size;
1294 int ret;
1295 int i;
1296
1297 new_hash = alloc_ftrace_hash(size_bits);
1298 if (!new_hash)
1299 return NULL;
1300
1301 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001302 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001303 return new_hash;
1304
1305 size = 1 << hash->size_bits;
1306 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001307 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001308 ret = add_hash_entry(new_hash, entry->ip);
1309 if (ret < 0)
1310 goto free_hash;
1311 }
1312 }
1313
1314 FTRACE_WARN_ON(new_hash->count != hash->count);
1315
1316 return new_hash;
1317
1318 free_hash:
1319 free_ftrace_hash(new_hash);
1320 return NULL;
1321}
1322
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001323static void
1324ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1325static void
1326ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1327
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001329ftrace_hash_move(struct ftrace_ops *ops, int enable,
1330 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001331{
1332 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001333 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001334 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001335 struct ftrace_hash *old_hash;
1336 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001337 int size = src->count;
1338 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001339 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001340 int i;
1341
1342 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001343 * Remove the current set, update the hash and add
1344 * them back.
1345 */
1346 ftrace_hash_rec_disable(ops, enable);
1347
1348 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001349 * If the new source is empty, just free dst and assign it
1350 * the empty_hash.
1351 */
1352 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001353 free_ftrace_hash_rcu(*dst);
1354 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001355 /* still need to update the function records */
1356 ret = 0;
1357 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001358 }
1359
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001360 /*
1361 * Make the hash size about 1/2 the # found
1362 */
1363 for (size /= 2; size; size >>= 1)
1364 bits++;
1365
1366 /* Don't allocate too much */
1367 if (bits > FTRACE_HASH_MAX_BITS)
1368 bits = FTRACE_HASH_MAX_BITS;
1369
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001370 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001371 new_hash = alloc_ftrace_hash(bits);
1372 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001373 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001374
1375 size = 1 << src->size_bits;
1376 for (i = 0; i < size; i++) {
1377 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001378 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001379 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001380 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001381 }
1382 }
1383
Steven Rostedt07fd5512011-05-05 18:03:47 -04001384 old_hash = *dst;
1385 rcu_assign_pointer(*dst, new_hash);
1386 free_ftrace_hash_rcu(old_hash);
1387
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001388 ret = 0;
1389 out:
1390 /*
1391 * Enable regardless of ret:
1392 * On success, we enable the new hash.
1393 * On failure, we re-enable the original hash.
1394 */
1395 ftrace_hash_rec_enable(ops, enable);
1396
1397 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001398}
1399
Steven Rostedt265c8312009-02-13 12:43:56 -05001400/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001401 * Test the hashes for this ops to see if we want to call
1402 * the ops->func or not.
1403 *
1404 * It's a match if the ip is in the ops->filter_hash or
1405 * the filter_hash does not exist or is empty,
1406 * AND
1407 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001408 *
1409 * This needs to be called with preemption disabled as
1410 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001411 */
1412static int
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04001413ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001414{
1415 struct ftrace_hash *filter_hash;
1416 struct ftrace_hash *notrace_hash;
1417 int ret;
1418
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04001419#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1420 /*
1421 * There's a small race when adding ops that the ftrace handler
1422 * that wants regs, may be called without them. We can not
1423 * allow that handler to be called if regs is NULL.
1424 */
1425 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1426 return 0;
1427#endif
1428
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001429 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1430 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001431
Steven Rostedt06a51d92011-12-19 19:07:36 -05001432 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001433 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001434 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001435 !ftrace_lookup_ip(notrace_hash, ip)))
1436 ret = 1;
1437 else
1438 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001439
1440 return ret;
1441}
1442
1443/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001444 * This is a double for. Do not use 'break' to break out of the loop,
1445 * you must use a goto.
1446 */
1447#define do_for_each_ftrace_rec(pg, rec) \
1448 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1449 int _____i; \
1450 for (_____i = 0; _____i < pg->index; _____i++) { \
1451 rec = &pg->records[_____i];
1452
1453#define while_for_each_ftrace_rec() \
1454 } \
1455 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301456
Steven Rostedt5855fea2011-12-16 19:27:42 -05001457
1458static int ftrace_cmp_recs(const void *a, const void *b)
1459{
Steven Rostedta650e022012-04-25 13:48:13 -04001460 const struct dyn_ftrace *key = a;
1461 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001462
Steven Rostedta650e022012-04-25 13:48:13 -04001463 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001464 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001465 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1466 return 1;
1467 return 0;
1468}
1469
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001470static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001471{
1472 struct ftrace_page *pg;
1473 struct dyn_ftrace *rec;
1474 struct dyn_ftrace key;
1475
1476 key.ip = start;
1477 key.flags = end; /* overload flags, as it is unsigned long */
1478
1479 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1480 if (end < pg->records[0].ip ||
1481 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1482 continue;
1483 rec = bsearch(&key, pg->records, pg->index,
1484 sizeof(struct dyn_ftrace),
1485 ftrace_cmp_recs);
1486 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001487 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001488 }
1489
Steven Rostedt5855fea2011-12-16 19:27:42 -05001490 return 0;
1491}
1492
Steven Rostedtc88fd862011-08-16 09:53:39 -04001493/**
1494 * ftrace_location - return true if the ip giving is a traced location
1495 * @ip: the instruction pointer to check
1496 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001497 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001498 * That is, the instruction that is either a NOP or call to
1499 * the function tracer. It checks the ftrace internal tables to
1500 * determine if the address belongs or not.
1501 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001502unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001503{
Steven Rostedta650e022012-04-25 13:48:13 -04001504 return ftrace_location_range(ip, ip);
1505}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001506
Steven Rostedta650e022012-04-25 13:48:13 -04001507/**
1508 * ftrace_text_reserved - return true if range contains an ftrace location
1509 * @start: start of range to search
1510 * @end: end of range to search (inclusive). @end points to the last byte to check.
1511 *
1512 * Returns 1 if @start and @end contains a ftrace location.
1513 * 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 */
1517int ftrace_text_reserved(void *start, void *end)
1518{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001519 unsigned long ret;
1520
1521 ret = ftrace_location_range((unsigned long)start,
1522 (unsigned long)end);
1523
1524 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001525}
1526
Steven Rostedted926f92011-05-03 13:25:24 -04001527static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1528 int filter_hash,
1529 bool inc)
1530{
1531 struct ftrace_hash *hash;
1532 struct ftrace_hash *other_hash;
1533 struct ftrace_page *pg;
1534 struct dyn_ftrace *rec;
1535 int count = 0;
1536 int all = 0;
1537
1538 /* Only update if the ops has been registered */
1539 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1540 return;
1541
1542 /*
1543 * In the filter_hash case:
1544 * If the count is zero, we update all records.
1545 * Otherwise we just update the items in the hash.
1546 *
1547 * In the notrace_hash case:
1548 * We enable the update in the hash.
1549 * As disabling notrace means enabling the tracing,
1550 * and enabling notrace means disabling, the inc variable
1551 * gets inversed.
1552 */
1553 if (filter_hash) {
1554 hash = ops->filter_hash;
1555 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001556 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001557 all = 1;
1558 } else {
1559 inc = !inc;
1560 hash = ops->notrace_hash;
1561 other_hash = ops->filter_hash;
1562 /*
1563 * If the notrace hash has no items,
1564 * then there's nothing to do.
1565 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001566 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001567 return;
1568 }
1569
1570 do_for_each_ftrace_rec(pg, rec) {
1571 int in_other_hash = 0;
1572 int in_hash = 0;
1573 int match = 0;
1574
1575 if (all) {
1576 /*
1577 * Only the filter_hash affects all records.
1578 * Update if the record is not in the notrace hash.
1579 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001580 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001581 match = 1;
1582 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001583 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1584 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001585
1586 /*
1587 *
1588 */
1589 if (filter_hash && in_hash && !in_other_hash)
1590 match = 1;
1591 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001592 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001593 match = 1;
1594 }
1595 if (!match)
1596 continue;
1597
1598 if (inc) {
1599 rec->flags++;
1600 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1601 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001602 /*
1603 * If any ops wants regs saved for this function
1604 * then all ops will get saved regs.
1605 */
1606 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1607 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001608 } else {
1609 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1610 return;
1611 rec->flags--;
1612 }
1613 count++;
1614 /* Shortcut, if we handled all records, we are done. */
1615 if (!all && count == hash->count)
1616 return;
1617 } while_for_each_ftrace_rec();
1618}
1619
1620static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1621 int filter_hash)
1622{
1623 __ftrace_hash_rec_update(ops, filter_hash, 0);
1624}
1625
1626static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1627 int filter_hash)
1628{
1629 __ftrace_hash_rec_update(ops, filter_hash, 1);
1630}
1631
Steven Rostedt05736a42008-09-22 14:55:47 -07001632static void print_ip_ins(const char *fmt, unsigned char *p)
1633{
1634 int i;
1635
1636 printk(KERN_CONT "%s", fmt);
1637
1638 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1639 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1640}
1641
Steven Rostedtc88fd862011-08-16 09:53:39 -04001642/**
1643 * ftrace_bug - report and shutdown function tracer
1644 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1645 * @ip: The address that failed
1646 *
1647 * The arch code that enables or disables the function tracing
1648 * can call ftrace_bug() when it has detected a problem in
1649 * modifying the code. @failed should be one of either:
1650 * EFAULT - if the problem happens on reading the @ip address
1651 * EINVAL - if what is read at @ip is not what was expected
1652 * EPERM - if the problem happens on writting to the @ip address
1653 */
1654void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001655{
1656 switch (failed) {
1657 case -EFAULT:
1658 FTRACE_WARN_ON_ONCE(1);
1659 pr_info("ftrace faulted on modifying ");
1660 print_ip_sym(ip);
1661 break;
1662 case -EINVAL:
1663 FTRACE_WARN_ON_ONCE(1);
1664 pr_info("ftrace failed to modify ");
1665 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001666 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001667 printk(KERN_CONT "\n");
1668 break;
1669 case -EPERM:
1670 FTRACE_WARN_ON_ONCE(1);
1671 pr_info("ftrace faulted on writing ");
1672 print_ip_sym(ip);
1673 break;
1674 default:
1675 FTRACE_WARN_ON_ONCE(1);
1676 pr_info("ftrace faulted on unknown error ");
1677 print_ip_sym(ip);
1678 }
1679}
1680
Steven Rostedtc88fd862011-08-16 09:53:39 -04001681static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001682{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001683 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001684
Steven Rostedt982c3502008-11-15 16:31:41 -05001685 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001686 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001687 *
Steven Rostedted926f92011-05-03 13:25:24 -04001688 * If the record has a ref count, then we need to enable it
1689 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001690 *
Steven Rostedted926f92011-05-03 13:25:24 -04001691 * Otherwise we make sure its disabled.
1692 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001693 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001694 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001695 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001696 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001697 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001698
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001699 /*
1700 * If enabling and the REGS flag does not match the REGS_EN, then
1701 * do not ignore this record. Set flags to fail the compare against
1702 * ENABLED.
1703 */
1704 if (flag &&
1705 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1706 flag |= FTRACE_FL_REGS;
1707
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001708 /* If the state of this record hasn't changed, then do nothing */
1709 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001710 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001711
1712 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001713 /* Save off if rec is being enabled (for return value) */
1714 flag ^= rec->flags & FTRACE_FL_ENABLED;
1715
1716 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001717 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001718 if (flag & FTRACE_FL_REGS) {
1719 if (rec->flags & FTRACE_FL_REGS)
1720 rec->flags |= FTRACE_FL_REGS_EN;
1721 else
1722 rec->flags &= ~FTRACE_FL_REGS_EN;
1723 }
1724 }
1725
1726 /*
1727 * If this record is being updated from a nop, then
1728 * return UPDATE_MAKE_CALL.
1729 * Otherwise, if the EN flag is set, then return
1730 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1731 * from the non-save regs, to a save regs function.
1732 * Otherwise,
1733 * return UPDATE_MODIFY_CALL to tell the caller to convert
1734 * from the save regs, to a non-save regs function.
1735 */
1736 if (flag & FTRACE_FL_ENABLED)
1737 return FTRACE_UPDATE_MAKE_CALL;
1738 else if (rec->flags & FTRACE_FL_REGS_EN)
1739 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1740 else
1741 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001742 }
1743
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001744 if (update) {
1745 /* If there's no more users, clear all flags */
1746 if (!(rec->flags & ~FTRACE_FL_MASK))
1747 rec->flags = 0;
1748 else
1749 /* Just disable the record (keep REGS state) */
1750 rec->flags &= ~FTRACE_FL_ENABLED;
1751 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001752
1753 return FTRACE_UPDATE_MAKE_NOP;
1754}
1755
1756/**
1757 * ftrace_update_record, set a record that now is tracing or not
1758 * @rec: the record to update
1759 * @enable: set to 1 if the record is tracing, zero to force disable
1760 *
1761 * The records that represent all functions that can be traced need
1762 * to be updated when tracing has been enabled.
1763 */
1764int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1765{
1766 return ftrace_check_record(rec, enable, 1);
1767}
1768
1769/**
1770 * ftrace_test_record, check if the record has been enabled or not
1771 * @rec: the record to test
1772 * @enable: set to 1 to check if enabled, 0 if it is disabled
1773 *
1774 * The arch code may need to test if a record is already set to
1775 * tracing to determine how to modify the function code that it
1776 * represents.
1777 */
1778int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1779{
1780 return ftrace_check_record(rec, enable, 0);
1781}
1782
1783static int
1784__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1785{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001786 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001787 unsigned long ftrace_addr;
1788 int ret;
1789
Steven Rostedtc88fd862011-08-16 09:53:39 -04001790 ret = ftrace_update_record(rec, enable);
1791
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001792 if (rec->flags & FTRACE_FL_REGS)
1793 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1794 else
1795 ftrace_addr = (unsigned long)FTRACE_ADDR;
1796
Steven Rostedtc88fd862011-08-16 09:53:39 -04001797 switch (ret) {
1798 case FTRACE_UPDATE_IGNORE:
1799 return 0;
1800
1801 case FTRACE_UPDATE_MAKE_CALL:
1802 return ftrace_make_call(rec, ftrace_addr);
1803
1804 case FTRACE_UPDATE_MAKE_NOP:
1805 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001806
1807 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1808 case FTRACE_UPDATE_MODIFY_CALL:
1809 if (rec->flags & FTRACE_FL_REGS)
1810 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1811 else
1812 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1813
1814 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001815 }
1816
1817 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001818}
1819
Steven Rostedte4f5d542012-04-27 09:13:18 -04001820void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001821{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001822 struct dyn_ftrace *rec;
1823 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001824 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001825
Steven Rostedt45a4a232011-04-21 23:16:46 -04001826 if (unlikely(ftrace_disabled))
1827 return;
1828
Steven Rostedt265c8312009-02-13 12:43:56 -05001829 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001830 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001831 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001832 ftrace_bug(failed, rec->ip);
1833 /* Stop processing */
1834 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001835 }
1836 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001837}
1838
Steven Rostedtc88fd862011-08-16 09:53:39 -04001839struct ftrace_rec_iter {
1840 struct ftrace_page *pg;
1841 int index;
1842};
1843
1844/**
1845 * ftrace_rec_iter_start, start up iterating over traced functions
1846 *
1847 * Returns an iterator handle that is used to iterate over all
1848 * the records that represent address locations where functions
1849 * are traced.
1850 *
1851 * May return NULL if no records are available.
1852 */
1853struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1854{
1855 /*
1856 * We only use a single iterator.
1857 * Protected by the ftrace_lock mutex.
1858 */
1859 static struct ftrace_rec_iter ftrace_rec_iter;
1860 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1861
1862 iter->pg = ftrace_pages_start;
1863 iter->index = 0;
1864
1865 /* Could have empty pages */
1866 while (iter->pg && !iter->pg->index)
1867 iter->pg = iter->pg->next;
1868
1869 if (!iter->pg)
1870 return NULL;
1871
1872 return iter;
1873}
1874
1875/**
1876 * ftrace_rec_iter_next, get the next record to process.
1877 * @iter: The handle to the iterator.
1878 *
1879 * Returns the next iterator after the given iterator @iter.
1880 */
1881struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1882{
1883 iter->index++;
1884
1885 if (iter->index >= iter->pg->index) {
1886 iter->pg = iter->pg->next;
1887 iter->index = 0;
1888
1889 /* Could have empty pages */
1890 while (iter->pg && !iter->pg->index)
1891 iter->pg = iter->pg->next;
1892 }
1893
1894 if (!iter->pg)
1895 return NULL;
1896
1897 return iter;
1898}
1899
1900/**
1901 * ftrace_rec_iter_record, get the record at the iterator location
1902 * @iter: The current iterator location
1903 *
1904 * Returns the record that the current @iter is at.
1905 */
1906struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1907{
1908 return &iter->pg->records[iter->index];
1909}
1910
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301911static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001912ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001913{
1914 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001915 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001916
1917 ip = rec->ip;
1918
Steven Rostedt45a4a232011-04-21 23:16:46 -04001919 if (unlikely(ftrace_disabled))
1920 return 0;
1921
Shaohua Li25aac9d2009-01-09 11:29:40 +08001922 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001923 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001924 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301925 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001926 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301927 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001928}
1929
Steven Rostedt000ab692009-02-17 13:35:06 -05001930/*
1931 * archs can override this function if they must do something
1932 * before the modifying code is performed.
1933 */
1934int __weak ftrace_arch_code_modify_prepare(void)
1935{
1936 return 0;
1937}
1938
1939/*
1940 * archs can override this function if they must do something
1941 * after the modifying code is performed.
1942 */
1943int __weak ftrace_arch_code_modify_post_process(void)
1944{
1945 return 0;
1946}
1947
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001948void ftrace_modify_all_code(int command)
1949{
1950 if (command & FTRACE_UPDATE_CALLS)
1951 ftrace_replace_code(1);
1952 else if (command & FTRACE_DISABLE_CALLS)
1953 ftrace_replace_code(0);
1954
1955 if (command & FTRACE_UPDATE_TRACE_FUNC)
1956 ftrace_update_ftrace_func(ftrace_trace_function);
1957
1958 if (command & FTRACE_START_FUNC_RET)
1959 ftrace_enable_ftrace_graph_caller();
1960 else if (command & FTRACE_STOP_FUNC_RET)
1961 ftrace_disable_ftrace_graph_caller();
1962}
1963
Ingo Molnare309b412008-05-12 21:20:51 +02001964static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001965{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001966 int *command = data;
1967
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001968 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001969
Steven Rostedtc88fd862011-08-16 09:53:39 -04001970 return 0;
1971}
1972
1973/**
1974 * ftrace_run_stop_machine, go back to the stop machine method
1975 * @command: The command to tell ftrace what to do
1976 *
1977 * If an arch needs to fall back to the stop machine method, the
1978 * it can call this function.
1979 */
1980void ftrace_run_stop_machine(int command)
1981{
1982 stop_machine(__ftrace_modify_code, &command, NULL);
1983}
1984
1985/**
1986 * arch_ftrace_update_code, modify the code to trace or not trace
1987 * @command: The command that needs to be done
1988 *
1989 * Archs can override this function if it does not need to
1990 * run stop_machine() to modify code.
1991 */
1992void __weak arch_ftrace_update_code(int command)
1993{
1994 ftrace_run_stop_machine(command);
1995}
1996
1997static void ftrace_run_update_code(int command)
1998{
1999 int ret;
2000
2001 ret = ftrace_arch_code_modify_prepare();
2002 FTRACE_WARN_ON(ret);
2003 if (ret)
2004 return;
2005 /*
2006 * Do not call function tracer while we update the code.
2007 * We are in stop machine.
2008 */
2009 function_trace_stop++;
2010
2011 /*
2012 * By default we use stop_machine() to modify the code.
2013 * But archs can do what ever they want as long as it
2014 * is safe. The stop_machine() is the safest, but also
2015 * produces the most overhead.
2016 */
2017 arch_ftrace_update_code(command);
2018
Steven Rostedt6331c282011-07-13 15:11:02 -04002019 function_trace_stop--;
2020
Steven Rostedt000ab692009-02-17 13:35:06 -05002021 ret = ftrace_arch_code_modify_post_process();
2022 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002023}
2024
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002025static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002026static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002027static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002028
2029static void ftrace_startup_enable(int command)
2030{
2031 if (saved_ftrace_func != ftrace_trace_function) {
2032 saved_ftrace_func = ftrace_trace_function;
2033 command |= FTRACE_UPDATE_TRACE_FUNC;
2034 }
2035
2036 if (!command || !ftrace_enabled)
2037 return;
2038
2039 ftrace_run_update_code(command);
2040}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002041
Steven Rostedta1cd6172011-05-23 15:24:25 -04002042static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002043{
Steven Rostedtb8489142011-05-04 09:27:52 -04002044 bool hash_enable = true;
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002045 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002046
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002047 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002048 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002049
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002050 ret = __register_ftrace_function(ops);
2051 if (ret)
2052 return ret;
2053
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002054 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002055 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002056
Steven Rostedtb8489142011-05-04 09:27:52 -04002057 /* ops marked global share the filter hashes */
2058 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2059 ops = &global_ops;
2060 /* Don't update hash if global is already set */
2061 if (global_start_up)
2062 hash_enable = false;
2063 global_start_up++;
2064 }
2065
Steven Rostedted926f92011-05-03 13:25:24 -04002066 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002067 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002068 ftrace_hash_rec_enable(ops, 1);
2069
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002070 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002071
2072 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002073}
2074
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002075static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002076{
Steven Rostedtb8489142011-05-04 09:27:52 -04002077 bool hash_disable = true;
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002078 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002079
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002080 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002081 return -ENODEV;
2082
2083 ret = __unregister_ftrace_function(ops);
2084 if (ret)
2085 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002086
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002087 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002088 /*
2089 * Just warn in case of unbalance, no need to kill ftrace, it's not
2090 * critical but the ftrace_call callers may be never nopped again after
2091 * further ftrace uses.
2092 */
2093 WARN_ON_ONCE(ftrace_start_up < 0);
2094
Steven Rostedtb8489142011-05-04 09:27:52 -04002095 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2096 ops = &global_ops;
2097 global_start_up--;
2098 WARN_ON_ONCE(global_start_up < 0);
2099 /* Don't update hash if global still has users */
2100 if (global_start_up) {
2101 WARN_ON_ONCE(!ftrace_start_up);
2102 hash_disable = false;
2103 }
2104 }
2105
2106 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002107 ftrace_hash_rec_disable(ops, 1);
2108
Steven Rostedtb8489142011-05-04 09:27:52 -04002109 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002110 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002111
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002112 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002113
2114 if (saved_ftrace_func != ftrace_trace_function) {
2115 saved_ftrace_func = ftrace_trace_function;
2116 command |= FTRACE_UPDATE_TRACE_FUNC;
2117 }
2118
2119 if (!command || !ftrace_enabled)
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002120 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002121
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002122 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002123 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002124}
2125
Ingo Molnare309b412008-05-12 21:20:51 +02002126static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002127{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002128 if (unlikely(ftrace_disabled))
2129 return;
2130
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002131 /* Force update next time */
2132 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002133 /* ftrace_start_up is true if we want ftrace running */
2134 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002135 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002136}
2137
Ingo Molnare309b412008-05-12 21:20:51 +02002138static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002139{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002140 if (unlikely(ftrace_disabled))
2141 return;
2142
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002143 /* ftrace_start_up is true if ftrace is running */
2144 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002145 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002146}
2147
Steven Rostedt3d083392008-05-12 21:20:42 +02002148static cycle_t ftrace_update_time;
2149static unsigned long ftrace_update_cnt;
2150unsigned long ftrace_update_tot_cnt;
2151
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002152static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002153{
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002154 /*
2155 * Filter_hash being empty will default to trace module.
2156 * But notrace hash requires a test of individual module functions.
2157 */
2158 return ftrace_hash_empty(ops->filter_hash) &&
2159 ftrace_hash_empty(ops->notrace_hash);
2160}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002161
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002162/*
2163 * Check if the current ops references the record.
2164 *
2165 * If the ops traces all functions, then it was already accounted for.
2166 * If the ops does not trace the current record function, skip it.
2167 * If the ops ignores the function via notrace filter, skip it.
2168 */
2169static inline bool
2170ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2171{
2172 /* If ops isn't enabled, ignore it */
2173 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2174 return 0;
2175
2176 /* If ops traces all mods, we already accounted for it */
2177 if (ops_traces_mod(ops))
2178 return 0;
2179
2180 /* The function must be in the filter */
2181 if (!ftrace_hash_empty(ops->filter_hash) &&
2182 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2183 return 0;
2184
2185 /* If in notrace hash, we ignore it too */
2186 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2187 return 0;
2188
2189 return 1;
2190}
2191
2192static int referenced_filters(struct dyn_ftrace *rec)
2193{
2194 struct ftrace_ops *ops;
2195 int cnt = 0;
2196
2197 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2198 if (ops_references_rec(ops, rec))
2199 cnt++;
2200 }
2201
2202 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002203}
2204
Steven Rostedt31e88902008-11-14 16:21:19 -08002205static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002206{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002207 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002208 struct dyn_ftrace *p;
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302209 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002210 unsigned long ref = 0;
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002211 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002212 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002213
2214 /*
2215 * When adding a module, we need to check if tracers are
2216 * currently enabled and if they are set to trace all functions.
2217 * If they are, we need to enable the module functions as well
2218 * as update the reference counts for those function records.
2219 */
2220 if (mod) {
2221 struct ftrace_ops *ops;
2222
2223 for (ops = ftrace_ops_list;
2224 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002225 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2226 if (ops_traces_mod(ops))
2227 ref++;
2228 else
2229 test = true;
2230 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002231 }
2232 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002233
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002234 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002235 ftrace_update_cnt = 0;
2236
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002237 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302238
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002239 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002240 int cnt = ref;
2241
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002242 /* If something went wrong, bail without enabling anything */
2243 if (unlikely(ftrace_disabled))
2244 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002245
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002246 p = &pg->records[i];
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002247 if (test)
2248 cnt += referenced_filters(p);
2249 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302250
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002251 /*
2252 * Do the initial record conversion from mcount jump
2253 * to the NOP instructions.
2254 */
2255 if (!ftrace_code_disable(mod, p))
2256 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002257
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002258 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002259
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002260 /*
2261 * If the tracing is enabled, go ahead and enable the record.
2262 *
2263 * The reason not to enable the record immediatelly is the
2264 * inherent check of ftrace_make_nop/ftrace_make_call for
2265 * correct previous instructions. Making first the NOP
2266 * conversion puts the module to the correct state, thus
2267 * passing the ftrace_make_call check.
2268 */
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002269 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002270 int failed = __ftrace_replace_code(p, 1);
2271 if (failed)
2272 ftrace_bug(failed, p->ip);
2273 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002274 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002275 }
2276
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002277 ftrace_new_pgs = NULL;
2278
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002279 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002280 ftrace_update_time = stop - start;
2281 ftrace_update_tot_cnt += ftrace_update_cnt;
2282
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002283 return 0;
2284}
2285
Steven Rostedta7900872011-12-16 16:23:44 -05002286static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002287{
Steven Rostedta7900872011-12-16 16:23:44 -05002288 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002289 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002290
Steven Rostedta7900872011-12-16 16:23:44 -05002291 if (WARN_ON(!count))
2292 return -EINVAL;
2293
2294 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002295
2296 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002297 * We want to fill as much as possible. No more than a page
2298 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002299 */
Steven Rostedta7900872011-12-16 16:23:44 -05002300 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2301 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002302
Steven Rostedta7900872011-12-16 16:23:44 -05002303 again:
2304 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2305
2306 if (!pg->records) {
2307 /* if we can't allocate this size, try something smaller */
2308 if (!order)
2309 return -ENOMEM;
2310 order >>= 1;
2311 goto again;
2312 }
2313
2314 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2315 pg->size = cnt;
2316
2317 if (cnt > count)
2318 cnt = count;
2319
2320 return cnt;
2321}
2322
2323static struct ftrace_page *
2324ftrace_allocate_pages(unsigned long num_to_init)
2325{
2326 struct ftrace_page *start_pg;
2327 struct ftrace_page *pg;
2328 int order;
2329 int cnt;
2330
2331 if (!num_to_init)
2332 return 0;
2333
2334 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2335 if (!pg)
2336 return NULL;
2337
2338 /*
2339 * Try to allocate as much as possible in one continues
2340 * location that fills in all of the space. We want to
2341 * waste as little space as possible.
2342 */
2343 for (;;) {
2344 cnt = ftrace_allocate_records(pg, num_to_init);
2345 if (cnt < 0)
2346 goto free_pages;
2347
2348 num_to_init -= cnt;
2349 if (!num_to_init)
2350 break;
2351
2352 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2353 if (!pg->next)
2354 goto free_pages;
2355
2356 pg = pg->next;
2357 }
2358
2359 return start_pg;
2360
2361 free_pages:
2362 while (start_pg) {
2363 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2364 free_pages((unsigned long)pg->records, order);
2365 start_pg = pg->next;
2366 kfree(pg);
2367 pg = start_pg;
2368 }
2369 pr_info("ftrace: FAILED to allocate memory for functions\n");
2370 return NULL;
2371}
2372
2373static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2374{
2375 int cnt;
2376
2377 if (!num_to_init) {
2378 pr_info("ftrace: No functions to be traced?\n");
2379 return -1;
2380 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002381
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002382 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002383 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002384 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002385
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002386 return 0;
2387}
2388
Steven Rostedt5072c592008-05-12 21:20:43 +02002389#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2390
2391struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002392 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002393 loff_t func_pos;
2394 struct ftrace_page *pg;
2395 struct dyn_ftrace *func;
2396 struct ftrace_func_probe *probe;
2397 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002398 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002399 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002400 int hidx;
2401 int idx;
2402 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002403};
2404
Ingo Molnare309b412008-05-12 21:20:51 +02002405static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002406t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002407{
2408 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002409 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002410 struct hlist_head *hhd;
2411
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002412 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002413 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002414
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002415 if (iter->probe)
2416 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002417 retry:
2418 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2419 return NULL;
2420
2421 hhd = &ftrace_func_hash[iter->hidx];
2422
2423 if (hlist_empty(hhd)) {
2424 iter->hidx++;
2425 hnd = NULL;
2426 goto retry;
2427 }
2428
2429 if (!hnd)
2430 hnd = hhd->first;
2431 else {
2432 hnd = hnd->next;
2433 if (!hnd) {
2434 iter->hidx++;
2435 goto retry;
2436 }
2437 }
2438
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002439 if (WARN_ON_ONCE(!hnd))
2440 return NULL;
2441
2442 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2443
2444 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002445}
2446
2447static void *t_hash_start(struct seq_file *m, loff_t *pos)
2448{
2449 struct ftrace_iterator *iter = m->private;
2450 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002451 loff_t l;
2452
Steven Rostedt69a30832011-12-19 15:21:16 -05002453 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2454 return NULL;
2455
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002456 if (iter->func_pos > *pos)
2457 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002458
Li Zefand82d6242009-06-24 09:54:54 +08002459 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002460 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002461 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002462 if (!p)
2463 break;
2464 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002465 if (!p)
2466 return NULL;
2467
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002468 /* Only set this if we have an item */
2469 iter->flags |= FTRACE_ITER_HASH;
2470
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002471 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002472}
2473
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002474static int
2475t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002476{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002477 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002478
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002479 rec = iter->probe;
2480 if (WARN_ON_ONCE(!rec))
2481 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002482
Steven Rostedt809dcf22009-02-16 23:06:01 -05002483 if (rec->ops->print)
2484 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2485
Steven Rostedtb375a112009-09-17 00:05:58 -04002486 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002487
2488 if (rec->data)
2489 seq_printf(m, ":%p", rec->data);
2490 seq_putc(m, '\n');
2491
2492 return 0;
2493}
2494
2495static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002496t_next(struct seq_file *m, void *v, loff_t *pos)
2497{
2498 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002499 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002500 struct dyn_ftrace *rec = NULL;
2501
Steven Rostedt45a4a232011-04-21 23:16:46 -04002502 if (unlikely(ftrace_disabled))
2503 return NULL;
2504
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002505 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002506 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002507
Steven Rostedt5072c592008-05-12 21:20:43 +02002508 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002509 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002510
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002511 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002512 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002513
Steven Rostedt5072c592008-05-12 21:20:43 +02002514 retry:
2515 if (iter->idx >= iter->pg->index) {
2516 if (iter->pg->next) {
2517 iter->pg = iter->pg->next;
2518 iter->idx = 0;
2519 goto retry;
2520 }
2521 } else {
2522 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002523 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002524 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002525
Steven Rostedt41c52c02008-05-22 11:46:33 -04002526 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002527 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2528
2529 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002530 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002531
Steven Rostedt5072c592008-05-12 21:20:43 +02002532 rec = NULL;
2533 goto retry;
2534 }
2535 }
2536
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002537 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002538 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002539
2540 iter->func = rec;
2541
2542 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002543}
2544
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002545static void reset_iter_read(struct ftrace_iterator *iter)
2546{
2547 iter->pos = 0;
2548 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002549 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002550}
2551
2552static void *t_start(struct seq_file *m, loff_t *pos)
2553{
2554 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002555 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002556 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002557 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002558
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002559 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002560
2561 if (unlikely(ftrace_disabled))
2562 return NULL;
2563
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002564 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002565 * If an lseek was done, then reset and start from beginning.
2566 */
2567 if (*pos < iter->pos)
2568 reset_iter_read(iter);
2569
2570 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002571 * For set_ftrace_filter reading, if we have the filter
2572 * off, we can short cut and just print out that all
2573 * functions are enabled.
2574 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002575 if (iter->flags & FTRACE_ITER_FILTER &&
2576 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002577 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002578 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002579 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002580 /* reset in case of seek/pread */
2581 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002582 return iter;
2583 }
2584
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002585 if (iter->flags & FTRACE_ITER_HASH)
2586 return t_hash_start(m, pos);
2587
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002588 /*
2589 * Unfortunately, we need to restart at ftrace_pages_start
2590 * every time we let go of the ftrace_mutex. This is because
2591 * those pointers can change without the lock.
2592 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002593 iter->pg = ftrace_pages_start;
2594 iter->idx = 0;
2595 for (l = 0; l <= *pos; ) {
2596 p = t_next(m, p, &l);
2597 if (!p)
2598 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002599 }
walimis5821e1b2008-11-15 15:19:06 +08002600
Steven Rostedt69a30832011-12-19 15:21:16 -05002601 if (!p)
2602 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002603
2604 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002605}
2606
2607static void t_stop(struct seq_file *m, void *p)
2608{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002609 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002610}
2611
2612static int t_show(struct seq_file *m, void *v)
2613{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002614 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002615 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002616
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002617 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002618 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002619
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002620 if (iter->flags & FTRACE_ITER_PRINTALL) {
2621 seq_printf(m, "#### all functions enabled ####\n");
2622 return 0;
2623 }
2624
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002625 rec = iter->func;
2626
Steven Rostedt5072c592008-05-12 21:20:43 +02002627 if (!rec)
2628 return 0;
2629
Steven Rostedt647bcd02011-05-03 14:39:21 -04002630 seq_printf(m, "%ps", (void *)rec->ip);
2631 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002632 seq_printf(m, " (%ld)%s",
2633 rec->flags & ~FTRACE_FL_MASK,
2634 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002635 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002636
2637 return 0;
2638}
2639
James Morris88e9d342009-09-22 16:43:43 -07002640static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002641 .start = t_start,
2642 .next = t_next,
2643 .stop = t_stop,
2644 .show = t_show,
2645};
2646
Ingo Molnare309b412008-05-12 21:20:51 +02002647static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002648ftrace_avail_open(struct inode *inode, struct file *file)
2649{
2650 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002651
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002652 if (unlikely(ftrace_disabled))
2653 return -ENODEV;
2654
Jiri Olsa50e18b92012-04-25 10:23:39 +02002655 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2656 if (iter) {
2657 iter->pg = ftrace_pages_start;
2658 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002659 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002660
Jiri Olsa50e18b92012-04-25 10:23:39 +02002661 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002662}
2663
Steven Rostedt647bcd02011-05-03 14:39:21 -04002664static int
2665ftrace_enabled_open(struct inode *inode, struct file *file)
2666{
2667 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002668
2669 if (unlikely(ftrace_disabled))
2670 return -ENODEV;
2671
Jiri Olsa50e18b92012-04-25 10:23:39 +02002672 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2673 if (iter) {
2674 iter->pg = ftrace_pages_start;
2675 iter->flags = FTRACE_ITER_ENABLED;
2676 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002677 }
2678
Jiri Olsa50e18b92012-04-25 10:23:39 +02002679 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002680}
2681
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002682static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002683{
Steven Rostedt52baf112009-02-14 01:15:39 -05002684 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002685 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002686 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002687}
2688
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002689/**
2690 * ftrace_regex_open - initialize function tracer filter files
2691 * @ops: The ftrace_ops that hold the hash filters
2692 * @flag: The type of filter to process
2693 * @inode: The inode, usually passed in to your open routine
2694 * @file: The file, usually passed in to your open routine
2695 *
2696 * ftrace_regex_open() initializes the filter files for the
2697 * @ops. Depending on @flag it may process the filter hash or
2698 * the notrace hash of @ops. With this called from the open
2699 * routine, you can use ftrace_filter_write() for the write
2700 * routine if @flag has FTRACE_ITER_FILTER set, or
2701 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04002702 * ftrace_filter_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002703 * release must call ftrace_regex_release().
2704 */
2705int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002706ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002707 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002708{
2709 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002710 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002711 int ret = 0;
2712
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002713 ftrace_ops_init(ops);
2714
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002715 if (unlikely(ftrace_disabled))
2716 return -ENODEV;
2717
Steven Rostedt5072c592008-05-12 21:20:43 +02002718 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2719 if (!iter)
2720 return -ENOMEM;
2721
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002722 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2723 kfree(iter);
2724 return -ENOMEM;
2725 }
2726
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002727 iter->ops = ops;
2728 iter->flags = flag;
2729
2730 mutex_lock(&ops->regex_lock);
2731
Steven Rostedtf45948e2011-05-02 12:29:25 -04002732 if (flag & FTRACE_ITER_NOTRACE)
2733 hash = ops->notrace_hash;
2734 else
2735 hash = ops->filter_hash;
2736
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002737 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002738 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002739 if (!iter->hash) {
2740 trace_parser_put(&iter->parser);
2741 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002742 ret = -ENOMEM;
2743 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002744 }
2745 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002746
Steven Rostedt5072c592008-05-12 21:20:43 +02002747 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002748 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002749 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002750
2751 if (file->f_mode & FMODE_READ) {
2752 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002753
2754 ret = seq_open(file, &show_ftrace_seq_ops);
2755 if (!ret) {
2756 struct seq_file *m = file->private_data;
2757 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002758 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002759 /* Failed */
2760 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002761 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002762 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002763 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002764 } else
2765 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002766
2767 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002768 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002769
2770 return ret;
2771}
2772
Steven Rostedt41c52c02008-05-22 11:46:33 -04002773static int
2774ftrace_filter_open(struct inode *inode, struct file *file)
2775{
Steven Rostedt69a30832011-12-19 15:21:16 -05002776 return ftrace_regex_open(&global_ops,
2777 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2778 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002779}
2780
2781static int
2782ftrace_notrace_open(struct inode *inode, struct file *file)
2783{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002784 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002785 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002786}
2787
Steven Rostedt64e7c442009-02-13 17:08:48 -05002788static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002789{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002790 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002791 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002792
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002793 switch (type) {
2794 case MATCH_FULL:
2795 if (strcmp(str, regex) == 0)
2796 matched = 1;
2797 break;
2798 case MATCH_FRONT_ONLY:
2799 if (strncmp(str, regex, len) == 0)
2800 matched = 1;
2801 break;
2802 case MATCH_MIDDLE_ONLY:
2803 if (strstr(str, regex))
2804 matched = 1;
2805 break;
2806 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002807 slen = strlen(str);
2808 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002809 matched = 1;
2810 break;
2811 }
2812
2813 return matched;
2814}
2815
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002816static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002817enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002818{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002819 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002820 int ret = 0;
2821
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002822 entry = ftrace_lookup_ip(hash, rec->ip);
2823 if (not) {
2824 /* Do nothing if it doesn't exist */
2825 if (!entry)
2826 return 0;
2827
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002828 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002829 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002830 /* Do nothing if it exists */
2831 if (entry)
2832 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002833
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002834 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002835 }
2836 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002837}
2838
Steven Rostedt64e7c442009-02-13 17:08:48 -05002839static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002840ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2841 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002842{
2843 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002844 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002845
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002846 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2847
2848 if (mod) {
2849 /* module lookup requires matching the module */
2850 if (!modname || strcmp(modname, mod))
2851 return 0;
2852
2853 /* blank search means to match all funcs in the mod */
2854 if (!len)
2855 return 1;
2856 }
2857
Steven Rostedt64e7c442009-02-13 17:08:48 -05002858 return ftrace_match(str, regex, len, type);
2859}
2860
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002861static int
2862match_records(struct ftrace_hash *hash, char *buff,
2863 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002864{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002865 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002866 struct ftrace_page *pg;
2867 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002868 int type = MATCH_FULL;
2869 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002870 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002871 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002872
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002873 if (len) {
2874 type = filter_parse_regex(buff, len, &search, &not);
2875 search_len = strlen(search);
2876 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002877
Steven Rostedt52baf112009-02-14 01:15:39 -05002878 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002879
2880 if (unlikely(ftrace_disabled))
2881 goto out_unlock;
2882
Steven Rostedt265c8312009-02-13 12:43:56 -05002883 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002884 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002885 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002886 if (ret < 0) {
2887 found = ret;
2888 goto out_unlock;
2889 }
Li Zefan311d16d2009-12-08 11:15:11 +08002890 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002891 }
2892 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002893 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002894 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002895
2896 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002897}
2898
Steven Rostedt64e7c442009-02-13 17:08:48 -05002899static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002900ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002901{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002902 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002903}
2904
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002905static int
2906ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002907{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002908 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002909
Steven Rostedt64e7c442009-02-13 17:08:48 -05002910 /* blank or '*' mean the same */
2911 if (strcmp(buff, "*") == 0)
2912 buff[0] = 0;
2913
2914 /* handle the case of 'dont filter this module' */
2915 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2916 buff[0] = 0;
2917 not = 1;
2918 }
2919
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002920 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002921}
2922
Steven Rostedtf6180772009-02-14 00:40:25 -05002923/*
2924 * We register the module command as a template to show others how
2925 * to register the a command as well.
2926 */
2927
2928static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002929ftrace_mod_callback(struct ftrace_hash *hash,
2930 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002931{
2932 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002933 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002934
2935 /*
2936 * cmd == 'mod' because we only registered this func
2937 * for the 'mod' ftrace_func_command.
2938 * But if you register one func with multiple commands,
2939 * you can tell which command was used by the cmd
2940 * parameter.
2941 */
2942
2943 /* we must have a module name */
2944 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002945 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002946
2947 mod = strsep(&param, ":");
2948 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002949 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002950
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002951 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002952 if (!ret)
2953 ret = -EINVAL;
2954 if (ret < 0)
2955 return ret;
2956
2957 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002958}
2959
2960static struct ftrace_func_command ftrace_mod_cmd = {
2961 .name = "mod",
2962 .func = ftrace_mod_callback,
2963};
2964
2965static int __init ftrace_mod_cmd_init(void)
2966{
2967 return register_ftrace_command(&ftrace_mod_cmd);
2968}
Steven Rostedt6f415672012-10-05 12:13:07 -04002969core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002970
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002971static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002972 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002973{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002974 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002975 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002976 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002977
2978 key = hash_long(ip, FTRACE_HASH_BITS);
2979
2980 hhd = &ftrace_func_hash[key];
2981
2982 if (hlist_empty(hhd))
2983 return;
2984
2985 /*
2986 * Disable preemption for these calls to prevent a RCU grace
2987 * period. This syncs the hash iteration and freeing of items
2988 * on the hash. rcu_read_lock is too dangerous here.
2989 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002990 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04002991 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002992 if (entry->ip == ip)
2993 entry->ops->func(ip, parent_ip, &entry->data);
2994 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002995 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002996}
2997
Steven Rostedtb6887d72009-02-17 12:32:04 -05002998static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002999{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003000 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003001 .flags = FTRACE_OPS_FL_INITIALIZED,
3002 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003003};
3004
Steven Rostedtb6887d72009-02-17 12:32:04 -05003005static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003006
Steven Rostedtb6887d72009-02-17 12:32:04 -05003007static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003008{
Steven Rostedtb8489142011-05-04 09:27:52 -04003009 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003010 int i;
3011
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003012 if (ftrace_probe_registered) {
3013 /* still need to update the function call sites */
3014 if (ftrace_enabled)
3015 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003016 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003017 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003018
3019 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3020 struct hlist_head *hhd = &ftrace_func_hash[i];
3021 if (hhd->first)
3022 break;
3023 }
3024 /* Nothing registered? */
3025 if (i == FTRACE_FUNC_HASHSIZE)
3026 return;
3027
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05003028 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003029
Steven Rostedtb6887d72009-02-17 12:32:04 -05003030 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003031}
3032
Steven Rostedtb6887d72009-02-17 12:32:04 -05003033static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003034{
3035 int i;
3036
Steven Rostedtb6887d72009-02-17 12:32:04 -05003037 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003038 return;
3039
3040 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3041 struct hlist_head *hhd = &ftrace_func_hash[i];
3042 if (hhd->first)
3043 return;
3044 }
3045
3046 /* no more funcs left */
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05003047 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003048
Steven Rostedtb6887d72009-02-17 12:32:04 -05003049 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003050}
3051
3052
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003053static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003054{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003055 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003056 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003057 kfree(entry);
3058}
3059
Steven Rostedt59df055f2009-02-14 15:29:06 -05003060int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003061register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003062 void *data)
3063{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003064 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003065 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3066 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003067 struct ftrace_page *pg;
3068 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003069 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003070 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003071 int count = 0;
3072 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003073 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003074
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003075 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003076 len = strlen(search);
3077
Steven Rostedtb6887d72009-02-17 12:32:04 -05003078 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003079 if (WARN_ON(not))
3080 return -EINVAL;
3081
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003082 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003083
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003084 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3085 if (!hash) {
3086 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003087 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003088 }
3089
3090 if (unlikely(ftrace_disabled)) {
3091 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003092 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003093 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003094
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003095 mutex_lock(&ftrace_lock);
3096
Steven Rostedt59df055f2009-02-14 15:29:06 -05003097 do_for_each_ftrace_rec(pg, rec) {
3098
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003099 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003100 continue;
3101
3102 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3103 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003104 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003105 if (!count)
3106 count = -ENOMEM;
3107 goto out_unlock;
3108 }
3109
3110 count++;
3111
3112 entry->data = data;
3113
3114 /*
3115 * The caller might want to do something special
3116 * for each function we find. We call the callback
3117 * to give the caller an opportunity to do so.
3118 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003119 if (ops->init) {
3120 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003121 /* caller does not like this func */
3122 kfree(entry);
3123 continue;
3124 }
3125 }
3126
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003127 ret = enter_record(hash, rec, 0);
3128 if (ret < 0) {
3129 kfree(entry);
3130 count = ret;
3131 goto out_unlock;
3132 }
3133
Steven Rostedt59df055f2009-02-14 15:29:06 -05003134 entry->ops = ops;
3135 entry->ip = rec->ip;
3136
3137 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3138 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3139
3140 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003141
3142 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3143 if (ret < 0)
3144 count = ret;
3145
Steven Rostedtb6887d72009-02-17 12:32:04 -05003146 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003147
3148 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003149 mutex_unlock(&ftrace_lock);
3150 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003151 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003152 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003153
3154 return count;
3155}
3156
3157enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003158 PROBE_TEST_FUNC = 1,
3159 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003160};
3161
3162static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003163__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003164 void *data, int flags)
3165{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003166 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003167 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003168 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003169 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003170 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003171 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003172 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003173 char str[KSYM_SYMBOL_LEN];
3174 int type = MATCH_FULL;
3175 int i, len = 0;
3176 char *search;
3177
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003178 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003179 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003180 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003181 int not;
3182
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003183 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003184 len = strlen(search);
3185
Steven Rostedtb6887d72009-02-17 12:32:04 -05003186 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003187 if (WARN_ON(not))
3188 return;
3189 }
3190
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003191 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003192
3193 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3194 if (!hash)
3195 /* Hmm, should report this somehow */
3196 goto out_unlock;
3197
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003198 INIT_LIST_HEAD(&free_list);
3199
Steven Rostedt59df055f2009-02-14 15:29:06 -05003200 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3201 struct hlist_head *hhd = &ftrace_func_hash[i];
3202
Sasha Levinb67bfe02013-02-27 17:06:00 -08003203 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003204
3205 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003206 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003207 continue;
3208
Steven Rostedtb6887d72009-02-17 12:32:04 -05003209 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003210 continue;
3211
3212 /* do this last, since it is the most expensive */
3213 if (glob) {
3214 kallsyms_lookup(entry->ip, NULL, NULL,
3215 NULL, str);
3216 if (!ftrace_match(str, glob, len, type))
3217 continue;
3218 }
3219
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003220 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3221 /* It is possible more than one entry had this ip */
3222 if (rec_entry)
3223 free_hash_entry(hash, rec_entry);
3224
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003225 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003226 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003227 }
3228 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003229 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003230 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003231 /*
3232 * Remove after the disable is called. Otherwise, if the last
3233 * probe is removed, a null hash means *all enabled*.
3234 */
3235 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003236 synchronize_sched();
3237 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3238 list_del(&entry->free_list);
3239 ftrace_free_entry(entry);
3240 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003241 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003242
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003243 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003244 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003245 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003246}
3247
3248void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003249unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003250 void *data)
3251{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003252 __unregister_ftrace_function_probe(glob, ops, data,
3253 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003254}
3255
3256void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003257unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003258{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003259 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003260}
3261
Steven Rostedtb6887d72009-02-17 12:32:04 -05003262void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003263{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003264 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003265}
3266
Steven Rostedtf6180772009-02-14 00:40:25 -05003267static LIST_HEAD(ftrace_commands);
3268static DEFINE_MUTEX(ftrace_cmd_mutex);
3269
3270int register_ftrace_command(struct ftrace_func_command *cmd)
3271{
3272 struct ftrace_func_command *p;
3273 int ret = 0;
3274
3275 mutex_lock(&ftrace_cmd_mutex);
3276 list_for_each_entry(p, &ftrace_commands, list) {
3277 if (strcmp(cmd->name, p->name) == 0) {
3278 ret = -EBUSY;
3279 goto out_unlock;
3280 }
3281 }
3282 list_add(&cmd->list, &ftrace_commands);
3283 out_unlock:
3284 mutex_unlock(&ftrace_cmd_mutex);
3285
3286 return ret;
3287}
3288
3289int unregister_ftrace_command(struct ftrace_func_command *cmd)
3290{
3291 struct ftrace_func_command *p, *n;
3292 int ret = -ENODEV;
3293
3294 mutex_lock(&ftrace_cmd_mutex);
3295 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3296 if (strcmp(cmd->name, p->name) == 0) {
3297 ret = 0;
3298 list_del_init(&p->list);
3299 goto out_unlock;
3300 }
3301 }
3302 out_unlock:
3303 mutex_unlock(&ftrace_cmd_mutex);
3304
3305 return ret;
3306}
3307
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003308static int ftrace_process_regex(struct ftrace_hash *hash,
3309 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003310{
Steven Rostedtf6180772009-02-14 00:40:25 -05003311 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003312 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003313 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003314
3315 func = strsep(&next, ":");
3316
3317 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003318 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003319 if (!ret)
3320 ret = -EINVAL;
3321 if (ret < 0)
3322 return ret;
3323 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003324 }
3325
Steven Rostedtf6180772009-02-14 00:40:25 -05003326 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003327
3328 command = strsep(&next, ":");
3329
Steven Rostedtf6180772009-02-14 00:40:25 -05003330 mutex_lock(&ftrace_cmd_mutex);
3331 list_for_each_entry(p, &ftrace_commands, list) {
3332 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003333 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003334 goto out_unlock;
3335 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003336 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003337 out_unlock:
3338 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003339
Steven Rostedtf6180772009-02-14 00:40:25 -05003340 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003341}
3342
Ingo Molnare309b412008-05-12 21:20:51 +02003343static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003344ftrace_regex_write(struct file *file, const char __user *ubuf,
3345 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003346{
3347 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003348 struct trace_parser *parser;
3349 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003350
Li Zefan4ba79782009-09-22 13:52:20 +08003351 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003352 return 0;
3353
Steven Rostedt5072c592008-05-12 21:20:43 +02003354 if (file->f_mode & FMODE_READ) {
3355 struct seq_file *m = file->private_data;
3356 iter = m->private;
3357 } else
3358 iter = file->private_data;
3359
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003360 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003361 return -ENODEV;
3362
3363 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003364
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003365 parser = &iter->parser;
3366 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003367
Li Zefan4ba79782009-09-22 13:52:20 +08003368 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003369 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003370 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003371 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003372 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003373 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003374 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003375 }
3376
Steven Rostedt5072c592008-05-12 21:20:43 +02003377 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003378 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003379 return ret;
3380}
3381
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003382ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003383ftrace_filter_write(struct file *file, const char __user *ubuf,
3384 size_t cnt, loff_t *ppos)
3385{
3386 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3387}
3388
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003389ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003390ftrace_notrace_write(struct file *file, const char __user *ubuf,
3391 size_t cnt, loff_t *ppos)
3392{
3393 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3394}
3395
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003396static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003397ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3398{
3399 struct ftrace_func_entry *entry;
3400
3401 if (!ftrace_location(ip))
3402 return -EINVAL;
3403
3404 if (remove) {
3405 entry = ftrace_lookup_ip(hash, ip);
3406 if (!entry)
3407 return -ENOENT;
3408 free_hash_entry(hash, entry);
3409 return 0;
3410 }
3411
3412 return add_hash_entry(hash, ip);
3413}
3414
3415static int
3416ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3417 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003418{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003419 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003420 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003421 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003422
Steven Rostedt936e0742011-05-05 22:54:01 -04003423 /* All global ops uses the global ops filters */
3424 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3425 ops = &global_ops;
3426
Steven Rostedt41c52c02008-05-22 11:46:33 -04003427 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003428 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003429
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003430 mutex_lock(&ops->regex_lock);
3431
Steven Rostedtf45948e2011-05-02 12:29:25 -04003432 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003433 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003434 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003435 orig_hash = &ops->notrace_hash;
3436
3437 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003438 if (!hash) {
3439 ret = -ENOMEM;
3440 goto out_regex_unlock;
3441 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003442
Steven Rostedt41c52c02008-05-22 11:46:33 -04003443 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003444 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003445 if (buf && !ftrace_match_records(hash, buf, len)) {
3446 ret = -EINVAL;
3447 goto out_regex_unlock;
3448 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003449 if (ip) {
3450 ret = ftrace_match_addr(hash, ip, remove);
3451 if (ret < 0)
3452 goto out_regex_unlock;
3453 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003454
3455 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003456 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003457 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3458 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003459 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003460
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003461 mutex_unlock(&ftrace_lock);
3462
Jiri Olsaac483c42012-01-02 10:04:14 +01003463 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003464 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003465
3466 free_ftrace_hash(hash);
3467 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003468}
3469
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003470static int
3471ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3472 int reset, int enable)
3473{
3474 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3475}
3476
3477/**
3478 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3479 * @ops - the ops to set the filter with
3480 * @ip - the address to add to or remove from the filter.
3481 * @remove - non zero to remove the ip from the filter
3482 * @reset - non zero to reset all filters before applying this filter.
3483 *
3484 * Filters denote which functions should be enabled when tracing is enabled
3485 * If @ip is NULL, it failes to update filter.
3486 */
3487int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3488 int remove, int reset)
3489{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003490 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003491 return ftrace_set_addr(ops, ip, remove, reset, 1);
3492}
3493EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3494
3495static int
3496ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3497 int reset, int enable)
3498{
3499 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3500}
3501
Steven Rostedt77a2b372008-05-12 21:20:45 +02003502/**
3503 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003504 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003505 * @buf - the string that holds the function filter text.
3506 * @len - the length of the string.
3507 * @reset - non zero to reset all filters before applying this filter.
3508 *
3509 * Filters denote which functions should be enabled when tracing is enabled.
3510 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3511 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003512int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003513 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003514{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003515 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003516 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003517}
Steven Rostedt936e0742011-05-05 22:54:01 -04003518EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003519
Steven Rostedt41c52c02008-05-22 11:46:33 -04003520/**
3521 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003522 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003523 * @buf - the string that holds the function notrace text.
3524 * @len - the length of the string.
3525 * @reset - non zero to reset all filters before applying this filter.
3526 *
3527 * Notrace Filters denote which functions should not be enabled when tracing
3528 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3529 * for tracing.
3530 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003531int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003532 int len, int reset)
3533{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003534 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003535 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003536}
3537EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3538/**
3539 * ftrace_set_filter - set a function to filter on in ftrace
3540 * @ops - the ops to set the filter with
3541 * @buf - the string that holds the function filter text.
3542 * @len - the length of the string.
3543 * @reset - non zero to reset all filters before applying this filter.
3544 *
3545 * Filters denote which functions should be enabled when tracing is enabled.
3546 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3547 */
3548void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3549{
3550 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3551}
3552EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3553
3554/**
3555 * ftrace_set_notrace - set a function to not trace in ftrace
3556 * @ops - the ops to set the notrace filter with
3557 * @buf - the string that holds the function notrace text.
3558 * @len - the length of the string.
3559 * @reset - non zero to reset all filters before applying this filter.
3560 *
3561 * Notrace Filters denote which functions should not be enabled when tracing
3562 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3563 * for tracing.
3564 */
3565void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003566{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003567 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003568}
Steven Rostedt936e0742011-05-05 22:54:01 -04003569EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003570
Steven Rostedt2af15d62009-05-28 13:37:24 -04003571/*
3572 * command line interface to allow users to set filters on boot up.
3573 */
3574#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3575static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3576static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3577
3578static int __init set_ftrace_notrace(char *str)
3579{
Chen Gang75761cc2013-04-08 12:12:39 +08003580 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003581 return 1;
3582}
3583__setup("ftrace_notrace=", set_ftrace_notrace);
3584
3585static int __init set_ftrace_filter(char *str)
3586{
Chen Gang75761cc2013-04-08 12:12:39 +08003587 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003588 return 1;
3589}
3590__setup("ftrace_filter=", set_ftrace_filter);
3591
Stefan Assmann369bc182009-10-12 22:17:21 +02003592#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003593static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003594static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3595
Stefan Assmann369bc182009-10-12 22:17:21 +02003596static int __init set_graph_function(char *str)
3597{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003598 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003599 return 1;
3600}
3601__setup("ftrace_graph_filter=", set_graph_function);
3602
3603static void __init set_ftrace_early_graph(char *buf)
3604{
3605 int ret;
3606 char *func;
3607
3608 while (buf) {
3609 func = strsep(&buf, ",");
3610 /* we allow only one expression at a time */
3611 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3612 func);
3613 if (ret)
3614 printk(KERN_DEBUG "ftrace: function %s not "
3615 "traceable\n", func);
3616 }
3617}
3618#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3619
Steven Rostedt2a85a372011-12-19 21:57:44 -05003620void __init
3621ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003622{
3623 char *func;
3624
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003625 ftrace_ops_init(ops);
3626
Steven Rostedt2af15d62009-05-28 13:37:24 -04003627 while (buf) {
3628 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003629 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003630 }
3631}
3632
3633static void __init set_ftrace_early_filters(void)
3634{
3635 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003636 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003637 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003638 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003639#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3640 if (ftrace_graph_buf[0])
3641 set_ftrace_early_graph(ftrace_graph_buf);
3642#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003643}
3644
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003645int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003646{
3647 struct seq_file *m = (struct seq_file *)file->private_data;
3648 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003649 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003650 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003651 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003652 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003653
Steven Rostedt5072c592008-05-12 21:20:43 +02003654 if (file->f_mode & FMODE_READ) {
3655 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003656 seq_release(inode, file);
3657 } else
3658 iter = file->private_data;
3659
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003660 parser = &iter->parser;
3661 if (trace_parser_loaded(parser)) {
3662 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003663 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003664 }
3665
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003666 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003667
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003668 mutex_lock(&iter->ops->regex_lock);
3669
Steven Rostedt058e2972011-04-29 22:35:33 -04003670 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003671 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3672
3673 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003674 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003675 else
3676 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003677
Steven Rostedt058e2972011-04-29 22:35:33 -04003678 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003679 ret = ftrace_hash_move(iter->ops, filter_hash,
3680 orig_hash, iter->hash);
3681 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3682 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003683 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003684
Steven Rostedt058e2972011-04-29 22:35:33 -04003685 mutex_unlock(&ftrace_lock);
3686 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003687
3688 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003689 free_ftrace_hash(iter->hash);
3690 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003691
Steven Rostedt5072c592008-05-12 21:20:43 +02003692 return 0;
3693}
3694
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003695static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003696 .open = ftrace_avail_open,
3697 .read = seq_read,
3698 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003699 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003700};
3701
Steven Rostedt647bcd02011-05-03 14:39:21 -04003702static const struct file_operations ftrace_enabled_fops = {
3703 .open = ftrace_enabled_open,
3704 .read = seq_read,
3705 .llseek = seq_lseek,
3706 .release = seq_release_private,
3707};
3708
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003709static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003710 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003711 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003712 .write = ftrace_filter_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003713 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003714 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003715};
3716
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003717static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003718 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003719 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003720 .write = ftrace_notrace_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003721 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003722 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003723};
3724
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003725#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3726
3727static DEFINE_MUTEX(graph_lock);
3728
3729int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003730int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003731unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3732
3733static void *
Li Zefan85951842009-06-24 09:54:00 +08003734__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003735{
Li Zefan85951842009-06-24 09:54:00 +08003736 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003737 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003738 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003739}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003740
Li Zefan85951842009-06-24 09:54:00 +08003741static void *
3742g_next(struct seq_file *m, void *v, loff_t *pos)
3743{
3744 (*pos)++;
3745 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003746}
3747
3748static void *g_start(struct seq_file *m, loff_t *pos)
3749{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003750 mutex_lock(&graph_lock);
3751
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003752 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003753 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003754 return (void *)1;
3755
Li Zefan85951842009-06-24 09:54:00 +08003756 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003757}
3758
3759static void g_stop(struct seq_file *m, void *p)
3760{
3761 mutex_unlock(&graph_lock);
3762}
3763
3764static int g_show(struct seq_file *m, void *v)
3765{
3766 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003767
3768 if (!ptr)
3769 return 0;
3770
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003771 if (ptr == (unsigned long *)1) {
3772 seq_printf(m, "#### all functions enabled ####\n");
3773 return 0;
3774 }
3775
Steven Rostedtb375a112009-09-17 00:05:58 -04003776 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003777
3778 return 0;
3779}
3780
James Morris88e9d342009-09-22 16:43:43 -07003781static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003782 .start = g_start,
3783 .next = g_next,
3784 .stop = g_stop,
3785 .show = g_show,
3786};
3787
3788static int
3789ftrace_graph_open(struct inode *inode, struct file *file)
3790{
3791 int ret = 0;
3792
3793 if (unlikely(ftrace_disabled))
3794 return -ENODEV;
3795
3796 mutex_lock(&graph_lock);
3797 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003798 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003799 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003800 ftrace_graph_count = 0;
3801 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3802 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003803 mutex_unlock(&graph_lock);
3804
Li Zefana4ec5e02009-09-18 14:06:28 +08003805 if (file->f_mode & FMODE_READ)
3806 ret = seq_open(file, &ftrace_graph_seq_ops);
3807
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003808 return ret;
3809}
3810
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003811static int
Li Zefan87827112009-07-23 11:29:11 +08003812ftrace_graph_release(struct inode *inode, struct file *file)
3813{
3814 if (file->f_mode & FMODE_READ)
3815 seq_release(inode, file);
3816 return 0;
3817}
3818
3819static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003820ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003821{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003822 struct dyn_ftrace *rec;
3823 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003824 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003825 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003826 int type, not;
3827 char *search;
3828 bool exists;
3829 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003830
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003831 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003832 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003833 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3834 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003835
3836 search_len = strlen(search);
3837
Steven Rostedt52baf112009-02-14 01:15:39 -05003838 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003839
3840 if (unlikely(ftrace_disabled)) {
3841 mutex_unlock(&ftrace_lock);
3842 return -ENODEV;
3843 }
3844
Steven Rostedt265c8312009-02-13 12:43:56 -05003845 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003846
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003847 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003848 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003849 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003850 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003851 if (array[i] == rec->ip) {
3852 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003853 break;
3854 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003855 }
3856
3857 if (!not) {
3858 fail = 0;
3859 if (!exists) {
3860 array[(*idx)++] = rec->ip;
3861 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3862 goto out;
3863 }
3864 } else {
3865 if (exists) {
3866 array[i] = array[--(*idx)];
3867 array[*idx] = 0;
3868 fail = 0;
3869 }
3870 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003871 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003872 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003873out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003874 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003875
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003876 if (fail)
3877 return -EINVAL;
3878
Namhyung Kim9f50afc2013-04-11 16:01:38 +09003879 ftrace_graph_filter_enabled = !!(*idx);
3880
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003881 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003882}
3883
3884static ssize_t
3885ftrace_graph_write(struct file *file, const char __user *ubuf,
3886 size_t cnt, loff_t *ppos)
3887{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003888 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003889 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003890
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003891 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003892 return 0;
3893
3894 mutex_lock(&graph_lock);
3895
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003896 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3897 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003898 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003899 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003900
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003901 read = trace_get_user(&parser, ubuf, cnt, ppos);
3902
Li Zefan4ba79782009-09-22 13:52:20 +08003903 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003904 parser.buffer[parser.idx] = 0;
3905
3906 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003907 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003908 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003909 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003910 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003911 }
3912
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003913 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003914
3915out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003916 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003917out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003918 mutex_unlock(&graph_lock);
3919
3920 return ret;
3921}
3922
3923static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003924 .open = ftrace_graph_open,
3925 .read = seq_read,
3926 .write = ftrace_graph_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003927 .llseek = ftrace_filter_lseek,
Li Zefan87827112009-07-23 11:29:11 +08003928 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003929};
3930#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3931
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003932static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003933{
Steven Rostedt5072c592008-05-12 21:20:43 +02003934
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003935 trace_create_file("available_filter_functions", 0444,
3936 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003937
Steven Rostedt647bcd02011-05-03 14:39:21 -04003938 trace_create_file("enabled_functions", 0444,
3939 d_tracer, NULL, &ftrace_enabled_fops);
3940
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003941 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3942 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003943
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003944 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003945 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003946
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003947#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003948 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003949 NULL,
3950 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003951#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3952
Steven Rostedt5072c592008-05-12 21:20:43 +02003953 return 0;
3954}
3955
Steven Rostedt9fd49322012-04-24 22:32:06 -04003956static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003957{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003958 const unsigned long *ipa = a;
3959 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003960
Steven Rostedt9fd49322012-04-24 22:32:06 -04003961 if (*ipa > *ipb)
3962 return 1;
3963 if (*ipa < *ipb)
3964 return -1;
3965 return 0;
3966}
3967
3968static void ftrace_swap_ips(void *a, void *b, int size)
3969{
3970 unsigned long *ipa = a;
3971 unsigned long *ipb = b;
3972 unsigned long t;
3973
3974 t = *ipa;
3975 *ipa = *ipb;
3976 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003977}
3978
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003979static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003980 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003981 unsigned long *end)
3982{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003983 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003984 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003985 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003986 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003987 unsigned long *p;
3988 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003989 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003990 int ret = -ENOMEM;
3991
3992 count = end - start;
3993
3994 if (!count)
3995 return 0;
3996
Steven Rostedt9fd49322012-04-24 22:32:06 -04003997 sort(start, count, sizeof(*start),
3998 ftrace_cmp_ips, ftrace_swap_ips);
3999
Steven Rostedt706c81f2012-04-24 23:45:26 -04004000 start_pg = ftrace_allocate_pages(count);
4001 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004002 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004003
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004004 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004005
Steven Rostedt32082302011-12-16 14:42:37 -05004006 /*
4007 * Core and each module needs their own pages, as
4008 * modules will free them when they are removed.
4009 * Force a new page to be allocated for modules.
4010 */
Steven Rostedta7900872011-12-16 16:23:44 -05004011 if (!mod) {
4012 WARN_ON(ftrace_pages || ftrace_pages_start);
4013 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004014 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004015 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004016 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004017 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004018
Steven Rostedta7900872011-12-16 16:23:44 -05004019 if (WARN_ON(ftrace_pages->next)) {
4020 /* Hmm, we have free pages? */
4021 while (ftrace_pages->next)
4022 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004023 }
Steven Rostedta7900872011-12-16 16:23:44 -05004024
Steven Rostedt706c81f2012-04-24 23:45:26 -04004025 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004026 }
4027
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004028 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004029 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004030 while (p < end) {
4031 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004032 /*
4033 * Some architecture linkers will pad between
4034 * the different mcount_loc sections of different
4035 * object files to satisfy alignments.
4036 * Skip any NULL pointers.
4037 */
4038 if (!addr)
4039 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004040
4041 if (pg->index == pg->size) {
4042 /* We should have allocated enough */
4043 if (WARN_ON(!pg->next))
4044 break;
4045 pg = pg->next;
4046 }
4047
4048 rec = &pg->records[pg->index++];
4049 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004050 }
4051
Steven Rostedt706c81f2012-04-24 23:45:26 -04004052 /* We should have used all pages */
4053 WARN_ON(pg->next);
4054
4055 /* Assign the last page to ftrace_pages */
4056 ftrace_pages = pg;
4057
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004058 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004059 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004060
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004061 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004062 * We only need to disable interrupts on start up
4063 * because we are modifying code that an interrupt
4064 * may execute, and the modification is not atomic.
4065 * But for modules, nothing runs the code we modify
4066 * until we are finished with it, and there's no
4067 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004068 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004069 if (!mod)
4070 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08004071 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004072 if (!mod)
4073 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004074 ret = 0;
4075 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004076 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004077
Steven Rostedta7900872011-12-16 16:23:44 -05004078 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004079}
4080
Steven Rostedt93eb6772009-04-15 13:24:06 -04004081#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004082
4083#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4084
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004085void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004086{
4087 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004088 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004089 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004090 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004091
Steven Rostedt93eb6772009-04-15 13:24:06 -04004092 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004093
4094 if (ftrace_disabled)
4095 goto out_unlock;
4096
Steven Rostedt32082302011-12-16 14:42:37 -05004097 /*
4098 * Each module has its own ftrace_pages, remove
4099 * them from the list.
4100 */
4101 last_pg = &ftrace_pages_start;
4102 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4103 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004104 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004105 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004106 * As core pages are first, the first
4107 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004108 */
Steven Rostedt32082302011-12-16 14:42:37 -05004109 if (WARN_ON(pg == ftrace_pages_start))
4110 goto out_unlock;
4111
4112 /* Check if we are deleting the last page */
4113 if (pg == ftrace_pages)
4114 ftrace_pages = next_to_ftrace_page(last_pg);
4115
4116 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004117 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4118 free_pages((unsigned long)pg->records, order);
4119 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004120 } else
4121 last_pg = &pg->next;
4122 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004123 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004124 mutex_unlock(&ftrace_lock);
4125}
4126
4127static void ftrace_init_module(struct module *mod,
4128 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004129{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004130 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004131 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004132 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004133}
4134
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004135static int ftrace_module_notify_enter(struct notifier_block *self,
4136 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004137{
4138 struct module *mod = data;
4139
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004140 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004141 ftrace_init_module(mod, mod->ftrace_callsites,
4142 mod->ftrace_callsites +
4143 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004144 return 0;
4145}
4146
4147static int ftrace_module_notify_exit(struct notifier_block *self,
4148 unsigned long val, void *data)
4149{
4150 struct module *mod = data;
4151
4152 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004153 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004154
4155 return 0;
4156}
4157#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004158static int ftrace_module_notify_enter(struct notifier_block *self,
4159 unsigned long val, void *data)
4160{
4161 return 0;
4162}
4163static int ftrace_module_notify_exit(struct notifier_block *self,
4164 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004165{
4166 return 0;
4167}
4168#endif /* CONFIG_MODULES */
4169
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004170struct notifier_block ftrace_module_enter_nb = {
4171 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004172 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004173};
4174
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004175struct notifier_block ftrace_module_exit_nb = {
4176 .notifier_call = ftrace_module_notify_exit,
4177 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4178};
4179
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004180extern unsigned long __start_mcount_loc[];
4181extern unsigned long __stop_mcount_loc[];
4182
4183void __init ftrace_init(void)
4184{
4185 unsigned long count, addr, flags;
4186 int ret;
4187
4188 /* Keep the ftrace pointer to the stub */
4189 addr = (unsigned long)ftrace_stub;
4190
4191 local_irq_save(flags);
4192 ftrace_dyn_arch_init(&addr);
4193 local_irq_restore(flags);
4194
4195 /* ftrace_dyn_arch_init places the return code in addr */
4196 if (addr)
4197 goto failed;
4198
4199 count = __stop_mcount_loc - __start_mcount_loc;
4200
4201 ret = ftrace_dyn_table_alloc(count);
4202 if (ret)
4203 goto failed;
4204
4205 last_ftrace_enabled = ftrace_enabled = 1;
4206
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004207 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004208 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004209 __stop_mcount_loc);
4210
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004211 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004212 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004213 pr_warning("Failed to register trace ftrace module enter notifier\n");
4214
4215 ret = register_module_notifier(&ftrace_module_exit_nb);
4216 if (ret)
4217 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004218
Steven Rostedt2af15d62009-05-28 13:37:24 -04004219 set_ftrace_early_filters();
4220
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004221 return;
4222 failed:
4223 ftrace_disabled = 1;
4224}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004225
Steven Rostedt3d083392008-05-12 21:20:42 +02004226#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004227
Steven Rostedt2b499382011-05-03 22:49:52 -04004228static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004229 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004230 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4231 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004232};
4233
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004234static int __init ftrace_nodyn_init(void)
4235{
4236 ftrace_enabled = 1;
4237 return 0;
4238}
Steven Rostedt6f415672012-10-05 12:13:07 -04004239core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004240
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004241static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4242static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004243/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004244# define ftrace_startup(ops, command) \
4245 ({ \
4246 int ___ret = __register_ftrace_function(ops); \
4247 if (!___ret) \
4248 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4249 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004250 })
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004251# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4252
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004253# define ftrace_startup_sysctl() do { } while (0)
4254# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004255
4256static inline int
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04004257ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004258{
4259 return 1;
4260}
4261
Steven Rostedt3d083392008-05-12 21:20:42 +02004262#endif /* CONFIG_DYNAMIC_FTRACE */
4263
Steven Rostedtb8489142011-05-04 09:27:52 -04004264static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004265ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004266 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004267{
Jiri Olsae2484912012-02-15 15:51:48 +01004268 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4269 return;
4270
4271 /*
4272 * Some of the ops may be dynamically allocated,
4273 * they must be freed after a synchronize_sched().
4274 */
4275 preempt_disable_notrace();
4276 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt0a016402012-11-02 17:03:03 -04004277 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004278 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4279 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04004280 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004281 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004282 } while_for_each_ftrace_op(op);
Jiri Olsae2484912012-02-15 15:51:48 +01004283 trace_recursion_clear(TRACE_CONTROL_BIT);
4284 preempt_enable_notrace();
4285}
4286
4287static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004288 .func = ftrace_ops_control_func,
4289 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4290 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004291};
4292
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004293static inline void
4294__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004295 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004296{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004297 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004298 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004299
Steven Rostedtccf36722012-06-05 09:44:25 -04004300 if (function_trace_stop)
4301 return;
4302
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004303 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4304 if (bit < 0)
4305 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004306
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004307 /*
4308 * Some of the ops may be dynamically allocated,
4309 * they must be freed after a synchronize_sched().
4310 */
4311 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004312 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04004313 if (ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004314 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004315 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004316 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004317 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004318}
4319
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004320/*
4321 * Some archs only support passing ip and parent_ip. Even though
4322 * the list function ignores the op parameter, we do not want any
4323 * C side effects, where a function is called without the caller
4324 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004325 * Archs are to support both the regs and ftrace_ops at the same time.
4326 * If they support ftrace_ops, it is assumed they support regs.
4327 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004328 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4329 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004330 * An architecture can pass partial regs with ftrace_ops and still
4331 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004332 */
4333#if ARCH_SUPPORTS_FTRACE_OPS
4334static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004335 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004336{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004337 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004338}
4339#else
4340static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4341{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004342 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004343}
4344#endif
4345
Steven Rostedte32d8952008-12-04 00:26:41 -05004346static void clear_ftrace_swapper(void)
4347{
4348 struct task_struct *p;
4349 int cpu;
4350
4351 get_online_cpus();
4352 for_each_online_cpu(cpu) {
4353 p = idle_task(cpu);
4354 clear_tsk_trace_trace(p);
4355 }
4356 put_online_cpus();
4357}
4358
4359static void set_ftrace_swapper(void)
4360{
4361 struct task_struct *p;
4362 int cpu;
4363
4364 get_online_cpus();
4365 for_each_online_cpu(cpu) {
4366 p = idle_task(cpu);
4367 set_tsk_trace_trace(p);
4368 }
4369 put_online_cpus();
4370}
4371
4372static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004373{
4374 struct task_struct *p;
4375
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004376 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004377 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004378 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004379 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004380 rcu_read_unlock();
4381
Steven Rostedte32d8952008-12-04 00:26:41 -05004382 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004383}
4384
Steven Rostedte32d8952008-12-04 00:26:41 -05004385static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004386{
4387 struct task_struct *p;
4388
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004389 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004390 do_each_pid_task(pid, PIDTYPE_PID, p) {
4391 set_tsk_trace_trace(p);
4392 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004393 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004394}
4395
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004396static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004397{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004398 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004399 clear_ftrace_swapper();
4400 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004401 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004402}
4403
4404static void set_ftrace_pid_task(struct pid *pid)
4405{
4406 if (pid == ftrace_swapper_pid)
4407 set_ftrace_swapper();
4408 else
4409 set_ftrace_pid(pid);
4410}
4411
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004412static int ftrace_pid_add(int p)
4413{
4414 struct pid *pid;
4415 struct ftrace_pid *fpid;
4416 int ret = -EINVAL;
4417
4418 mutex_lock(&ftrace_lock);
4419
4420 if (!p)
4421 pid = ftrace_swapper_pid;
4422 else
4423 pid = find_get_pid(p);
4424
4425 if (!pid)
4426 goto out;
4427
4428 ret = 0;
4429
4430 list_for_each_entry(fpid, &ftrace_pids, list)
4431 if (fpid->pid == pid)
4432 goto out_put;
4433
4434 ret = -ENOMEM;
4435
4436 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4437 if (!fpid)
4438 goto out_put;
4439
4440 list_add(&fpid->list, &ftrace_pids);
4441 fpid->pid = pid;
4442
4443 set_ftrace_pid_task(pid);
4444
4445 ftrace_update_pid_func();
4446 ftrace_startup_enable(0);
4447
4448 mutex_unlock(&ftrace_lock);
4449 return 0;
4450
4451out_put:
4452 if (pid != ftrace_swapper_pid)
4453 put_pid(pid);
4454
4455out:
4456 mutex_unlock(&ftrace_lock);
4457 return ret;
4458}
4459
4460static void ftrace_pid_reset(void)
4461{
4462 struct ftrace_pid *fpid, *safe;
4463
4464 mutex_lock(&ftrace_lock);
4465 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4466 struct pid *pid = fpid->pid;
4467
4468 clear_ftrace_pid_task(pid);
4469
4470 list_del(&fpid->list);
4471 kfree(fpid);
4472 }
4473
4474 ftrace_update_pid_func();
4475 ftrace_startup_enable(0);
4476
4477 mutex_unlock(&ftrace_lock);
4478}
4479
4480static void *fpid_start(struct seq_file *m, loff_t *pos)
4481{
4482 mutex_lock(&ftrace_lock);
4483
4484 if (list_empty(&ftrace_pids) && (!*pos))
4485 return (void *) 1;
4486
4487 return seq_list_start(&ftrace_pids, *pos);
4488}
4489
4490static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4491{
4492 if (v == (void *)1)
4493 return NULL;
4494
4495 return seq_list_next(v, &ftrace_pids, pos);
4496}
4497
4498static void fpid_stop(struct seq_file *m, void *p)
4499{
4500 mutex_unlock(&ftrace_lock);
4501}
4502
4503static int fpid_show(struct seq_file *m, void *v)
4504{
4505 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4506
4507 if (v == (void *)1) {
4508 seq_printf(m, "no pid\n");
4509 return 0;
4510 }
4511
4512 if (fpid->pid == ftrace_swapper_pid)
4513 seq_printf(m, "swapper tasks\n");
4514 else
4515 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4516
4517 return 0;
4518}
4519
4520static const struct seq_operations ftrace_pid_sops = {
4521 .start = fpid_start,
4522 .next = fpid_next,
4523 .stop = fpid_stop,
4524 .show = fpid_show,
4525};
4526
4527static int
4528ftrace_pid_open(struct inode *inode, struct file *file)
4529{
4530 int ret = 0;
4531
4532 if ((file->f_mode & FMODE_WRITE) &&
4533 (file->f_flags & O_TRUNC))
4534 ftrace_pid_reset();
4535
4536 if (file->f_mode & FMODE_READ)
4537 ret = seq_open(file, &ftrace_pid_sops);
4538
4539 return ret;
4540}
4541
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004542static ssize_t
4543ftrace_pid_write(struct file *filp, const char __user *ubuf,
4544 size_t cnt, loff_t *ppos)
4545{
Ingo Molnar457dc922009-11-23 11:03:28 +01004546 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004547 long val;
4548 int ret;
4549
4550 if (cnt >= sizeof(buf))
4551 return -EINVAL;
4552
4553 if (copy_from_user(&buf, ubuf, cnt))
4554 return -EFAULT;
4555
4556 buf[cnt] = 0;
4557
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004558 /*
4559 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4560 * to clean the filter quietly.
4561 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004562 tmp = strstrip(buf);
4563 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004564 return 1;
4565
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004566 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004567 if (ret < 0)
4568 return ret;
4569
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004570 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004571
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004572 return ret ? ret : cnt;
4573}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004574
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004575static int
4576ftrace_pid_release(struct inode *inode, struct file *file)
4577{
4578 if (file->f_mode & FMODE_READ)
4579 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004580
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004581 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004582}
4583
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004584static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004585 .open = ftrace_pid_open,
4586 .write = ftrace_pid_write,
4587 .read = seq_read,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09004588 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004589 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004590};
4591
4592static __init int ftrace_init_debugfs(void)
4593{
4594 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004595
4596 d_tracer = tracing_init_dentry();
4597 if (!d_tracer)
4598 return 0;
4599
4600 ftrace_init_dyn_debugfs(d_tracer);
4601
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004602 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4603 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004604
4605 ftrace_profile_debugfs(d_tracer);
4606
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004607 return 0;
4608}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004609fs_initcall(ftrace_init_debugfs);
4610
Steven Rostedt3d083392008-05-12 21:20:42 +02004611/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004612 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004613 *
4614 * This function should be used by panic code. It stops ftrace
4615 * but in a not so nice way. If you need to simply kill ftrace
4616 * from a non-atomic section, use ftrace_kill.
4617 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004618void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004619{
4620 ftrace_disabled = 1;
4621 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004622 clear_ftrace_function();
4623}
4624
4625/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004626 * Test if ftrace is dead or not.
4627 */
4628int ftrace_is_dead(void)
4629{
4630 return ftrace_disabled;
4631}
4632
4633/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004634 * register_ftrace_function - register a function for profiling
4635 * @ops - ops structure that holds the function for profiling.
4636 *
4637 * Register a function to be called by all functions in the
4638 * kernel.
4639 *
4640 * Note: @ops->func and all the functions it calls must be labeled
4641 * with "notrace", otherwise it will go into a
4642 * recursive loop.
4643 */
4644int register_ftrace_function(struct ftrace_ops *ops)
4645{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004646 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004647
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004648 ftrace_ops_init(ops);
4649
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004650 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004651
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004652 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004653
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004654 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004655
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004656 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004657}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004658EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004659
4660/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004661 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004662 * @ops - ops structure that holds the function to unregister
4663 *
4664 * Unregister a function that was added to be called by ftrace profiling.
4665 */
4666int unregister_ftrace_function(struct ftrace_ops *ops)
4667{
4668 int ret;
4669
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004670 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004671 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004672 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004673
4674 return ret;
4675}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004676EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004677
Ingo Molnare309b412008-05-12 21:20:51 +02004678int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004679ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004680 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004681 loff_t *ppos)
4682{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004683 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004684
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004685 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004686
Steven Rostedt45a4a232011-04-21 23:16:46 -04004687 if (unlikely(ftrace_disabled))
4688 goto out;
4689
4690 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004691
Li Zefana32c7762009-06-26 16:55:51 +08004692 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004693 goto out;
4694
Li Zefana32c7762009-06-26 16:55:51 +08004695 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004696
4697 if (ftrace_enabled) {
4698
4699 ftrace_startup_sysctl();
4700
4701 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004702 if (ftrace_ops_list != &ftrace_list_end)
4703 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004704
4705 } else {
4706 /* stopping ftrace calls (just send to ftrace_stub) */
4707 ftrace_trace_function = ftrace_stub;
4708
4709 ftrace_shutdown_sysctl();
4710 }
4711
4712 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004713 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004714 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004715}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004716
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004717#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004718
Steven Rostedt597af812009-04-03 15:24:12 -04004719static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004720static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004721
Steven Rostedte49dc192008-12-02 23:50:05 -05004722int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4723{
4724 return 0;
4725}
4726
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004727/* The callbacks that hook a function */
4728trace_func_graph_ret_t ftrace_graph_return =
4729 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004730trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004731
4732/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4733static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4734{
4735 int i;
4736 int ret = 0;
4737 unsigned long flags;
4738 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4739 struct task_struct *g, *t;
4740
4741 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4742 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4743 * sizeof(struct ftrace_ret_stack),
4744 GFP_KERNEL);
4745 if (!ret_stack_list[i]) {
4746 start = 0;
4747 end = i;
4748 ret = -ENOMEM;
4749 goto free;
4750 }
4751 }
4752
4753 read_lock_irqsave(&tasklist_lock, flags);
4754 do_each_thread(g, t) {
4755 if (start == end) {
4756 ret = -EAGAIN;
4757 goto unlock;
4758 }
4759
4760 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004761 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004762 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004763 t->curr_ret_stack = -1;
4764 /* Make sure the tasks see the -1 first: */
4765 smp_wmb();
4766 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004767 }
4768 } while_each_thread(g, t);
4769
4770unlock:
4771 read_unlock_irqrestore(&tasklist_lock, flags);
4772free:
4773 for (i = start; i < end; i++)
4774 kfree(ret_stack_list[i]);
4775 return ret;
4776}
4777
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004778static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004779ftrace_graph_probe_sched_switch(void *ignore,
4780 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004781{
4782 unsigned long long timestamp;
4783 int index;
4784
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004785 /*
4786 * Does the user want to count the time a function was asleep.
4787 * If so, do not update the time stamps.
4788 */
4789 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4790 return;
4791
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004792 timestamp = trace_clock_local();
4793
4794 prev->ftrace_timestamp = timestamp;
4795
4796 /* only process tasks that we timestamped */
4797 if (!next->ftrace_timestamp)
4798 return;
4799
4800 /*
4801 * Update all the counters in next to make up for the
4802 * time next was sleeping.
4803 */
4804 timestamp -= next->ftrace_timestamp;
4805
4806 for (index = next->curr_ret_stack; index >= 0; index--)
4807 next->ret_stack[index].calltime += timestamp;
4808}
4809
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004810/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004811static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004812{
4813 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004814 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004815
4816 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4817 sizeof(struct ftrace_ret_stack *),
4818 GFP_KERNEL);
4819
4820 if (!ret_stack_list)
4821 return -ENOMEM;
4822
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004823 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004824 for_each_online_cpu(cpu) {
4825 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004826 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004827 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004828
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004829 do {
4830 ret = alloc_retstack_tasklist(ret_stack_list);
4831 } while (ret == -EAGAIN);
4832
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004833 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004834 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004835 if (ret)
4836 pr_info("ftrace_graph: Couldn't activate tracepoint"
4837 " probe to kernel_sched_switch\n");
4838 }
4839
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004840 kfree(ret_stack_list);
4841 return ret;
4842}
4843
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004844/*
4845 * Hibernation protection.
4846 * The state of the current task is too much unstable during
4847 * suspend/restore to disk. We want to protect against that.
4848 */
4849static int
4850ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4851 void *unused)
4852{
4853 switch (state) {
4854 case PM_HIBERNATION_PREPARE:
4855 pause_graph_tracing();
4856 break;
4857
4858 case PM_POST_HIBERNATION:
4859 unpause_graph_tracing();
4860 break;
4861 }
4862 return NOTIFY_DONE;
4863}
4864
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004865/* Just a place holder for function graph */
4866static struct ftrace_ops fgraph_ops __read_mostly = {
4867 .func = ftrace_stub,
4868 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
4869 FTRACE_OPS_FL_RECURSION_SAFE,
4870};
4871
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004872int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4873 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004874{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004875 int ret = 0;
4876
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004877 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004878
Steven Rostedt05ce5812009-03-24 00:18:31 -04004879 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004880 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004881 ret = -EBUSY;
4882 goto out;
4883 }
4884
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004885 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4886 register_pm_notifier(&ftrace_suspend_notifier);
4887
Steven Rostedt597af812009-04-03 15:24:12 -04004888 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004889 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004890 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004891 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004892 goto out;
4893 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004894
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004895 ftrace_graph_return = retfunc;
4896 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004897
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004898 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004899
4900out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004901 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004902 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004903}
4904
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004905void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004906{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004907 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004908
Steven Rostedt597af812009-04-03 15:24:12 -04004909 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004910 goto out;
4911
Steven Rostedt597af812009-04-03 15:24:12 -04004912 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004913 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004914 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004915 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004916 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004917 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004918
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004919 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004920 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004921}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004922
Steven Rostedt868baf02011-02-10 21:26:13 -05004923static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4924
4925static void
4926graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4927{
4928 atomic_set(&t->tracing_graph_pause, 0);
4929 atomic_set(&t->trace_overrun, 0);
4930 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004931 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004932 smp_wmb();
4933 t->ret_stack = ret_stack;
4934}
4935
4936/*
4937 * Allocate a return stack for the idle task. May be the first
4938 * time through, or it may be done by CPU hotplug online.
4939 */
4940void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4941{
4942 t->curr_ret_stack = -1;
4943 /*
4944 * The idle task has no parent, it either has its own
4945 * stack or no stack at all.
4946 */
4947 if (t->ret_stack)
4948 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4949
4950 if (ftrace_graph_active) {
4951 struct ftrace_ret_stack *ret_stack;
4952
4953 ret_stack = per_cpu(idle_ret_stack, cpu);
4954 if (!ret_stack) {
4955 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4956 * sizeof(struct ftrace_ret_stack),
4957 GFP_KERNEL);
4958 if (!ret_stack)
4959 return;
4960 per_cpu(idle_ret_stack, cpu) = ret_stack;
4961 }
4962 graph_init_task(t, ret_stack);
4963 }
4964}
4965
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004966/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004967void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004968{
Steven Rostedt84047e32009-06-02 16:51:55 -04004969 /* Make sure we do not use the parent ret_stack */
4970 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004971 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004972
Steven Rostedt597af812009-04-03 15:24:12 -04004973 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004974 struct ftrace_ret_stack *ret_stack;
4975
4976 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004977 * sizeof(struct ftrace_ret_stack),
4978 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004979 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004980 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004981 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004982 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004983}
4984
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004985void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004986{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004987 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4988
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004989 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004990 /* NULL must become visible to IRQs before we free it: */
4991 barrier();
4992
4993 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004994}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004995
4996void ftrace_graph_stop(void)
4997{
4998 ftrace_stop();
4999}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005000#endif