blob: 4b93b841225248e692e0dd9ed7c9796a1f7cb1e0 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Jiri Olsae2484912012-02-15 15:51:48 +010065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
68#define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
70#else
71#define INIT_REGEX_LOCK(opsname)
72#endif
73
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040074static struct ftrace_ops ftrace_list_end __read_mostly = {
75 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040076 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077};
78
Steven Rostedt4eebcc82008-05-12 21:20:48 +020079/* ftrace_enabled is a method to turn ftrace on or off */
80int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020081static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020082
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050083/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040084int function_trace_stop __read_mostly;
85
86/* Current function tracing op */
87struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedta0d0a2a2014-02-07 14:41:17 -050088/* What to set function_trace_op to */
89static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050090
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040091/* List for set_ftrace_pid's pids. */
92LIST_HEAD(ftrace_pids);
93struct ftrace_pid {
94 struct list_head list;
95 struct pid *pid;
96};
97
Steven Rostedt4eebcc82008-05-12 21:20:48 +020098/*
99 * ftrace_disabled is set when an anomaly is discovered.
100 * ftrace_disabled is much stronger than ftrace_enabled.
101 */
102static int ftrace_disabled __read_mostly;
103
Steven Rostedt52baf112009-02-14 01:15:39 -0500104static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200105
Steven Rostedtb8489142011-05-04 09:27:52 -0400106static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
Jiri Olsae2484912012-02-15 15:51:48 +0100107static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400108static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200109ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500110ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400111static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100112static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200113
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400114#if ARCH_SUPPORTS_FTRACE_OPS
115static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400116 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400117#else
118/* See comment below, where ftrace_ops_list_func is defined */
119static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
120#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
121#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400122
Steven Rostedt0a016402012-11-02 17:03:03 -0400123/*
124 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400125 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400126 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400127 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400128 * concurrent insertions into the ftrace_global_list.
129 *
130 * Silly Alpha and silly pointer-speculation compiler optimizations!
131 */
132#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400133 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400134 do
135
136/*
137 * Optimized for just a single item in the list (as that is the normal case).
138 */
139#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400140 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400141 unlikely((op) != &ftrace_list_end))
142
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900143static inline void ftrace_ops_init(struct ftrace_ops *ops)
144{
145#ifdef CONFIG_DYNAMIC_FTRACE
146 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
147 mutex_init(&ops->regex_lock);
148 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
149 }
150#endif
151}
152
Steven Rostedtea701f12012-07-20 13:08:05 -0400153/**
154 * ftrace_nr_registered_ops - return number of ops registered
155 *
156 * Returns the number of ftrace_ops registered and tracing functions
157 */
158int ftrace_nr_registered_ops(void)
159{
160 struct ftrace_ops *ops;
161 int cnt = 0;
162
163 mutex_lock(&ftrace_lock);
164
165 for (ops = ftrace_ops_list;
166 ops != &ftrace_list_end; ops = ops->next)
167 cnt++;
168
169 mutex_unlock(&ftrace_lock);
170
171 return cnt;
172}
173
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400174static void
175ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400176 struct ftrace_ops *op, struct pt_regs *regs)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200177{
Steven Rostedtc29f1222012-11-02 17:17:59 -0400178 int bit;
179
Steven Rostedtedc15ca2012-11-02 17:47:21 -0400180 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
181 if (bit < 0)
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400182 return;
183
Steven Rostedt0a016402012-11-02 17:03:03 -0400184 do_for_each_ftrace_op(op, ftrace_global_list) {
Steven Rostedta1e2e312011-08-09 12:50:46 -0400185 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -0400186 } while_for_each_ftrace_op(op);
Steven Rostedtedc15ca2012-11-02 17:47:21 -0400187
188 trace_clear_recursion(bit);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200189}
190
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400191static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400192 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500193{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500194 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500195 return;
196
Steven Rostedta1e2e312011-08-09 12:50:46 -0400197 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500198}
199
200static void set_ftrace_pid_function(ftrace_func_t func)
201{
202 /* do not set ftrace_pid_function to itself! */
203 if (func != ftrace_pid_func)
204 ftrace_pid_function = func;
205}
206
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200207/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200208 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200209 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200210 * This NULLs the ftrace function and in essence stops
211 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200212 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200213void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200214{
Steven Rostedt3d083392008-05-12 21:20:42 +0200215 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500216 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200217}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200218
Jiri Olsae2484912012-02-15 15:51:48 +0100219static void control_ops_disable_all(struct ftrace_ops *ops)
220{
221 int cpu;
222
223 for_each_possible_cpu(cpu)
224 *per_cpu_ptr(ops->disabled, cpu) = 1;
225}
226
227static int control_ops_alloc(struct ftrace_ops *ops)
228{
229 int __percpu *disabled;
230
231 disabled = alloc_percpu(int);
232 if (!disabled)
233 return -ENOMEM;
234
235 ops->disabled = disabled;
236 control_ops_disable_all(ops);
237 return 0;
238}
239
240static void control_ops_free(struct ftrace_ops *ops)
241{
242 free_percpu(ops->disabled);
243}
244
Steven Rostedt2b499382011-05-03 22:49:52 -0400245static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400246{
247 ftrace_func_t func;
248
249 /*
250 * If there's only one function registered, then call that
251 * function directly. Otherwise, we need to iterate over the
252 * registered callers.
253 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400254 if (ftrace_global_list == &ftrace_list_end ||
Steven Rostedt63503792012-11-02 16:58:56 -0400255 ftrace_global_list->next == &ftrace_list_end) {
Steven Rostedtb8489142011-05-04 09:27:52 -0400256 func = ftrace_global_list->func;
Steven Rostedt63503792012-11-02 16:58:56 -0400257 /*
258 * As we are calling the function directly.
259 * If it does not have recursion protection,
260 * the function_trace_op needs to be updated
261 * accordingly.
262 */
263 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
264 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
265 else
266 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
267 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -0400268 func = ftrace_global_list_func;
Steven Rostedt63503792012-11-02 16:58:56 -0400269 /* The list has its own recursion protection. */
270 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
271 }
272
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400273
274 /* If we filter on pids, update to use the pid function */
275 if (!list_empty(&ftrace_pids)) {
276 set_ftrace_pid_function(func);
277 func = ftrace_pid_func;
278 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400279
280 global_ops.func = func;
281}
282
Steven Rostedta0d0a2a2014-02-07 14:41:17 -0500283static void ftrace_sync(struct work_struct *work)
284{
285 /*
286 * This function is just a stub to implement a hard force
287 * of synchronize_sched(). This requires synchronizing
288 * tasks even in userspace and idle.
289 *
290 * Yes, function tracing is rude.
291 */
292}
293
294static void ftrace_sync_ipi(void *data)
295{
296 /* Probably not needed, but do it anyway */
297 smp_rmb();
298}
299
Steven Rostedt1499a3e2014-02-07 14:42:35 -0500300#ifdef CONFIG_FUNCTION_GRAPH_TRACER
301static void update_function_graph_func(void);
302#else
303static inline void update_function_graph_func(void) { }
304#endif
305
Steven Rostedt2b499382011-05-03 22:49:52 -0400306static void update_ftrace_function(void)
307{
308 ftrace_func_t func;
309
310 update_global_ops();
311
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400312 /*
313 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400314 * recursion safe and not dynamic and the arch supports passing ops,
315 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400316 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400317 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400318 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400319 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400320 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400321 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400322 /* Set the ftrace_ops that the arch callback uses */
323 if (ftrace_ops_list == &global_ops)
Steven Rostedta0d0a2a2014-02-07 14:41:17 -0500324 set_function_trace_op = ftrace_global_list;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400325 else
Steven Rostedta0d0a2a2014-02-07 14:41:17 -0500326 set_function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400327 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400328 } else {
329 /* Just use the default ftrace_ops */
Steven Rostedta0d0a2a2014-02-07 14:41:17 -0500330 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400331 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400332 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400333
Steven Rostedta0d0a2a2014-02-07 14:41:17 -0500334 /* If there's no change, then do nothing more here */
335 if (ftrace_trace_function == func)
336 return;
337
Steven Rostedt1499a3e2014-02-07 14:42:35 -0500338 update_function_graph_func();
339
Steven Rostedta0d0a2a2014-02-07 14:41:17 -0500340 /*
341 * If we are using the list function, it doesn't care
342 * about the function_trace_ops.
343 */
344 if (func == ftrace_ops_list_func) {
345 ftrace_trace_function = func;
346 /*
347 * Don't even bother setting function_trace_ops,
348 * it would be racy to do so anyway.
349 */
350 return;
351 }
352
353#ifndef CONFIG_DYNAMIC_FTRACE
354 /*
355 * For static tracing, we need to be a bit more careful.
356 * The function change takes affect immediately. Thus,
357 * we need to coorditate the setting of the function_trace_ops
358 * with the setting of the ftrace_trace_function.
359 *
360 * Set the function to the list ops, which will call the
361 * function we want, albeit indirectly, but it handles the
362 * ftrace_ops and doesn't depend on function_trace_op.
363 */
364 ftrace_trace_function = ftrace_ops_list_func;
365 /*
366 * Make sure all CPUs see this. Yes this is slow, but static
367 * tracing is slow and nasty to have enabled.
368 */
369 schedule_on_each_cpu(ftrace_sync);
370 /* Now all cpus are using the list ops. */
371 function_trace_op = set_function_trace_op;
372 /* Make sure the function_trace_op is visible on all CPUs */
373 smp_wmb();
374 /* Nasty way to force a rmb on all cpus */
375 smp_call_function(ftrace_sync_ipi, NULL, 1);
376 /* OK, we are all set to update the ftrace_trace_function now! */
377#endif /* !CONFIG_DYNAMIC_FTRACE */
378
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400379 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400380}
381
Steven Rostedt2b499382011-05-03 22:49:52 -0400382static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200383{
Steven Rostedt2b499382011-05-03 22:49:52 -0400384 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200385 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400386 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200387 * CPU might be walking that list. We need to make sure
388 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400389 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200390 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400391 rcu_assign_pointer(*list, ops);
392}
Steven Rostedt3d083392008-05-12 21:20:42 +0200393
Steven Rostedt2b499382011-05-03 22:49:52 -0400394static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
395{
396 struct ftrace_ops **p;
397
398 /*
399 * If we are removing the last function, then simply point
400 * to the ftrace_stub.
401 */
402 if (*list == ops && ops->next == &ftrace_list_end) {
403 *list = &ftrace_list_end;
404 return 0;
405 }
406
407 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
408 if (*p == ops)
409 break;
410
411 if (*p != ops)
412 return -1;
413
414 *p = (*p)->next;
415 return 0;
416}
417
Jiri Olsae2484912012-02-15 15:51:48 +0100418static void add_ftrace_list_ops(struct ftrace_ops **list,
419 struct ftrace_ops *main_ops,
420 struct ftrace_ops *ops)
421{
422 int first = *list == &ftrace_list_end;
423 add_ftrace_ops(list, ops);
424 if (first)
425 add_ftrace_ops(&ftrace_ops_list, main_ops);
426}
427
428static int remove_ftrace_list_ops(struct ftrace_ops **list,
429 struct ftrace_ops *main_ops,
430 struct ftrace_ops *ops)
431{
432 int ret = remove_ftrace_ops(list, ops);
433 if (!ret && *list == &ftrace_list_end)
434 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
435 return ret;
436}
437
Steven Rostedt2b499382011-05-03 22:49:52 -0400438static int __register_ftrace_function(struct ftrace_ops *ops)
439{
Steven Rostedt2b499382011-05-03 22:49:52 -0400440 if (FTRACE_WARN_ON(ops == &global_ops))
441 return -EINVAL;
442
Steven Rostedtb8489142011-05-04 09:27:52 -0400443 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
444 return -EBUSY;
445
Jiri Olsae2484912012-02-15 15:51:48 +0100446 /* We don't support both control and global flags set. */
447 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
448 return -EINVAL;
449
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900450#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400451 /*
452 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
453 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
454 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
455 */
456 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
457 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
458 return -EINVAL;
459
460 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
461 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
462#endif
463
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400464 if (!core_kernel_data((unsigned long)ops))
465 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
466
Steven Rostedtb8489142011-05-04 09:27:52 -0400467 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100468 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400469 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100470 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
471 if (control_ops_alloc(ops))
472 return -ENOMEM;
473 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400474 } else
475 add_ftrace_ops(&ftrace_ops_list, ops);
476
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400477 if (ftrace_enabled)
478 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200479
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200480 return 0;
481}
482
Ingo Molnare309b412008-05-12 21:20:51 +0200483static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200484{
Steven Rostedt2b499382011-05-03 22:49:52 -0400485 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200486
Steven Rostedtb8489142011-05-04 09:27:52 -0400487 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
488 return -EBUSY;
489
Steven Rostedt2b499382011-05-03 22:49:52 -0400490 if (FTRACE_WARN_ON(ops == &global_ops))
491 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200492
Steven Rostedtb8489142011-05-04 09:27:52 -0400493 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100494 ret = remove_ftrace_list_ops(&ftrace_global_list,
495 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400496 if (!ret)
497 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100498 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
499 ret = remove_ftrace_list_ops(&ftrace_control_list,
500 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400501 } else
502 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
503
Steven Rostedt2b499382011-05-03 22:49:52 -0400504 if (ret < 0)
505 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400506
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400507 if (ftrace_enabled)
508 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200509
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500510 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200511}
512
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500513static void ftrace_update_pid_func(void)
514{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400515 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500516 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900517 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500518
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400519 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500520}
521
Steven Rostedt493762f2009-03-23 17:12:36 -0400522#ifdef CONFIG_FUNCTION_PROFILER
523struct ftrace_profile {
524 struct hlist_node node;
525 unsigned long ip;
526 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400527#ifdef CONFIG_FUNCTION_GRAPH_TRACER
528 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400529 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400530#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400531};
532
533struct ftrace_profile_page {
534 struct ftrace_profile_page *next;
535 unsigned long index;
536 struct ftrace_profile records[];
537};
538
Steven Rostedtcafb1682009-03-24 20:50:39 -0400539struct ftrace_profile_stat {
540 atomic_t disabled;
541 struct hlist_head *hash;
542 struct ftrace_profile_page *pages;
543 struct ftrace_profile_page *start;
544 struct tracer_stat stat;
545};
546
Steven Rostedt493762f2009-03-23 17:12:36 -0400547#define PROFILE_RECORDS_SIZE \
548 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
549
550#define PROFILES_PER_PAGE \
551 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
552
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400553static int ftrace_profile_enabled __read_mostly;
554
555/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400556static DEFINE_MUTEX(ftrace_profile_lock);
557
Steven Rostedtcafb1682009-03-24 20:50:39 -0400558static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400559
Namhyung Kim20079eb2013-04-10 08:55:50 +0900560#define FTRACE_PROFILE_HASH_BITS 10
561#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400562
Steven Rostedt493762f2009-03-23 17:12:36 -0400563static void *
564function_stat_next(void *v, int idx)
565{
566 struct ftrace_profile *rec = v;
567 struct ftrace_profile_page *pg;
568
569 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
570
571 again:
Li Zefan0296e422009-06-26 11:15:37 +0800572 if (idx != 0)
573 rec++;
574
Steven Rostedt493762f2009-03-23 17:12:36 -0400575 if ((void *)rec >= (void *)&pg->records[pg->index]) {
576 pg = pg->next;
577 if (!pg)
578 return NULL;
579 rec = &pg->records[0];
580 if (!rec->counter)
581 goto again;
582 }
583
584 return rec;
585}
586
587static void *function_stat_start(struct tracer_stat *trace)
588{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400589 struct ftrace_profile_stat *stat =
590 container_of(trace, struct ftrace_profile_stat, stat);
591
592 if (!stat || !stat->start)
593 return NULL;
594
595 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400596}
597
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400598#ifdef CONFIG_FUNCTION_GRAPH_TRACER
599/* function graph compares on total time */
600static int function_stat_cmp(void *p1, void *p2)
601{
602 struct ftrace_profile *a = p1;
603 struct ftrace_profile *b = p2;
604
605 if (a->time < b->time)
606 return -1;
607 if (a->time > b->time)
608 return 1;
609 else
610 return 0;
611}
612#else
613/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400614static int function_stat_cmp(void *p1, void *p2)
615{
616 struct ftrace_profile *a = p1;
617 struct ftrace_profile *b = p2;
618
619 if (a->counter < b->counter)
620 return -1;
621 if (a->counter > b->counter)
622 return 1;
623 else
624 return 0;
625}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400626#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400627
628static int function_stat_headers(struct seq_file *m)
629{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400630#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400631 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400632 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400633 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400634 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400635#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400636 seq_printf(m, " Function Hit\n"
637 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400638#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400639 return 0;
640}
641
642static int function_stat_show(struct seq_file *m, void *v)
643{
644 struct ftrace_profile *rec = v;
645 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800646 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400647#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400648 static struct trace_seq s;
649 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400650 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400651#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800652 mutex_lock(&ftrace_profile_lock);
653
654 /* we raced with function_profile_reset() */
655 if (unlikely(rec->counter == 0)) {
656 ret = -EBUSY;
657 goto out;
658 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400659
660 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400661 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400662
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400663#ifdef CONFIG_FUNCTION_GRAPH_TRACER
664 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400665 avg = rec->time;
666 do_div(avg, rec->counter);
667
Chase Douglase330b3b2010-04-26 14:02:05 -0400668 /* Sample standard deviation (s^2) */
669 if (rec->counter <= 1)
670 stddev = 0;
671 else {
672 stddev = rec->time_squared - rec->counter * avg * avg;
673 /*
674 * Divide only 1000 for ns^2 -> us^2 conversion.
675 * trace_print_graph_duration will divide 1000 again.
676 */
677 do_div(stddev, (rec->counter - 1) * 1000);
678 }
679
Steven Rostedt34886c82009-03-25 21:00:47 -0400680 trace_seq_init(&s);
681 trace_print_graph_duration(rec->time, &s);
682 trace_seq_puts(&s, " ");
683 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400684 trace_seq_puts(&s, " ");
685 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400686 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400687#endif
688 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800689out:
690 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400691
Li Zefan3aaba202010-08-23 16:50:12 +0800692 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400693}
694
Steven Rostedtcafb1682009-03-24 20:50:39 -0400695static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400696{
697 struct ftrace_profile_page *pg;
698
Steven Rostedtcafb1682009-03-24 20:50:39 -0400699 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400700
701 while (pg) {
702 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
703 pg->index = 0;
704 pg = pg->next;
705 }
706
Steven Rostedtcafb1682009-03-24 20:50:39 -0400707 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400708 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
709}
710
Steven Rostedtcafb1682009-03-24 20:50:39 -0400711int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400712{
713 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400714 int functions;
715 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400716 int i;
717
718 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400719 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400720 return 0;
721
Steven Rostedtcafb1682009-03-24 20:50:39 -0400722 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
723 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400724 return -ENOMEM;
725
Steven Rostedt318e0a72009-03-25 20:06:34 -0400726#ifdef CONFIG_DYNAMIC_FTRACE
727 functions = ftrace_update_tot_cnt;
728#else
729 /*
730 * We do not know the number of functions that exist because
731 * dynamic tracing is what counts them. With past experience
732 * we have around 20K functions. That should be more than enough.
733 * It is highly unlikely we will execute every function in
734 * the kernel.
735 */
736 functions = 20000;
737#endif
738
Steven Rostedtcafb1682009-03-24 20:50:39 -0400739 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400740
Steven Rostedt318e0a72009-03-25 20:06:34 -0400741 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
742
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900743 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400744 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400745 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400746 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400747 pg = pg->next;
748 }
749
750 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400751
752 out_free:
753 pg = stat->start;
754 while (pg) {
755 unsigned long tmp = (unsigned long)pg;
756
757 pg = pg->next;
758 free_page(tmp);
759 }
760
Steven Rostedt318e0a72009-03-25 20:06:34 -0400761 stat->pages = NULL;
762 stat->start = NULL;
763
764 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400765}
766
Steven Rostedtcafb1682009-03-24 20:50:39 -0400767static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400768{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400769 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400770 int size;
771
Steven Rostedtcafb1682009-03-24 20:50:39 -0400772 stat = &per_cpu(ftrace_profile_stats, cpu);
773
774 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400775 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400776 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400777 return 0;
778 }
779
780 /*
781 * We are profiling all functions, but usually only a few thousand
782 * functions are hit. We'll make a hash of 1024 items.
783 */
784 size = FTRACE_PROFILE_HASH_SIZE;
785
Steven Rostedtcafb1682009-03-24 20:50:39 -0400786 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400787
Steven Rostedtcafb1682009-03-24 20:50:39 -0400788 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400789 return -ENOMEM;
790
Steven Rostedt318e0a72009-03-25 20:06:34 -0400791 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400792 if (ftrace_profile_pages_init(stat) < 0) {
793 kfree(stat->hash);
794 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400795 return -ENOMEM;
796 }
797
798 return 0;
799}
800
Steven Rostedtcafb1682009-03-24 20:50:39 -0400801static int ftrace_profile_init(void)
802{
803 int cpu;
804 int ret = 0;
805
Miao Xie885154c2013-12-16 15:20:01 +0800806 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400807 ret = ftrace_profile_init_cpu(cpu);
808 if (ret)
809 break;
810 }
811
812 return ret;
813}
814
Steven Rostedt493762f2009-03-23 17:12:36 -0400815/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400816static struct ftrace_profile *
817ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400818{
819 struct ftrace_profile *rec;
820 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400821 unsigned long key;
822
Namhyung Kim20079eb2013-04-10 08:55:50 +0900823 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400825
826 if (hlist_empty(hhd))
827 return NULL;
828
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400829 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400830 if (rec->ip == ip)
831 return rec;
832 }
833
834 return NULL;
835}
836
Steven Rostedtcafb1682009-03-24 20:50:39 -0400837static void ftrace_add_profile(struct ftrace_profile_stat *stat,
838 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400839{
840 unsigned long key;
841
Namhyung Kim20079eb2013-04-10 08:55:50 +0900842 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400843 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400844}
845
Steven Rostedt318e0a72009-03-25 20:06:34 -0400846/*
847 * The memory is already allocated, this simply finds a new record to use.
848 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400849static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400850ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400851{
852 struct ftrace_profile *rec = NULL;
853
Steven Rostedt318e0a72009-03-25 20:06:34 -0400854 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400855 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400856 goto out;
857
Steven Rostedt493762f2009-03-23 17:12:36 -0400858 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400859 * Try to find the function again since an NMI
860 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400861 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400862 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400863 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400864 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400865
Steven Rostedtcafb1682009-03-24 20:50:39 -0400866 if (stat->pages->index == PROFILES_PER_PAGE) {
867 if (!stat->pages->next)
868 goto out;
869 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400870 }
871
Steven Rostedtcafb1682009-03-24 20:50:39 -0400872 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400873 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400874 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400875
Steven Rostedt493762f2009-03-23 17:12:36 -0400876 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400877 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400878
879 return rec;
880}
881
Steven Rostedt493762f2009-03-23 17:12:36 -0400882static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400883function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400884 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400885{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400886 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400887 struct ftrace_profile *rec;
888 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400889
890 if (!ftrace_profile_enabled)
891 return;
892
Steven Rostedt493762f2009-03-23 17:12:36 -0400893 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400894
895 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400896 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400897 goto out;
898
899 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400900 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400901 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400902 if (!rec)
903 goto out;
904 }
905
906 rec->counter++;
907 out:
908 local_irq_restore(flags);
909}
910
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400911#ifdef CONFIG_FUNCTION_GRAPH_TRACER
912static int profile_graph_entry(struct ftrace_graph_ent *trace)
913{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400914 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400915 return 1;
916}
917
918static void profile_graph_return(struct ftrace_graph_ret *trace)
919{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400920 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400921 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400922 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400923 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400924
925 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400926 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400927 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400928 goto out;
929
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400930 /* If the calltime was zero'd ignore it */
931 if (!trace->calltime)
932 goto out;
933
Steven Rostedta2a16d62009-03-24 23:17:58 -0400934 calltime = trace->rettime - trace->calltime;
935
936 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
937 int index;
938
939 index = trace->depth;
940
941 /* Append this call time to the parent time to subtract */
942 if (index)
943 current->ret_stack[index - 1].subtime += calltime;
944
945 if (current->ret_stack[index].subtime < calltime)
946 calltime -= current->ret_stack[index].subtime;
947 else
948 calltime = 0;
949 }
950
Steven Rostedtcafb1682009-03-24 20:50:39 -0400951 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400952 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400953 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400954 rec->time_squared += calltime * calltime;
955 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400956
Steven Rostedtcafb1682009-03-24 20:50:39 -0400957 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400958 local_irq_restore(flags);
959}
960
961static int register_ftrace_profiler(void)
962{
963 return register_ftrace_graph(&profile_graph_return,
964 &profile_graph_entry);
965}
966
967static void unregister_ftrace_profiler(void)
968{
969 unregister_ftrace_graph();
970}
971#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100972static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400973 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900974 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
975 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400976};
977
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400978static int register_ftrace_profiler(void)
979{
980 return register_ftrace_function(&ftrace_profile_ops);
981}
982
983static void unregister_ftrace_profiler(void)
984{
985 unregister_ftrace_function(&ftrace_profile_ops);
986}
987#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
988
Steven Rostedt493762f2009-03-23 17:12:36 -0400989static ssize_t
990ftrace_profile_write(struct file *filp, const char __user *ubuf,
991 size_t cnt, loff_t *ppos)
992{
993 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400994 int ret;
995
Peter Huewe22fe9b52011-06-07 21:58:27 +0200996 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
997 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400998 return ret;
999
1000 val = !!val;
1001
1002 mutex_lock(&ftrace_profile_lock);
1003 if (ftrace_profile_enabled ^ val) {
1004 if (val) {
1005 ret = ftrace_profile_init();
1006 if (ret < 0) {
1007 cnt = ret;
1008 goto out;
1009 }
1010
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001011 ret = register_ftrace_profiler();
1012 if (ret < 0) {
1013 cnt = ret;
1014 goto out;
1015 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001016 ftrace_profile_enabled = 1;
1017 } else {
1018 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -04001019 /*
1020 * unregister_ftrace_profiler calls stop_machine
1021 * so this acts like an synchronize_sched.
1022 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001023 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -04001024 }
1025 }
1026 out:
1027 mutex_unlock(&ftrace_profile_lock);
1028
Jiri Olsacf8517c2009-10-23 19:36:16 -04001029 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001030
1031 return cnt;
1032}
1033
1034static ssize_t
1035ftrace_profile_read(struct file *filp, char __user *ubuf,
1036 size_t cnt, loff_t *ppos)
1037{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001038 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001039 int r;
1040
1041 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1042 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1043}
1044
1045static const struct file_operations ftrace_profile_fops = {
1046 .open = tracing_open_generic,
1047 .read = ftrace_profile_read,
1048 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001049 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001050};
1051
Steven Rostedtcafb1682009-03-24 20:50:39 -04001052/* used to initialize the real stat files */
1053static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001054 .name = "functions",
1055 .stat_start = function_stat_start,
1056 .stat_next = function_stat_next,
1057 .stat_cmp = function_stat_cmp,
1058 .stat_headers = function_stat_headers,
1059 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001060};
1061
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001062static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001063{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001064 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001065 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001066 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001067 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001068 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001069
Steven Rostedtcafb1682009-03-24 20:50:39 -04001070 for_each_possible_cpu(cpu) {
1071 stat = &per_cpu(ftrace_profile_stats, cpu);
1072
1073 /* allocate enough for function name + cpu number */
1074 name = kmalloc(32, GFP_KERNEL);
1075 if (!name) {
1076 /*
1077 * The files created are permanent, if something happens
1078 * we still do not free memory.
1079 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001080 WARN(1,
1081 "Could not allocate stat file for cpu %d\n",
1082 cpu);
1083 return;
1084 }
1085 stat->stat = function_stats;
1086 snprintf(name, 32, "function%d", cpu);
1087 stat->stat.name = name;
1088 ret = register_stat_tracer(&stat->stat);
1089 if (ret) {
1090 WARN(1,
1091 "Could not register function stat for cpu %d\n",
1092 cpu);
1093 kfree(name);
1094 return;
1095 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001096 }
1097
1098 entry = debugfs_create_file("function_profile_enabled", 0644,
1099 d_tracer, NULL, &ftrace_profile_fops);
1100 if (!entry)
1101 pr_warning("Could not create debugfs "
1102 "'function_profile_enabled' entry\n");
1103}
1104
1105#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001106static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001107{
1108}
1109#endif /* CONFIG_FUNCTION_PROFILER */
1110
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001111static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1112
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04001113loff_t
1114ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1115{
1116 loff_t ret;
1117
1118 if (file->f_mode & FMODE_READ)
1119 ret = seq_lseek(file, offset, whence);
1120 else
1121 file->f_pos = ret = 1;
1122
1123 return ret;
1124}
1125
Steven Rostedt3d083392008-05-12 21:20:42 +02001126#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001127
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001128#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001129# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001130#endif
1131
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001132static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1133
Steven Rostedtb6887d72009-02-17 12:32:04 -05001134struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001135 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001136 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001137 unsigned long flags;
1138 unsigned long ip;
1139 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001140 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001141};
1142
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001143struct ftrace_func_entry {
1144 struct hlist_node hlist;
1145 unsigned long ip;
1146};
1147
1148struct ftrace_hash {
1149 unsigned long size_bits;
1150 struct hlist_head *buckets;
1151 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001152 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001153};
1154
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001155/*
1156 * We make these constant because no one should touch them,
1157 * but they are used as the default "empty hash", to avoid allocating
1158 * it all the time. These are in a read only section such that if
1159 * anyone does try to modify it, it will cause an exception.
1160 */
1161static const struct hlist_head empty_buckets[1];
1162static const struct ftrace_hash empty_hash = {
1163 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001164};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001165#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001166
Steven Rostedt2b499382011-05-03 22:49:52 -04001167static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001168 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001169 .notrace_hash = EMPTY_HASH,
1170 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001171 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1172 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001173};
1174
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001175struct ftrace_page {
1176 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001177 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001178 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001179 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001180};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001181
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001182static struct ftrace_page *ftrace_new_pgs;
1183
Steven Rostedta7900872011-12-16 16:23:44 -05001184#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1185#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001186
1187/* estimate from running different kernels */
1188#define NR_TO_INIT 10000
1189
1190static struct ftrace_page *ftrace_pages_start;
1191static struct ftrace_page *ftrace_pages;
1192
Steven Rostedt06a51d92011-12-19 19:07:36 -05001193static bool ftrace_hash_empty(struct ftrace_hash *hash)
1194{
1195 return !hash || !hash->count;
1196}
1197
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001198static struct ftrace_func_entry *
1199ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1200{
1201 unsigned long key;
1202 struct ftrace_func_entry *entry;
1203 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001204
Steven Rostedt06a51d92011-12-19 19:07:36 -05001205 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001206 return NULL;
1207
1208 if (hash->size_bits > 0)
1209 key = hash_long(ip, hash->size_bits);
1210 else
1211 key = 0;
1212
1213 hhd = &hash->buckets[key];
1214
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001215 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001216 if (entry->ip == ip)
1217 return entry;
1218 }
1219 return NULL;
1220}
1221
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001222static void __add_hash_entry(struct ftrace_hash *hash,
1223 struct ftrace_func_entry *entry)
1224{
1225 struct hlist_head *hhd;
1226 unsigned long key;
1227
1228 if (hash->size_bits)
1229 key = hash_long(entry->ip, hash->size_bits);
1230 else
1231 key = 0;
1232
1233 hhd = &hash->buckets[key];
1234 hlist_add_head(&entry->hlist, hhd);
1235 hash->count++;
1236}
1237
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001238static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1239{
1240 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001241
1242 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1243 if (!entry)
1244 return -ENOMEM;
1245
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001246 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001247 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001248
1249 return 0;
1250}
1251
1252static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001253free_hash_entry(struct ftrace_hash *hash,
1254 struct ftrace_func_entry *entry)
1255{
1256 hlist_del(&entry->hlist);
1257 kfree(entry);
1258 hash->count--;
1259}
1260
1261static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001262remove_hash_entry(struct ftrace_hash *hash,
1263 struct ftrace_func_entry *entry)
1264{
1265 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001266 hash->count--;
1267}
1268
1269static void ftrace_hash_clear(struct ftrace_hash *hash)
1270{
1271 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001272 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001273 struct ftrace_func_entry *entry;
1274 int size = 1 << hash->size_bits;
1275 int i;
1276
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001277 if (!hash->count)
1278 return;
1279
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001280 for (i = 0; i < size; i++) {
1281 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001282 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001283 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001284 }
1285 FTRACE_WARN_ON(hash->count);
1286}
1287
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001288static void free_ftrace_hash(struct ftrace_hash *hash)
1289{
1290 if (!hash || hash == EMPTY_HASH)
1291 return;
1292 ftrace_hash_clear(hash);
1293 kfree(hash->buckets);
1294 kfree(hash);
1295}
1296
Steven Rostedt07fd5512011-05-05 18:03:47 -04001297static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1298{
1299 struct ftrace_hash *hash;
1300
1301 hash = container_of(rcu, struct ftrace_hash, rcu);
1302 free_ftrace_hash(hash);
1303}
1304
1305static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1306{
1307 if (!hash || hash == EMPTY_HASH)
1308 return;
1309 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1310}
1311
Jiri Olsa5500fa52012-02-15 15:51:54 +01001312void ftrace_free_filter(struct ftrace_ops *ops)
1313{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001314 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001315 free_ftrace_hash(ops->filter_hash);
1316 free_ftrace_hash(ops->notrace_hash);
1317}
1318
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001319static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1320{
1321 struct ftrace_hash *hash;
1322 int size;
1323
1324 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1325 if (!hash)
1326 return NULL;
1327
1328 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001329 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001330
1331 if (!hash->buckets) {
1332 kfree(hash);
1333 return NULL;
1334 }
1335
1336 hash->size_bits = size_bits;
1337
1338 return hash;
1339}
1340
1341static struct ftrace_hash *
1342alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1343{
1344 struct ftrace_func_entry *entry;
1345 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001346 int size;
1347 int ret;
1348 int i;
1349
1350 new_hash = alloc_ftrace_hash(size_bits);
1351 if (!new_hash)
1352 return NULL;
1353
1354 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001355 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001356 return new_hash;
1357
1358 size = 1 << hash->size_bits;
1359 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001360 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001361 ret = add_hash_entry(new_hash, entry->ip);
1362 if (ret < 0)
1363 goto free_hash;
1364 }
1365 }
1366
1367 FTRACE_WARN_ON(new_hash->count != hash->count);
1368
1369 return new_hash;
1370
1371 free_hash:
1372 free_ftrace_hash(new_hash);
1373 return NULL;
1374}
1375
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001376static void
1377ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1378static void
1379ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1380
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001381static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001382ftrace_hash_move(struct ftrace_ops *ops, int enable,
1383 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001384{
1385 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001386 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001387 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001388 struct ftrace_hash *old_hash;
1389 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001390 int size = src->count;
1391 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001392 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001393 int i;
1394
1395 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001396 * Remove the current set, update the hash and add
1397 * them back.
1398 */
1399 ftrace_hash_rec_disable(ops, enable);
1400
1401 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001402 * If the new source is empty, just free dst and assign it
1403 * the empty_hash.
1404 */
1405 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001406 free_ftrace_hash_rcu(*dst);
1407 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001408 /* still need to update the function records */
1409 ret = 0;
1410 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001411 }
1412
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001413 /*
1414 * Make the hash size about 1/2 the # found
1415 */
1416 for (size /= 2; size; size >>= 1)
1417 bits++;
1418
1419 /* Don't allocate too much */
1420 if (bits > FTRACE_HASH_MAX_BITS)
1421 bits = FTRACE_HASH_MAX_BITS;
1422
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001423 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001424 new_hash = alloc_ftrace_hash(bits);
1425 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001426 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001427
1428 size = 1 << src->size_bits;
1429 for (i = 0; i < size; i++) {
1430 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001431 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001432 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001433 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001434 }
1435 }
1436
Steven Rostedt07fd5512011-05-05 18:03:47 -04001437 old_hash = *dst;
1438 rcu_assign_pointer(*dst, new_hash);
1439 free_ftrace_hash_rcu(old_hash);
1440
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001441 ret = 0;
1442 out:
1443 /*
1444 * Enable regardless of ret:
1445 * On success, we enable the new hash.
1446 * On failure, we re-enable the original hash.
1447 */
1448 ftrace_hash_rec_enable(ops, enable);
1449
1450 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001451}
1452
Steven Rostedt265c8312009-02-13 12:43:56 -05001453/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001454 * Test the hashes for this ops to see if we want to call
1455 * the ops->func or not.
1456 *
1457 * It's a match if the ip is in the ops->filter_hash or
1458 * the filter_hash does not exist or is empty,
1459 * AND
1460 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001461 *
1462 * This needs to be called with preemption disabled as
1463 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001464 */
1465static int
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04001466ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001467{
1468 struct ftrace_hash *filter_hash;
1469 struct ftrace_hash *notrace_hash;
1470 int ret;
1471
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04001472#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1473 /*
1474 * There's a small race when adding ops that the ftrace handler
1475 * that wants regs, may be called without them. We can not
1476 * allow that handler to be called if regs is NULL.
1477 */
1478 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1479 return 0;
1480#endif
1481
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001482 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1483 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001484
Steven Rostedt06a51d92011-12-19 19:07:36 -05001485 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001486 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001487 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001488 !ftrace_lookup_ip(notrace_hash, ip)))
1489 ret = 1;
1490 else
1491 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001492
1493 return ret;
1494}
1495
1496/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001497 * This is a double for. Do not use 'break' to break out of the loop,
1498 * you must use a goto.
1499 */
1500#define do_for_each_ftrace_rec(pg, rec) \
1501 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1502 int _____i; \
1503 for (_____i = 0; _____i < pg->index; _____i++) { \
1504 rec = &pg->records[_____i];
1505
1506#define while_for_each_ftrace_rec() \
1507 } \
1508 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301509
Steven Rostedt5855fea2011-12-16 19:27:42 -05001510
1511static int ftrace_cmp_recs(const void *a, const void *b)
1512{
Steven Rostedta650e022012-04-25 13:48:13 -04001513 const struct dyn_ftrace *key = a;
1514 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001515
Steven Rostedta650e022012-04-25 13:48:13 -04001516 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001517 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001518 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1519 return 1;
1520 return 0;
1521}
1522
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001523static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001524{
1525 struct ftrace_page *pg;
1526 struct dyn_ftrace *rec;
1527 struct dyn_ftrace key;
1528
1529 key.ip = start;
1530 key.flags = end; /* overload flags, as it is unsigned long */
1531
1532 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1533 if (end < pg->records[0].ip ||
1534 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1535 continue;
1536 rec = bsearch(&key, pg->records, pg->index,
1537 sizeof(struct dyn_ftrace),
1538 ftrace_cmp_recs);
1539 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001540 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001541 }
1542
Steven Rostedt5855fea2011-12-16 19:27:42 -05001543 return 0;
1544}
1545
Steven Rostedtc88fd862011-08-16 09:53:39 -04001546/**
1547 * ftrace_location - return true if the ip giving is a traced location
1548 * @ip: the instruction pointer to check
1549 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001550 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001551 * That is, the instruction that is either a NOP or call to
1552 * the function tracer. It checks the ftrace internal tables to
1553 * determine if the address belongs or not.
1554 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001555unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001556{
Steven Rostedta650e022012-04-25 13:48:13 -04001557 return ftrace_location_range(ip, ip);
1558}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001559
Steven Rostedta650e022012-04-25 13:48:13 -04001560/**
1561 * ftrace_text_reserved - return true if range contains an ftrace location
1562 * @start: start of range to search
1563 * @end: end of range to search (inclusive). @end points to the last byte to check.
1564 *
1565 * Returns 1 if @start and @end contains a ftrace location.
1566 * That is, the instruction that is either a NOP or call to
1567 * the function tracer. It checks the ftrace internal tables to
1568 * determine if the address belongs or not.
1569 */
1570int ftrace_text_reserved(void *start, void *end)
1571{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001572 unsigned long ret;
1573
1574 ret = ftrace_location_range((unsigned long)start,
1575 (unsigned long)end);
1576
1577 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001578}
1579
Steven Rostedted926f92011-05-03 13:25:24 -04001580static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1581 int filter_hash,
1582 bool inc)
1583{
1584 struct ftrace_hash *hash;
1585 struct ftrace_hash *other_hash;
1586 struct ftrace_page *pg;
1587 struct dyn_ftrace *rec;
1588 int count = 0;
1589 int all = 0;
1590
1591 /* Only update if the ops has been registered */
1592 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1593 return;
1594
1595 /*
1596 * In the filter_hash case:
1597 * If the count is zero, we update all records.
1598 * Otherwise we just update the items in the hash.
1599 *
1600 * In the notrace_hash case:
1601 * We enable the update in the hash.
1602 * As disabling notrace means enabling the tracing,
1603 * and enabling notrace means disabling, the inc variable
1604 * gets inversed.
1605 */
1606 if (filter_hash) {
1607 hash = ops->filter_hash;
1608 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001609 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001610 all = 1;
1611 } else {
1612 inc = !inc;
1613 hash = ops->notrace_hash;
1614 other_hash = ops->filter_hash;
1615 /*
1616 * If the notrace hash has no items,
1617 * then there's nothing to do.
1618 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001619 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001620 return;
1621 }
1622
1623 do_for_each_ftrace_rec(pg, rec) {
1624 int in_other_hash = 0;
1625 int in_hash = 0;
1626 int match = 0;
1627
1628 if (all) {
1629 /*
1630 * Only the filter_hash affects all records.
1631 * Update if the record is not in the notrace hash.
1632 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001633 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001634 match = 1;
1635 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001636 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1637 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001638
1639 /*
1640 *
1641 */
1642 if (filter_hash && in_hash && !in_other_hash)
1643 match = 1;
1644 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001645 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001646 match = 1;
1647 }
1648 if (!match)
1649 continue;
1650
1651 if (inc) {
1652 rec->flags++;
1653 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1654 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001655 /*
1656 * If any ops wants regs saved for this function
1657 * then all ops will get saved regs.
1658 */
1659 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1660 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001661 } else {
1662 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1663 return;
1664 rec->flags--;
1665 }
1666 count++;
1667 /* Shortcut, if we handled all records, we are done. */
1668 if (!all && count == hash->count)
1669 return;
1670 } while_for_each_ftrace_rec();
1671}
1672
1673static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1674 int filter_hash)
1675{
1676 __ftrace_hash_rec_update(ops, filter_hash, 0);
1677}
1678
1679static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1680 int filter_hash)
1681{
1682 __ftrace_hash_rec_update(ops, filter_hash, 1);
1683}
1684
Steven Rostedt05736a42008-09-22 14:55:47 -07001685static void print_ip_ins(const char *fmt, unsigned char *p)
1686{
1687 int i;
1688
1689 printk(KERN_CONT "%s", fmt);
1690
1691 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1692 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1693}
1694
Steven Rostedtc88fd862011-08-16 09:53:39 -04001695/**
1696 * ftrace_bug - report and shutdown function tracer
1697 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1698 * @ip: The address that failed
1699 *
1700 * The arch code that enables or disables the function tracing
1701 * can call ftrace_bug() when it has detected a problem in
1702 * modifying the code. @failed should be one of either:
1703 * EFAULT - if the problem happens on reading the @ip address
1704 * EINVAL - if what is read at @ip is not what was expected
1705 * EPERM - if the problem happens on writting to the @ip address
1706 */
1707void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001708{
1709 switch (failed) {
1710 case -EFAULT:
1711 FTRACE_WARN_ON_ONCE(1);
1712 pr_info("ftrace faulted on modifying ");
1713 print_ip_sym(ip);
1714 break;
1715 case -EINVAL:
1716 FTRACE_WARN_ON_ONCE(1);
1717 pr_info("ftrace failed to modify ");
1718 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001719 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001720 printk(KERN_CONT "\n");
1721 break;
1722 case -EPERM:
1723 FTRACE_WARN_ON_ONCE(1);
1724 pr_info("ftrace faulted on writing ");
1725 print_ip_sym(ip);
1726 break;
1727 default:
1728 FTRACE_WARN_ON_ONCE(1);
1729 pr_info("ftrace faulted on unknown error ");
1730 print_ip_sym(ip);
1731 }
1732}
1733
Steven Rostedtc88fd862011-08-16 09:53:39 -04001734static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001735{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001736 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001737
Steven Rostedt982c3502008-11-15 16:31:41 -05001738 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001739 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001740 *
Steven Rostedted926f92011-05-03 13:25:24 -04001741 * If the record has a ref count, then we need to enable it
1742 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001743 *
Steven Rostedted926f92011-05-03 13:25:24 -04001744 * Otherwise we make sure its disabled.
1745 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001746 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001747 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001748 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001749 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001750 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001751
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001752 /*
1753 * If enabling and the REGS flag does not match the REGS_EN, then
1754 * do not ignore this record. Set flags to fail the compare against
1755 * ENABLED.
1756 */
1757 if (flag &&
1758 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1759 flag |= FTRACE_FL_REGS;
1760
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001761 /* If the state of this record hasn't changed, then do nothing */
1762 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001763 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001764
1765 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001766 /* Save off if rec is being enabled (for return value) */
1767 flag ^= rec->flags & FTRACE_FL_ENABLED;
1768
1769 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001770 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001771 if (flag & FTRACE_FL_REGS) {
1772 if (rec->flags & FTRACE_FL_REGS)
1773 rec->flags |= FTRACE_FL_REGS_EN;
1774 else
1775 rec->flags &= ~FTRACE_FL_REGS_EN;
1776 }
1777 }
1778
1779 /*
1780 * If this record is being updated from a nop, then
1781 * return UPDATE_MAKE_CALL.
1782 * Otherwise, if the EN flag is set, then return
1783 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1784 * from the non-save regs, to a save regs function.
1785 * Otherwise,
1786 * return UPDATE_MODIFY_CALL to tell the caller to convert
1787 * from the save regs, to a non-save regs function.
1788 */
1789 if (flag & FTRACE_FL_ENABLED)
1790 return FTRACE_UPDATE_MAKE_CALL;
1791 else if (rec->flags & FTRACE_FL_REGS_EN)
1792 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1793 else
1794 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001795 }
1796
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001797 if (update) {
1798 /* If there's no more users, clear all flags */
1799 if (!(rec->flags & ~FTRACE_FL_MASK))
1800 rec->flags = 0;
1801 else
1802 /* Just disable the record (keep REGS state) */
1803 rec->flags &= ~FTRACE_FL_ENABLED;
1804 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001805
1806 return FTRACE_UPDATE_MAKE_NOP;
1807}
1808
1809/**
1810 * ftrace_update_record, set a record that now is tracing or not
1811 * @rec: the record to update
1812 * @enable: set to 1 if the record is tracing, zero to force disable
1813 *
1814 * The records that represent all functions that can be traced need
1815 * to be updated when tracing has been enabled.
1816 */
1817int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1818{
1819 return ftrace_check_record(rec, enable, 1);
1820}
1821
1822/**
1823 * ftrace_test_record, check if the record has been enabled or not
1824 * @rec: the record to test
1825 * @enable: set to 1 to check if enabled, 0 if it is disabled
1826 *
1827 * The arch code may need to test if a record is already set to
1828 * tracing to determine how to modify the function code that it
1829 * represents.
1830 */
1831int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1832{
1833 return ftrace_check_record(rec, enable, 0);
1834}
1835
1836static int
1837__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1838{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001839 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001840 unsigned long ftrace_addr;
1841 int ret;
1842
Steven Rostedtc88fd862011-08-16 09:53:39 -04001843 ret = ftrace_update_record(rec, enable);
1844
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001845 if (rec->flags & FTRACE_FL_REGS)
1846 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1847 else
1848 ftrace_addr = (unsigned long)FTRACE_ADDR;
1849
Steven Rostedtc88fd862011-08-16 09:53:39 -04001850 switch (ret) {
1851 case FTRACE_UPDATE_IGNORE:
1852 return 0;
1853
1854 case FTRACE_UPDATE_MAKE_CALL:
1855 return ftrace_make_call(rec, ftrace_addr);
1856
1857 case FTRACE_UPDATE_MAKE_NOP:
1858 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001859
1860 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1861 case FTRACE_UPDATE_MODIFY_CALL:
1862 if (rec->flags & FTRACE_FL_REGS)
1863 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1864 else
1865 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1866
1867 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001868 }
1869
1870 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001871}
1872
Steven Rostedte4f5d542012-04-27 09:13:18 -04001873void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001874{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001875 struct dyn_ftrace *rec;
1876 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001877 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001878
Steven Rostedt45a4a232011-04-21 23:16:46 -04001879 if (unlikely(ftrace_disabled))
1880 return;
1881
Steven Rostedt265c8312009-02-13 12:43:56 -05001882 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001883 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001884 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001885 ftrace_bug(failed, rec->ip);
1886 /* Stop processing */
1887 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001888 }
1889 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001890}
1891
Steven Rostedtc88fd862011-08-16 09:53:39 -04001892struct ftrace_rec_iter {
1893 struct ftrace_page *pg;
1894 int index;
1895};
1896
1897/**
1898 * ftrace_rec_iter_start, start up iterating over traced functions
1899 *
1900 * Returns an iterator handle that is used to iterate over all
1901 * the records that represent address locations where functions
1902 * are traced.
1903 *
1904 * May return NULL if no records are available.
1905 */
1906struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1907{
1908 /*
1909 * We only use a single iterator.
1910 * Protected by the ftrace_lock mutex.
1911 */
1912 static struct ftrace_rec_iter ftrace_rec_iter;
1913 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1914
1915 iter->pg = ftrace_pages_start;
1916 iter->index = 0;
1917
1918 /* Could have empty pages */
1919 while (iter->pg && !iter->pg->index)
1920 iter->pg = iter->pg->next;
1921
1922 if (!iter->pg)
1923 return NULL;
1924
1925 return iter;
1926}
1927
1928/**
1929 * ftrace_rec_iter_next, get the next record to process.
1930 * @iter: The handle to the iterator.
1931 *
1932 * Returns the next iterator after the given iterator @iter.
1933 */
1934struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1935{
1936 iter->index++;
1937
1938 if (iter->index >= iter->pg->index) {
1939 iter->pg = iter->pg->next;
1940 iter->index = 0;
1941
1942 /* Could have empty pages */
1943 while (iter->pg && !iter->pg->index)
1944 iter->pg = iter->pg->next;
1945 }
1946
1947 if (!iter->pg)
1948 return NULL;
1949
1950 return iter;
1951}
1952
1953/**
1954 * ftrace_rec_iter_record, get the record at the iterator location
1955 * @iter: The current iterator location
1956 *
1957 * Returns the record that the current @iter is at.
1958 */
1959struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1960{
1961 return &iter->pg->records[iter->index];
1962}
1963
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301964static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001965ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001966{
1967 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001968 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001969
1970 ip = rec->ip;
1971
Steven Rostedt45a4a232011-04-21 23:16:46 -04001972 if (unlikely(ftrace_disabled))
1973 return 0;
1974
Shaohua Li25aac9d2009-01-09 11:29:40 +08001975 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001976 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001977 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301978 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001979 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301980 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001981}
1982
Steven Rostedt000ab692009-02-17 13:35:06 -05001983/*
1984 * archs can override this function if they must do something
1985 * before the modifying code is performed.
1986 */
1987int __weak ftrace_arch_code_modify_prepare(void)
1988{
1989 return 0;
1990}
1991
1992/*
1993 * archs can override this function if they must do something
1994 * after the modifying code is performed.
1995 */
1996int __weak ftrace_arch_code_modify_post_process(void)
1997{
1998 return 0;
1999}
2000
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002001void ftrace_modify_all_code(int command)
2002{
2003 if (command & FTRACE_UPDATE_CALLS)
2004 ftrace_replace_code(1);
2005 else if (command & FTRACE_DISABLE_CALLS)
2006 ftrace_replace_code(0);
2007
Steven Rostedta0d0a2a2014-02-07 14:41:17 -05002008 if (command & FTRACE_UPDATE_TRACE_FUNC) {
2009 function_trace_op = set_function_trace_op;
2010 smp_wmb();
2011 /* If irqs are disabled, we are in stop machine */
2012 if (!irqs_disabled())
2013 smp_call_function(ftrace_sync_ipi, NULL, 1);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002014 ftrace_update_ftrace_func(ftrace_trace_function);
Steven Rostedta0d0a2a2014-02-07 14:41:17 -05002015 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002016
2017 if (command & FTRACE_START_FUNC_RET)
2018 ftrace_enable_ftrace_graph_caller();
2019 else if (command & FTRACE_STOP_FUNC_RET)
2020 ftrace_disable_ftrace_graph_caller();
2021}
2022
Ingo Molnare309b412008-05-12 21:20:51 +02002023static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002024{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002025 int *command = data;
2026
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002027 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002028
Steven Rostedtc88fd862011-08-16 09:53:39 -04002029 return 0;
2030}
2031
2032/**
2033 * ftrace_run_stop_machine, go back to the stop machine method
2034 * @command: The command to tell ftrace what to do
2035 *
2036 * If an arch needs to fall back to the stop machine method, the
2037 * it can call this function.
2038 */
2039void ftrace_run_stop_machine(int command)
2040{
2041 stop_machine(__ftrace_modify_code, &command, NULL);
2042}
2043
2044/**
2045 * arch_ftrace_update_code, modify the code to trace or not trace
2046 * @command: The command that needs to be done
2047 *
2048 * Archs can override this function if it does not need to
2049 * run stop_machine() to modify code.
2050 */
2051void __weak arch_ftrace_update_code(int command)
2052{
2053 ftrace_run_stop_machine(command);
2054}
2055
2056static void ftrace_run_update_code(int command)
2057{
2058 int ret;
2059
2060 ret = ftrace_arch_code_modify_prepare();
2061 FTRACE_WARN_ON(ret);
2062 if (ret)
2063 return;
2064 /*
2065 * Do not call function tracer while we update the code.
2066 * We are in stop machine.
2067 */
2068 function_trace_stop++;
2069
2070 /*
2071 * By default we use stop_machine() to modify the code.
2072 * But archs can do what ever they want as long as it
2073 * is safe. The stop_machine() is the safest, but also
2074 * produces the most overhead.
2075 */
2076 arch_ftrace_update_code(command);
2077
Steven Rostedt6331c282011-07-13 15:11:02 -04002078 function_trace_stop--;
2079
Steven Rostedt000ab692009-02-17 13:35:06 -05002080 ret = ftrace_arch_code_modify_post_process();
2081 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002082}
2083
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002084static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002085static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002086static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002087
2088static void ftrace_startup_enable(int command)
2089{
2090 if (saved_ftrace_func != ftrace_trace_function) {
2091 saved_ftrace_func = ftrace_trace_function;
2092 command |= FTRACE_UPDATE_TRACE_FUNC;
2093 }
2094
2095 if (!command || !ftrace_enabled)
2096 return;
2097
2098 ftrace_run_update_code(command);
2099}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002100
Steven Rostedta1cd6172011-05-23 15:24:25 -04002101static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002102{
Steven Rostedtb8489142011-05-04 09:27:52 -04002103 bool hash_enable = true;
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002104 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002105
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002106 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002107 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002108
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002109 ret = __register_ftrace_function(ops);
2110 if (ret)
2111 return ret;
2112
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002113 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002114 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002115
Steven Rostedtb8489142011-05-04 09:27:52 -04002116 /* ops marked global share the filter hashes */
2117 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2118 ops = &global_ops;
2119 /* Don't update hash if global is already set */
2120 if (global_start_up)
2121 hash_enable = false;
2122 global_start_up++;
2123 }
2124
Steven Rostedted926f92011-05-03 13:25:24 -04002125 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002126 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002127 ftrace_hash_rec_enable(ops, 1);
2128
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002129 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002130
2131 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002132}
2133
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002134static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002135{
Steven Rostedtb8489142011-05-04 09:27:52 -04002136 bool hash_disable = true;
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002137 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002138
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002139 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002140 return -ENODEV;
2141
2142 ret = __unregister_ftrace_function(ops);
2143 if (ret)
2144 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002145
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002146 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002147 /*
2148 * Just warn in case of unbalance, no need to kill ftrace, it's not
2149 * critical but the ftrace_call callers may be never nopped again after
2150 * further ftrace uses.
2151 */
2152 WARN_ON_ONCE(ftrace_start_up < 0);
2153
Steven Rostedtb8489142011-05-04 09:27:52 -04002154 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2155 ops = &global_ops;
2156 global_start_up--;
2157 WARN_ON_ONCE(global_start_up < 0);
2158 /* Don't update hash if global still has users */
2159 if (global_start_up) {
2160 WARN_ON_ONCE(!ftrace_start_up);
2161 hash_disable = false;
2162 }
2163 }
2164
2165 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002166 ftrace_hash_rec_disable(ops, 1);
2167
Steven Rostedtb8489142011-05-04 09:27:52 -04002168 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002169 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002170
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002171 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002172
2173 if (saved_ftrace_func != ftrace_trace_function) {
2174 saved_ftrace_func = ftrace_trace_function;
2175 command |= FTRACE_UPDATE_TRACE_FUNC;
2176 }
2177
Steven Rostedtb6c5a8d2014-02-07 14:42:01 -05002178 if (!command || !ftrace_enabled) {
2179 /*
2180 * If these are control ops, they still need their
2181 * per_cpu field freed. Since, function tracing is
2182 * not currently active, we can just free them
2183 * without synchronizing all CPUs.
2184 */
2185 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2186 control_ops_free(ops);
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002187 return 0;
Steven Rostedtb6c5a8d2014-02-07 14:42:01 -05002188 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002189
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002190 ftrace_run_update_code(command);
Steven Rostedtb6c5a8d2014-02-07 14:42:01 -05002191
2192 /*
2193 * Dynamic ops may be freed, we must make sure that all
2194 * callers are done before leaving this function.
2195 * The same goes for freeing the per_cpu data of the control
2196 * ops.
2197 *
2198 * Again, normal synchronize_sched() is not good enough.
2199 * We need to do a hard force of sched synchronization.
2200 * This is because we use preempt_disable() to do RCU, but
2201 * the function tracers can be called where RCU is not watching
2202 * (like before user_exit()). We can not rely on the RCU
2203 * infrastructure to do the synchronization, thus we must do it
2204 * ourselves.
2205 */
2206 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2207 schedule_on_each_cpu(ftrace_sync);
2208
2209 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2210 control_ops_free(ops);
2211 }
2212
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002213 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002214}
2215
Ingo Molnare309b412008-05-12 21:20:51 +02002216static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002217{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002218 if (unlikely(ftrace_disabled))
2219 return;
2220
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002221 /* Force update next time */
2222 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002223 /* ftrace_start_up is true if we want ftrace running */
2224 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002225 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002226}
2227
Ingo Molnare309b412008-05-12 21:20:51 +02002228static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002229{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002230 if (unlikely(ftrace_disabled))
2231 return;
2232
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002233 /* ftrace_start_up is true if ftrace is running */
2234 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002235 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002236}
2237
Steven Rostedt3d083392008-05-12 21:20:42 +02002238static cycle_t ftrace_update_time;
2239static unsigned long ftrace_update_cnt;
2240unsigned long ftrace_update_tot_cnt;
2241
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002242static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002243{
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002244 /*
2245 * Filter_hash being empty will default to trace module.
2246 * But notrace hash requires a test of individual module functions.
2247 */
2248 return ftrace_hash_empty(ops->filter_hash) &&
2249 ftrace_hash_empty(ops->notrace_hash);
2250}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002251
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002252/*
2253 * Check if the current ops references the record.
2254 *
2255 * If the ops traces all functions, then it was already accounted for.
2256 * If the ops does not trace the current record function, skip it.
2257 * If the ops ignores the function via notrace filter, skip it.
2258 */
2259static inline bool
2260ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2261{
2262 /* If ops isn't enabled, ignore it */
2263 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2264 return 0;
2265
2266 /* If ops traces all mods, we already accounted for it */
2267 if (ops_traces_mod(ops))
2268 return 0;
2269
2270 /* The function must be in the filter */
2271 if (!ftrace_hash_empty(ops->filter_hash) &&
2272 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2273 return 0;
2274
2275 /* If in notrace hash, we ignore it too */
2276 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2277 return 0;
2278
2279 return 1;
2280}
2281
2282static int referenced_filters(struct dyn_ftrace *rec)
2283{
2284 struct ftrace_ops *ops;
2285 int cnt = 0;
2286
2287 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2288 if (ops_references_rec(ops, rec))
2289 cnt++;
2290 }
2291
2292 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002293}
2294
Steven Rostedt31e88902008-11-14 16:21:19 -08002295static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002296{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002297 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002298 struct dyn_ftrace *p;
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302299 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002300 unsigned long ref = 0;
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002301 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002302 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002303
2304 /*
2305 * When adding a module, we need to check if tracers are
2306 * currently enabled and if they are set to trace all functions.
2307 * If they are, we need to enable the module functions as well
2308 * as update the reference counts for those function records.
2309 */
2310 if (mod) {
2311 struct ftrace_ops *ops;
2312
2313 for (ops = ftrace_ops_list;
2314 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002315 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2316 if (ops_traces_mod(ops))
2317 ref++;
2318 else
2319 test = true;
2320 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002321 }
2322 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002323
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002324 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002325 ftrace_update_cnt = 0;
2326
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002327 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302328
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002329 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002330 int cnt = ref;
2331
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002332 /* If something went wrong, bail without enabling anything */
2333 if (unlikely(ftrace_disabled))
2334 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002335
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002336 p = &pg->records[i];
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002337 if (test)
2338 cnt += referenced_filters(p);
2339 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302340
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002341 /*
2342 * Do the initial record conversion from mcount jump
2343 * to the NOP instructions.
2344 */
2345 if (!ftrace_code_disable(mod, p))
2346 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002347
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002348 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002349
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002350 /*
2351 * If the tracing is enabled, go ahead and enable the record.
2352 *
2353 * The reason not to enable the record immediatelly is the
2354 * inherent check of ftrace_make_nop/ftrace_make_call for
2355 * correct previous instructions. Making first the NOP
2356 * conversion puts the module to the correct state, thus
2357 * passing the ftrace_make_call check.
2358 */
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002359 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002360 int failed = __ftrace_replace_code(p, 1);
2361 if (failed)
2362 ftrace_bug(failed, p->ip);
2363 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002364 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002365 }
2366
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002367 ftrace_new_pgs = NULL;
2368
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002369 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002370 ftrace_update_time = stop - start;
2371 ftrace_update_tot_cnt += ftrace_update_cnt;
2372
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002373 return 0;
2374}
2375
Steven Rostedta7900872011-12-16 16:23:44 -05002376static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002377{
Steven Rostedta7900872011-12-16 16:23:44 -05002378 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002379 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002380
Steven Rostedta7900872011-12-16 16:23:44 -05002381 if (WARN_ON(!count))
2382 return -EINVAL;
2383
2384 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002385
2386 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002387 * We want to fill as much as possible. No more than a page
2388 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002389 */
Steven Rostedta7900872011-12-16 16:23:44 -05002390 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2391 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002392
Steven Rostedta7900872011-12-16 16:23:44 -05002393 again:
2394 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2395
2396 if (!pg->records) {
2397 /* if we can't allocate this size, try something smaller */
2398 if (!order)
2399 return -ENOMEM;
2400 order >>= 1;
2401 goto again;
2402 }
2403
2404 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2405 pg->size = cnt;
2406
2407 if (cnt > count)
2408 cnt = count;
2409
2410 return cnt;
2411}
2412
2413static struct ftrace_page *
2414ftrace_allocate_pages(unsigned long num_to_init)
2415{
2416 struct ftrace_page *start_pg;
2417 struct ftrace_page *pg;
2418 int order;
2419 int cnt;
2420
2421 if (!num_to_init)
2422 return 0;
2423
2424 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2425 if (!pg)
2426 return NULL;
2427
2428 /*
2429 * Try to allocate as much as possible in one continues
2430 * location that fills in all of the space. We want to
2431 * waste as little space as possible.
2432 */
2433 for (;;) {
2434 cnt = ftrace_allocate_records(pg, num_to_init);
2435 if (cnt < 0)
2436 goto free_pages;
2437
2438 num_to_init -= cnt;
2439 if (!num_to_init)
2440 break;
2441
2442 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2443 if (!pg->next)
2444 goto free_pages;
2445
2446 pg = pg->next;
2447 }
2448
2449 return start_pg;
2450
2451 free_pages:
2452 while (start_pg) {
2453 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2454 free_pages((unsigned long)pg->records, order);
2455 start_pg = pg->next;
2456 kfree(pg);
2457 pg = start_pg;
2458 }
2459 pr_info("ftrace: FAILED to allocate memory for functions\n");
2460 return NULL;
2461}
2462
2463static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2464{
2465 int cnt;
2466
2467 if (!num_to_init) {
2468 pr_info("ftrace: No functions to be traced?\n");
2469 return -1;
2470 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002471
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002472 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002473 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002474 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002475
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002476 return 0;
2477}
2478
Steven Rostedt5072c592008-05-12 21:20:43 +02002479#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2480
2481struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002482 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002483 loff_t func_pos;
2484 struct ftrace_page *pg;
2485 struct dyn_ftrace *func;
2486 struct ftrace_func_probe *probe;
2487 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002488 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002489 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002490 int hidx;
2491 int idx;
2492 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002493};
2494
Ingo Molnare309b412008-05-12 21:20:51 +02002495static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002496t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002497{
2498 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002499 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002500 struct hlist_head *hhd;
2501
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002502 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002503 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002504
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002505 if (iter->probe)
2506 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002507 retry:
2508 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2509 return NULL;
2510
2511 hhd = &ftrace_func_hash[iter->hidx];
2512
2513 if (hlist_empty(hhd)) {
2514 iter->hidx++;
2515 hnd = NULL;
2516 goto retry;
2517 }
2518
2519 if (!hnd)
2520 hnd = hhd->first;
2521 else {
2522 hnd = hnd->next;
2523 if (!hnd) {
2524 iter->hidx++;
2525 goto retry;
2526 }
2527 }
2528
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002529 if (WARN_ON_ONCE(!hnd))
2530 return NULL;
2531
2532 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2533
2534 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002535}
2536
2537static void *t_hash_start(struct seq_file *m, loff_t *pos)
2538{
2539 struct ftrace_iterator *iter = m->private;
2540 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002541 loff_t l;
2542
Steven Rostedt69a30832011-12-19 15:21:16 -05002543 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2544 return NULL;
2545
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002546 if (iter->func_pos > *pos)
2547 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002548
Li Zefand82d6242009-06-24 09:54:54 +08002549 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002550 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002551 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002552 if (!p)
2553 break;
2554 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002555 if (!p)
2556 return NULL;
2557
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002558 /* Only set this if we have an item */
2559 iter->flags |= FTRACE_ITER_HASH;
2560
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002561 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002562}
2563
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002564static int
2565t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002566{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002567 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002568
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002569 rec = iter->probe;
2570 if (WARN_ON_ONCE(!rec))
2571 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002572
Steven Rostedt809dcf22009-02-16 23:06:01 -05002573 if (rec->ops->print)
2574 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2575
Steven Rostedtb375a112009-09-17 00:05:58 -04002576 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002577
2578 if (rec->data)
2579 seq_printf(m, ":%p", rec->data);
2580 seq_putc(m, '\n');
2581
2582 return 0;
2583}
2584
2585static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002586t_next(struct seq_file *m, void *v, loff_t *pos)
2587{
2588 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002589 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002590 struct dyn_ftrace *rec = NULL;
2591
Steven Rostedt45a4a232011-04-21 23:16:46 -04002592 if (unlikely(ftrace_disabled))
2593 return NULL;
2594
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002595 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002596 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002597
Steven Rostedt5072c592008-05-12 21:20:43 +02002598 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002599 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002600
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002601 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002602 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002603
Steven Rostedt5072c592008-05-12 21:20:43 +02002604 retry:
2605 if (iter->idx >= iter->pg->index) {
2606 if (iter->pg->next) {
2607 iter->pg = iter->pg->next;
2608 iter->idx = 0;
2609 goto retry;
2610 }
2611 } else {
2612 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002613 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002614 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002615
Steven Rostedt41c52c02008-05-22 11:46:33 -04002616 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002617 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2618
2619 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002620 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002621
Steven Rostedt5072c592008-05-12 21:20:43 +02002622 rec = NULL;
2623 goto retry;
2624 }
2625 }
2626
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002627 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002628 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002629
2630 iter->func = rec;
2631
2632 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002633}
2634
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002635static void reset_iter_read(struct ftrace_iterator *iter)
2636{
2637 iter->pos = 0;
2638 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002639 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002640}
2641
2642static void *t_start(struct seq_file *m, loff_t *pos)
2643{
2644 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002645 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002646 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002647 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002648
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002649 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002650
2651 if (unlikely(ftrace_disabled))
2652 return NULL;
2653
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002654 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002655 * If an lseek was done, then reset and start from beginning.
2656 */
2657 if (*pos < iter->pos)
2658 reset_iter_read(iter);
2659
2660 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002661 * For set_ftrace_filter reading, if we have the filter
2662 * off, we can short cut and just print out that all
2663 * functions are enabled.
2664 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002665 if (iter->flags & FTRACE_ITER_FILTER &&
2666 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002667 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002668 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002669 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002670 /* reset in case of seek/pread */
2671 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002672 return iter;
2673 }
2674
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002675 if (iter->flags & FTRACE_ITER_HASH)
2676 return t_hash_start(m, pos);
2677
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002678 /*
2679 * Unfortunately, we need to restart at ftrace_pages_start
2680 * every time we let go of the ftrace_mutex. This is because
2681 * those pointers can change without the lock.
2682 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002683 iter->pg = ftrace_pages_start;
2684 iter->idx = 0;
2685 for (l = 0; l <= *pos; ) {
2686 p = t_next(m, p, &l);
2687 if (!p)
2688 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002689 }
walimis5821e1b2008-11-15 15:19:06 +08002690
Steven Rostedt69a30832011-12-19 15:21:16 -05002691 if (!p)
2692 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002693
2694 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002695}
2696
2697static void t_stop(struct seq_file *m, void *p)
2698{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002699 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002700}
2701
2702static int t_show(struct seq_file *m, void *v)
2703{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002704 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002705 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002706
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002707 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002708 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002709
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002710 if (iter->flags & FTRACE_ITER_PRINTALL) {
2711 seq_printf(m, "#### all functions enabled ####\n");
2712 return 0;
2713 }
2714
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002715 rec = iter->func;
2716
Steven Rostedt5072c592008-05-12 21:20:43 +02002717 if (!rec)
2718 return 0;
2719
Steven Rostedt647bcd02011-05-03 14:39:21 -04002720 seq_printf(m, "%ps", (void *)rec->ip);
2721 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002722 seq_printf(m, " (%ld)%s",
2723 rec->flags & ~FTRACE_FL_MASK,
2724 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002725 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002726
2727 return 0;
2728}
2729
James Morris88e9d342009-09-22 16:43:43 -07002730static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002731 .start = t_start,
2732 .next = t_next,
2733 .stop = t_stop,
2734 .show = t_show,
2735};
2736
Ingo Molnare309b412008-05-12 21:20:51 +02002737static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002738ftrace_avail_open(struct inode *inode, struct file *file)
2739{
2740 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002741
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002742 if (unlikely(ftrace_disabled))
2743 return -ENODEV;
2744
Jiri Olsa50e18b92012-04-25 10:23:39 +02002745 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2746 if (iter) {
2747 iter->pg = ftrace_pages_start;
2748 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002749 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002750
Jiri Olsa50e18b92012-04-25 10:23:39 +02002751 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002752}
2753
Steven Rostedt647bcd02011-05-03 14:39:21 -04002754static int
2755ftrace_enabled_open(struct inode *inode, struct file *file)
2756{
2757 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002758
2759 if (unlikely(ftrace_disabled))
2760 return -ENODEV;
2761
Jiri Olsa50e18b92012-04-25 10:23:39 +02002762 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2763 if (iter) {
2764 iter->pg = ftrace_pages_start;
2765 iter->flags = FTRACE_ITER_ENABLED;
2766 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002767 }
2768
Jiri Olsa50e18b92012-04-25 10:23:39 +02002769 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002770}
2771
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002772static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002773{
Steven Rostedt52baf112009-02-14 01:15:39 -05002774 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002775 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002776 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002777}
2778
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002779/**
2780 * ftrace_regex_open - initialize function tracer filter files
2781 * @ops: The ftrace_ops that hold the hash filters
2782 * @flag: The type of filter to process
2783 * @inode: The inode, usually passed in to your open routine
2784 * @file: The file, usually passed in to your open routine
2785 *
2786 * ftrace_regex_open() initializes the filter files for the
2787 * @ops. Depending on @flag it may process the filter hash or
2788 * the notrace hash of @ops. With this called from the open
2789 * routine, you can use ftrace_filter_write() for the write
2790 * routine if @flag has FTRACE_ITER_FILTER set, or
2791 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04002792 * ftrace_filter_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002793 * release must call ftrace_regex_release().
2794 */
2795int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002796ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002797 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002798{
2799 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002800 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002801 int ret = 0;
2802
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002803 ftrace_ops_init(ops);
2804
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002805 if (unlikely(ftrace_disabled))
2806 return -ENODEV;
2807
Steven Rostedt5072c592008-05-12 21:20:43 +02002808 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2809 if (!iter)
2810 return -ENOMEM;
2811
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002812 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2813 kfree(iter);
2814 return -ENOMEM;
2815 }
2816
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002817 iter->ops = ops;
2818 iter->flags = flag;
2819
2820 mutex_lock(&ops->regex_lock);
2821
Steven Rostedtf45948e2011-05-02 12:29:25 -04002822 if (flag & FTRACE_ITER_NOTRACE)
2823 hash = ops->notrace_hash;
2824 else
2825 hash = ops->filter_hash;
2826
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002827 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002828 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002829 if (!iter->hash) {
2830 trace_parser_put(&iter->parser);
2831 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002832 ret = -ENOMEM;
2833 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002834 }
2835 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002836
Steven Rostedt5072c592008-05-12 21:20:43 +02002837 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002838 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002839 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002840
2841 if (file->f_mode & FMODE_READ) {
2842 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002843
2844 ret = seq_open(file, &show_ftrace_seq_ops);
2845 if (!ret) {
2846 struct seq_file *m = file->private_data;
2847 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002848 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002849 /* Failed */
2850 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002851 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002852 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002853 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002854 } else
2855 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002856
2857 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002858 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002859
2860 return ret;
2861}
2862
Steven Rostedt41c52c02008-05-22 11:46:33 -04002863static int
2864ftrace_filter_open(struct inode *inode, struct file *file)
2865{
Steven Rostedt69a30832011-12-19 15:21:16 -05002866 return ftrace_regex_open(&global_ops,
2867 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2868 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002869}
2870
2871static int
2872ftrace_notrace_open(struct inode *inode, struct file *file)
2873{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002874 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002875 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002876}
2877
Steven Rostedt64e7c442009-02-13 17:08:48 -05002878static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002879{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002880 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002881 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002882
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002883 switch (type) {
2884 case MATCH_FULL:
2885 if (strcmp(str, regex) == 0)
2886 matched = 1;
2887 break;
2888 case MATCH_FRONT_ONLY:
2889 if (strncmp(str, regex, len) == 0)
2890 matched = 1;
2891 break;
2892 case MATCH_MIDDLE_ONLY:
2893 if (strstr(str, regex))
2894 matched = 1;
2895 break;
2896 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002897 slen = strlen(str);
2898 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002899 matched = 1;
2900 break;
2901 }
2902
2903 return matched;
2904}
2905
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002906static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002907enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002908{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002909 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002910 int ret = 0;
2911
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002912 entry = ftrace_lookup_ip(hash, rec->ip);
2913 if (not) {
2914 /* Do nothing if it doesn't exist */
2915 if (!entry)
2916 return 0;
2917
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002918 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002919 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002920 /* Do nothing if it exists */
2921 if (entry)
2922 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002923
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002924 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002925 }
2926 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002927}
2928
Steven Rostedt64e7c442009-02-13 17:08:48 -05002929static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002930ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2931 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002932{
2933 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002934 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002935
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002936 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2937
2938 if (mod) {
2939 /* module lookup requires matching the module */
2940 if (!modname || strcmp(modname, mod))
2941 return 0;
2942
2943 /* blank search means to match all funcs in the mod */
2944 if (!len)
2945 return 1;
2946 }
2947
Steven Rostedt64e7c442009-02-13 17:08:48 -05002948 return ftrace_match(str, regex, len, type);
2949}
2950
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002951static int
2952match_records(struct ftrace_hash *hash, char *buff,
2953 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002954{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002955 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002956 struct ftrace_page *pg;
2957 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002958 int type = MATCH_FULL;
2959 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002960 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002961 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002962
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002963 if (len) {
2964 type = filter_parse_regex(buff, len, &search, &not);
2965 search_len = strlen(search);
2966 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002967
Steven Rostedt52baf112009-02-14 01:15:39 -05002968 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002969
2970 if (unlikely(ftrace_disabled))
2971 goto out_unlock;
2972
Steven Rostedt265c8312009-02-13 12:43:56 -05002973 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002974 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002975 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002976 if (ret < 0) {
2977 found = ret;
2978 goto out_unlock;
2979 }
Li Zefan311d16d2009-12-08 11:15:11 +08002980 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002981 }
2982 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002983 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002984 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002985
2986 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002987}
2988
Steven Rostedt64e7c442009-02-13 17:08:48 -05002989static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002990ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002991{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002992 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002993}
2994
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002995static int
2996ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002997{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002998 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002999
Steven Rostedt64e7c442009-02-13 17:08:48 -05003000 /* blank or '*' mean the same */
3001 if (strcmp(buff, "*") == 0)
3002 buff[0] = 0;
3003
3004 /* handle the case of 'dont filter this module' */
3005 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3006 buff[0] = 0;
3007 not = 1;
3008 }
3009
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003010 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003011}
3012
Steven Rostedtf6180772009-02-14 00:40:25 -05003013/*
3014 * We register the module command as a template to show others how
3015 * to register the a command as well.
3016 */
3017
3018static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003019ftrace_mod_callback(struct ftrace_hash *hash,
3020 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003021{
3022 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003023 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003024
3025 /*
3026 * cmd == 'mod' because we only registered this func
3027 * for the 'mod' ftrace_func_command.
3028 * But if you register one func with multiple commands,
3029 * you can tell which command was used by the cmd
3030 * parameter.
3031 */
3032
3033 /* we must have a module name */
3034 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003035 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003036
3037 mod = strsep(&param, ":");
3038 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003039 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003040
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003041 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003042 if (!ret)
3043 ret = -EINVAL;
3044 if (ret < 0)
3045 return ret;
3046
3047 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003048}
3049
3050static struct ftrace_func_command ftrace_mod_cmd = {
3051 .name = "mod",
3052 .func = ftrace_mod_callback,
3053};
3054
3055static int __init ftrace_mod_cmd_init(void)
3056{
3057 return register_ftrace_command(&ftrace_mod_cmd);
3058}
Steven Rostedt6f415672012-10-05 12:13:07 -04003059core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003060
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003061static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003062 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003063{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003064 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003065 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003066 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003067
3068 key = hash_long(ip, FTRACE_HASH_BITS);
3069
3070 hhd = &ftrace_func_hash[key];
3071
3072 if (hlist_empty(hhd))
3073 return;
3074
3075 /*
3076 * Disable preemption for these calls to prevent a RCU grace
3077 * period. This syncs the hash iteration and freeing of items
3078 * on the hash. rcu_read_lock is too dangerous here.
3079 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003080 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003081 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003082 if (entry->ip == ip)
3083 entry->ops->func(ip, parent_ip, &entry->data);
3084 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003085 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003086}
3087
Steven Rostedtb6887d72009-02-17 12:32:04 -05003088static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003089{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003090 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003091 .flags = FTRACE_OPS_FL_INITIALIZED,
3092 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003093};
3094
Steven Rostedtb6887d72009-02-17 12:32:04 -05003095static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003096
Steven Rostedtb6887d72009-02-17 12:32:04 -05003097static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003098{
Steven Rostedtb8489142011-05-04 09:27:52 -04003099 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003100 int i;
3101
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003102 if (ftrace_probe_registered) {
3103 /* still need to update the function call sites */
3104 if (ftrace_enabled)
3105 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003106 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003107 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003108
3109 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3110 struct hlist_head *hhd = &ftrace_func_hash[i];
3111 if (hhd->first)
3112 break;
3113 }
3114 /* Nothing registered? */
3115 if (i == FTRACE_FUNC_HASHSIZE)
3116 return;
3117
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05003118 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003119
Steven Rostedtb6887d72009-02-17 12:32:04 -05003120 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003121}
3122
Steven Rostedtb6887d72009-02-17 12:32:04 -05003123static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003124{
3125 int i;
3126
Steven Rostedtb6887d72009-02-17 12:32:04 -05003127 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003128 return;
3129
3130 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3131 struct hlist_head *hhd = &ftrace_func_hash[i];
3132 if (hhd->first)
3133 return;
3134 }
3135
3136 /* no more funcs left */
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05003137 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003138
Steven Rostedtb6887d72009-02-17 12:32:04 -05003139 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003140}
3141
3142
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003143static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003144{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003145 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003146 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003147 kfree(entry);
3148}
3149
Steven Rostedt59df055f2009-02-14 15:29:06 -05003150int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003151register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003152 void *data)
3153{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003154 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003155 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3156 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003157 struct ftrace_page *pg;
3158 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003159 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003160 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003161 int count = 0;
3162 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003163 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003164
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003165 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003166 len = strlen(search);
3167
Steven Rostedtb6887d72009-02-17 12:32:04 -05003168 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003169 if (WARN_ON(not))
3170 return -EINVAL;
3171
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003172 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003173
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003174 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3175 if (!hash) {
3176 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003177 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003178 }
3179
3180 if (unlikely(ftrace_disabled)) {
3181 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003182 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003183 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003184
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003185 mutex_lock(&ftrace_lock);
3186
Steven Rostedt59df055f2009-02-14 15:29:06 -05003187 do_for_each_ftrace_rec(pg, rec) {
3188
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003189 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003190 continue;
3191
3192 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3193 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003194 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003195 if (!count)
3196 count = -ENOMEM;
3197 goto out_unlock;
3198 }
3199
3200 count++;
3201
3202 entry->data = data;
3203
3204 /*
3205 * The caller might want to do something special
3206 * for each function we find. We call the callback
3207 * to give the caller an opportunity to do so.
3208 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003209 if (ops->init) {
3210 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003211 /* caller does not like this func */
3212 kfree(entry);
3213 continue;
3214 }
3215 }
3216
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003217 ret = enter_record(hash, rec, 0);
3218 if (ret < 0) {
3219 kfree(entry);
3220 count = ret;
3221 goto out_unlock;
3222 }
3223
Steven Rostedt59df055f2009-02-14 15:29:06 -05003224 entry->ops = ops;
3225 entry->ip = rec->ip;
3226
3227 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3228 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3229
3230 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003231
3232 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3233 if (ret < 0)
3234 count = ret;
3235
Steven Rostedtb6887d72009-02-17 12:32:04 -05003236 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003237
3238 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003239 mutex_unlock(&ftrace_lock);
3240 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003241 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003242 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003243
3244 return count;
3245}
3246
3247enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003248 PROBE_TEST_FUNC = 1,
3249 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003250};
3251
3252static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003253__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003254 void *data, int flags)
3255{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003256 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003257 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003258 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003259 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003260 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003261 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003262 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003263 char str[KSYM_SYMBOL_LEN];
3264 int type = MATCH_FULL;
3265 int i, len = 0;
3266 char *search;
3267
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003268 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003269 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003270 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003271 int not;
3272
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003273 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003274 len = strlen(search);
3275
Steven Rostedtb6887d72009-02-17 12:32:04 -05003276 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003277 if (WARN_ON(not))
3278 return;
3279 }
3280
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003281 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003282
3283 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3284 if (!hash)
3285 /* Hmm, should report this somehow */
3286 goto out_unlock;
3287
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003288 INIT_LIST_HEAD(&free_list);
3289
Steven Rostedt59df055f2009-02-14 15:29:06 -05003290 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3291 struct hlist_head *hhd = &ftrace_func_hash[i];
3292
Sasha Levinb67bfe02013-02-27 17:06:00 -08003293 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003294
3295 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003296 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003297 continue;
3298
Steven Rostedtb6887d72009-02-17 12:32:04 -05003299 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003300 continue;
3301
3302 /* do this last, since it is the most expensive */
3303 if (glob) {
3304 kallsyms_lookup(entry->ip, NULL, NULL,
3305 NULL, str);
3306 if (!ftrace_match(str, glob, len, type))
3307 continue;
3308 }
3309
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003310 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3311 /* It is possible more than one entry had this ip */
3312 if (rec_entry)
3313 free_hash_entry(hash, rec_entry);
3314
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003315 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003316 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003317 }
3318 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003319 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003320 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003321 /*
3322 * Remove after the disable is called. Otherwise, if the last
3323 * probe is removed, a null hash means *all enabled*.
3324 */
3325 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003326 synchronize_sched();
3327 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3328 list_del(&entry->free_list);
3329 ftrace_free_entry(entry);
3330 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003331 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003332
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003333 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003334 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003335 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003336}
3337
3338void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003339unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003340 void *data)
3341{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003342 __unregister_ftrace_function_probe(glob, ops, data,
3343 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003344}
3345
3346void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003347unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003348{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003349 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003350}
3351
Steven Rostedtb6887d72009-02-17 12:32:04 -05003352void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003353{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003354 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003355}
3356
Steven Rostedtf6180772009-02-14 00:40:25 -05003357static LIST_HEAD(ftrace_commands);
3358static DEFINE_MUTEX(ftrace_cmd_mutex);
3359
3360int register_ftrace_command(struct ftrace_func_command *cmd)
3361{
3362 struct ftrace_func_command *p;
3363 int ret = 0;
3364
3365 mutex_lock(&ftrace_cmd_mutex);
3366 list_for_each_entry(p, &ftrace_commands, list) {
3367 if (strcmp(cmd->name, p->name) == 0) {
3368 ret = -EBUSY;
3369 goto out_unlock;
3370 }
3371 }
3372 list_add(&cmd->list, &ftrace_commands);
3373 out_unlock:
3374 mutex_unlock(&ftrace_cmd_mutex);
3375
3376 return ret;
3377}
3378
3379int unregister_ftrace_command(struct ftrace_func_command *cmd)
3380{
3381 struct ftrace_func_command *p, *n;
3382 int ret = -ENODEV;
3383
3384 mutex_lock(&ftrace_cmd_mutex);
3385 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3386 if (strcmp(cmd->name, p->name) == 0) {
3387 ret = 0;
3388 list_del_init(&p->list);
3389 goto out_unlock;
3390 }
3391 }
3392 out_unlock:
3393 mutex_unlock(&ftrace_cmd_mutex);
3394
3395 return ret;
3396}
3397
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003398static int ftrace_process_regex(struct ftrace_hash *hash,
3399 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003400{
Steven Rostedtf6180772009-02-14 00:40:25 -05003401 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003402 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003403 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003404
3405 func = strsep(&next, ":");
3406
3407 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003408 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003409 if (!ret)
3410 ret = -EINVAL;
3411 if (ret < 0)
3412 return ret;
3413 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003414 }
3415
Steven Rostedtf6180772009-02-14 00:40:25 -05003416 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003417
3418 command = strsep(&next, ":");
3419
Steven Rostedtf6180772009-02-14 00:40:25 -05003420 mutex_lock(&ftrace_cmd_mutex);
3421 list_for_each_entry(p, &ftrace_commands, list) {
3422 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003423 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003424 goto out_unlock;
3425 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003426 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003427 out_unlock:
3428 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003429
Steven Rostedtf6180772009-02-14 00:40:25 -05003430 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003431}
3432
Ingo Molnare309b412008-05-12 21:20:51 +02003433static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003434ftrace_regex_write(struct file *file, const char __user *ubuf,
3435 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003436{
3437 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003438 struct trace_parser *parser;
3439 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003440
Li Zefan4ba79782009-09-22 13:52:20 +08003441 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003442 return 0;
3443
Steven Rostedt5072c592008-05-12 21:20:43 +02003444 if (file->f_mode & FMODE_READ) {
3445 struct seq_file *m = file->private_data;
3446 iter = m->private;
3447 } else
3448 iter = file->private_data;
3449
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003450 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003451 return -ENODEV;
3452
3453 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003454
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003455 parser = &iter->parser;
3456 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003457
Li Zefan4ba79782009-09-22 13:52:20 +08003458 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003459 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003460 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003461 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003462 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003463 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003464 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003465 }
3466
Steven Rostedt5072c592008-05-12 21:20:43 +02003467 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003468 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003469 return ret;
3470}
3471
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003472ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003473ftrace_filter_write(struct file *file, const char __user *ubuf,
3474 size_t cnt, loff_t *ppos)
3475{
3476 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3477}
3478
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003479ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003480ftrace_notrace_write(struct file *file, const char __user *ubuf,
3481 size_t cnt, loff_t *ppos)
3482{
3483 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3484}
3485
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003486static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003487ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3488{
3489 struct ftrace_func_entry *entry;
3490
3491 if (!ftrace_location(ip))
3492 return -EINVAL;
3493
3494 if (remove) {
3495 entry = ftrace_lookup_ip(hash, ip);
3496 if (!entry)
3497 return -ENOENT;
3498 free_hash_entry(hash, entry);
3499 return 0;
3500 }
3501
3502 return add_hash_entry(hash, ip);
3503}
3504
3505static int
3506ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3507 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003508{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003509 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003510 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003511 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003512
Steven Rostedt936e0742011-05-05 22:54:01 -04003513 /* All global ops uses the global ops filters */
3514 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3515 ops = &global_ops;
3516
Steven Rostedt41c52c02008-05-22 11:46:33 -04003517 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003518 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003519
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003520 mutex_lock(&ops->regex_lock);
3521
Steven Rostedtf45948e2011-05-02 12:29:25 -04003522 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003523 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003524 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003525 orig_hash = &ops->notrace_hash;
3526
3527 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003528 if (!hash) {
3529 ret = -ENOMEM;
3530 goto out_regex_unlock;
3531 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003532
Steven Rostedt41c52c02008-05-22 11:46:33 -04003533 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003534 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003535 if (buf && !ftrace_match_records(hash, buf, len)) {
3536 ret = -EINVAL;
3537 goto out_regex_unlock;
3538 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003539 if (ip) {
3540 ret = ftrace_match_addr(hash, ip, remove);
3541 if (ret < 0)
3542 goto out_regex_unlock;
3543 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003544
3545 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003546 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003547 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3548 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003549 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003550
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003551 mutex_unlock(&ftrace_lock);
3552
Jiri Olsaac483c42012-01-02 10:04:14 +01003553 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003554 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003555
3556 free_ftrace_hash(hash);
3557 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003558}
3559
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003560static int
3561ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3562 int reset, int enable)
3563{
3564 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3565}
3566
3567/**
3568 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3569 * @ops - the ops to set the filter with
3570 * @ip - the address to add to or remove from the filter.
3571 * @remove - non zero to remove the ip from the filter
3572 * @reset - non zero to reset all filters before applying this filter.
3573 *
3574 * Filters denote which functions should be enabled when tracing is enabled
3575 * If @ip is NULL, it failes to update filter.
3576 */
3577int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3578 int remove, int reset)
3579{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003580 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003581 return ftrace_set_addr(ops, ip, remove, reset, 1);
3582}
3583EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3584
3585static int
3586ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3587 int reset, int enable)
3588{
3589 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3590}
3591
Steven Rostedt77a2b372008-05-12 21:20:45 +02003592/**
3593 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003594 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003595 * @buf - the string that holds the function filter text.
3596 * @len - the length of the string.
3597 * @reset - non zero to reset all filters before applying this filter.
3598 *
3599 * Filters denote which functions should be enabled when tracing is enabled.
3600 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3601 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003602int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003603 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003604{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003605 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003606 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003607}
Steven Rostedt936e0742011-05-05 22:54:01 -04003608EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003609
Steven Rostedt41c52c02008-05-22 11:46:33 -04003610/**
3611 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003612 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003613 * @buf - the string that holds the function notrace text.
3614 * @len - the length of the string.
3615 * @reset - non zero to reset all filters before applying this filter.
3616 *
3617 * Notrace Filters denote which functions should not be enabled when tracing
3618 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3619 * for tracing.
3620 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003621int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003622 int len, int reset)
3623{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003624 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003625 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003626}
3627EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3628/**
3629 * ftrace_set_filter - set a function to filter on in ftrace
3630 * @ops - the ops to set the filter with
3631 * @buf - the string that holds the function filter text.
3632 * @len - the length of the string.
3633 * @reset - non zero to reset all filters before applying this filter.
3634 *
3635 * Filters denote which functions should be enabled when tracing is enabled.
3636 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3637 */
3638void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3639{
3640 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3641}
3642EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3643
3644/**
3645 * ftrace_set_notrace - set a function to not trace in ftrace
3646 * @ops - the ops to set the notrace filter with
3647 * @buf - the string that holds the function notrace text.
3648 * @len - the length of the string.
3649 * @reset - non zero to reset all filters before applying this filter.
3650 *
3651 * Notrace Filters denote which functions should not be enabled when tracing
3652 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3653 * for tracing.
3654 */
3655void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003656{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003657 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003658}
Steven Rostedt936e0742011-05-05 22:54:01 -04003659EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003660
Steven Rostedt2af15d62009-05-28 13:37:24 -04003661/*
3662 * command line interface to allow users to set filters on boot up.
3663 */
3664#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3665static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3666static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3667
3668static int __init set_ftrace_notrace(char *str)
3669{
Chen Gang75761cc2013-04-08 12:12:39 +08003670 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003671 return 1;
3672}
3673__setup("ftrace_notrace=", set_ftrace_notrace);
3674
3675static int __init set_ftrace_filter(char *str)
3676{
Chen Gang75761cc2013-04-08 12:12:39 +08003677 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003678 return 1;
3679}
3680__setup("ftrace_filter=", set_ftrace_filter);
3681
Stefan Assmann369bc182009-10-12 22:17:21 +02003682#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003683static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003684static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3685
Stefan Assmann369bc182009-10-12 22:17:21 +02003686static int __init set_graph_function(char *str)
3687{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003688 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003689 return 1;
3690}
3691__setup("ftrace_graph_filter=", set_graph_function);
3692
3693static void __init set_ftrace_early_graph(char *buf)
3694{
3695 int ret;
3696 char *func;
3697
3698 while (buf) {
3699 func = strsep(&buf, ",");
3700 /* we allow only one expression at a time */
3701 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3702 func);
3703 if (ret)
3704 printk(KERN_DEBUG "ftrace: function %s not "
3705 "traceable\n", func);
3706 }
3707}
3708#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3709
Steven Rostedt2a85a372011-12-19 21:57:44 -05003710void __init
3711ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003712{
3713 char *func;
3714
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003715 ftrace_ops_init(ops);
3716
Steven Rostedt2af15d62009-05-28 13:37:24 -04003717 while (buf) {
3718 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003719 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003720 }
3721}
3722
3723static void __init set_ftrace_early_filters(void)
3724{
3725 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003726 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003727 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003728 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003729#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3730 if (ftrace_graph_buf[0])
3731 set_ftrace_early_graph(ftrace_graph_buf);
3732#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003733}
3734
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003735int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003736{
3737 struct seq_file *m = (struct seq_file *)file->private_data;
3738 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003739 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003740 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003741 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003742 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003743
Steven Rostedt5072c592008-05-12 21:20:43 +02003744 if (file->f_mode & FMODE_READ) {
3745 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003746 seq_release(inode, file);
3747 } else
3748 iter = file->private_data;
3749
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003750 parser = &iter->parser;
3751 if (trace_parser_loaded(parser)) {
3752 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003753 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003754 }
3755
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003756 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003757
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003758 mutex_lock(&iter->ops->regex_lock);
3759
Steven Rostedt058e2972011-04-29 22:35:33 -04003760 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003761 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3762
3763 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003764 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003765 else
3766 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003767
Steven Rostedt058e2972011-04-29 22:35:33 -04003768 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003769 ret = ftrace_hash_move(iter->ops, filter_hash,
3770 orig_hash, iter->hash);
3771 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3772 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003773 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003774
Steven Rostedt058e2972011-04-29 22:35:33 -04003775 mutex_unlock(&ftrace_lock);
3776 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003777
3778 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003779 free_ftrace_hash(iter->hash);
3780 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003781
Steven Rostedt5072c592008-05-12 21:20:43 +02003782 return 0;
3783}
3784
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003785static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003786 .open = ftrace_avail_open,
3787 .read = seq_read,
3788 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003789 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003790};
3791
Steven Rostedt647bcd02011-05-03 14:39:21 -04003792static const struct file_operations ftrace_enabled_fops = {
3793 .open = ftrace_enabled_open,
3794 .read = seq_read,
3795 .llseek = seq_lseek,
3796 .release = seq_release_private,
3797};
3798
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003799static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003800 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003801 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003802 .write = ftrace_filter_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003803 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003804 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003805};
3806
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003807static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003808 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003809 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003810 .write = ftrace_notrace_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003811 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003812 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003813};
3814
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003815#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3816
3817static DEFINE_MUTEX(graph_lock);
3818
3819int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003820int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003821unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3822
3823static void *
Li Zefan85951842009-06-24 09:54:00 +08003824__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003825{
Li Zefan85951842009-06-24 09:54:00 +08003826 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003827 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003828 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003829}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003830
Li Zefan85951842009-06-24 09:54:00 +08003831static void *
3832g_next(struct seq_file *m, void *v, loff_t *pos)
3833{
3834 (*pos)++;
3835 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003836}
3837
3838static void *g_start(struct seq_file *m, loff_t *pos)
3839{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003840 mutex_lock(&graph_lock);
3841
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003842 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003843 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003844 return (void *)1;
3845
Li Zefan85951842009-06-24 09:54:00 +08003846 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003847}
3848
3849static void g_stop(struct seq_file *m, void *p)
3850{
3851 mutex_unlock(&graph_lock);
3852}
3853
3854static int g_show(struct seq_file *m, void *v)
3855{
3856 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003857
3858 if (!ptr)
3859 return 0;
3860
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003861 if (ptr == (unsigned long *)1) {
3862 seq_printf(m, "#### all functions enabled ####\n");
3863 return 0;
3864 }
3865
Steven Rostedtb375a112009-09-17 00:05:58 -04003866 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003867
3868 return 0;
3869}
3870
James Morris88e9d342009-09-22 16:43:43 -07003871static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003872 .start = g_start,
3873 .next = g_next,
3874 .stop = g_stop,
3875 .show = g_show,
3876};
3877
3878static int
3879ftrace_graph_open(struct inode *inode, struct file *file)
3880{
3881 int ret = 0;
3882
3883 if (unlikely(ftrace_disabled))
3884 return -ENODEV;
3885
3886 mutex_lock(&graph_lock);
3887 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003888 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003889 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003890 ftrace_graph_count = 0;
3891 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3892 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003893 mutex_unlock(&graph_lock);
3894
Li Zefana4ec5e02009-09-18 14:06:28 +08003895 if (file->f_mode & FMODE_READ)
3896 ret = seq_open(file, &ftrace_graph_seq_ops);
3897
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003898 return ret;
3899}
3900
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003901static int
Li Zefan87827112009-07-23 11:29:11 +08003902ftrace_graph_release(struct inode *inode, struct file *file)
3903{
3904 if (file->f_mode & FMODE_READ)
3905 seq_release(inode, file);
3906 return 0;
3907}
3908
3909static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003910ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003911{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003912 struct dyn_ftrace *rec;
3913 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003914 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003915 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003916 int type, not;
3917 char *search;
3918 bool exists;
3919 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003920
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003921 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003922 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003923 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3924 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003925
3926 search_len = strlen(search);
3927
Steven Rostedt52baf112009-02-14 01:15:39 -05003928 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003929
3930 if (unlikely(ftrace_disabled)) {
3931 mutex_unlock(&ftrace_lock);
3932 return -ENODEV;
3933 }
3934
Steven Rostedt265c8312009-02-13 12:43:56 -05003935 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003936
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003937 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003938 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003939 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003940 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003941 if (array[i] == rec->ip) {
3942 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003943 break;
3944 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003945 }
3946
3947 if (!not) {
3948 fail = 0;
3949 if (!exists) {
3950 array[(*idx)++] = rec->ip;
3951 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3952 goto out;
3953 }
3954 } else {
3955 if (exists) {
3956 array[i] = array[--(*idx)];
3957 array[*idx] = 0;
3958 fail = 0;
3959 }
3960 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003961 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003962 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003963out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003964 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003965
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003966 if (fail)
3967 return -EINVAL;
3968
Namhyung Kim9f50afc2013-04-11 16:01:38 +09003969 ftrace_graph_filter_enabled = !!(*idx);
3970
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003971 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003972}
3973
3974static ssize_t
3975ftrace_graph_write(struct file *file, const char __user *ubuf,
3976 size_t cnt, loff_t *ppos)
3977{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003978 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003979 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003980
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003981 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003982 return 0;
3983
3984 mutex_lock(&graph_lock);
3985
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003986 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3987 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003988 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003989 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003990
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003991 read = trace_get_user(&parser, ubuf, cnt, ppos);
3992
Li Zefan4ba79782009-09-22 13:52:20 +08003993 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003994 parser.buffer[parser.idx] = 0;
3995
3996 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003997 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003998 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003999 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08004000 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004001 }
4002
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004003 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004004
4005out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004006 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08004007out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004008 mutex_unlock(&graph_lock);
4009
4010 return ret;
4011}
4012
4013static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004014 .open = ftrace_graph_open,
4015 .read = seq_read,
4016 .write = ftrace_graph_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09004017 .llseek = ftrace_filter_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004018 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004019};
4020#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4021
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004022static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004023{
Steven Rostedt5072c592008-05-12 21:20:43 +02004024
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004025 trace_create_file("available_filter_functions", 0444,
4026 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004027
Steven Rostedt647bcd02011-05-03 14:39:21 -04004028 trace_create_file("enabled_functions", 0444,
4029 d_tracer, NULL, &ftrace_enabled_fops);
4030
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004031 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4032 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004033
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004034 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004035 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004036
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004037#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004038 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004039 NULL,
4040 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004041#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4042
Steven Rostedt5072c592008-05-12 21:20:43 +02004043 return 0;
4044}
4045
Steven Rostedt9fd49322012-04-24 22:32:06 -04004046static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004047{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004048 const unsigned long *ipa = a;
4049 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004050
Steven Rostedt9fd49322012-04-24 22:32:06 -04004051 if (*ipa > *ipb)
4052 return 1;
4053 if (*ipa < *ipb)
4054 return -1;
4055 return 0;
4056}
4057
4058static void ftrace_swap_ips(void *a, void *b, int size)
4059{
4060 unsigned long *ipa = a;
4061 unsigned long *ipb = b;
4062 unsigned long t;
4063
4064 t = *ipa;
4065 *ipa = *ipb;
4066 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004067}
4068
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004069static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004070 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004071 unsigned long *end)
4072{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004073 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004074 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004075 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004076 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004077 unsigned long *p;
4078 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004079 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004080 int ret = -ENOMEM;
4081
4082 count = end - start;
4083
4084 if (!count)
4085 return 0;
4086
Steven Rostedt9fd49322012-04-24 22:32:06 -04004087 sort(start, count, sizeof(*start),
4088 ftrace_cmp_ips, ftrace_swap_ips);
4089
Steven Rostedt706c81f2012-04-24 23:45:26 -04004090 start_pg = ftrace_allocate_pages(count);
4091 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004092 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004093
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004094 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004095
Steven Rostedt32082302011-12-16 14:42:37 -05004096 /*
4097 * Core and each module needs their own pages, as
4098 * modules will free them when they are removed.
4099 * Force a new page to be allocated for modules.
4100 */
Steven Rostedta7900872011-12-16 16:23:44 -05004101 if (!mod) {
4102 WARN_ON(ftrace_pages || ftrace_pages_start);
4103 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004104 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004105 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004106 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004107 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004108
Steven Rostedta7900872011-12-16 16:23:44 -05004109 if (WARN_ON(ftrace_pages->next)) {
4110 /* Hmm, we have free pages? */
4111 while (ftrace_pages->next)
4112 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004113 }
Steven Rostedta7900872011-12-16 16:23:44 -05004114
Steven Rostedt706c81f2012-04-24 23:45:26 -04004115 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004116 }
4117
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004118 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004119 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004120 while (p < end) {
4121 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004122 /*
4123 * Some architecture linkers will pad between
4124 * the different mcount_loc sections of different
4125 * object files to satisfy alignments.
4126 * Skip any NULL pointers.
4127 */
4128 if (!addr)
4129 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004130
4131 if (pg->index == pg->size) {
4132 /* We should have allocated enough */
4133 if (WARN_ON(!pg->next))
4134 break;
4135 pg = pg->next;
4136 }
4137
4138 rec = &pg->records[pg->index++];
4139 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004140 }
4141
Steven Rostedt706c81f2012-04-24 23:45:26 -04004142 /* We should have used all pages */
4143 WARN_ON(pg->next);
4144
4145 /* Assign the last page to ftrace_pages */
4146 ftrace_pages = pg;
4147
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004148 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004149 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004150
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004151 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004152 * We only need to disable interrupts on start up
4153 * because we are modifying code that an interrupt
4154 * may execute, and the modification is not atomic.
4155 * But for modules, nothing runs the code we modify
4156 * until we are finished with it, and there's no
4157 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004158 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004159 if (!mod)
4160 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08004161 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004162 if (!mod)
4163 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004164 ret = 0;
4165 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004166 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004167
Steven Rostedta7900872011-12-16 16:23:44 -05004168 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004169}
4170
Steven Rostedt93eb6772009-04-15 13:24:06 -04004171#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004172
4173#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4174
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004175void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004176{
4177 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004178 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004179 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004180 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004181
Steven Rostedt93eb6772009-04-15 13:24:06 -04004182 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004183
4184 if (ftrace_disabled)
4185 goto out_unlock;
4186
Steven Rostedt32082302011-12-16 14:42:37 -05004187 /*
4188 * Each module has its own ftrace_pages, remove
4189 * them from the list.
4190 */
4191 last_pg = &ftrace_pages_start;
4192 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4193 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004194 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004195 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004196 * As core pages are first, the first
4197 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004198 */
Steven Rostedt32082302011-12-16 14:42:37 -05004199 if (WARN_ON(pg == ftrace_pages_start))
4200 goto out_unlock;
4201
4202 /* Check if we are deleting the last page */
4203 if (pg == ftrace_pages)
4204 ftrace_pages = next_to_ftrace_page(last_pg);
4205
4206 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004207 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4208 free_pages((unsigned long)pg->records, order);
4209 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004210 } else
4211 last_pg = &pg->next;
4212 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004213 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004214 mutex_unlock(&ftrace_lock);
4215}
4216
4217static void ftrace_init_module(struct module *mod,
4218 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004219{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004220 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004221 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004222 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004223}
4224
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004225static int ftrace_module_notify_enter(struct notifier_block *self,
4226 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004227{
4228 struct module *mod = data;
4229
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004230 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004231 ftrace_init_module(mod, mod->ftrace_callsites,
4232 mod->ftrace_callsites +
4233 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004234 return 0;
4235}
4236
4237static int ftrace_module_notify_exit(struct notifier_block *self,
4238 unsigned long val, void *data)
4239{
4240 struct module *mod = data;
4241
4242 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004243 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004244
4245 return 0;
4246}
4247#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004248static int ftrace_module_notify_enter(struct notifier_block *self,
4249 unsigned long val, void *data)
4250{
4251 return 0;
4252}
4253static int ftrace_module_notify_exit(struct notifier_block *self,
4254 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004255{
4256 return 0;
4257}
4258#endif /* CONFIG_MODULES */
4259
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004260struct notifier_block ftrace_module_enter_nb = {
4261 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004262 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004263};
4264
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004265struct notifier_block ftrace_module_exit_nb = {
4266 .notifier_call = ftrace_module_notify_exit,
4267 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4268};
4269
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004270extern unsigned long __start_mcount_loc[];
4271extern unsigned long __stop_mcount_loc[];
4272
4273void __init ftrace_init(void)
4274{
4275 unsigned long count, addr, flags;
4276 int ret;
4277
4278 /* Keep the ftrace pointer to the stub */
4279 addr = (unsigned long)ftrace_stub;
4280
4281 local_irq_save(flags);
4282 ftrace_dyn_arch_init(&addr);
4283 local_irq_restore(flags);
4284
4285 /* ftrace_dyn_arch_init places the return code in addr */
4286 if (addr)
4287 goto failed;
4288
4289 count = __stop_mcount_loc - __start_mcount_loc;
4290
4291 ret = ftrace_dyn_table_alloc(count);
4292 if (ret)
4293 goto failed;
4294
4295 last_ftrace_enabled = ftrace_enabled = 1;
4296
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004297 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004298 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004299 __stop_mcount_loc);
4300
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004301 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004302 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004303 pr_warning("Failed to register trace ftrace module enter notifier\n");
4304
4305 ret = register_module_notifier(&ftrace_module_exit_nb);
4306 if (ret)
4307 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004308
Steven Rostedt2af15d62009-05-28 13:37:24 -04004309 set_ftrace_early_filters();
4310
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004311 return;
4312 failed:
4313 ftrace_disabled = 1;
4314}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004315
Steven Rostedt3d083392008-05-12 21:20:42 +02004316#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004317
Steven Rostedt2b499382011-05-03 22:49:52 -04004318static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004319 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004320 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4321 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004322};
4323
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004324static int __init ftrace_nodyn_init(void)
4325{
4326 ftrace_enabled = 1;
4327 return 0;
4328}
Steven Rostedt6f415672012-10-05 12:13:07 -04004329core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004330
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004331static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4332static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004333/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004334# define ftrace_startup(ops, command) \
4335 ({ \
4336 int ___ret = __register_ftrace_function(ops); \
4337 if (!___ret) \
4338 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4339 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004340 })
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004341# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4342
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004343# define ftrace_startup_sysctl() do { } while (0)
4344# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004345
4346static inline int
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04004347ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004348{
4349 return 1;
4350}
4351
Steven Rostedt3d083392008-05-12 21:20:42 +02004352#endif /* CONFIG_DYNAMIC_FTRACE */
4353
Steven Rostedtb8489142011-05-04 09:27:52 -04004354static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004355ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004356 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004357{
Jiri Olsae2484912012-02-15 15:51:48 +01004358 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4359 return;
4360
4361 /*
4362 * Some of the ops may be dynamically allocated,
4363 * they must be freed after a synchronize_sched().
4364 */
4365 preempt_disable_notrace();
4366 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt0a016402012-11-02 17:03:03 -04004367 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004368 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4369 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04004370 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004371 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004372 } while_for_each_ftrace_op(op);
Jiri Olsae2484912012-02-15 15:51:48 +01004373 trace_recursion_clear(TRACE_CONTROL_BIT);
4374 preempt_enable_notrace();
4375}
4376
4377static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004378 .func = ftrace_ops_control_func,
4379 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4380 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004381};
4382
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004383static inline void
4384__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004385 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004386{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004387 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004388 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004389
Steven Rostedtccf36722012-06-05 09:44:25 -04004390 if (function_trace_stop)
4391 return;
4392
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004393 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4394 if (bit < 0)
4395 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004396
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004397 /*
4398 * Some of the ops may be dynamically allocated,
4399 * they must be freed after a synchronize_sched().
4400 */
4401 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004402 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04004403 if (ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004404 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004405 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004406 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004407 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004408}
4409
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004410/*
4411 * Some archs only support passing ip and parent_ip. Even though
4412 * the list function ignores the op parameter, we do not want any
4413 * C side effects, where a function is called without the caller
4414 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004415 * Archs are to support both the regs and ftrace_ops at the same time.
4416 * If they support ftrace_ops, it is assumed they support regs.
4417 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004418 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4419 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004420 * An architecture can pass partial regs with ftrace_ops and still
4421 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004422 */
4423#if ARCH_SUPPORTS_FTRACE_OPS
4424static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004425 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004426{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004427 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004428}
4429#else
4430static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4431{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004432 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004433}
4434#endif
4435
Steven Rostedte32d8952008-12-04 00:26:41 -05004436static void clear_ftrace_swapper(void)
4437{
4438 struct task_struct *p;
4439 int cpu;
4440
4441 get_online_cpus();
4442 for_each_online_cpu(cpu) {
4443 p = idle_task(cpu);
4444 clear_tsk_trace_trace(p);
4445 }
4446 put_online_cpus();
4447}
4448
4449static void set_ftrace_swapper(void)
4450{
4451 struct task_struct *p;
4452 int cpu;
4453
4454 get_online_cpus();
4455 for_each_online_cpu(cpu) {
4456 p = idle_task(cpu);
4457 set_tsk_trace_trace(p);
4458 }
4459 put_online_cpus();
4460}
4461
4462static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004463{
4464 struct task_struct *p;
4465
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004466 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004467 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004468 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004469 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004470 rcu_read_unlock();
4471
Steven Rostedte32d8952008-12-04 00:26:41 -05004472 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004473}
4474
Steven Rostedte32d8952008-12-04 00:26:41 -05004475static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004476{
4477 struct task_struct *p;
4478
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004479 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004480 do_each_pid_task(pid, PIDTYPE_PID, p) {
4481 set_tsk_trace_trace(p);
4482 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004483 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004484}
4485
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004486static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004487{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004488 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004489 clear_ftrace_swapper();
4490 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004491 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004492}
4493
4494static void set_ftrace_pid_task(struct pid *pid)
4495{
4496 if (pid == ftrace_swapper_pid)
4497 set_ftrace_swapper();
4498 else
4499 set_ftrace_pid(pid);
4500}
4501
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004502static int ftrace_pid_add(int p)
4503{
4504 struct pid *pid;
4505 struct ftrace_pid *fpid;
4506 int ret = -EINVAL;
4507
4508 mutex_lock(&ftrace_lock);
4509
4510 if (!p)
4511 pid = ftrace_swapper_pid;
4512 else
4513 pid = find_get_pid(p);
4514
4515 if (!pid)
4516 goto out;
4517
4518 ret = 0;
4519
4520 list_for_each_entry(fpid, &ftrace_pids, list)
4521 if (fpid->pid == pid)
4522 goto out_put;
4523
4524 ret = -ENOMEM;
4525
4526 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4527 if (!fpid)
4528 goto out_put;
4529
4530 list_add(&fpid->list, &ftrace_pids);
4531 fpid->pid = pid;
4532
4533 set_ftrace_pid_task(pid);
4534
4535 ftrace_update_pid_func();
4536 ftrace_startup_enable(0);
4537
4538 mutex_unlock(&ftrace_lock);
4539 return 0;
4540
4541out_put:
4542 if (pid != ftrace_swapper_pid)
4543 put_pid(pid);
4544
4545out:
4546 mutex_unlock(&ftrace_lock);
4547 return ret;
4548}
4549
4550static void ftrace_pid_reset(void)
4551{
4552 struct ftrace_pid *fpid, *safe;
4553
4554 mutex_lock(&ftrace_lock);
4555 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4556 struct pid *pid = fpid->pid;
4557
4558 clear_ftrace_pid_task(pid);
4559
4560 list_del(&fpid->list);
4561 kfree(fpid);
4562 }
4563
4564 ftrace_update_pid_func();
4565 ftrace_startup_enable(0);
4566
4567 mutex_unlock(&ftrace_lock);
4568}
4569
4570static void *fpid_start(struct seq_file *m, loff_t *pos)
4571{
4572 mutex_lock(&ftrace_lock);
4573
4574 if (list_empty(&ftrace_pids) && (!*pos))
4575 return (void *) 1;
4576
4577 return seq_list_start(&ftrace_pids, *pos);
4578}
4579
4580static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4581{
4582 if (v == (void *)1)
4583 return NULL;
4584
4585 return seq_list_next(v, &ftrace_pids, pos);
4586}
4587
4588static void fpid_stop(struct seq_file *m, void *p)
4589{
4590 mutex_unlock(&ftrace_lock);
4591}
4592
4593static int fpid_show(struct seq_file *m, void *v)
4594{
4595 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4596
4597 if (v == (void *)1) {
4598 seq_printf(m, "no pid\n");
4599 return 0;
4600 }
4601
4602 if (fpid->pid == ftrace_swapper_pid)
4603 seq_printf(m, "swapper tasks\n");
4604 else
4605 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4606
4607 return 0;
4608}
4609
4610static const struct seq_operations ftrace_pid_sops = {
4611 .start = fpid_start,
4612 .next = fpid_next,
4613 .stop = fpid_stop,
4614 .show = fpid_show,
4615};
4616
4617static int
4618ftrace_pid_open(struct inode *inode, struct file *file)
4619{
4620 int ret = 0;
4621
4622 if ((file->f_mode & FMODE_WRITE) &&
4623 (file->f_flags & O_TRUNC))
4624 ftrace_pid_reset();
4625
4626 if (file->f_mode & FMODE_READ)
4627 ret = seq_open(file, &ftrace_pid_sops);
4628
4629 return ret;
4630}
4631
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004632static ssize_t
4633ftrace_pid_write(struct file *filp, const char __user *ubuf,
4634 size_t cnt, loff_t *ppos)
4635{
Ingo Molnar457dc922009-11-23 11:03:28 +01004636 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004637 long val;
4638 int ret;
4639
4640 if (cnt >= sizeof(buf))
4641 return -EINVAL;
4642
4643 if (copy_from_user(&buf, ubuf, cnt))
4644 return -EFAULT;
4645
4646 buf[cnt] = 0;
4647
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004648 /*
4649 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4650 * to clean the filter quietly.
4651 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004652 tmp = strstrip(buf);
4653 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004654 return 1;
4655
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004656 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004657 if (ret < 0)
4658 return ret;
4659
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004660 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004661
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004662 return ret ? ret : cnt;
4663}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004664
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004665static int
4666ftrace_pid_release(struct inode *inode, struct file *file)
4667{
4668 if (file->f_mode & FMODE_READ)
4669 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004670
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004671 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004672}
4673
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004674static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004675 .open = ftrace_pid_open,
4676 .write = ftrace_pid_write,
4677 .read = seq_read,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09004678 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004679 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004680};
4681
4682static __init int ftrace_init_debugfs(void)
4683{
4684 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004685
4686 d_tracer = tracing_init_dentry();
4687 if (!d_tracer)
4688 return 0;
4689
4690 ftrace_init_dyn_debugfs(d_tracer);
4691
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004692 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4693 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004694
4695 ftrace_profile_debugfs(d_tracer);
4696
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004697 return 0;
4698}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004699fs_initcall(ftrace_init_debugfs);
4700
Steven Rostedt3d083392008-05-12 21:20:42 +02004701/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004702 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004703 *
4704 * This function should be used by panic code. It stops ftrace
4705 * but in a not so nice way. If you need to simply kill ftrace
4706 * from a non-atomic section, use ftrace_kill.
4707 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004708void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004709{
4710 ftrace_disabled = 1;
4711 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004712 clear_ftrace_function();
4713}
4714
4715/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004716 * Test if ftrace is dead or not.
4717 */
4718int ftrace_is_dead(void)
4719{
4720 return ftrace_disabled;
4721}
4722
4723/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004724 * register_ftrace_function - register a function for profiling
4725 * @ops - ops structure that holds the function for profiling.
4726 *
4727 * Register a function to be called by all functions in the
4728 * kernel.
4729 *
4730 * Note: @ops->func and all the functions it calls must be labeled
4731 * with "notrace", otherwise it will go into a
4732 * recursive loop.
4733 */
4734int register_ftrace_function(struct ftrace_ops *ops)
4735{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004736 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004737
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004738 ftrace_ops_init(ops);
4739
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004740 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004741
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004742 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004743
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004744 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004745
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004746 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004747}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004748EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004749
4750/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004751 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004752 * @ops - ops structure that holds the function to unregister
4753 *
4754 * Unregister a function that was added to be called by ftrace profiling.
4755 */
4756int unregister_ftrace_function(struct ftrace_ops *ops)
4757{
4758 int ret;
4759
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004760 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004761 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004762 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004763
4764 return ret;
4765}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004766EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004767
Ingo Molnare309b412008-05-12 21:20:51 +02004768int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004769ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004770 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004771 loff_t *ppos)
4772{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004773 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004774
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004775 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004776
Steven Rostedt45a4a232011-04-21 23:16:46 -04004777 if (unlikely(ftrace_disabled))
4778 goto out;
4779
4780 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004781
Li Zefana32c7762009-06-26 16:55:51 +08004782 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004783 goto out;
4784
Li Zefana32c7762009-06-26 16:55:51 +08004785 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004786
4787 if (ftrace_enabled) {
4788
4789 ftrace_startup_sysctl();
4790
4791 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004792 if (ftrace_ops_list != &ftrace_list_end)
4793 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004794
4795 } else {
4796 /* stopping ftrace calls (just send to ftrace_stub) */
4797 ftrace_trace_function = ftrace_stub;
4798
4799 ftrace_shutdown_sysctl();
4800 }
4801
4802 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004803 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004804 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004805}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004806
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004807#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004808
Steven Rostedt597af812009-04-03 15:24:12 -04004809static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004810static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004811
Steven Rostedte49dc192008-12-02 23:50:05 -05004812int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4813{
4814 return 0;
4815}
4816
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004817/* The callbacks that hook a function */
4818trace_func_graph_ret_t ftrace_graph_return =
4819 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004820trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt1499a3e2014-02-07 14:42:35 -05004821static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004822
4823/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4824static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4825{
4826 int i;
4827 int ret = 0;
4828 unsigned long flags;
4829 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4830 struct task_struct *g, *t;
4831
4832 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4833 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4834 * sizeof(struct ftrace_ret_stack),
4835 GFP_KERNEL);
4836 if (!ret_stack_list[i]) {
4837 start = 0;
4838 end = i;
4839 ret = -ENOMEM;
4840 goto free;
4841 }
4842 }
4843
4844 read_lock_irqsave(&tasklist_lock, flags);
4845 do_each_thread(g, t) {
4846 if (start == end) {
4847 ret = -EAGAIN;
4848 goto unlock;
4849 }
4850
4851 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004852 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004853 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004854 t->curr_ret_stack = -1;
4855 /* Make sure the tasks see the -1 first: */
4856 smp_wmb();
4857 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004858 }
4859 } while_each_thread(g, t);
4860
4861unlock:
4862 read_unlock_irqrestore(&tasklist_lock, flags);
4863free:
4864 for (i = start; i < end; i++)
4865 kfree(ret_stack_list[i]);
4866 return ret;
4867}
4868
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004869static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004870ftrace_graph_probe_sched_switch(void *ignore,
4871 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004872{
4873 unsigned long long timestamp;
4874 int index;
4875
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004876 /*
4877 * Does the user want to count the time a function was asleep.
4878 * If so, do not update the time stamps.
4879 */
4880 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4881 return;
4882
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004883 timestamp = trace_clock_local();
4884
4885 prev->ftrace_timestamp = timestamp;
4886
4887 /* only process tasks that we timestamped */
4888 if (!next->ftrace_timestamp)
4889 return;
4890
4891 /*
4892 * Update all the counters in next to make up for the
4893 * time next was sleeping.
4894 */
4895 timestamp -= next->ftrace_timestamp;
4896
4897 for (index = next->curr_ret_stack; index >= 0; index--)
4898 next->ret_stack[index].calltime += timestamp;
4899}
4900
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004901/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004902static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004903{
4904 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004905 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004906
4907 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4908 sizeof(struct ftrace_ret_stack *),
4909 GFP_KERNEL);
4910
4911 if (!ret_stack_list)
4912 return -ENOMEM;
4913
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004914 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004915 for_each_online_cpu(cpu) {
4916 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004917 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004918 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004919
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004920 do {
4921 ret = alloc_retstack_tasklist(ret_stack_list);
4922 } while (ret == -EAGAIN);
4923
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004924 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004925 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004926 if (ret)
4927 pr_info("ftrace_graph: Couldn't activate tracepoint"
4928 " probe to kernel_sched_switch\n");
4929 }
4930
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004931 kfree(ret_stack_list);
4932 return ret;
4933}
4934
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004935/*
4936 * Hibernation protection.
4937 * The state of the current task is too much unstable during
4938 * suspend/restore to disk. We want to protect against that.
4939 */
4940static int
4941ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4942 void *unused)
4943{
4944 switch (state) {
4945 case PM_HIBERNATION_PREPARE:
4946 pause_graph_tracing();
4947 break;
4948
4949 case PM_POST_HIBERNATION:
4950 unpause_graph_tracing();
4951 break;
4952 }
4953 return NOTIFY_DONE;
4954}
4955
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004956/* Just a place holder for function graph */
4957static struct ftrace_ops fgraph_ops __read_mostly = {
4958 .func = ftrace_stub,
4959 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
4960 FTRACE_OPS_FL_RECURSION_SAFE,
4961};
4962
Steven Rostedt1499a3e2014-02-07 14:42:35 -05004963static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
4964{
4965 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
4966 return 0;
4967 return __ftrace_graph_entry(trace);
4968}
4969
4970/*
4971 * The function graph tracer should only trace the functions defined
4972 * by set_ftrace_filter and set_ftrace_notrace. If another function
4973 * tracer ops is registered, the graph tracer requires testing the
4974 * function against the global ops, and not just trace any function
4975 * that any ftrace_ops registered.
4976 */
4977static void update_function_graph_func(void)
4978{
4979 if (ftrace_ops_list == &ftrace_list_end ||
4980 (ftrace_ops_list == &global_ops &&
4981 global_ops.next == &ftrace_list_end))
4982 ftrace_graph_entry = __ftrace_graph_entry;
4983 else
4984 ftrace_graph_entry = ftrace_graph_entry_test;
4985}
4986
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004987int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4988 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004989{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004990 int ret = 0;
4991
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004992 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004993
Steven Rostedt05ce5812009-03-24 00:18:31 -04004994 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004995 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004996 ret = -EBUSY;
4997 goto out;
4998 }
4999
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005000 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5001 register_pm_notifier(&ftrace_suspend_notifier);
5002
Steven Rostedt597af812009-04-03 15:24:12 -04005003 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005004 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005005 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005006 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005007 goto out;
5008 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005009
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005010 ftrace_graph_return = retfunc;
Steven Rostedt1499a3e2014-02-07 14:42:35 -05005011
5012 /*
5013 * Update the indirect function to the entryfunc, and the
5014 * function that gets called to the entry_test first. Then
5015 * call the update fgraph entry function to determine if
5016 * the entryfunc should be called directly or not.
5017 */
5018 __ftrace_graph_entry = entryfunc;
5019 ftrace_graph_entry = ftrace_graph_entry_test;
5020 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005021
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05005022 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005023
5024out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005025 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005026 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005027}
5028
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005029void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005030{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005031 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005032
Steven Rostedt597af812009-04-03 15:24:12 -04005033 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005034 goto out;
5035
Steven Rostedt597af812009-04-03 15:24:12 -04005036 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005037 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005038 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt1499a3e2014-02-07 14:42:35 -05005039 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05005040 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005041 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005042 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005043
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005044 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005045 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005046}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005047
Steven Rostedt868baf02011-02-10 21:26:13 -05005048static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5049
5050static void
5051graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5052{
5053 atomic_set(&t->tracing_graph_pause, 0);
5054 atomic_set(&t->trace_overrun, 0);
5055 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005056 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005057 smp_wmb();
5058 t->ret_stack = ret_stack;
5059}
5060
5061/*
5062 * Allocate a return stack for the idle task. May be the first
5063 * time through, or it may be done by CPU hotplug online.
5064 */
5065void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5066{
5067 t->curr_ret_stack = -1;
5068 /*
5069 * The idle task has no parent, it either has its own
5070 * stack or no stack at all.
5071 */
5072 if (t->ret_stack)
5073 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5074
5075 if (ftrace_graph_active) {
5076 struct ftrace_ret_stack *ret_stack;
5077
5078 ret_stack = per_cpu(idle_ret_stack, cpu);
5079 if (!ret_stack) {
5080 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5081 * sizeof(struct ftrace_ret_stack),
5082 GFP_KERNEL);
5083 if (!ret_stack)
5084 return;
5085 per_cpu(idle_ret_stack, cpu) = ret_stack;
5086 }
5087 graph_init_task(t, ret_stack);
5088 }
5089}
5090
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005091/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005092void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005093{
Steven Rostedt84047e32009-06-02 16:51:55 -04005094 /* Make sure we do not use the parent ret_stack */
5095 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005096 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005097
Steven Rostedt597af812009-04-03 15:24:12 -04005098 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005099 struct ftrace_ret_stack *ret_stack;
5100
5101 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005102 * sizeof(struct ftrace_ret_stack),
5103 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005104 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005105 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005106 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005107 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005108}
5109
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005110void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005111{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005112 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5113
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005114 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005115 /* NULL must become visible to IRQs before we free it: */
5116 barrier();
5117
5118 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005119}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005120
5121void ftrace_graph_stop(void)
5122{
5123 ftrace_stop();
5124}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005125#endif