blob: 0e9f9eaade2f6a2dd0e729cd2d3bb38b4f6f8ec0 [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 Rostedt6912896e2008-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 Rostedt6912896e2008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt6912896e2008-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 Rostedt6912896e2008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt6912896e2008-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
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400413static void ftrace_sync(struct work_struct *work)
414{
415 /*
416 * This function is just a stub to implement a hard force
417 * of synchronize_sched(). This requires synchronizing
418 * tasks even in userspace and idle.
419 *
420 * Yes, function tracing is rude.
421 */
422}
423
Ingo Molnare309b412008-05-12 21:20:51 +0200424static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200425{
Steven Rostedt2b499382011-05-03 22:49:52 -0400426 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200427
Steven Rostedtb8489142011-05-04 09:27:52 -0400428 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
429 return -EBUSY;
430
Steven Rostedt2b499382011-05-03 22:49:52 -0400431 if (FTRACE_WARN_ON(ops == &global_ops))
432 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200433
Steven Rostedtb8489142011-05-04 09:27:52 -0400434 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100435 ret = remove_ftrace_list_ops(&ftrace_global_list,
436 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400437 if (!ret)
438 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100439 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
440 ret = remove_ftrace_list_ops(&ftrace_control_list,
441 &control_ops, ops);
442 if (!ret) {
443 /*
444 * The ftrace_ops is now removed from the list,
445 * so there'll be no new users. We must ensure
446 * all current users are done before we free
447 * the control data.
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400448 * Note synchronize_sched() is not enough, as we
449 * use preempt_disable() to do RCU, but the function
450 * tracer can be called where RCU is not active
451 * (before user_exit()).
Jiri Olsae2484912012-02-15 15:51:48 +0100452 */
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400453 schedule_on_each_cpu(ftrace_sync);
Jiri Olsae2484912012-02-15 15:51:48 +0100454 control_ops_free(ops);
455 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400456 } else
457 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
458
Steven Rostedt2b499382011-05-03 22:49:52 -0400459 if (ret < 0)
460 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400461
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400462 if (ftrace_enabled)
463 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200464
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400465 /*
466 * Dynamic ops may be freed, we must make sure that all
467 * callers are done before leaving this function.
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400468 *
469 * Again, normal synchronize_sched() is not good enough.
470 * We need to do a hard force of sched synchronization.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400471 */
472 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400473 schedule_on_each_cpu(ftrace_sync);
474
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400475
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500476 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200477}
478
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500479static void ftrace_update_pid_func(void)
480{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400481 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500482 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900483 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500484
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400485 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500486}
487
Steven Rostedt493762f2009-03-23 17:12:36 -0400488#ifdef CONFIG_FUNCTION_PROFILER
489struct ftrace_profile {
490 struct hlist_node node;
491 unsigned long ip;
492 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400493#ifdef CONFIG_FUNCTION_GRAPH_TRACER
494 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400495 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400496#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400497};
498
499struct ftrace_profile_page {
500 struct ftrace_profile_page *next;
501 unsigned long index;
502 struct ftrace_profile records[];
503};
504
Steven Rostedtcafb1682009-03-24 20:50:39 -0400505struct ftrace_profile_stat {
506 atomic_t disabled;
507 struct hlist_head *hash;
508 struct ftrace_profile_page *pages;
509 struct ftrace_profile_page *start;
510 struct tracer_stat stat;
511};
512
Steven Rostedt493762f2009-03-23 17:12:36 -0400513#define PROFILE_RECORDS_SIZE \
514 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
515
516#define PROFILES_PER_PAGE \
517 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
518
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400519static int ftrace_profile_enabled __read_mostly;
520
521/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400522static DEFINE_MUTEX(ftrace_profile_lock);
523
Steven Rostedtcafb1682009-03-24 20:50:39 -0400524static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400525
Namhyung Kim20079eb2013-04-10 08:55:50 +0900526#define FTRACE_PROFILE_HASH_BITS 10
527#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400528
Steven Rostedt493762f2009-03-23 17:12:36 -0400529static void *
530function_stat_next(void *v, int idx)
531{
532 struct ftrace_profile *rec = v;
533 struct ftrace_profile_page *pg;
534
535 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
536
537 again:
Li Zefan0296e422009-06-26 11:15:37 +0800538 if (idx != 0)
539 rec++;
540
Steven Rostedt493762f2009-03-23 17:12:36 -0400541 if ((void *)rec >= (void *)&pg->records[pg->index]) {
542 pg = pg->next;
543 if (!pg)
544 return NULL;
545 rec = &pg->records[0];
546 if (!rec->counter)
547 goto again;
548 }
549
550 return rec;
551}
552
553static void *function_stat_start(struct tracer_stat *trace)
554{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400555 struct ftrace_profile_stat *stat =
556 container_of(trace, struct ftrace_profile_stat, stat);
557
558 if (!stat || !stat->start)
559 return NULL;
560
561 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400562}
563
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400564#ifdef CONFIG_FUNCTION_GRAPH_TRACER
565/* function graph compares on total time */
566static int function_stat_cmp(void *p1, void *p2)
567{
568 struct ftrace_profile *a = p1;
569 struct ftrace_profile *b = p2;
570
571 if (a->time < b->time)
572 return -1;
573 if (a->time > b->time)
574 return 1;
575 else
576 return 0;
577}
578#else
579/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400580static int function_stat_cmp(void *p1, void *p2)
581{
582 struct ftrace_profile *a = p1;
583 struct ftrace_profile *b = p2;
584
585 if (a->counter < b->counter)
586 return -1;
587 if (a->counter > b->counter)
588 return 1;
589 else
590 return 0;
591}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400592#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400593
594static int function_stat_headers(struct seq_file *m)
595{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400596#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400597 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400598 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400599 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400600 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400601#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400602 seq_printf(m, " Function Hit\n"
603 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400604#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400605 return 0;
606}
607
608static int function_stat_show(struct seq_file *m, void *v)
609{
610 struct ftrace_profile *rec = v;
611 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800612 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400613#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400614 static struct trace_seq s;
615 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400616 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400617#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800618 mutex_lock(&ftrace_profile_lock);
619
620 /* we raced with function_profile_reset() */
621 if (unlikely(rec->counter == 0)) {
622 ret = -EBUSY;
623 goto out;
624 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400625
626 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400627 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400628
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400629#ifdef CONFIG_FUNCTION_GRAPH_TRACER
630 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400631 avg = rec->time;
632 do_div(avg, rec->counter);
633
Chase Douglase330b3b2010-04-26 14:02:05 -0400634 /* Sample standard deviation (s^2) */
635 if (rec->counter <= 1)
636 stddev = 0;
637 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200638 /*
639 * Apply Welford's method:
640 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
641 */
642 stddev = rec->counter * rec->time_squared -
643 rec->time * rec->time;
644
Chase Douglase330b3b2010-04-26 14:02:05 -0400645 /*
646 * Divide only 1000 for ns^2 -> us^2 conversion.
647 * trace_print_graph_duration will divide 1000 again.
648 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200649 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400650 }
651
Steven Rostedt34886c82009-03-25 21:00:47 -0400652 trace_seq_init(&s);
653 trace_print_graph_duration(rec->time, &s);
654 trace_seq_puts(&s, " ");
655 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400656 trace_seq_puts(&s, " ");
657 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400658 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400659#endif
660 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800661out:
662 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400663
Li Zefan3aaba202010-08-23 16:50:12 +0800664 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400665}
666
Steven Rostedtcafb1682009-03-24 20:50:39 -0400667static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400668{
669 struct ftrace_profile_page *pg;
670
Steven Rostedtcafb1682009-03-24 20:50:39 -0400671 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400672
673 while (pg) {
674 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
675 pg->index = 0;
676 pg = pg->next;
677 }
678
Steven Rostedtcafb1682009-03-24 20:50:39 -0400679 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400680 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
681}
682
Steven Rostedtcafb1682009-03-24 20:50:39 -0400683int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400684{
685 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400686 int functions;
687 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400688 int i;
689
690 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400691 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400692 return 0;
693
Steven Rostedtcafb1682009-03-24 20:50:39 -0400694 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
695 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400696 return -ENOMEM;
697
Steven Rostedt318e0a72009-03-25 20:06:34 -0400698#ifdef CONFIG_DYNAMIC_FTRACE
699 functions = ftrace_update_tot_cnt;
700#else
701 /*
702 * We do not know the number of functions that exist because
703 * dynamic tracing is what counts them. With past experience
704 * we have around 20K functions. That should be more than enough.
705 * It is highly unlikely we will execute every function in
706 * the kernel.
707 */
708 functions = 20000;
709#endif
710
Steven Rostedtcafb1682009-03-24 20:50:39 -0400711 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400712
Steven Rostedt318e0a72009-03-25 20:06:34 -0400713 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
714
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900715 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400716 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400717 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400718 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400719 pg = pg->next;
720 }
721
722 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400723
724 out_free:
725 pg = stat->start;
726 while (pg) {
727 unsigned long tmp = (unsigned long)pg;
728
729 pg = pg->next;
730 free_page(tmp);
731 }
732
Steven Rostedt318e0a72009-03-25 20:06:34 -0400733 stat->pages = NULL;
734 stat->start = NULL;
735
736 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400737}
738
Steven Rostedtcafb1682009-03-24 20:50:39 -0400739static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400740{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400742 int size;
743
Steven Rostedtcafb1682009-03-24 20:50:39 -0400744 stat = &per_cpu(ftrace_profile_stats, cpu);
745
746 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400747 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 return 0;
750 }
751
752 /*
753 * We are profiling all functions, but usually only a few thousand
754 * functions are hit. We'll make a hash of 1024 items.
755 */
756 size = FTRACE_PROFILE_HASH_SIZE;
757
Steven Rostedtcafb1682009-03-24 20:50:39 -0400758 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400759
Steven Rostedtcafb1682009-03-24 20:50:39 -0400760 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400761 return -ENOMEM;
762
Steven Rostedt318e0a72009-03-25 20:06:34 -0400763 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400764 if (ftrace_profile_pages_init(stat) < 0) {
765 kfree(stat->hash);
766 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400767 return -ENOMEM;
768 }
769
770 return 0;
771}
772
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773static int ftrace_profile_init(void)
774{
775 int cpu;
776 int ret = 0;
777
778 for_each_online_cpu(cpu) {
779 ret = ftrace_profile_init_cpu(cpu);
780 if (ret)
781 break;
782 }
783
784 return ret;
785}
786
Steven Rostedt493762f2009-03-23 17:12:36 -0400787/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400788static struct ftrace_profile *
789ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400790{
791 struct ftrace_profile *rec;
792 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400793 unsigned long key;
794
Namhyung Kim20079eb2013-04-10 08:55:50 +0900795 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400796 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400797
798 if (hlist_empty(hhd))
799 return NULL;
800
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400801 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400802 if (rec->ip == ip)
803 return rec;
804 }
805
806 return NULL;
807}
808
Steven Rostedtcafb1682009-03-24 20:50:39 -0400809static void ftrace_add_profile(struct ftrace_profile_stat *stat,
810 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400811{
812 unsigned long key;
813
Namhyung Kim20079eb2013-04-10 08:55:50 +0900814 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400815 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400816}
817
Steven Rostedt318e0a72009-03-25 20:06:34 -0400818/*
819 * The memory is already allocated, this simply finds a new record to use.
820 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400821static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400822ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400823{
824 struct ftrace_profile *rec = NULL;
825
Steven Rostedt318e0a72009-03-25 20:06:34 -0400826 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400827 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400828 goto out;
829
Steven Rostedt493762f2009-03-23 17:12:36 -0400830 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400831 * Try to find the function again since an NMI
832 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400833 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400834 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400835 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400836 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400837
Steven Rostedtcafb1682009-03-24 20:50:39 -0400838 if (stat->pages->index == PROFILES_PER_PAGE) {
839 if (!stat->pages->next)
840 goto out;
841 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400842 }
843
Steven Rostedtcafb1682009-03-24 20:50:39 -0400844 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400845 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400846 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400847
Steven Rostedt493762f2009-03-23 17:12:36 -0400848 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400849 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400850
851 return rec;
852}
853
Steven Rostedt493762f2009-03-23 17:12:36 -0400854static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400855function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400856 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400857{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400858 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400859 struct ftrace_profile *rec;
860 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400861
862 if (!ftrace_profile_enabled)
863 return;
864
Steven Rostedt493762f2009-03-23 17:12:36 -0400865 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400866
867 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400868 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400869 goto out;
870
871 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400872 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400873 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400874 if (!rec)
875 goto out;
876 }
877
878 rec->counter++;
879 out:
880 local_irq_restore(flags);
881}
882
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400883#ifdef CONFIG_FUNCTION_GRAPH_TRACER
884static int profile_graph_entry(struct ftrace_graph_ent *trace)
885{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400886 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400887 return 1;
888}
889
890static void profile_graph_return(struct ftrace_graph_ret *trace)
891{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400892 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400893 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400894 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400895 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400896
897 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400898 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400899 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400900 goto out;
901
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400902 /* If the calltime was zero'd ignore it */
903 if (!trace->calltime)
904 goto out;
905
Steven Rostedta2a16d62009-03-24 23:17:58 -0400906 calltime = trace->rettime - trace->calltime;
907
908 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
909 int index;
910
911 index = trace->depth;
912
913 /* Append this call time to the parent time to subtract */
914 if (index)
915 current->ret_stack[index - 1].subtime += calltime;
916
917 if (current->ret_stack[index].subtime < calltime)
918 calltime -= current->ret_stack[index].subtime;
919 else
920 calltime = 0;
921 }
922
Steven Rostedtcafb1682009-03-24 20:50:39 -0400923 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400924 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400925 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400926 rec->time_squared += calltime * calltime;
927 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400928
Steven Rostedtcafb1682009-03-24 20:50:39 -0400929 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400930 local_irq_restore(flags);
931}
932
933static int register_ftrace_profiler(void)
934{
935 return register_ftrace_graph(&profile_graph_return,
936 &profile_graph_entry);
937}
938
939static void unregister_ftrace_profiler(void)
940{
941 unregister_ftrace_graph();
942}
943#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100944static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400945 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900946 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
947 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400948};
949
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400950static int register_ftrace_profiler(void)
951{
952 return register_ftrace_function(&ftrace_profile_ops);
953}
954
955static void unregister_ftrace_profiler(void)
956{
957 unregister_ftrace_function(&ftrace_profile_ops);
958}
959#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
960
Steven Rostedt493762f2009-03-23 17:12:36 -0400961static ssize_t
962ftrace_profile_write(struct file *filp, const char __user *ubuf,
963 size_t cnt, loff_t *ppos)
964{
965 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400966 int ret;
967
Peter Huewe22fe9b52011-06-07 21:58:27 +0200968 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
969 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400970 return ret;
971
972 val = !!val;
973
974 mutex_lock(&ftrace_profile_lock);
975 if (ftrace_profile_enabled ^ val) {
976 if (val) {
977 ret = ftrace_profile_init();
978 if (ret < 0) {
979 cnt = ret;
980 goto out;
981 }
982
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400983 ret = register_ftrace_profiler();
984 if (ret < 0) {
985 cnt = ret;
986 goto out;
987 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400988 ftrace_profile_enabled = 1;
989 } else {
990 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400991 /*
992 * unregister_ftrace_profiler calls stop_machine
993 * so this acts like an synchronize_sched.
994 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400995 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400996 }
997 }
998 out:
999 mutex_unlock(&ftrace_profile_lock);
1000
Jiri Olsacf8517c2009-10-23 19:36:16 -04001001 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001002
1003 return cnt;
1004}
1005
1006static ssize_t
1007ftrace_profile_read(struct file *filp, char __user *ubuf,
1008 size_t cnt, loff_t *ppos)
1009{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001010 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001011 int r;
1012
1013 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1014 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1015}
1016
1017static const struct file_operations ftrace_profile_fops = {
1018 .open = tracing_open_generic,
1019 .read = ftrace_profile_read,
1020 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001021 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001022};
1023
Steven Rostedtcafb1682009-03-24 20:50:39 -04001024/* used to initialize the real stat files */
1025static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001026 .name = "functions",
1027 .stat_start = function_stat_start,
1028 .stat_next = function_stat_next,
1029 .stat_cmp = function_stat_cmp,
1030 .stat_headers = function_stat_headers,
1031 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001032};
1033
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001034static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001035{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001036 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001037 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001038 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001039 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001040 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001041
Steven Rostedtcafb1682009-03-24 20:50:39 -04001042 for_each_possible_cpu(cpu) {
1043 stat = &per_cpu(ftrace_profile_stats, cpu);
1044
1045 /* allocate enough for function name + cpu number */
1046 name = kmalloc(32, GFP_KERNEL);
1047 if (!name) {
1048 /*
1049 * The files created are permanent, if something happens
1050 * we still do not free memory.
1051 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001052 WARN(1,
1053 "Could not allocate stat file for cpu %d\n",
1054 cpu);
1055 return;
1056 }
1057 stat->stat = function_stats;
1058 snprintf(name, 32, "function%d", cpu);
1059 stat->stat.name = name;
1060 ret = register_stat_tracer(&stat->stat);
1061 if (ret) {
1062 WARN(1,
1063 "Could not register function stat for cpu %d\n",
1064 cpu);
1065 kfree(name);
1066 return;
1067 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001068 }
1069
1070 entry = debugfs_create_file("function_profile_enabled", 0644,
1071 d_tracer, NULL, &ftrace_profile_fops);
1072 if (!entry)
1073 pr_warning("Could not create debugfs "
1074 "'function_profile_enabled' entry\n");
1075}
1076
1077#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001078static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001079{
1080}
1081#endif /* CONFIG_FUNCTION_PROFILER */
1082
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001083static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1084
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04001085loff_t
1086ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1087{
1088 loff_t ret;
1089
1090 if (file->f_mode & FMODE_READ)
1091 ret = seq_lseek(file, offset, whence);
1092 else
1093 file->f_pos = ret = 1;
1094
1095 return ret;
1096}
1097
Steven Rostedt3d083392008-05-12 21:20:42 +02001098#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001099
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001100#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001101# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001102#endif
1103
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001104static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1105
Steven Rostedtb6887d72009-02-17 12:32:04 -05001106struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001107 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001108 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001109 unsigned long flags;
1110 unsigned long ip;
1111 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001112 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001113};
1114
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001115struct ftrace_func_entry {
1116 struct hlist_node hlist;
1117 unsigned long ip;
1118};
1119
1120struct ftrace_hash {
1121 unsigned long size_bits;
1122 struct hlist_head *buckets;
1123 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001124 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001125};
1126
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001127/*
1128 * We make these constant because no one should touch them,
1129 * but they are used as the default "empty hash", to avoid allocating
1130 * it all the time. These are in a read only section such that if
1131 * anyone does try to modify it, it will cause an exception.
1132 */
1133static const struct hlist_head empty_buckets[1];
1134static const struct ftrace_hash empty_hash = {
1135 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001136};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001137#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001138
Steven Rostedt2b499382011-05-03 22:49:52 -04001139static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001140 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001141 .notrace_hash = EMPTY_HASH,
1142 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001143 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1144 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001145};
1146
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001147struct ftrace_page {
1148 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001149 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001150 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001151 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001152};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001153
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001154static struct ftrace_page *ftrace_new_pgs;
1155
Steven Rostedta7900872011-12-16 16:23:44 -05001156#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1157#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001158
1159/* estimate from running different kernels */
1160#define NR_TO_INIT 10000
1161
1162static struct ftrace_page *ftrace_pages_start;
1163static struct ftrace_page *ftrace_pages;
1164
Steven Rostedt06a51d92011-12-19 19:07:36 -05001165static bool ftrace_hash_empty(struct ftrace_hash *hash)
1166{
1167 return !hash || !hash->count;
1168}
1169
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001170static struct ftrace_func_entry *
1171ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1172{
1173 unsigned long key;
1174 struct ftrace_func_entry *entry;
1175 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001176
Steven Rostedt06a51d92011-12-19 19:07:36 -05001177 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001178 return NULL;
1179
1180 if (hash->size_bits > 0)
1181 key = hash_long(ip, hash->size_bits);
1182 else
1183 key = 0;
1184
1185 hhd = &hash->buckets[key];
1186
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001187 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001188 if (entry->ip == ip)
1189 return entry;
1190 }
1191 return NULL;
1192}
1193
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001194static void __add_hash_entry(struct ftrace_hash *hash,
1195 struct ftrace_func_entry *entry)
1196{
1197 struct hlist_head *hhd;
1198 unsigned long key;
1199
1200 if (hash->size_bits)
1201 key = hash_long(entry->ip, hash->size_bits);
1202 else
1203 key = 0;
1204
1205 hhd = &hash->buckets[key];
1206 hlist_add_head(&entry->hlist, hhd);
1207 hash->count++;
1208}
1209
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001210static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1211{
1212 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001213
1214 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1215 if (!entry)
1216 return -ENOMEM;
1217
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001218 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001219 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001220
1221 return 0;
1222}
1223
1224static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001225free_hash_entry(struct ftrace_hash *hash,
1226 struct ftrace_func_entry *entry)
1227{
1228 hlist_del(&entry->hlist);
1229 kfree(entry);
1230 hash->count--;
1231}
1232
1233static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001234remove_hash_entry(struct ftrace_hash *hash,
1235 struct ftrace_func_entry *entry)
1236{
1237 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001238 hash->count--;
1239}
1240
1241static void ftrace_hash_clear(struct ftrace_hash *hash)
1242{
1243 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001244 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001245 struct ftrace_func_entry *entry;
1246 int size = 1 << hash->size_bits;
1247 int i;
1248
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001249 if (!hash->count)
1250 return;
1251
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001252 for (i = 0; i < size; i++) {
1253 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001254 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001255 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001256 }
1257 FTRACE_WARN_ON(hash->count);
1258}
1259
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001260static void free_ftrace_hash(struct ftrace_hash *hash)
1261{
1262 if (!hash || hash == EMPTY_HASH)
1263 return;
1264 ftrace_hash_clear(hash);
1265 kfree(hash->buckets);
1266 kfree(hash);
1267}
1268
Steven Rostedt07fd5512011-05-05 18:03:47 -04001269static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1270{
1271 struct ftrace_hash *hash;
1272
1273 hash = container_of(rcu, struct ftrace_hash, rcu);
1274 free_ftrace_hash(hash);
1275}
1276
1277static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1278{
1279 if (!hash || hash == EMPTY_HASH)
1280 return;
1281 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1282}
1283
Jiri Olsa5500fa52012-02-15 15:51:54 +01001284void ftrace_free_filter(struct ftrace_ops *ops)
1285{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001286 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001287 free_ftrace_hash(ops->filter_hash);
1288 free_ftrace_hash(ops->notrace_hash);
1289}
1290
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001291static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1292{
1293 struct ftrace_hash *hash;
1294 int size;
1295
1296 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1297 if (!hash)
1298 return NULL;
1299
1300 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001301 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001302
1303 if (!hash->buckets) {
1304 kfree(hash);
1305 return NULL;
1306 }
1307
1308 hash->size_bits = size_bits;
1309
1310 return hash;
1311}
1312
1313static struct ftrace_hash *
1314alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1315{
1316 struct ftrace_func_entry *entry;
1317 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001318 int size;
1319 int ret;
1320 int i;
1321
1322 new_hash = alloc_ftrace_hash(size_bits);
1323 if (!new_hash)
1324 return NULL;
1325
1326 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001327 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328 return new_hash;
1329
1330 size = 1 << hash->size_bits;
1331 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001332 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001333 ret = add_hash_entry(new_hash, entry->ip);
1334 if (ret < 0)
1335 goto free_hash;
1336 }
1337 }
1338
1339 FTRACE_WARN_ON(new_hash->count != hash->count);
1340
1341 return new_hash;
1342
1343 free_hash:
1344 free_ftrace_hash(new_hash);
1345 return NULL;
1346}
1347
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001348static void
1349ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1350static void
1351ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1352
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001353static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001354ftrace_hash_move(struct ftrace_ops *ops, int enable,
1355 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001356{
1357 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001358 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001359 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001360 struct ftrace_hash *old_hash;
1361 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001362 int size = src->count;
1363 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001364 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001365 int i;
1366
1367 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001368 * Remove the current set, update the hash and add
1369 * them back.
1370 */
1371 ftrace_hash_rec_disable(ops, enable);
1372
1373 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001374 * If the new source is empty, just free dst and assign it
1375 * the empty_hash.
1376 */
1377 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001378 free_ftrace_hash_rcu(*dst);
1379 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b982011-11-04 20:32:39 -04001380 /* still need to update the function records */
1381 ret = 0;
1382 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001383 }
1384
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001385 /*
1386 * Make the hash size about 1/2 the # found
1387 */
1388 for (size /= 2; size; size >>= 1)
1389 bits++;
1390
1391 /* Don't allocate too much */
1392 if (bits > FTRACE_HASH_MAX_BITS)
1393 bits = FTRACE_HASH_MAX_BITS;
1394
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001395 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001396 new_hash = alloc_ftrace_hash(bits);
1397 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001398 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001399
1400 size = 1 << src->size_bits;
1401 for (i = 0; i < size; i++) {
1402 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001403 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001404 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001405 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001406 }
1407 }
1408
Steven Rostedt07fd5512011-05-05 18:03:47 -04001409 old_hash = *dst;
1410 rcu_assign_pointer(*dst, new_hash);
1411 free_ftrace_hash_rcu(old_hash);
1412
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001413 ret = 0;
1414 out:
1415 /*
1416 * Enable regardless of ret:
1417 * On success, we enable the new hash.
1418 * On failure, we re-enable the original hash.
1419 */
1420 ftrace_hash_rec_enable(ops, enable);
1421
1422 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001423}
1424
Steven Rostedt265c8312009-02-13 12:43:56 -05001425/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001426 * Test the hashes for this ops to see if we want to call
1427 * the ops->func or not.
1428 *
1429 * It's a match if the ip is in the ops->filter_hash or
1430 * the filter_hash does not exist or is empty,
1431 * AND
1432 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001433 *
1434 * This needs to be called with preemption disabled as
1435 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001436 */
1437static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001438ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001439{
1440 struct ftrace_hash *filter_hash;
1441 struct ftrace_hash *notrace_hash;
1442 int ret;
1443
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001444#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1445 /*
1446 * There's a small race when adding ops that the ftrace handler
1447 * that wants regs, may be called without them. We can not
1448 * allow that handler to be called if regs is NULL.
1449 */
1450 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1451 return 0;
1452#endif
1453
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001454 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1455 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001456
Steven Rostedt06a51d92011-12-19 19:07:36 -05001457 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001458 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001459 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001460 !ftrace_lookup_ip(notrace_hash, ip)))
1461 ret = 1;
1462 else
1463 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001464
1465 return ret;
1466}
1467
1468/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001469 * This is a double for. Do not use 'break' to break out of the loop,
1470 * you must use a goto.
1471 */
1472#define do_for_each_ftrace_rec(pg, rec) \
1473 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1474 int _____i; \
1475 for (_____i = 0; _____i < pg->index; _____i++) { \
1476 rec = &pg->records[_____i];
1477
1478#define while_for_each_ftrace_rec() \
1479 } \
1480 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301481
Steven Rostedt5855fea2011-12-16 19:27:42 -05001482
1483static int ftrace_cmp_recs(const void *a, const void *b)
1484{
Steven Rostedta650e022012-04-25 13:48:13 -04001485 const struct dyn_ftrace *key = a;
1486 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001487
Steven Rostedta650e022012-04-25 13:48:13 -04001488 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001489 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001490 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1491 return 1;
1492 return 0;
1493}
1494
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001495static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001496{
1497 struct ftrace_page *pg;
1498 struct dyn_ftrace *rec;
1499 struct dyn_ftrace key;
1500
1501 key.ip = start;
1502 key.flags = end; /* overload flags, as it is unsigned long */
1503
1504 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1505 if (end < pg->records[0].ip ||
1506 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1507 continue;
1508 rec = bsearch(&key, pg->records, pg->index,
1509 sizeof(struct dyn_ftrace),
1510 ftrace_cmp_recs);
1511 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001512 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001513 }
1514
Steven Rostedt5855fea2011-12-16 19:27:42 -05001515 return 0;
1516}
1517
Steven Rostedtc88fd862011-08-16 09:53:39 -04001518/**
1519 * ftrace_location - return true if the ip giving is a traced location
1520 * @ip: the instruction pointer to check
1521 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001522 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001523 * That is, the instruction that is either a NOP or call to
1524 * the function tracer. It checks the ftrace internal tables to
1525 * determine if the address belongs or not.
1526 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001527unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001528{
Steven Rostedta650e022012-04-25 13:48:13 -04001529 return ftrace_location_range(ip, ip);
1530}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001531
Steven Rostedta650e022012-04-25 13:48:13 -04001532/**
1533 * ftrace_text_reserved - return true if range contains an ftrace location
1534 * @start: start of range to search
1535 * @end: end of range to search (inclusive). @end points to the last byte to check.
1536 *
1537 * Returns 1 if @start and @end contains a ftrace location.
1538 * That is, the instruction that is either a NOP or call to
1539 * the function tracer. It checks the ftrace internal tables to
1540 * determine if the address belongs or not.
1541 */
1542int ftrace_text_reserved(void *start, void *end)
1543{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001544 unsigned long ret;
1545
1546 ret = ftrace_location_range((unsigned long)start,
1547 (unsigned long)end);
1548
1549 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001550}
1551
Steven Rostedted926f92011-05-03 13:25:24 -04001552static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1553 int filter_hash,
1554 bool inc)
1555{
1556 struct ftrace_hash *hash;
1557 struct ftrace_hash *other_hash;
1558 struct ftrace_page *pg;
1559 struct dyn_ftrace *rec;
1560 int count = 0;
1561 int all = 0;
1562
1563 /* Only update if the ops has been registered */
1564 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1565 return;
1566
1567 /*
1568 * In the filter_hash case:
1569 * If the count is zero, we update all records.
1570 * Otherwise we just update the items in the hash.
1571 *
1572 * In the notrace_hash case:
1573 * We enable the update in the hash.
1574 * As disabling notrace means enabling the tracing,
1575 * and enabling notrace means disabling, the inc variable
1576 * gets inversed.
1577 */
1578 if (filter_hash) {
1579 hash = ops->filter_hash;
1580 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001581 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001582 all = 1;
1583 } else {
1584 inc = !inc;
1585 hash = ops->notrace_hash;
1586 other_hash = ops->filter_hash;
1587 /*
1588 * If the notrace hash has no items,
1589 * then there's nothing to do.
1590 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001591 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001592 return;
1593 }
1594
1595 do_for_each_ftrace_rec(pg, rec) {
1596 int in_other_hash = 0;
1597 int in_hash = 0;
1598 int match = 0;
1599
1600 if (all) {
1601 /*
1602 * Only the filter_hash affects all records.
1603 * Update if the record is not in the notrace hash.
1604 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001605 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001606 match = 1;
1607 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001608 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1609 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001610
1611 /*
1612 *
1613 */
1614 if (filter_hash && in_hash && !in_other_hash)
1615 match = 1;
1616 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001617 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001618 match = 1;
1619 }
1620 if (!match)
1621 continue;
1622
1623 if (inc) {
1624 rec->flags++;
1625 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1626 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001627 /*
1628 * If any ops wants regs saved for this function
1629 * then all ops will get saved regs.
1630 */
1631 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1632 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001633 } else {
1634 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1635 return;
1636 rec->flags--;
1637 }
1638 count++;
1639 /* Shortcut, if we handled all records, we are done. */
1640 if (!all && count == hash->count)
1641 return;
1642 } while_for_each_ftrace_rec();
1643}
1644
1645static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1646 int filter_hash)
1647{
1648 __ftrace_hash_rec_update(ops, filter_hash, 0);
1649}
1650
1651static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1652 int filter_hash)
1653{
1654 __ftrace_hash_rec_update(ops, filter_hash, 1);
1655}
1656
Steven Rostedt05736a42008-09-22 14:55:47 -07001657static void print_ip_ins(const char *fmt, unsigned char *p)
1658{
1659 int i;
1660
1661 printk(KERN_CONT "%s", fmt);
1662
1663 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1664 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1665}
1666
Steven Rostedtc88fd862011-08-16 09:53:39 -04001667/**
1668 * ftrace_bug - report and shutdown function tracer
1669 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1670 * @ip: The address that failed
1671 *
1672 * The arch code that enables or disables the function tracing
1673 * can call ftrace_bug() when it has detected a problem in
1674 * modifying the code. @failed should be one of either:
1675 * EFAULT - if the problem happens on reading the @ip address
1676 * EINVAL - if what is read at @ip is not what was expected
1677 * EPERM - if the problem happens on writting to the @ip address
1678 */
1679void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001680{
1681 switch (failed) {
1682 case -EFAULT:
1683 FTRACE_WARN_ON_ONCE(1);
1684 pr_info("ftrace faulted on modifying ");
1685 print_ip_sym(ip);
1686 break;
1687 case -EINVAL:
1688 FTRACE_WARN_ON_ONCE(1);
1689 pr_info("ftrace failed to modify ");
1690 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001691 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001692 printk(KERN_CONT "\n");
1693 break;
1694 case -EPERM:
1695 FTRACE_WARN_ON_ONCE(1);
1696 pr_info("ftrace faulted on writing ");
1697 print_ip_sym(ip);
1698 break;
1699 default:
1700 FTRACE_WARN_ON_ONCE(1);
1701 pr_info("ftrace faulted on unknown error ");
1702 print_ip_sym(ip);
1703 }
1704}
1705
Steven Rostedtc88fd862011-08-16 09:53:39 -04001706static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001707{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001708 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001709
Steven Rostedt982c3502008-11-15 16:31:41 -05001710 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001711 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001712 *
Steven Rostedted926f92011-05-03 13:25:24 -04001713 * If the record has a ref count, then we need to enable it
1714 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001715 *
Steven Rostedted926f92011-05-03 13:25:24 -04001716 * Otherwise we make sure its disabled.
1717 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001718 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001719 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001720 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001721 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001722 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001723
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001724 /*
1725 * If enabling and the REGS flag does not match the REGS_EN, then
1726 * do not ignore this record. Set flags to fail the compare against
1727 * ENABLED.
1728 */
1729 if (flag &&
1730 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1731 flag |= FTRACE_FL_REGS;
1732
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001733 /* If the state of this record hasn't changed, then do nothing */
1734 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001735 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001736
1737 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001738 /* Save off if rec is being enabled (for return value) */
1739 flag ^= rec->flags & FTRACE_FL_ENABLED;
1740
1741 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001742 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001743 if (flag & FTRACE_FL_REGS) {
1744 if (rec->flags & FTRACE_FL_REGS)
1745 rec->flags |= FTRACE_FL_REGS_EN;
1746 else
1747 rec->flags &= ~FTRACE_FL_REGS_EN;
1748 }
1749 }
1750
1751 /*
1752 * If this record is being updated from a nop, then
1753 * return UPDATE_MAKE_CALL.
1754 * Otherwise, if the EN flag is set, then return
1755 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1756 * from the non-save regs, to a save regs function.
1757 * Otherwise,
1758 * return UPDATE_MODIFY_CALL to tell the caller to convert
1759 * from the save regs, to a non-save regs function.
1760 */
1761 if (flag & FTRACE_FL_ENABLED)
1762 return FTRACE_UPDATE_MAKE_CALL;
1763 else if (rec->flags & FTRACE_FL_REGS_EN)
1764 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1765 else
1766 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001767 }
1768
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001769 if (update) {
1770 /* If there's no more users, clear all flags */
1771 if (!(rec->flags & ~FTRACE_FL_MASK))
1772 rec->flags = 0;
1773 else
1774 /* Just disable the record (keep REGS state) */
1775 rec->flags &= ~FTRACE_FL_ENABLED;
1776 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001777
1778 return FTRACE_UPDATE_MAKE_NOP;
1779}
1780
1781/**
1782 * ftrace_update_record, set a record that now is tracing or not
1783 * @rec: the record to update
1784 * @enable: set to 1 if the record is tracing, zero to force disable
1785 *
1786 * The records that represent all functions that can be traced need
1787 * to be updated when tracing has been enabled.
1788 */
1789int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1790{
1791 return ftrace_check_record(rec, enable, 1);
1792}
1793
1794/**
1795 * ftrace_test_record, check if the record has been enabled or not
1796 * @rec: the record to test
1797 * @enable: set to 1 to check if enabled, 0 if it is disabled
1798 *
1799 * The arch code may need to test if a record is already set to
1800 * tracing to determine how to modify the function code that it
1801 * represents.
1802 */
1803int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1804{
1805 return ftrace_check_record(rec, enable, 0);
1806}
1807
1808static int
1809__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1810{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001811 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001812 unsigned long ftrace_addr;
1813 int ret;
1814
Steven Rostedtc88fd862011-08-16 09:53:39 -04001815 ret = ftrace_update_record(rec, enable);
1816
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001817 if (rec->flags & FTRACE_FL_REGS)
1818 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1819 else
1820 ftrace_addr = (unsigned long)FTRACE_ADDR;
1821
Steven Rostedtc88fd862011-08-16 09:53:39 -04001822 switch (ret) {
1823 case FTRACE_UPDATE_IGNORE:
1824 return 0;
1825
1826 case FTRACE_UPDATE_MAKE_CALL:
1827 return ftrace_make_call(rec, ftrace_addr);
1828
1829 case FTRACE_UPDATE_MAKE_NOP:
1830 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001831
1832 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1833 case FTRACE_UPDATE_MODIFY_CALL:
1834 if (rec->flags & FTRACE_FL_REGS)
1835 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1836 else
1837 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1838
1839 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001840 }
1841
1842 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001843}
1844
Steven Rostedte4f5d542012-04-27 09:13:18 -04001845void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001846{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001847 struct dyn_ftrace *rec;
1848 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001849 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001850
Steven Rostedt45a4a232011-04-21 23:16:46 -04001851 if (unlikely(ftrace_disabled))
1852 return;
1853
Steven Rostedt265c8312009-02-13 12:43:56 -05001854 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001855 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001856 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001857 ftrace_bug(failed, rec->ip);
1858 /* Stop processing */
1859 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001860 }
1861 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001862}
1863
Steven Rostedtc88fd862011-08-16 09:53:39 -04001864struct ftrace_rec_iter {
1865 struct ftrace_page *pg;
1866 int index;
1867};
1868
1869/**
1870 * ftrace_rec_iter_start, start up iterating over traced functions
1871 *
1872 * Returns an iterator handle that is used to iterate over all
1873 * the records that represent address locations where functions
1874 * are traced.
1875 *
1876 * May return NULL if no records are available.
1877 */
1878struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1879{
1880 /*
1881 * We only use a single iterator.
1882 * Protected by the ftrace_lock mutex.
1883 */
1884 static struct ftrace_rec_iter ftrace_rec_iter;
1885 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1886
1887 iter->pg = ftrace_pages_start;
1888 iter->index = 0;
1889
1890 /* Could have empty pages */
1891 while (iter->pg && !iter->pg->index)
1892 iter->pg = iter->pg->next;
1893
1894 if (!iter->pg)
1895 return NULL;
1896
1897 return iter;
1898}
1899
1900/**
1901 * ftrace_rec_iter_next, get the next record to process.
1902 * @iter: The handle to the iterator.
1903 *
1904 * Returns the next iterator after the given iterator @iter.
1905 */
1906struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1907{
1908 iter->index++;
1909
1910 if (iter->index >= iter->pg->index) {
1911 iter->pg = iter->pg->next;
1912 iter->index = 0;
1913
1914 /* Could have empty pages */
1915 while (iter->pg && !iter->pg->index)
1916 iter->pg = iter->pg->next;
1917 }
1918
1919 if (!iter->pg)
1920 return NULL;
1921
1922 return iter;
1923}
1924
1925/**
1926 * ftrace_rec_iter_record, get the record at the iterator location
1927 * @iter: The current iterator location
1928 *
1929 * Returns the record that the current @iter is at.
1930 */
1931struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1932{
1933 return &iter->pg->records[iter->index];
1934}
1935
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301936static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001937ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001938{
1939 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001940 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001941
1942 ip = rec->ip;
1943
Steven Rostedt45a4a232011-04-21 23:16:46 -04001944 if (unlikely(ftrace_disabled))
1945 return 0;
1946
Shaohua Li25aac9d2009-01-09 11:29:40 +08001947 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001948 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001949 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301950 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001951 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301952 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001953}
1954
Steven Rostedt000ab692009-02-17 13:35:06 -05001955/*
1956 * archs can override this function if they must do something
1957 * before the modifying code is performed.
1958 */
1959int __weak ftrace_arch_code_modify_prepare(void)
1960{
1961 return 0;
1962}
1963
1964/*
1965 * archs can override this function if they must do something
1966 * after the modifying code is performed.
1967 */
1968int __weak ftrace_arch_code_modify_post_process(void)
1969{
1970 return 0;
1971}
1972
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001973void ftrace_modify_all_code(int command)
1974{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001975 int update = command & FTRACE_UPDATE_TRACE_FUNC;
1976
1977 /*
1978 * If the ftrace_caller calls a ftrace_ops func directly,
1979 * we need to make sure that it only traces functions it
1980 * expects to trace. When doing the switch of functions,
1981 * we need to update to the ftrace_ops_list_func first
1982 * before the transition between old and new calls are set,
1983 * as the ftrace_ops_list_func will check the ops hashes
1984 * to make sure the ops are having the right functions
1985 * traced.
1986 */
1987 if (update)
1988 ftrace_update_ftrace_func(ftrace_ops_list_func);
1989
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001990 if (command & FTRACE_UPDATE_CALLS)
1991 ftrace_replace_code(1);
1992 else if (command & FTRACE_DISABLE_CALLS)
1993 ftrace_replace_code(0);
1994
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001995 if (update && ftrace_trace_function != ftrace_ops_list_func)
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001996 ftrace_update_ftrace_func(ftrace_trace_function);
1997
1998 if (command & FTRACE_START_FUNC_RET)
1999 ftrace_enable_ftrace_graph_caller();
2000 else if (command & FTRACE_STOP_FUNC_RET)
2001 ftrace_disable_ftrace_graph_caller();
2002}
2003
Ingo Molnare309b412008-05-12 21:20:51 +02002004static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002005{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002006 int *command = data;
2007
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002008 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002009
Steven Rostedtc88fd862011-08-16 09:53:39 -04002010 return 0;
2011}
2012
2013/**
2014 * ftrace_run_stop_machine, go back to the stop machine method
2015 * @command: The command to tell ftrace what to do
2016 *
2017 * If an arch needs to fall back to the stop machine method, the
2018 * it can call this function.
2019 */
2020void ftrace_run_stop_machine(int command)
2021{
2022 stop_machine(__ftrace_modify_code, &command, NULL);
2023}
2024
2025/**
2026 * arch_ftrace_update_code, modify the code to trace or not trace
2027 * @command: The command that needs to be done
2028 *
2029 * Archs can override this function if it does not need to
2030 * run stop_machine() to modify code.
2031 */
2032void __weak arch_ftrace_update_code(int command)
2033{
2034 ftrace_run_stop_machine(command);
2035}
2036
2037static void ftrace_run_update_code(int command)
2038{
2039 int ret;
2040
2041 ret = ftrace_arch_code_modify_prepare();
2042 FTRACE_WARN_ON(ret);
2043 if (ret)
2044 return;
2045 /*
2046 * Do not call function tracer while we update the code.
2047 * We are in stop machine.
2048 */
2049 function_trace_stop++;
2050
2051 /*
2052 * By default we use stop_machine() to modify the code.
2053 * But archs can do what ever they want as long as it
2054 * is safe. The stop_machine() is the safest, but also
2055 * produces the most overhead.
2056 */
2057 arch_ftrace_update_code(command);
2058
Steven Rostedt6331c282011-07-13 15:11:02 -04002059 function_trace_stop--;
2060
Steven Rostedt000ab692009-02-17 13:35:06 -05002061 ret = ftrace_arch_code_modify_post_process();
2062 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002063}
2064
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002065static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002066static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002067static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002068
2069static void ftrace_startup_enable(int command)
2070{
2071 if (saved_ftrace_func != ftrace_trace_function) {
2072 saved_ftrace_func = ftrace_trace_function;
2073 command |= FTRACE_UPDATE_TRACE_FUNC;
2074 }
2075
2076 if (!command || !ftrace_enabled)
2077 return;
2078
2079 ftrace_run_update_code(command);
2080}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002081
Steven Rostedta1cd6172011-05-23 15:24:25 -04002082static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002083{
Steven Rostedtb8489142011-05-04 09:27:52 -04002084 bool hash_enable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002085 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002086
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002087 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002088 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002089
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002090 ret = __register_ftrace_function(ops);
2091 if (ret)
2092 return ret;
2093
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002094 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002095 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002096
Steven Rostedtb8489142011-05-04 09:27:52 -04002097 /* ops marked global share the filter hashes */
2098 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2099 ops = &global_ops;
2100 /* Don't update hash if global is already set */
2101 if (global_start_up)
2102 hash_enable = false;
2103 global_start_up++;
2104 }
2105
Steven Rostedted926f92011-05-03 13:25:24 -04002106 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002107 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002108 ftrace_hash_rec_enable(ops, 1);
2109
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002110 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002111
2112 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002113}
2114
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002115static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002116{
Steven Rostedtb8489142011-05-04 09:27:52 -04002117 bool hash_disable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002118 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002119
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002120 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002121 return -ENODEV;
2122
2123 ret = __unregister_ftrace_function(ops);
2124 if (ret)
2125 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002126
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002127 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002128 /*
2129 * Just warn in case of unbalance, no need to kill ftrace, it's not
2130 * critical but the ftrace_call callers may be never nopped again after
2131 * further ftrace uses.
2132 */
2133 WARN_ON_ONCE(ftrace_start_up < 0);
2134
Steven Rostedtb8489142011-05-04 09:27:52 -04002135 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2136 ops = &global_ops;
2137 global_start_up--;
2138 WARN_ON_ONCE(global_start_up < 0);
2139 /* Don't update hash if global still has users */
2140 if (global_start_up) {
2141 WARN_ON_ONCE(!ftrace_start_up);
2142 hash_disable = false;
2143 }
2144 }
2145
2146 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002147 ftrace_hash_rec_disable(ops, 1);
2148
Steven Rostedtb8489142011-05-04 09:27:52 -04002149 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002150 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002151
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002152 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002153
2154 if (saved_ftrace_func != ftrace_trace_function) {
2155 saved_ftrace_func = ftrace_trace_function;
2156 command |= FTRACE_UPDATE_TRACE_FUNC;
2157 }
2158
2159 if (!command || !ftrace_enabled)
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002160 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002161
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002162 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002163 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002164}
2165
Ingo Molnare309b412008-05-12 21:20:51 +02002166static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002167{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002168 if (unlikely(ftrace_disabled))
2169 return;
2170
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002171 /* Force update next time */
2172 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002173 /* ftrace_start_up is true if we want ftrace running */
2174 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002175 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002176}
2177
Ingo Molnare309b412008-05-12 21:20:51 +02002178static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002179{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002180 if (unlikely(ftrace_disabled))
2181 return;
2182
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002183 /* ftrace_start_up is true if ftrace is running */
2184 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002185 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002186}
2187
Steven Rostedt3d083392008-05-12 21:20:42 +02002188static cycle_t ftrace_update_time;
2189static unsigned long ftrace_update_cnt;
2190unsigned long ftrace_update_tot_cnt;
2191
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002192static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002193{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002194 /*
2195 * Filter_hash being empty will default to trace module.
2196 * But notrace hash requires a test of individual module functions.
2197 */
2198 return ftrace_hash_empty(ops->filter_hash) &&
2199 ftrace_hash_empty(ops->notrace_hash);
2200}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002201
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002202/*
2203 * Check if the current ops references the record.
2204 *
2205 * If the ops traces all functions, then it was already accounted for.
2206 * If the ops does not trace the current record function, skip it.
2207 * If the ops ignores the function via notrace filter, skip it.
2208 */
2209static inline bool
2210ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2211{
2212 /* If ops isn't enabled, ignore it */
2213 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2214 return 0;
2215
2216 /* If ops traces all mods, we already accounted for it */
2217 if (ops_traces_mod(ops))
2218 return 0;
2219
2220 /* The function must be in the filter */
2221 if (!ftrace_hash_empty(ops->filter_hash) &&
2222 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2223 return 0;
2224
2225 /* If in notrace hash, we ignore it too */
2226 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2227 return 0;
2228
2229 return 1;
2230}
2231
2232static int referenced_filters(struct dyn_ftrace *rec)
2233{
2234 struct ftrace_ops *ops;
2235 int cnt = 0;
2236
2237 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2238 if (ops_references_rec(ops, rec))
2239 cnt++;
2240 }
2241
2242 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002243}
2244
Steven Rostedt31e88902008-11-14 16:21:19 -08002245static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002246{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002247 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002248 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302249 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002250 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002251 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002252 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002253
2254 /*
2255 * When adding a module, we need to check if tracers are
2256 * currently enabled and if they are set to trace all functions.
2257 * If they are, we need to enable the module functions as well
2258 * as update the reference counts for those function records.
2259 */
2260 if (mod) {
2261 struct ftrace_ops *ops;
2262
2263 for (ops = ftrace_ops_list;
2264 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002265 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2266 if (ops_traces_mod(ops))
2267 ref++;
2268 else
2269 test = true;
2270 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002271 }
2272 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002273
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002274 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002275 ftrace_update_cnt = 0;
2276
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002277 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302278
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002279 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002280 int cnt = ref;
2281
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002282 /* If something went wrong, bail without enabling anything */
2283 if (unlikely(ftrace_disabled))
2284 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002285
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002286 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002287 if (test)
2288 cnt += referenced_filters(p);
2289 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302290
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002291 /*
2292 * Do the initial record conversion from mcount jump
2293 * to the NOP instructions.
2294 */
2295 if (!ftrace_code_disable(mod, p))
2296 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002297
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002298 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002299
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002300 /*
2301 * If the tracing is enabled, go ahead and enable the record.
2302 *
2303 * The reason not to enable the record immediatelly is the
2304 * inherent check of ftrace_make_nop/ftrace_make_call for
2305 * correct previous instructions. Making first the NOP
2306 * conversion puts the module to the correct state, thus
2307 * passing the ftrace_make_call check.
2308 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002309 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002310 int failed = __ftrace_replace_code(p, 1);
2311 if (failed)
2312 ftrace_bug(failed, p->ip);
2313 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002314 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002315 }
2316
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002317 ftrace_new_pgs = NULL;
2318
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002319 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002320 ftrace_update_time = stop - start;
2321 ftrace_update_tot_cnt += ftrace_update_cnt;
2322
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002323 return 0;
2324}
2325
Steven Rostedta7900872011-12-16 16:23:44 -05002326static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002327{
Steven Rostedta7900872011-12-16 16:23:44 -05002328 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002329 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002330
Steven Rostedta7900872011-12-16 16:23:44 -05002331 if (WARN_ON(!count))
2332 return -EINVAL;
2333
2334 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002335
2336 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002337 * We want to fill as much as possible. No more than a page
2338 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002339 */
Steven Rostedta7900872011-12-16 16:23:44 -05002340 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2341 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002342
Steven Rostedta7900872011-12-16 16:23:44 -05002343 again:
2344 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2345
2346 if (!pg->records) {
2347 /* if we can't allocate this size, try something smaller */
2348 if (!order)
2349 return -ENOMEM;
2350 order >>= 1;
2351 goto again;
2352 }
2353
2354 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2355 pg->size = cnt;
2356
2357 if (cnt > count)
2358 cnt = count;
2359
2360 return cnt;
2361}
2362
2363static struct ftrace_page *
2364ftrace_allocate_pages(unsigned long num_to_init)
2365{
2366 struct ftrace_page *start_pg;
2367 struct ftrace_page *pg;
2368 int order;
2369 int cnt;
2370
2371 if (!num_to_init)
2372 return 0;
2373
2374 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2375 if (!pg)
2376 return NULL;
2377
2378 /*
2379 * Try to allocate as much as possible in one continues
2380 * location that fills in all of the space. We want to
2381 * waste as little space as possible.
2382 */
2383 for (;;) {
2384 cnt = ftrace_allocate_records(pg, num_to_init);
2385 if (cnt < 0)
2386 goto free_pages;
2387
2388 num_to_init -= cnt;
2389 if (!num_to_init)
2390 break;
2391
2392 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2393 if (!pg->next)
2394 goto free_pages;
2395
2396 pg = pg->next;
2397 }
2398
2399 return start_pg;
2400
2401 free_pages:
2402 while (start_pg) {
2403 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2404 free_pages((unsigned long)pg->records, order);
2405 start_pg = pg->next;
2406 kfree(pg);
2407 pg = start_pg;
2408 }
2409 pr_info("ftrace: FAILED to allocate memory for functions\n");
2410 return NULL;
2411}
2412
2413static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2414{
2415 int cnt;
2416
2417 if (!num_to_init) {
2418 pr_info("ftrace: No functions to be traced?\n");
2419 return -1;
2420 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002421
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002422 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002423 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002424 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002425
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002426 return 0;
2427}
2428
Steven Rostedt5072c592008-05-12 21:20:43 +02002429#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2430
2431struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002432 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002433 loff_t func_pos;
2434 struct ftrace_page *pg;
2435 struct dyn_ftrace *func;
2436 struct ftrace_func_probe *probe;
2437 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002438 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002439 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002440 int hidx;
2441 int idx;
2442 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002443};
2444
Ingo Molnare309b412008-05-12 21:20:51 +02002445static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002446t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002447{
2448 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002449 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002450 struct hlist_head *hhd;
2451
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002452 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002453 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002454
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002455 if (iter->probe)
2456 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002457 retry:
2458 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2459 return NULL;
2460
2461 hhd = &ftrace_func_hash[iter->hidx];
2462
2463 if (hlist_empty(hhd)) {
2464 iter->hidx++;
2465 hnd = NULL;
2466 goto retry;
2467 }
2468
2469 if (!hnd)
2470 hnd = hhd->first;
2471 else {
2472 hnd = hnd->next;
2473 if (!hnd) {
2474 iter->hidx++;
2475 goto retry;
2476 }
2477 }
2478
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002479 if (WARN_ON_ONCE(!hnd))
2480 return NULL;
2481
2482 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2483
2484 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002485}
2486
2487static void *t_hash_start(struct seq_file *m, loff_t *pos)
2488{
2489 struct ftrace_iterator *iter = m->private;
2490 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002491 loff_t l;
2492
Steven Rostedt69a30832011-12-19 15:21:16 -05002493 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2494 return NULL;
2495
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002496 if (iter->func_pos > *pos)
2497 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002498
Li Zefand82d6242009-06-24 09:54:54 +08002499 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002500 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002501 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002502 if (!p)
2503 break;
2504 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002505 if (!p)
2506 return NULL;
2507
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002508 /* Only set this if we have an item */
2509 iter->flags |= FTRACE_ITER_HASH;
2510
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002511 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002512}
2513
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002514static int
2515t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002516{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002517 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002518
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002519 rec = iter->probe;
2520 if (WARN_ON_ONCE(!rec))
2521 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002522
Steven Rostedt809dcf22009-02-16 23:06:01 -05002523 if (rec->ops->print)
2524 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2525
Steven Rostedtb375a112009-09-17 00:05:58 -04002526 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002527
2528 if (rec->data)
2529 seq_printf(m, ":%p", rec->data);
2530 seq_putc(m, '\n');
2531
2532 return 0;
2533}
2534
2535static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002536t_next(struct seq_file *m, void *v, loff_t *pos)
2537{
2538 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002539 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002540 struct dyn_ftrace *rec = NULL;
2541
Steven Rostedt45a4a232011-04-21 23:16:46 -04002542 if (unlikely(ftrace_disabled))
2543 return NULL;
2544
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002545 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002546 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002547
Steven Rostedt5072c592008-05-12 21:20:43 +02002548 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002549 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002550
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002551 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002552 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002553
Steven Rostedt5072c592008-05-12 21:20:43 +02002554 retry:
2555 if (iter->idx >= iter->pg->index) {
2556 if (iter->pg->next) {
2557 iter->pg = iter->pg->next;
2558 iter->idx = 0;
2559 goto retry;
2560 }
2561 } else {
2562 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05002563 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002564 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05002565
Steven Rostedt41c52c02008-05-22 11:46:33 -04002566 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002567 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2568
2569 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002570 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002571
Steven Rostedt5072c592008-05-12 21:20:43 +02002572 rec = NULL;
2573 goto retry;
2574 }
2575 }
2576
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002577 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002578 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002579
2580 iter->func = rec;
2581
2582 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002583}
2584
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002585static void reset_iter_read(struct ftrace_iterator *iter)
2586{
2587 iter->pos = 0;
2588 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03002589 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002590}
2591
2592static void *t_start(struct seq_file *m, loff_t *pos)
2593{
2594 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002595 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002596 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002597 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002598
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002599 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002600
2601 if (unlikely(ftrace_disabled))
2602 return NULL;
2603
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002604 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002605 * If an lseek was done, then reset and start from beginning.
2606 */
2607 if (*pos < iter->pos)
2608 reset_iter_read(iter);
2609
2610 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002611 * For set_ftrace_filter reading, if we have the filter
2612 * off, we can short cut and just print out that all
2613 * functions are enabled.
2614 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002615 if (iter->flags & FTRACE_ITER_FILTER &&
2616 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002617 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002618 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002619 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002620 /* reset in case of seek/pread */
2621 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002622 return iter;
2623 }
2624
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002625 if (iter->flags & FTRACE_ITER_HASH)
2626 return t_hash_start(m, pos);
2627
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002628 /*
2629 * Unfortunately, we need to restart at ftrace_pages_start
2630 * every time we let go of the ftrace_mutex. This is because
2631 * those pointers can change without the lock.
2632 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002633 iter->pg = ftrace_pages_start;
2634 iter->idx = 0;
2635 for (l = 0; l <= *pos; ) {
2636 p = t_next(m, p, &l);
2637 if (!p)
2638 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002639 }
walimis5821e1b2008-11-15 15:19:06 +08002640
Steven Rostedt69a30832011-12-19 15:21:16 -05002641 if (!p)
2642 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002643
2644 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002645}
2646
2647static void t_stop(struct seq_file *m, void *p)
2648{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002649 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002650}
2651
2652static int t_show(struct seq_file *m, void *v)
2653{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002654 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002655 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002656
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002657 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002658 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002659
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002660 if (iter->flags & FTRACE_ITER_PRINTALL) {
2661 seq_printf(m, "#### all functions enabled ####\n");
2662 return 0;
2663 }
2664
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002665 rec = iter->func;
2666
Steven Rostedt5072c592008-05-12 21:20:43 +02002667 if (!rec)
2668 return 0;
2669
Steven Rostedt647bcd02011-05-03 14:39:21 -04002670 seq_printf(m, "%ps", (void *)rec->ip);
2671 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002672 seq_printf(m, " (%ld)%s",
2673 rec->flags & ~FTRACE_FL_MASK,
2674 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002675 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002676
2677 return 0;
2678}
2679
James Morris88e9d342009-09-22 16:43:43 -07002680static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002681 .start = t_start,
2682 .next = t_next,
2683 .stop = t_stop,
2684 .show = t_show,
2685};
2686
Ingo Molnare309b412008-05-12 21:20:51 +02002687static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002688ftrace_avail_open(struct inode *inode, struct file *file)
2689{
2690 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002691
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002692 if (unlikely(ftrace_disabled))
2693 return -ENODEV;
2694
Jiri Olsa50e18b92012-04-25 10:23:39 +02002695 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2696 if (iter) {
2697 iter->pg = ftrace_pages_start;
2698 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002699 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002700
Jiri Olsa50e18b92012-04-25 10:23:39 +02002701 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002702}
2703
Steven Rostedt647bcd02011-05-03 14:39:21 -04002704static int
2705ftrace_enabled_open(struct inode *inode, struct file *file)
2706{
2707 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002708
2709 if (unlikely(ftrace_disabled))
2710 return -ENODEV;
2711
Jiri Olsa50e18b92012-04-25 10:23:39 +02002712 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2713 if (iter) {
2714 iter->pg = ftrace_pages_start;
2715 iter->flags = FTRACE_ITER_ENABLED;
2716 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002717 }
2718
Jiri Olsa50e18b92012-04-25 10:23:39 +02002719 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002720}
2721
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002722static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002723{
Steven Rostedt52baf112009-02-14 01:15:39 -05002724 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002725 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002726 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002727}
2728
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002729/**
2730 * ftrace_regex_open - initialize function tracer filter files
2731 * @ops: The ftrace_ops that hold the hash filters
2732 * @flag: The type of filter to process
2733 * @inode: The inode, usually passed in to your open routine
2734 * @file: The file, usually passed in to your open routine
2735 *
2736 * ftrace_regex_open() initializes the filter files for the
2737 * @ops. Depending on @flag it may process the filter hash or
2738 * the notrace hash of @ops. With this called from the open
2739 * routine, you can use ftrace_filter_write() for the write
2740 * routine if @flag has FTRACE_ITER_FILTER set, or
2741 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04002742 * ftrace_filter_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002743 * release must call ftrace_regex_release().
2744 */
2745int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002746ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002747 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002748{
2749 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002750 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002751 int ret = 0;
2752
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002753 ftrace_ops_init(ops);
2754
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002755 if (unlikely(ftrace_disabled))
2756 return -ENODEV;
2757
Steven Rostedt5072c592008-05-12 21:20:43 +02002758 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2759 if (!iter)
2760 return -ENOMEM;
2761
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002762 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2763 kfree(iter);
2764 return -ENOMEM;
2765 }
2766
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002767 iter->ops = ops;
2768 iter->flags = flag;
2769
2770 mutex_lock(&ops->regex_lock);
2771
Steven Rostedtf45948e2011-05-02 12:29:25 -04002772 if (flag & FTRACE_ITER_NOTRACE)
2773 hash = ops->notrace_hash;
2774 else
2775 hash = ops->filter_hash;
2776
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002777 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002778 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002779 if (!iter->hash) {
2780 trace_parser_put(&iter->parser);
2781 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002782 ret = -ENOMEM;
2783 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002784 }
2785 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002786
Steven Rostedt5072c592008-05-12 21:20:43 +02002787 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002788 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002789 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002790
2791 if (file->f_mode & FMODE_READ) {
2792 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002793
2794 ret = seq_open(file, &show_ftrace_seq_ops);
2795 if (!ret) {
2796 struct seq_file *m = file->private_data;
2797 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002798 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002799 /* Failed */
2800 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002801 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002802 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002803 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002804 } else
2805 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002806
2807 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002808 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002809
2810 return ret;
2811}
2812
Steven Rostedt41c52c02008-05-22 11:46:33 -04002813static int
2814ftrace_filter_open(struct inode *inode, struct file *file)
2815{
Steven Rostedt69a30832011-12-19 15:21:16 -05002816 return ftrace_regex_open(&global_ops,
2817 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2818 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002819}
2820
2821static int
2822ftrace_notrace_open(struct inode *inode, struct file *file)
2823{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002824 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002825 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002826}
2827
Steven Rostedt64e7c442009-02-13 17:08:48 -05002828static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002829{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002830 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002831 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002832
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002833 switch (type) {
2834 case MATCH_FULL:
2835 if (strcmp(str, regex) == 0)
2836 matched = 1;
2837 break;
2838 case MATCH_FRONT_ONLY:
2839 if (strncmp(str, regex, len) == 0)
2840 matched = 1;
2841 break;
2842 case MATCH_MIDDLE_ONLY:
2843 if (strstr(str, regex))
2844 matched = 1;
2845 break;
2846 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002847 slen = strlen(str);
2848 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002849 matched = 1;
2850 break;
2851 }
2852
2853 return matched;
2854}
2855
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002856static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002857enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002858{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002859 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002860 int ret = 0;
2861
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002862 entry = ftrace_lookup_ip(hash, rec->ip);
2863 if (not) {
2864 /* Do nothing if it doesn't exist */
2865 if (!entry)
2866 return 0;
2867
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002868 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002869 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002870 /* Do nothing if it exists */
2871 if (entry)
2872 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002873
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002874 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002875 }
2876 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002877}
2878
Steven Rostedt64e7c442009-02-13 17:08:48 -05002879static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002880ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2881 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002882{
2883 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002884 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002885
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002886 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2887
2888 if (mod) {
2889 /* module lookup requires matching the module */
2890 if (!modname || strcmp(modname, mod))
2891 return 0;
2892
2893 /* blank search means to match all funcs in the mod */
2894 if (!len)
2895 return 1;
2896 }
2897
Steven Rostedt64e7c442009-02-13 17:08:48 -05002898 return ftrace_match(str, regex, len, type);
2899}
2900
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002901static int
2902match_records(struct ftrace_hash *hash, char *buff,
2903 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002904{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002905 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002906 struct ftrace_page *pg;
2907 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002908 int type = MATCH_FULL;
2909 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002910 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002911 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002912
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002913 if (len) {
2914 type = filter_parse_regex(buff, len, &search, &not);
2915 search_len = strlen(search);
2916 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002917
Steven Rostedt52baf112009-02-14 01:15:39 -05002918 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002919
2920 if (unlikely(ftrace_disabled))
2921 goto out_unlock;
2922
Steven Rostedt265c8312009-02-13 12:43:56 -05002923 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002924 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002925 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002926 if (ret < 0) {
2927 found = ret;
2928 goto out_unlock;
2929 }
Li Zefan311d16d2009-12-08 11:15:11 +08002930 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002931 }
2932 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002933 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002934 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002935
2936 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002937}
2938
Steven Rostedt64e7c442009-02-13 17:08:48 -05002939static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002940ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002941{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002942 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002943}
2944
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002945static int
2946ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002947{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002948 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002949
Steven Rostedt64e7c442009-02-13 17:08:48 -05002950 /* blank or '*' mean the same */
2951 if (strcmp(buff, "*") == 0)
2952 buff[0] = 0;
2953
2954 /* handle the case of 'dont filter this module' */
2955 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2956 buff[0] = 0;
2957 not = 1;
2958 }
2959
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002960 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002961}
2962
Steven Rostedtf6180772009-02-14 00:40:25 -05002963/*
2964 * We register the module command as a template to show others how
2965 * to register the a command as well.
2966 */
2967
2968static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002969ftrace_mod_callback(struct ftrace_hash *hash,
2970 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002971{
2972 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002973 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002974
2975 /*
2976 * cmd == 'mod' because we only registered this func
2977 * for the 'mod' ftrace_func_command.
2978 * But if you register one func with multiple commands,
2979 * you can tell which command was used by the cmd
2980 * parameter.
2981 */
2982
2983 /* we must have a module name */
2984 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002985 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002986
2987 mod = strsep(&param, ":");
2988 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002989 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002990
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002991 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002992 if (!ret)
2993 ret = -EINVAL;
2994 if (ret < 0)
2995 return ret;
2996
2997 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002998}
2999
3000static struct ftrace_func_command ftrace_mod_cmd = {
3001 .name = "mod",
3002 .func = ftrace_mod_callback,
3003};
3004
3005static int __init ftrace_mod_cmd_init(void)
3006{
3007 return register_ftrace_command(&ftrace_mod_cmd);
3008}
Steven Rostedt6f415672012-10-05 12:13:07 -04003009core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003010
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003011static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003012 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003013{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003014 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003015 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003016 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003017
3018 key = hash_long(ip, FTRACE_HASH_BITS);
3019
3020 hhd = &ftrace_func_hash[key];
3021
3022 if (hlist_empty(hhd))
3023 return;
3024
3025 /*
3026 * Disable preemption for these calls to prevent a RCU grace
3027 * period. This syncs the hash iteration and freeing of items
3028 * on the hash. rcu_read_lock is too dangerous here.
3029 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003030 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003031 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003032 if (entry->ip == ip)
3033 entry->ops->func(ip, parent_ip, &entry->data);
3034 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003035 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003036}
3037
Steven Rostedtb6887d72009-02-17 12:32:04 -05003038static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003039{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003040 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003041 .flags = FTRACE_OPS_FL_INITIALIZED,
3042 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003043};
3044
Steven Rostedtb6887d72009-02-17 12:32:04 -05003045static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003046
Steven Rostedtb6887d72009-02-17 12:32:04 -05003047static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003048{
Steven Rostedtb8489142011-05-04 09:27:52 -04003049 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003050 int i;
3051
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003052 if (ftrace_probe_registered) {
3053 /* still need to update the function call sites */
3054 if (ftrace_enabled)
3055 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003056 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003057 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003058
3059 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3060 struct hlist_head *hhd = &ftrace_func_hash[i];
3061 if (hhd->first)
3062 break;
3063 }
3064 /* Nothing registered? */
3065 if (i == FTRACE_FUNC_HASHSIZE)
3066 return;
3067
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003068 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003069
Steven Rostedtb6887d72009-02-17 12:32:04 -05003070 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003071}
3072
Steven Rostedtb6887d72009-02-17 12:32:04 -05003073static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003074{
3075 int i;
3076
Steven Rostedtb6887d72009-02-17 12:32:04 -05003077 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003078 return;
3079
3080 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3081 struct hlist_head *hhd = &ftrace_func_hash[i];
3082 if (hhd->first)
3083 return;
3084 }
3085
3086 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003087 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003088
Steven Rostedtb6887d72009-02-17 12:32:04 -05003089 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003090}
3091
3092
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003093static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003094{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003095 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003096 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003097 kfree(entry);
3098}
3099
Steven Rostedt59df055f2009-02-14 15:29:06 -05003100int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003101register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003102 void *data)
3103{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003104 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003105 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3106 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003107 struct ftrace_page *pg;
3108 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003109 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003110 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003111 int count = 0;
3112 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003113 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003114
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003115 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003116 len = strlen(search);
3117
Steven Rostedtb6887d72009-02-17 12:32:04 -05003118 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003119 if (WARN_ON(not))
3120 return -EINVAL;
3121
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003122 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003123
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003124 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3125 if (!hash) {
3126 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003127 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003128 }
3129
3130 if (unlikely(ftrace_disabled)) {
3131 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003132 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003133 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003134
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003135 mutex_lock(&ftrace_lock);
3136
Steven Rostedt59df055f2009-02-14 15:29:06 -05003137 do_for_each_ftrace_rec(pg, rec) {
3138
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003139 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003140 continue;
3141
3142 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3143 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003144 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003145 if (!count)
3146 count = -ENOMEM;
3147 goto out_unlock;
3148 }
3149
3150 count++;
3151
3152 entry->data = data;
3153
3154 /*
3155 * The caller might want to do something special
3156 * for each function we find. We call the callback
3157 * to give the caller an opportunity to do so.
3158 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003159 if (ops->init) {
3160 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003161 /* caller does not like this func */
3162 kfree(entry);
3163 continue;
3164 }
3165 }
3166
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003167 ret = enter_record(hash, rec, 0);
3168 if (ret < 0) {
3169 kfree(entry);
3170 count = ret;
3171 goto out_unlock;
3172 }
3173
Steven Rostedt59df055f2009-02-14 15:29:06 -05003174 entry->ops = ops;
3175 entry->ip = rec->ip;
3176
3177 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3178 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3179
3180 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003181
3182 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3183 if (ret < 0)
3184 count = ret;
3185
Steven Rostedtb6887d72009-02-17 12:32:04 -05003186 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003187
3188 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003189 mutex_unlock(&ftrace_lock);
3190 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003191 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003192 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003193
3194 return count;
3195}
3196
3197enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003198 PROBE_TEST_FUNC = 1,
3199 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003200};
3201
3202static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003203__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003204 void *data, int flags)
3205{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003206 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003207 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003208 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003209 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003210 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003211 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003212 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003213 char str[KSYM_SYMBOL_LEN];
3214 int type = MATCH_FULL;
3215 int i, len = 0;
3216 char *search;
3217
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003218 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003219 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003220 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003221 int not;
3222
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003223 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003224 len = strlen(search);
3225
Steven Rostedtb6887d72009-02-17 12:32:04 -05003226 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003227 if (WARN_ON(not))
3228 return;
3229 }
3230
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003231 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003232
3233 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3234 if (!hash)
3235 /* Hmm, should report this somehow */
3236 goto out_unlock;
3237
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003238 INIT_LIST_HEAD(&free_list);
3239
Steven Rostedt59df055f2009-02-14 15:29:06 -05003240 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3241 struct hlist_head *hhd = &ftrace_func_hash[i];
3242
Sasha Levinb67bfe02013-02-27 17:06:00 -08003243 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003244
3245 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003246 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003247 continue;
3248
Steven Rostedtb6887d72009-02-17 12:32:04 -05003249 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003250 continue;
3251
3252 /* do this last, since it is the most expensive */
3253 if (glob) {
3254 kallsyms_lookup(entry->ip, NULL, NULL,
3255 NULL, str);
3256 if (!ftrace_match(str, glob, len, type))
3257 continue;
3258 }
3259
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003260 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3261 /* It is possible more than one entry had this ip */
3262 if (rec_entry)
3263 free_hash_entry(hash, rec_entry);
3264
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003265 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003266 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003267 }
3268 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003269 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003270 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003271 /*
3272 * Remove after the disable is called. Otherwise, if the last
3273 * probe is removed, a null hash means *all enabled*.
3274 */
3275 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003276 synchronize_sched();
3277 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3278 list_del(&entry->free_list);
3279 ftrace_free_entry(entry);
3280 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003281 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003282
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003283 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003284 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003285 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003286}
3287
3288void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003289unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003290 void *data)
3291{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003292 __unregister_ftrace_function_probe(glob, ops, data,
3293 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003294}
3295
3296void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003297unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003298{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003299 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003300}
3301
Steven Rostedtb6887d72009-02-17 12:32:04 -05003302void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003303{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003304 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003305}
3306
Steven Rostedtf6180772009-02-14 00:40:25 -05003307static LIST_HEAD(ftrace_commands);
3308static DEFINE_MUTEX(ftrace_cmd_mutex);
3309
Tom Zanussi38de93a2013-10-24 08:34:18 -05003310/*
3311 * Currently we only register ftrace commands from __init, so mark this
3312 * __init too.
3313 */
3314__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003315{
3316 struct ftrace_func_command *p;
3317 int ret = 0;
3318
3319 mutex_lock(&ftrace_cmd_mutex);
3320 list_for_each_entry(p, &ftrace_commands, list) {
3321 if (strcmp(cmd->name, p->name) == 0) {
3322 ret = -EBUSY;
3323 goto out_unlock;
3324 }
3325 }
3326 list_add(&cmd->list, &ftrace_commands);
3327 out_unlock:
3328 mutex_unlock(&ftrace_cmd_mutex);
3329
3330 return ret;
3331}
3332
Tom Zanussi38de93a2013-10-24 08:34:18 -05003333/*
3334 * Currently we only unregister ftrace commands from __init, so mark
3335 * this __init too.
3336 */
3337__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003338{
3339 struct ftrace_func_command *p, *n;
3340 int ret = -ENODEV;
3341
3342 mutex_lock(&ftrace_cmd_mutex);
3343 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3344 if (strcmp(cmd->name, p->name) == 0) {
3345 ret = 0;
3346 list_del_init(&p->list);
3347 goto out_unlock;
3348 }
3349 }
3350 out_unlock:
3351 mutex_unlock(&ftrace_cmd_mutex);
3352
3353 return ret;
3354}
3355
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003356static int ftrace_process_regex(struct ftrace_hash *hash,
3357 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003358{
Steven Rostedtf6180772009-02-14 00:40:25 -05003359 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003360 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003361 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003362
3363 func = strsep(&next, ":");
3364
3365 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003366 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003367 if (!ret)
3368 ret = -EINVAL;
3369 if (ret < 0)
3370 return ret;
3371 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003372 }
3373
Steven Rostedtf6180772009-02-14 00:40:25 -05003374 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003375
3376 command = strsep(&next, ":");
3377
Steven Rostedtf6180772009-02-14 00:40:25 -05003378 mutex_lock(&ftrace_cmd_mutex);
3379 list_for_each_entry(p, &ftrace_commands, list) {
3380 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003381 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003382 goto out_unlock;
3383 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003384 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003385 out_unlock:
3386 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003387
Steven Rostedtf6180772009-02-14 00:40:25 -05003388 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003389}
3390
Ingo Molnare309b412008-05-12 21:20:51 +02003391static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003392ftrace_regex_write(struct file *file, const char __user *ubuf,
3393 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003394{
3395 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003396 struct trace_parser *parser;
3397 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003398
Li Zefan4ba79782009-09-22 13:52:20 +08003399 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003400 return 0;
3401
Steven Rostedt5072c592008-05-12 21:20:43 +02003402 if (file->f_mode & FMODE_READ) {
3403 struct seq_file *m = file->private_data;
3404 iter = m->private;
3405 } else
3406 iter = file->private_data;
3407
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003408 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003409 return -ENODEV;
3410
3411 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003412
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003413 parser = &iter->parser;
3414 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003415
Li Zefan4ba79782009-09-22 13:52:20 +08003416 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003417 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003418 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003419 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003420 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003421 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003422 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003423 }
3424
Steven Rostedt5072c592008-05-12 21:20:43 +02003425 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003426 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003427 return ret;
3428}
3429
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003430ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003431ftrace_filter_write(struct file *file, const char __user *ubuf,
3432 size_t cnt, loff_t *ppos)
3433{
3434 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3435}
3436
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003437ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003438ftrace_notrace_write(struct file *file, const char __user *ubuf,
3439 size_t cnt, loff_t *ppos)
3440{
3441 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3442}
3443
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003444static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003445ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3446{
3447 struct ftrace_func_entry *entry;
3448
3449 if (!ftrace_location(ip))
3450 return -EINVAL;
3451
3452 if (remove) {
3453 entry = ftrace_lookup_ip(hash, ip);
3454 if (!entry)
3455 return -ENOENT;
3456 free_hash_entry(hash, entry);
3457 return 0;
3458 }
3459
3460 return add_hash_entry(hash, ip);
3461}
3462
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003463static void ftrace_ops_update_code(struct ftrace_ops *ops)
3464{
3465 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3466 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3467}
3468
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003469static int
3470ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3471 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003472{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003473 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003474 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003475 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003476
Steven Rostedt936e0742011-05-05 22:54:01 -04003477 /* All global ops uses the global ops filters */
3478 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3479 ops = &global_ops;
3480
Steven Rostedt41c52c02008-05-22 11:46:33 -04003481 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003482 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003483
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003484 mutex_lock(&ops->regex_lock);
3485
Steven Rostedtf45948e2011-05-02 12:29:25 -04003486 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003487 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003488 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003489 orig_hash = &ops->notrace_hash;
3490
3491 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003492 if (!hash) {
3493 ret = -ENOMEM;
3494 goto out_regex_unlock;
3495 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003496
Steven Rostedt41c52c02008-05-22 11:46:33 -04003497 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003498 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003499 if (buf && !ftrace_match_records(hash, buf, len)) {
3500 ret = -EINVAL;
3501 goto out_regex_unlock;
3502 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003503 if (ip) {
3504 ret = ftrace_match_addr(hash, ip, remove);
3505 if (ret < 0)
3506 goto out_regex_unlock;
3507 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003508
3509 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003510 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003511 if (!ret)
3512 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003513
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003514 mutex_unlock(&ftrace_lock);
3515
Jiri Olsaac483c42012-01-02 10:04:14 +01003516 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003517 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003518
3519 free_ftrace_hash(hash);
3520 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003521}
3522
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003523static int
3524ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3525 int reset, int enable)
3526{
3527 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3528}
3529
3530/**
3531 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3532 * @ops - the ops to set the filter with
3533 * @ip - the address to add to or remove from the filter.
3534 * @remove - non zero to remove the ip from the filter
3535 * @reset - non zero to reset all filters before applying this filter.
3536 *
3537 * Filters denote which functions should be enabled when tracing is enabled
3538 * If @ip is NULL, it failes to update filter.
3539 */
3540int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3541 int remove, int reset)
3542{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003543 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003544 return ftrace_set_addr(ops, ip, remove, reset, 1);
3545}
3546EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3547
3548static int
3549ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3550 int reset, int enable)
3551{
3552 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3553}
3554
Steven Rostedt77a2b372008-05-12 21:20:45 +02003555/**
3556 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003557 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003558 * @buf - the string that holds the function filter text.
3559 * @len - the length of the string.
3560 * @reset - non zero to reset all filters before applying this filter.
3561 *
3562 * Filters denote which functions should be enabled when tracing is enabled.
3563 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3564 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003565int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003566 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003567{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003568 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003569 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003570}
Steven Rostedt936e0742011-05-05 22:54:01 -04003571EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003572
Steven Rostedt41c52c02008-05-22 11:46:33 -04003573/**
3574 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003575 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003576 * @buf - the string that holds the function notrace text.
3577 * @len - the length of the string.
3578 * @reset - non zero to reset all filters before applying this filter.
3579 *
3580 * Notrace Filters denote which functions should not be enabled when tracing
3581 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3582 * for tracing.
3583 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003584int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003585 int len, int reset)
3586{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003587 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003588 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003589}
3590EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3591/**
3592 * ftrace_set_filter - set a function to filter on in ftrace
3593 * @ops - the ops to set the filter with
3594 * @buf - the string that holds the function filter text.
3595 * @len - the length of the string.
3596 * @reset - non zero to reset all filters before applying this filter.
3597 *
3598 * Filters denote which functions should be enabled when tracing is enabled.
3599 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3600 */
3601void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3602{
3603 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3604}
3605EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3606
3607/**
3608 * ftrace_set_notrace - set a function to not trace in ftrace
3609 * @ops - the ops to set the notrace filter with
3610 * @buf - the string that holds the function notrace text.
3611 * @len - the length of the string.
3612 * @reset - non zero to reset all filters before applying this filter.
3613 *
3614 * Notrace Filters denote which functions should not be enabled when tracing
3615 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3616 * for tracing.
3617 */
3618void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003619{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003620 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003621}
Steven Rostedt936e0742011-05-05 22:54:01 -04003622EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003623
Steven Rostedt2af15d62009-05-28 13:37:24 -04003624/*
3625 * command line interface to allow users to set filters on boot up.
3626 */
3627#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3628static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3629static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3630
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003631/* Used by function selftest to not test if filter is set */
3632bool ftrace_filter_param __initdata;
3633
Steven Rostedt2af15d62009-05-28 13:37:24 -04003634static int __init set_ftrace_notrace(char *str)
3635{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003636 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003637 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003638 return 1;
3639}
3640__setup("ftrace_notrace=", set_ftrace_notrace);
3641
3642static int __init set_ftrace_filter(char *str)
3643{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003644 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003645 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003646 return 1;
3647}
3648__setup("ftrace_filter=", set_ftrace_filter);
3649
Stefan Assmann369bc182009-10-12 22:17:21 +02003650#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003651static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003652static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003653
Stefan Assmann369bc182009-10-12 22:17:21 +02003654static int __init set_graph_function(char *str)
3655{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003656 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003657 return 1;
3658}
3659__setup("ftrace_graph_filter=", set_graph_function);
3660
3661static void __init set_ftrace_early_graph(char *buf)
3662{
3663 int ret;
3664 char *func;
3665
3666 while (buf) {
3667 func = strsep(&buf, ",");
3668 /* we allow only one expression at a time */
3669 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003670 FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003671 if (ret)
3672 printk(KERN_DEBUG "ftrace: function %s not "
3673 "traceable\n", func);
3674 }
3675}
3676#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3677
Steven Rostedt2a85a372011-12-19 21:57:44 -05003678void __init
3679ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003680{
3681 char *func;
3682
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003683 ftrace_ops_init(ops);
3684
Steven Rostedt2af15d62009-05-28 13:37:24 -04003685 while (buf) {
3686 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003687 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003688 }
3689}
3690
3691static void __init set_ftrace_early_filters(void)
3692{
3693 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003694 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003695 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003696 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003697#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3698 if (ftrace_graph_buf[0])
3699 set_ftrace_early_graph(ftrace_graph_buf);
3700#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003701}
3702
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003703int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003704{
3705 struct seq_file *m = (struct seq_file *)file->private_data;
3706 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003707 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003708 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003709 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003710 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003711
Steven Rostedt5072c592008-05-12 21:20:43 +02003712 if (file->f_mode & FMODE_READ) {
3713 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003714 seq_release(inode, file);
3715 } else
3716 iter = file->private_data;
3717
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003718 parser = &iter->parser;
3719 if (trace_parser_loaded(parser)) {
3720 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003721 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003722 }
3723
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003724 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003725
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003726 mutex_lock(&iter->ops->regex_lock);
3727
Steven Rostedt058e2972011-04-29 22:35:33 -04003728 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003729 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3730
3731 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003732 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003733 else
3734 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003735
Steven Rostedt058e2972011-04-29 22:35:33 -04003736 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003737 ret = ftrace_hash_move(iter->ops, filter_hash,
3738 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003739 if (!ret)
3740 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003741
Steven Rostedt058e2972011-04-29 22:35:33 -04003742 mutex_unlock(&ftrace_lock);
3743 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003744
3745 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003746 free_ftrace_hash(iter->hash);
3747 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003748
Steven Rostedt5072c592008-05-12 21:20:43 +02003749 return 0;
3750}
3751
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003752static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003753 .open = ftrace_avail_open,
3754 .read = seq_read,
3755 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003756 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003757};
3758
Steven Rostedt647bcd02011-05-03 14:39:21 -04003759static const struct file_operations ftrace_enabled_fops = {
3760 .open = ftrace_enabled_open,
3761 .read = seq_read,
3762 .llseek = seq_lseek,
3763 .release = seq_release_private,
3764};
3765
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003766static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003767 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003768 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003769 .write = ftrace_filter_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003770 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003771 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003772};
3773
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003774static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003775 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003776 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003777 .write = ftrace_notrace_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003778 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003779 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003780};
3781
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003782#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3783
3784static DEFINE_MUTEX(graph_lock);
3785
3786int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003787int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003788unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003789unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003790
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003791struct ftrace_graph_data {
3792 unsigned long *table;
3793 size_t size;
3794 int *count;
3795 const struct seq_operations *seq_ops;
3796};
3797
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003798static void *
Li Zefan85951842009-06-24 09:54:00 +08003799__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003800{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003801 struct ftrace_graph_data *fgd = m->private;
3802
3803 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003804 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003805 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003806}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003807
Li Zefan85951842009-06-24 09:54:00 +08003808static void *
3809g_next(struct seq_file *m, void *v, loff_t *pos)
3810{
3811 (*pos)++;
3812 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003813}
3814
3815static void *g_start(struct seq_file *m, loff_t *pos)
3816{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003817 struct ftrace_graph_data *fgd = m->private;
3818
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003819 mutex_lock(&graph_lock);
3820
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003821 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003822 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003823 return (void *)1;
3824
Li Zefan85951842009-06-24 09:54:00 +08003825 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003826}
3827
3828static void g_stop(struct seq_file *m, void *p)
3829{
3830 mutex_unlock(&graph_lock);
3831}
3832
3833static int g_show(struct seq_file *m, void *v)
3834{
3835 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003836
3837 if (!ptr)
3838 return 0;
3839
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003840 if (ptr == (unsigned long *)1) {
3841 seq_printf(m, "#### all functions enabled ####\n");
3842 return 0;
3843 }
3844
Steven Rostedtb375a112009-09-17 00:05:58 -04003845 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003846
3847 return 0;
3848}
3849
James Morris88e9d342009-09-22 16:43:43 -07003850static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003851 .start = g_start,
3852 .next = g_next,
3853 .stop = g_stop,
3854 .show = g_show,
3855};
3856
3857static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003858__ftrace_graph_open(struct inode *inode, struct file *file,
3859 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003860{
3861 int ret = 0;
3862
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003863 mutex_lock(&graph_lock);
3864 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003865 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003866 *fgd->count = 0;
3867 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003868 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003869 mutex_unlock(&graph_lock);
3870
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003871 if (file->f_mode & FMODE_READ) {
3872 ret = seq_open(file, fgd->seq_ops);
3873 if (!ret) {
3874 struct seq_file *m = file->private_data;
3875 m->private = fgd;
3876 }
3877 } else
3878 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08003879
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003880 return ret;
3881}
3882
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003883static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003884ftrace_graph_open(struct inode *inode, struct file *file)
3885{
3886 struct ftrace_graph_data *fgd;
3887
3888 if (unlikely(ftrace_disabled))
3889 return -ENODEV;
3890
3891 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3892 if (fgd == NULL)
3893 return -ENOMEM;
3894
3895 fgd->table = ftrace_graph_funcs;
3896 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3897 fgd->count = &ftrace_graph_count;
3898 fgd->seq_ops = &ftrace_graph_seq_ops;
3899
3900 return __ftrace_graph_open(inode, file, fgd);
3901}
3902
3903static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003904ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3905{
3906 struct ftrace_graph_data *fgd;
3907
3908 if (unlikely(ftrace_disabled))
3909 return -ENODEV;
3910
3911 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3912 if (fgd == NULL)
3913 return -ENOMEM;
3914
3915 fgd->table = ftrace_graph_notrace_funcs;
3916 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3917 fgd->count = &ftrace_graph_notrace_count;
3918 fgd->seq_ops = &ftrace_graph_seq_ops;
3919
3920 return __ftrace_graph_open(inode, file, fgd);
3921}
3922
3923static int
Li Zefan87827112009-07-23 11:29:11 +08003924ftrace_graph_release(struct inode *inode, struct file *file)
3925{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003926 if (file->f_mode & FMODE_READ) {
3927 struct seq_file *m = file->private_data;
3928
3929 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08003930 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003931 } else {
3932 kfree(file->private_data);
3933 }
3934
Li Zefan87827112009-07-23 11:29:11 +08003935 return 0;
3936}
3937
3938static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003939ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003940{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003941 struct dyn_ftrace *rec;
3942 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003943 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003944 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003945 int type, not;
3946 char *search;
3947 bool exists;
3948 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003949
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003950 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003951 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003952 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003953 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003954
3955 search_len = strlen(search);
3956
Steven Rostedt52baf112009-02-14 01:15:39 -05003957 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003958
3959 if (unlikely(ftrace_disabled)) {
3960 mutex_unlock(&ftrace_lock);
3961 return -ENODEV;
3962 }
3963
Steven Rostedt265c8312009-02-13 12:43:56 -05003964 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003965
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003966 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003967 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003968 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003969 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003970 if (array[i] == rec->ip) {
3971 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003972 break;
3973 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003974 }
3975
3976 if (!not) {
3977 fail = 0;
3978 if (!exists) {
3979 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003980 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003981 goto out;
3982 }
3983 } else {
3984 if (exists) {
3985 array[i] = array[--(*idx)];
3986 array[*idx] = 0;
3987 fail = 0;
3988 }
3989 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003990 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003991 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003992out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003993 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003994
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003995 if (fail)
3996 return -EINVAL;
3997
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003998 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003999}
4000
4001static ssize_t
4002ftrace_graph_write(struct file *file, const char __user *ubuf,
4003 size_t cnt, loff_t *ppos)
4004{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004005 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004006 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004007 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004008
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004009 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004010 return 0;
4011
Namhyung Kim6a101082013-10-14 17:24:25 +09004012 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4013 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004014
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004015 read = trace_get_user(&parser, ubuf, cnt, ppos);
4016
Li Zefan4ba79782009-09-22 13:52:20 +08004017 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004018 parser.buffer[parser.idx] = 0;
4019
Namhyung Kim6a101082013-10-14 17:24:25 +09004020 mutex_lock(&graph_lock);
4021
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004022 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004023 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4024 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004025
4026 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004027 }
4028
Namhyung Kim6a101082013-10-14 17:24:25 +09004029 if (!ret)
4030 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004031
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004032 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004033
4034 return ret;
4035}
4036
4037static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004038 .open = ftrace_graph_open,
4039 .read = seq_read,
4040 .write = ftrace_graph_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09004041 .llseek = ftrace_filter_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004042 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004043};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004044
4045static const struct file_operations ftrace_graph_notrace_fops = {
4046 .open = ftrace_graph_notrace_open,
4047 .read = seq_read,
4048 .write = ftrace_graph_write,
4049 .llseek = ftrace_filter_lseek,
4050 .release = ftrace_graph_release,
4051};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004052#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4053
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004054static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004055{
Steven Rostedt5072c592008-05-12 21:20:43 +02004056
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004057 trace_create_file("available_filter_functions", 0444,
4058 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004059
Steven Rostedt647bcd02011-05-03 14:39:21 -04004060 trace_create_file("enabled_functions", 0444,
4061 d_tracer, NULL, &ftrace_enabled_fops);
4062
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004063 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4064 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004065
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004066 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004067 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004068
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004069#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004070 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004071 NULL,
4072 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004073 trace_create_file("set_graph_notrace", 0444, d_tracer,
4074 NULL,
4075 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004076#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4077
Steven Rostedt5072c592008-05-12 21:20:43 +02004078 return 0;
4079}
4080
Steven Rostedt9fd49322012-04-24 22:32:06 -04004081static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004082{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004083 const unsigned long *ipa = a;
4084 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004085
Steven Rostedt9fd49322012-04-24 22:32:06 -04004086 if (*ipa > *ipb)
4087 return 1;
4088 if (*ipa < *ipb)
4089 return -1;
4090 return 0;
4091}
4092
4093static void ftrace_swap_ips(void *a, void *b, int size)
4094{
4095 unsigned long *ipa = a;
4096 unsigned long *ipb = b;
4097 unsigned long t;
4098
4099 t = *ipa;
4100 *ipa = *ipb;
4101 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004102}
4103
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004104static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004105 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004106 unsigned long *end)
4107{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004108 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004109 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004110 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004111 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004112 unsigned long *p;
4113 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004114 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004115 int ret = -ENOMEM;
4116
4117 count = end - start;
4118
4119 if (!count)
4120 return 0;
4121
Steven Rostedt9fd49322012-04-24 22:32:06 -04004122 sort(start, count, sizeof(*start),
4123 ftrace_cmp_ips, ftrace_swap_ips);
4124
Steven Rostedt706c81f2012-04-24 23:45:26 -04004125 start_pg = ftrace_allocate_pages(count);
4126 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004127 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004128
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004129 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004130
Steven Rostedt320823092011-12-16 14:42:37 -05004131 /*
4132 * Core and each module needs their own pages, as
4133 * modules will free them when they are removed.
4134 * Force a new page to be allocated for modules.
4135 */
Steven Rostedta7900872011-12-16 16:23:44 -05004136 if (!mod) {
4137 WARN_ON(ftrace_pages || ftrace_pages_start);
4138 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004139 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004140 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05004141 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004142 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05004143
Steven Rostedta7900872011-12-16 16:23:44 -05004144 if (WARN_ON(ftrace_pages->next)) {
4145 /* Hmm, we have free pages? */
4146 while (ftrace_pages->next)
4147 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05004148 }
Steven Rostedta7900872011-12-16 16:23:44 -05004149
Steven Rostedt706c81f2012-04-24 23:45:26 -04004150 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05004151 }
4152
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004153 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004154 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004155 while (p < end) {
4156 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004157 /*
4158 * Some architecture linkers will pad between
4159 * the different mcount_loc sections of different
4160 * object files to satisfy alignments.
4161 * Skip any NULL pointers.
4162 */
4163 if (!addr)
4164 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004165
4166 if (pg->index == pg->size) {
4167 /* We should have allocated enough */
4168 if (WARN_ON(!pg->next))
4169 break;
4170 pg = pg->next;
4171 }
4172
4173 rec = &pg->records[pg->index++];
4174 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004175 }
4176
Steven Rostedt706c81f2012-04-24 23:45:26 -04004177 /* We should have used all pages */
4178 WARN_ON(pg->next);
4179
4180 /* Assign the last page to ftrace_pages */
4181 ftrace_pages = pg;
4182
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004183 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004184 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004185
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004186 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004187 * We only need to disable interrupts on start up
4188 * because we are modifying code that an interrupt
4189 * may execute, and the modification is not atomic.
4190 * But for modules, nothing runs the code we modify
4191 * until we are finished with it, and there's no
4192 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004193 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004194 if (!mod)
4195 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08004196 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004197 if (!mod)
4198 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004199 ret = 0;
4200 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004201 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004202
Steven Rostedta7900872011-12-16 16:23:44 -05004203 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004204}
4205
Steven Rostedt93eb6772009-04-15 13:24:06 -04004206#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05004207
4208#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4209
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004210void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004211{
4212 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05004213 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004214 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004215 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004216
Steven Rostedt93eb6772009-04-15 13:24:06 -04004217 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004218
4219 if (ftrace_disabled)
4220 goto out_unlock;
4221
Steven Rostedt320823092011-12-16 14:42:37 -05004222 /*
4223 * Each module has its own ftrace_pages, remove
4224 * them from the list.
4225 */
4226 last_pg = &ftrace_pages_start;
4227 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4228 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004229 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004230 /*
Steven Rostedt320823092011-12-16 14:42:37 -05004231 * As core pages are first, the first
4232 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004233 */
Steven Rostedt320823092011-12-16 14:42:37 -05004234 if (WARN_ON(pg == ftrace_pages_start))
4235 goto out_unlock;
4236
4237 /* Check if we are deleting the last page */
4238 if (pg == ftrace_pages)
4239 ftrace_pages = next_to_ftrace_page(last_pg);
4240
4241 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004242 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4243 free_pages((unsigned long)pg->records, order);
4244 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05004245 } else
4246 last_pg = &pg->next;
4247 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004248 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004249 mutex_unlock(&ftrace_lock);
4250}
4251
4252static void ftrace_init_module(struct module *mod,
4253 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004254{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004255 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004256 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004257 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004258}
4259
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004260static int ftrace_module_notify_enter(struct notifier_block *self,
4261 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004262{
4263 struct module *mod = data;
4264
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004265 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004266 ftrace_init_module(mod, mod->ftrace_callsites,
4267 mod->ftrace_callsites +
4268 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004269 return 0;
4270}
4271
4272static int ftrace_module_notify_exit(struct notifier_block *self,
4273 unsigned long val, void *data)
4274{
4275 struct module *mod = data;
4276
4277 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004278 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004279
4280 return 0;
4281}
4282#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004283static int ftrace_module_notify_enter(struct notifier_block *self,
4284 unsigned long val, void *data)
4285{
4286 return 0;
4287}
4288static int ftrace_module_notify_exit(struct notifier_block *self,
4289 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004290{
4291 return 0;
4292}
4293#endif /* CONFIG_MODULES */
4294
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004295struct notifier_block ftrace_module_enter_nb = {
4296 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004297 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004298};
4299
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004300struct notifier_block ftrace_module_exit_nb = {
4301 .notifier_call = ftrace_module_notify_exit,
4302 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4303};
4304
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004305extern unsigned long __start_mcount_loc[];
4306extern unsigned long __stop_mcount_loc[];
4307
4308void __init ftrace_init(void)
4309{
4310 unsigned long count, addr, flags;
4311 int ret;
4312
4313 /* Keep the ftrace pointer to the stub */
4314 addr = (unsigned long)ftrace_stub;
4315
4316 local_irq_save(flags);
4317 ftrace_dyn_arch_init(&addr);
4318 local_irq_restore(flags);
4319
4320 /* ftrace_dyn_arch_init places the return code in addr */
4321 if (addr)
4322 goto failed;
4323
4324 count = __stop_mcount_loc - __start_mcount_loc;
4325
4326 ret = ftrace_dyn_table_alloc(count);
4327 if (ret)
4328 goto failed;
4329
4330 last_ftrace_enabled = ftrace_enabled = 1;
4331
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004332 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004333 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004334 __stop_mcount_loc);
4335
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004336 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004337 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004338 pr_warning("Failed to register trace ftrace module enter notifier\n");
4339
4340 ret = register_module_notifier(&ftrace_module_exit_nb);
4341 if (ret)
4342 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004343
Steven Rostedt2af15d62009-05-28 13:37:24 -04004344 set_ftrace_early_filters();
4345
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004346 return;
4347 failed:
4348 ftrace_disabled = 1;
4349}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004350
Steven Rostedt3d083392008-05-12 21:20:42 +02004351#else
Frederic Weisbecker0b6e4d562008-10-28 20:17:38 +01004352
Steven Rostedt2b499382011-05-03 22:49:52 -04004353static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004354 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004355 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4356 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004357};
4358
Frederic Weisbecker0b6e4d562008-10-28 20:17:38 +01004359static int __init ftrace_nodyn_init(void)
4360{
4361 ftrace_enabled = 1;
4362 return 0;
4363}
Steven Rostedt6f415672012-10-05 12:13:07 -04004364core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d562008-10-28 20:17:38 +01004365
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004366static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4367static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004368/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004369# define ftrace_startup(ops, command) \
4370 ({ \
4371 int ___ret = __register_ftrace_function(ops); \
4372 if (!___ret) \
4373 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4374 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004375 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004376# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4377
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004378# define ftrace_startup_sysctl() do { } while (0)
4379# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004380
4381static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004382ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004383{
4384 return 1;
4385}
4386
Steven Rostedt3d083392008-05-12 21:20:42 +02004387#endif /* CONFIG_DYNAMIC_FTRACE */
4388
Steven Rostedtb8489142011-05-04 09:27:52 -04004389static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004390ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004391 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004392{
Jiri Olsae2484912012-02-15 15:51:48 +01004393 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4394 return;
4395
4396 /*
4397 * Some of the ops may be dynamically allocated,
4398 * they must be freed after a synchronize_sched().
4399 */
4400 preempt_disable_notrace();
4401 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004402
4403 /*
4404 * Control funcs (perf) uses RCU. Only trace if
4405 * RCU is currently active.
4406 */
4407 if (!rcu_is_watching())
4408 goto out;
4409
Steven Rostedt0a016402012-11-02 17:03:03 -04004410 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004411 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4412 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004413 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004414 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004415 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004416 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004417 trace_recursion_clear(TRACE_CONTROL_BIT);
4418 preempt_enable_notrace();
4419}
4420
4421static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004422 .func = ftrace_ops_control_func,
4423 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4424 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004425};
4426
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004427static inline void
4428__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004429 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004430{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004431 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004432 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004433
Steven Rostedtccf36722012-06-05 09:44:25 -04004434 if (function_trace_stop)
4435 return;
4436
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004437 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4438 if (bit < 0)
4439 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004440
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004441 /*
4442 * Some of the ops may be dynamically allocated,
4443 * they must be freed after a synchronize_sched().
4444 */
4445 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004446 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004447 if (ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004448 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004449 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004450 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004451 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004452}
4453
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004454/*
4455 * Some archs only support passing ip and parent_ip. Even though
4456 * the list function ignores the op parameter, we do not want any
4457 * C side effects, where a function is called without the caller
4458 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004459 * Archs are to support both the regs and ftrace_ops at the same time.
4460 * If they support ftrace_ops, it is assumed they support regs.
4461 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004462 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4463 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004464 * An architecture can pass partial regs with ftrace_ops and still
4465 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004466 */
4467#if ARCH_SUPPORTS_FTRACE_OPS
4468static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004469 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004470{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004471 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004472}
4473#else
4474static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4475{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004476 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004477}
4478#endif
4479
Steven Rostedte32d8952008-12-04 00:26:41 -05004480static void clear_ftrace_swapper(void)
4481{
4482 struct task_struct *p;
4483 int cpu;
4484
4485 get_online_cpus();
4486 for_each_online_cpu(cpu) {
4487 p = idle_task(cpu);
4488 clear_tsk_trace_trace(p);
4489 }
4490 put_online_cpus();
4491}
4492
4493static void set_ftrace_swapper(void)
4494{
4495 struct task_struct *p;
4496 int cpu;
4497
4498 get_online_cpus();
4499 for_each_online_cpu(cpu) {
4500 p = idle_task(cpu);
4501 set_tsk_trace_trace(p);
4502 }
4503 put_online_cpus();
4504}
4505
4506static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004507{
4508 struct task_struct *p;
4509
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004510 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004511 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004512 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004513 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004514 rcu_read_unlock();
4515
Steven Rostedte32d8952008-12-04 00:26:41 -05004516 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004517}
4518
Steven Rostedte32d8952008-12-04 00:26:41 -05004519static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004520{
4521 struct task_struct *p;
4522
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004523 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004524 do_each_pid_task(pid, PIDTYPE_PID, p) {
4525 set_tsk_trace_trace(p);
4526 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004527 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004528}
4529
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004530static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004531{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004532 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004533 clear_ftrace_swapper();
4534 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004535 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004536}
4537
4538static void set_ftrace_pid_task(struct pid *pid)
4539{
4540 if (pid == ftrace_swapper_pid)
4541 set_ftrace_swapper();
4542 else
4543 set_ftrace_pid(pid);
4544}
4545
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004546static int ftrace_pid_add(int p)
4547{
4548 struct pid *pid;
4549 struct ftrace_pid *fpid;
4550 int ret = -EINVAL;
4551
4552 mutex_lock(&ftrace_lock);
4553
4554 if (!p)
4555 pid = ftrace_swapper_pid;
4556 else
4557 pid = find_get_pid(p);
4558
4559 if (!pid)
4560 goto out;
4561
4562 ret = 0;
4563
4564 list_for_each_entry(fpid, &ftrace_pids, list)
4565 if (fpid->pid == pid)
4566 goto out_put;
4567
4568 ret = -ENOMEM;
4569
4570 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4571 if (!fpid)
4572 goto out_put;
4573
4574 list_add(&fpid->list, &ftrace_pids);
4575 fpid->pid = pid;
4576
4577 set_ftrace_pid_task(pid);
4578
4579 ftrace_update_pid_func();
4580 ftrace_startup_enable(0);
4581
4582 mutex_unlock(&ftrace_lock);
4583 return 0;
4584
4585out_put:
4586 if (pid != ftrace_swapper_pid)
4587 put_pid(pid);
4588
4589out:
4590 mutex_unlock(&ftrace_lock);
4591 return ret;
4592}
4593
4594static void ftrace_pid_reset(void)
4595{
4596 struct ftrace_pid *fpid, *safe;
4597
4598 mutex_lock(&ftrace_lock);
4599 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4600 struct pid *pid = fpid->pid;
4601
4602 clear_ftrace_pid_task(pid);
4603
4604 list_del(&fpid->list);
4605 kfree(fpid);
4606 }
4607
4608 ftrace_update_pid_func();
4609 ftrace_startup_enable(0);
4610
4611 mutex_unlock(&ftrace_lock);
4612}
4613
4614static void *fpid_start(struct seq_file *m, loff_t *pos)
4615{
4616 mutex_lock(&ftrace_lock);
4617
4618 if (list_empty(&ftrace_pids) && (!*pos))
4619 return (void *) 1;
4620
4621 return seq_list_start(&ftrace_pids, *pos);
4622}
4623
4624static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4625{
4626 if (v == (void *)1)
4627 return NULL;
4628
4629 return seq_list_next(v, &ftrace_pids, pos);
4630}
4631
4632static void fpid_stop(struct seq_file *m, void *p)
4633{
4634 mutex_unlock(&ftrace_lock);
4635}
4636
4637static int fpid_show(struct seq_file *m, void *v)
4638{
4639 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4640
4641 if (v == (void *)1) {
4642 seq_printf(m, "no pid\n");
4643 return 0;
4644 }
4645
4646 if (fpid->pid == ftrace_swapper_pid)
4647 seq_printf(m, "swapper tasks\n");
4648 else
4649 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4650
4651 return 0;
4652}
4653
4654static const struct seq_operations ftrace_pid_sops = {
4655 .start = fpid_start,
4656 .next = fpid_next,
4657 .stop = fpid_stop,
4658 .show = fpid_show,
4659};
4660
4661static int
4662ftrace_pid_open(struct inode *inode, struct file *file)
4663{
4664 int ret = 0;
4665
4666 if ((file->f_mode & FMODE_WRITE) &&
4667 (file->f_flags & O_TRUNC))
4668 ftrace_pid_reset();
4669
4670 if (file->f_mode & FMODE_READ)
4671 ret = seq_open(file, &ftrace_pid_sops);
4672
4673 return ret;
4674}
4675
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004676static ssize_t
4677ftrace_pid_write(struct file *filp, const char __user *ubuf,
4678 size_t cnt, loff_t *ppos)
4679{
Ingo Molnar457dc922009-11-23 11:03:28 +01004680 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004681 long val;
4682 int ret;
4683
4684 if (cnt >= sizeof(buf))
4685 return -EINVAL;
4686
4687 if (copy_from_user(&buf, ubuf, cnt))
4688 return -EFAULT;
4689
4690 buf[cnt] = 0;
4691
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004692 /*
4693 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4694 * to clean the filter quietly.
4695 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004696 tmp = strstrip(buf);
4697 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004698 return 1;
4699
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004700 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004701 if (ret < 0)
4702 return ret;
4703
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004704 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004705
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004706 return ret ? ret : cnt;
4707}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004708
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004709static int
4710ftrace_pid_release(struct inode *inode, struct file *file)
4711{
4712 if (file->f_mode & FMODE_READ)
4713 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004714
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004715 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004716}
4717
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004718static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004719 .open = ftrace_pid_open,
4720 .write = ftrace_pid_write,
4721 .read = seq_read,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09004722 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004723 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004724};
4725
4726static __init int ftrace_init_debugfs(void)
4727{
4728 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004729
4730 d_tracer = tracing_init_dentry();
4731 if (!d_tracer)
4732 return 0;
4733
4734 ftrace_init_dyn_debugfs(d_tracer);
4735
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004736 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4737 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004738
4739 ftrace_profile_debugfs(d_tracer);
4740
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004741 return 0;
4742}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004743fs_initcall(ftrace_init_debugfs);
4744
Steven Rostedt3d083392008-05-12 21:20:42 +02004745/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004746 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004747 *
4748 * This function should be used by panic code. It stops ftrace
4749 * but in a not so nice way. If you need to simply kill ftrace
4750 * from a non-atomic section, use ftrace_kill.
4751 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004752void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004753{
4754 ftrace_disabled = 1;
4755 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004756 clear_ftrace_function();
4757}
4758
4759/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004760 * Test if ftrace is dead or not.
4761 */
4762int ftrace_is_dead(void)
4763{
4764 return ftrace_disabled;
4765}
4766
4767/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004768 * register_ftrace_function - register a function for profiling
4769 * @ops - ops structure that holds the function for profiling.
4770 *
4771 * Register a function to be called by all functions in the
4772 * kernel.
4773 *
4774 * Note: @ops->func and all the functions it calls must be labeled
4775 * with "notrace", otherwise it will go into a
4776 * recursive loop.
4777 */
4778int register_ftrace_function(struct ftrace_ops *ops)
4779{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004780 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004781
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004782 ftrace_ops_init(ops);
4783
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004784 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004785
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004786 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004787
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004788 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004789
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004790 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004791}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004792EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004793
4794/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004795 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004796 * @ops - ops structure that holds the function to unregister
4797 *
4798 * Unregister a function that was added to be called by ftrace profiling.
4799 */
4800int unregister_ftrace_function(struct ftrace_ops *ops)
4801{
4802 int ret;
4803
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004804 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004805 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004806 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004807
4808 return ret;
4809}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004810EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004811
Ingo Molnare309b412008-05-12 21:20:51 +02004812int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004813ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004814 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004815 loff_t *ppos)
4816{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004817 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004818
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004819 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004820
Steven Rostedt45a4a232011-04-21 23:16:46 -04004821 if (unlikely(ftrace_disabled))
4822 goto out;
4823
4824 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004825
Li Zefana32c7762009-06-26 16:55:51 +08004826 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004827 goto out;
4828
Li Zefana32c7762009-06-26 16:55:51 +08004829 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004830
4831 if (ftrace_enabled) {
4832
4833 ftrace_startup_sysctl();
4834
4835 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004836 if (ftrace_ops_list != &ftrace_list_end)
4837 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004838
4839 } else {
4840 /* stopping ftrace calls (just send to ftrace_stub) */
4841 ftrace_trace_function = ftrace_stub;
4842
4843 ftrace_shutdown_sysctl();
4844 }
4845
4846 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004847 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004848 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004849}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004850
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004851#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004852
Steven Rostedt597af812009-04-03 15:24:12 -04004853static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004854static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004855
Steven Rostedte49dc192008-12-02 23:50:05 -05004856int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4857{
4858 return 0;
4859}
4860
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004861/* The callbacks that hook a function */
4862trace_func_graph_ret_t ftrace_graph_return =
4863 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004864trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004865
4866/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4867static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4868{
4869 int i;
4870 int ret = 0;
4871 unsigned long flags;
4872 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4873 struct task_struct *g, *t;
4874
4875 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4876 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4877 * sizeof(struct ftrace_ret_stack),
4878 GFP_KERNEL);
4879 if (!ret_stack_list[i]) {
4880 start = 0;
4881 end = i;
4882 ret = -ENOMEM;
4883 goto free;
4884 }
4885 }
4886
4887 read_lock_irqsave(&tasklist_lock, flags);
4888 do_each_thread(g, t) {
4889 if (start == end) {
4890 ret = -EAGAIN;
4891 goto unlock;
4892 }
4893
4894 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004895 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004896 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004897 t->curr_ret_stack = -1;
4898 /* Make sure the tasks see the -1 first: */
4899 smp_wmb();
4900 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004901 }
4902 } while_each_thread(g, t);
4903
4904unlock:
4905 read_unlock_irqrestore(&tasklist_lock, flags);
4906free:
4907 for (i = start; i < end; i++)
4908 kfree(ret_stack_list[i]);
4909 return ret;
4910}
4911
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004912static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004913ftrace_graph_probe_sched_switch(void *ignore,
4914 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004915{
4916 unsigned long long timestamp;
4917 int index;
4918
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004919 /*
4920 * Does the user want to count the time a function was asleep.
4921 * If so, do not update the time stamps.
4922 */
4923 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4924 return;
4925
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004926 timestamp = trace_clock_local();
4927
4928 prev->ftrace_timestamp = timestamp;
4929
4930 /* only process tasks that we timestamped */
4931 if (!next->ftrace_timestamp)
4932 return;
4933
4934 /*
4935 * Update all the counters in next to make up for the
4936 * time next was sleeping.
4937 */
4938 timestamp -= next->ftrace_timestamp;
4939
4940 for (index = next->curr_ret_stack; index >= 0; index--)
4941 next->ret_stack[index].calltime += timestamp;
4942}
4943
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004944/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004945static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004946{
4947 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004948 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004949
4950 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4951 sizeof(struct ftrace_ret_stack *),
4952 GFP_KERNEL);
4953
4954 if (!ret_stack_list)
4955 return -ENOMEM;
4956
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004957 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004958 for_each_online_cpu(cpu) {
4959 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004960 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004961 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004962
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004963 do {
4964 ret = alloc_retstack_tasklist(ret_stack_list);
4965 } while (ret == -EAGAIN);
4966
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004967 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004968 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004969 if (ret)
4970 pr_info("ftrace_graph: Couldn't activate tracepoint"
4971 " probe to kernel_sched_switch\n");
4972 }
4973
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004974 kfree(ret_stack_list);
4975 return ret;
4976}
4977
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004978/*
4979 * Hibernation protection.
4980 * The state of the current task is too much unstable during
4981 * suspend/restore to disk. We want to protect against that.
4982 */
4983static int
4984ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4985 void *unused)
4986{
4987 switch (state) {
4988 case PM_HIBERNATION_PREPARE:
4989 pause_graph_tracing();
4990 break;
4991
4992 case PM_POST_HIBERNATION:
4993 unpause_graph_tracing();
4994 break;
4995 }
4996 return NOTIFY_DONE;
4997}
4998
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004999/* Just a place holder for function graph */
5000static struct ftrace_ops fgraph_ops __read_mostly = {
5001 .func = ftrace_stub,
5002 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
5003 FTRACE_OPS_FL_RECURSION_SAFE,
5004};
5005
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005006int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5007 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005008{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005009 int ret = 0;
5010
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005011 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005012
Steven Rostedt05ce5812009-03-24 00:18:31 -04005013 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005014 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005015 ret = -EBUSY;
5016 goto out;
5017 }
5018
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005019 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5020 register_pm_notifier(&ftrace_suspend_notifier);
5021
Steven Rostedt597af812009-04-03 15:24:12 -04005022 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005023 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005024 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005025 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005026 goto out;
5027 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005028
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005029 ftrace_graph_return = retfunc;
5030 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05005031
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005032 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005033
5034out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005035 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005036 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005037}
5038
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005039void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005040{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005041 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005042
Steven Rostedt597af812009-04-03 15:24:12 -04005043 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005044 goto out;
5045
Steven Rostedt597af812009-04-03 15:24:12 -04005046 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005047 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005048 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005049 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005050 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005051 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005052
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005053 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005054 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005055}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005056
Steven Rostedt868baf02011-02-10 21:26:13 -05005057static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5058
5059static void
5060graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5061{
5062 atomic_set(&t->tracing_graph_pause, 0);
5063 atomic_set(&t->trace_overrun, 0);
5064 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005065 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005066 smp_wmb();
5067 t->ret_stack = ret_stack;
5068}
5069
5070/*
5071 * Allocate a return stack for the idle task. May be the first
5072 * time through, or it may be done by CPU hotplug online.
5073 */
5074void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5075{
5076 t->curr_ret_stack = -1;
5077 /*
5078 * The idle task has no parent, it either has its own
5079 * stack or no stack at all.
5080 */
5081 if (t->ret_stack)
5082 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5083
5084 if (ftrace_graph_active) {
5085 struct ftrace_ret_stack *ret_stack;
5086
5087 ret_stack = per_cpu(idle_ret_stack, cpu);
5088 if (!ret_stack) {
5089 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5090 * sizeof(struct ftrace_ret_stack),
5091 GFP_KERNEL);
5092 if (!ret_stack)
5093 return;
5094 per_cpu(idle_ret_stack, cpu) = ret_stack;
5095 }
5096 graph_init_task(t, ret_stack);
5097 }
5098}
5099
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005100/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005101void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005102{
Steven Rostedt84047e32009-06-02 16:51:55 -04005103 /* Make sure we do not use the parent ret_stack */
5104 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005105 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005106
Steven Rostedt597af812009-04-03 15:24:12 -04005107 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005108 struct ftrace_ret_stack *ret_stack;
5109
5110 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005111 * sizeof(struct ftrace_ret_stack),
5112 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005113 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005114 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005115 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005116 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005117}
5118
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005119void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005120{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005121 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5122
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005123 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005124 /* NULL must become visible to IRQs before we free it: */
5125 barrier();
5126
5127 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005128}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005129
5130void ftrace_graph_stop(void)
5131{
5132 ftrace_stop();
5133}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005134#endif