blob: fc21312dad6d660a570bc5e1225938894b1dda22 [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 Rostedt2b499382011-05-03 22:49:52 -0400300static void update_ftrace_function(void)
301{
302 ftrace_func_t func;
303
304 update_global_ops();
305
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400306 /*
307 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400308 * recursion safe and not dynamic and the arch supports passing ops,
309 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400310 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400311 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400312 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400313 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400314 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400315 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400316 /* Set the ftrace_ops that the arch callback uses */
317 if (ftrace_ops_list == &global_ops)
Steven Rostedta0d0a2a2014-02-07 14:41:17 -0500318 set_function_trace_op = ftrace_global_list;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400319 else
Steven Rostedta0d0a2a2014-02-07 14:41:17 -0500320 set_function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400321 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400322 } else {
323 /* Just use the default ftrace_ops */
Steven Rostedta0d0a2a2014-02-07 14:41:17 -0500324 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400325 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400326 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400327
Steven Rostedta0d0a2a2014-02-07 14:41:17 -0500328 /* If there's no change, then do nothing more here */
329 if (ftrace_trace_function == func)
330 return;
331
332 /*
333 * If we are using the list function, it doesn't care
334 * about the function_trace_ops.
335 */
336 if (func == ftrace_ops_list_func) {
337 ftrace_trace_function = func;
338 /*
339 * Don't even bother setting function_trace_ops,
340 * it would be racy to do so anyway.
341 */
342 return;
343 }
344
345#ifndef CONFIG_DYNAMIC_FTRACE
346 /*
347 * For static tracing, we need to be a bit more careful.
348 * The function change takes affect immediately. Thus,
349 * we need to coorditate the setting of the function_trace_ops
350 * with the setting of the ftrace_trace_function.
351 *
352 * Set the function to the list ops, which will call the
353 * function we want, albeit indirectly, but it handles the
354 * ftrace_ops and doesn't depend on function_trace_op.
355 */
356 ftrace_trace_function = ftrace_ops_list_func;
357 /*
358 * Make sure all CPUs see this. Yes this is slow, but static
359 * tracing is slow and nasty to have enabled.
360 */
361 schedule_on_each_cpu(ftrace_sync);
362 /* Now all cpus are using the list ops. */
363 function_trace_op = set_function_trace_op;
364 /* Make sure the function_trace_op is visible on all CPUs */
365 smp_wmb();
366 /* Nasty way to force a rmb on all cpus */
367 smp_call_function(ftrace_sync_ipi, NULL, 1);
368 /* OK, we are all set to update the ftrace_trace_function now! */
369#endif /* !CONFIG_DYNAMIC_FTRACE */
370
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400371 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400372}
373
Steven Rostedt2b499382011-05-03 22:49:52 -0400374static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200375{
Steven Rostedt2b499382011-05-03 22:49:52 -0400376 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200377 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400378 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200379 * CPU might be walking that list. We need to make sure
380 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400381 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200382 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400383 rcu_assign_pointer(*list, ops);
384}
Steven Rostedt3d083392008-05-12 21:20:42 +0200385
Steven Rostedt2b499382011-05-03 22:49:52 -0400386static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
387{
388 struct ftrace_ops **p;
389
390 /*
391 * If we are removing the last function, then simply point
392 * to the ftrace_stub.
393 */
394 if (*list == ops && ops->next == &ftrace_list_end) {
395 *list = &ftrace_list_end;
396 return 0;
397 }
398
399 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
400 if (*p == ops)
401 break;
402
403 if (*p != ops)
404 return -1;
405
406 *p = (*p)->next;
407 return 0;
408}
409
Jiri Olsae2484912012-02-15 15:51:48 +0100410static void add_ftrace_list_ops(struct ftrace_ops **list,
411 struct ftrace_ops *main_ops,
412 struct ftrace_ops *ops)
413{
414 int first = *list == &ftrace_list_end;
415 add_ftrace_ops(list, ops);
416 if (first)
417 add_ftrace_ops(&ftrace_ops_list, main_ops);
418}
419
420static int remove_ftrace_list_ops(struct ftrace_ops **list,
421 struct ftrace_ops *main_ops,
422 struct ftrace_ops *ops)
423{
424 int ret = remove_ftrace_ops(list, ops);
425 if (!ret && *list == &ftrace_list_end)
426 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
427 return ret;
428}
429
Steven Rostedt2b499382011-05-03 22:49:52 -0400430static int __register_ftrace_function(struct ftrace_ops *ops)
431{
Steven Rostedt2b499382011-05-03 22:49:52 -0400432 if (FTRACE_WARN_ON(ops == &global_ops))
433 return -EINVAL;
434
Steven Rostedtb8489142011-05-04 09:27:52 -0400435 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
436 return -EBUSY;
437
Jiri Olsae2484912012-02-15 15:51:48 +0100438 /* We don't support both control and global flags set. */
439 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
440 return -EINVAL;
441
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900442#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400443 /*
444 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
445 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
446 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
447 */
448 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
449 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
450 return -EINVAL;
451
452 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
453 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
454#endif
455
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400456 if (!core_kernel_data((unsigned long)ops))
457 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
458
Steven Rostedtb8489142011-05-04 09:27:52 -0400459 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100460 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400461 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100462 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
463 if (control_ops_alloc(ops))
464 return -ENOMEM;
465 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400466 } else
467 add_ftrace_ops(&ftrace_ops_list, ops);
468
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400469 if (ftrace_enabled)
470 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200471
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200472 return 0;
473}
474
Ingo Molnare309b412008-05-12 21:20:51 +0200475static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200476{
Steven Rostedt2b499382011-05-03 22:49:52 -0400477 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200478
Steven Rostedtb8489142011-05-04 09:27:52 -0400479 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
480 return -EBUSY;
481
Steven Rostedt2b499382011-05-03 22:49:52 -0400482 if (FTRACE_WARN_ON(ops == &global_ops))
483 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200484
Steven Rostedtb8489142011-05-04 09:27:52 -0400485 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100486 ret = remove_ftrace_list_ops(&ftrace_global_list,
487 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400488 if (!ret)
489 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100490 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
491 ret = remove_ftrace_list_ops(&ftrace_control_list,
492 &control_ops, ops);
493 if (!ret) {
494 /*
495 * The ftrace_ops is now removed from the list,
496 * so there'll be no new users. We must ensure
497 * all current users are done before we free
498 * the control data.
499 */
500 synchronize_sched();
501 control_ops_free(ops);
502 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400503 } else
504 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
505
Steven Rostedt2b499382011-05-03 22:49:52 -0400506 if (ret < 0)
507 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400508
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400509 if (ftrace_enabled)
510 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200511
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400512 /*
513 * Dynamic ops may be freed, we must make sure that all
514 * callers are done before leaving this function.
515 */
516 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
517 synchronize_sched();
518
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500519 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200520}
521
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500522static void ftrace_update_pid_func(void)
523{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400524 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500525 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900526 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500527
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400528 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500529}
530
Steven Rostedt493762f2009-03-23 17:12:36 -0400531#ifdef CONFIG_FUNCTION_PROFILER
532struct ftrace_profile {
533 struct hlist_node node;
534 unsigned long ip;
535 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400536#ifdef CONFIG_FUNCTION_GRAPH_TRACER
537 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400538 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400539#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400540};
541
542struct ftrace_profile_page {
543 struct ftrace_profile_page *next;
544 unsigned long index;
545 struct ftrace_profile records[];
546};
547
Steven Rostedtcafb1682009-03-24 20:50:39 -0400548struct ftrace_profile_stat {
549 atomic_t disabled;
550 struct hlist_head *hash;
551 struct ftrace_profile_page *pages;
552 struct ftrace_profile_page *start;
553 struct tracer_stat stat;
554};
555
Steven Rostedt493762f2009-03-23 17:12:36 -0400556#define PROFILE_RECORDS_SIZE \
557 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
558
559#define PROFILES_PER_PAGE \
560 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
561
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400562static int ftrace_profile_enabled __read_mostly;
563
564/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400565static DEFINE_MUTEX(ftrace_profile_lock);
566
Steven Rostedtcafb1682009-03-24 20:50:39 -0400567static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400568
Namhyung Kim20079eb2013-04-10 08:55:50 +0900569#define FTRACE_PROFILE_HASH_BITS 10
570#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400571
Steven Rostedt493762f2009-03-23 17:12:36 -0400572static void *
573function_stat_next(void *v, int idx)
574{
575 struct ftrace_profile *rec = v;
576 struct ftrace_profile_page *pg;
577
578 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
579
580 again:
Li Zefan0296e422009-06-26 11:15:37 +0800581 if (idx != 0)
582 rec++;
583
Steven Rostedt493762f2009-03-23 17:12:36 -0400584 if ((void *)rec >= (void *)&pg->records[pg->index]) {
585 pg = pg->next;
586 if (!pg)
587 return NULL;
588 rec = &pg->records[0];
589 if (!rec->counter)
590 goto again;
591 }
592
593 return rec;
594}
595
596static void *function_stat_start(struct tracer_stat *trace)
597{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400598 struct ftrace_profile_stat *stat =
599 container_of(trace, struct ftrace_profile_stat, stat);
600
601 if (!stat || !stat->start)
602 return NULL;
603
604 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400605}
606
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400607#ifdef CONFIG_FUNCTION_GRAPH_TRACER
608/* function graph compares on total time */
609static int function_stat_cmp(void *p1, void *p2)
610{
611 struct ftrace_profile *a = p1;
612 struct ftrace_profile *b = p2;
613
614 if (a->time < b->time)
615 return -1;
616 if (a->time > b->time)
617 return 1;
618 else
619 return 0;
620}
621#else
622/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400623static int function_stat_cmp(void *p1, void *p2)
624{
625 struct ftrace_profile *a = p1;
626 struct ftrace_profile *b = p2;
627
628 if (a->counter < b->counter)
629 return -1;
630 if (a->counter > b->counter)
631 return 1;
632 else
633 return 0;
634}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400635#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400636
637static int function_stat_headers(struct seq_file *m)
638{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400639#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400640 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400641 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400642 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400643 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400644#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400645 seq_printf(m, " Function Hit\n"
646 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400647#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400648 return 0;
649}
650
651static int function_stat_show(struct seq_file *m, void *v)
652{
653 struct ftrace_profile *rec = v;
654 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800655 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400656#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400657 static struct trace_seq s;
658 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400659 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400660#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800661 mutex_lock(&ftrace_profile_lock);
662
663 /* we raced with function_profile_reset() */
664 if (unlikely(rec->counter == 0)) {
665 ret = -EBUSY;
666 goto out;
667 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400668
669 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400670 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400671
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400672#ifdef CONFIG_FUNCTION_GRAPH_TRACER
673 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400674 avg = rec->time;
675 do_div(avg, rec->counter);
676
Chase Douglase330b3b2010-04-26 14:02:05 -0400677 /* Sample standard deviation (s^2) */
678 if (rec->counter <= 1)
679 stddev = 0;
680 else {
681 stddev = rec->time_squared - rec->counter * avg * avg;
682 /*
683 * Divide only 1000 for ns^2 -> us^2 conversion.
684 * trace_print_graph_duration will divide 1000 again.
685 */
686 do_div(stddev, (rec->counter - 1) * 1000);
687 }
688
Steven Rostedt34886c82009-03-25 21:00:47 -0400689 trace_seq_init(&s);
690 trace_print_graph_duration(rec->time, &s);
691 trace_seq_puts(&s, " ");
692 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400693 trace_seq_puts(&s, " ");
694 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400695 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400696#endif
697 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800698out:
699 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400700
Li Zefan3aaba202010-08-23 16:50:12 +0800701 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400702}
703
Steven Rostedtcafb1682009-03-24 20:50:39 -0400704static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400705{
706 struct ftrace_profile_page *pg;
707
Steven Rostedtcafb1682009-03-24 20:50:39 -0400708 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400709
710 while (pg) {
711 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
712 pg->index = 0;
713 pg = pg->next;
714 }
715
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400717 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
718}
719
Steven Rostedtcafb1682009-03-24 20:50:39 -0400720int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400721{
722 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400723 int functions;
724 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400725 int i;
726
727 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400728 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400729 return 0;
730
Steven Rostedtcafb1682009-03-24 20:50:39 -0400731 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
732 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400733 return -ENOMEM;
734
Steven Rostedt318e0a72009-03-25 20:06:34 -0400735#ifdef CONFIG_DYNAMIC_FTRACE
736 functions = ftrace_update_tot_cnt;
737#else
738 /*
739 * We do not know the number of functions that exist because
740 * dynamic tracing is what counts them. With past experience
741 * we have around 20K functions. That should be more than enough.
742 * It is highly unlikely we will execute every function in
743 * the kernel.
744 */
745 functions = 20000;
746#endif
747
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400749
Steven Rostedt318e0a72009-03-25 20:06:34 -0400750 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
751
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900752 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400753 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400754 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400755 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400756 pg = pg->next;
757 }
758
759 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400760
761 out_free:
762 pg = stat->start;
763 while (pg) {
764 unsigned long tmp = (unsigned long)pg;
765
766 pg = pg->next;
767 free_page(tmp);
768 }
769
Steven Rostedt318e0a72009-03-25 20:06:34 -0400770 stat->pages = NULL;
771 stat->start = NULL;
772
773 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400774}
775
Steven Rostedtcafb1682009-03-24 20:50:39 -0400776static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400777{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400778 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400779 int size;
780
Steven Rostedtcafb1682009-03-24 20:50:39 -0400781 stat = &per_cpu(ftrace_profile_stats, cpu);
782
783 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400784 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400785 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400786 return 0;
787 }
788
789 /*
790 * We are profiling all functions, but usually only a few thousand
791 * functions are hit. We'll make a hash of 1024 items.
792 */
793 size = FTRACE_PROFILE_HASH_SIZE;
794
Steven Rostedtcafb1682009-03-24 20:50:39 -0400795 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400796
Steven Rostedtcafb1682009-03-24 20:50:39 -0400797 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400798 return -ENOMEM;
799
Steven Rostedt318e0a72009-03-25 20:06:34 -0400800 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400801 if (ftrace_profile_pages_init(stat) < 0) {
802 kfree(stat->hash);
803 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400804 return -ENOMEM;
805 }
806
807 return 0;
808}
809
Steven Rostedtcafb1682009-03-24 20:50:39 -0400810static int ftrace_profile_init(void)
811{
812 int cpu;
813 int ret = 0;
814
Miao Xie885154c2013-12-16 15:20:01 +0800815 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400816 ret = ftrace_profile_init_cpu(cpu);
817 if (ret)
818 break;
819 }
820
821 return ret;
822}
823
Steven Rostedt493762f2009-03-23 17:12:36 -0400824/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400825static struct ftrace_profile *
826ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400827{
828 struct ftrace_profile *rec;
829 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400830 unsigned long key;
831
Namhyung Kim20079eb2013-04-10 08:55:50 +0900832 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400833 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400834
835 if (hlist_empty(hhd))
836 return NULL;
837
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400838 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400839 if (rec->ip == ip)
840 return rec;
841 }
842
843 return NULL;
844}
845
Steven Rostedtcafb1682009-03-24 20:50:39 -0400846static void ftrace_add_profile(struct ftrace_profile_stat *stat,
847 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400848{
849 unsigned long key;
850
Namhyung Kim20079eb2013-04-10 08:55:50 +0900851 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400852 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400853}
854
Steven Rostedt318e0a72009-03-25 20:06:34 -0400855/*
856 * The memory is already allocated, this simply finds a new record to use.
857 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400858static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400859ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400860{
861 struct ftrace_profile *rec = NULL;
862
Steven Rostedt318e0a72009-03-25 20:06:34 -0400863 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400864 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400865 goto out;
866
Steven Rostedt493762f2009-03-23 17:12:36 -0400867 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400868 * Try to find the function again since an NMI
869 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400870 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400871 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400872 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400873 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400874
Steven Rostedtcafb1682009-03-24 20:50:39 -0400875 if (stat->pages->index == PROFILES_PER_PAGE) {
876 if (!stat->pages->next)
877 goto out;
878 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400879 }
880
Steven Rostedtcafb1682009-03-24 20:50:39 -0400881 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400882 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400883 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400884
Steven Rostedt493762f2009-03-23 17:12:36 -0400885 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400886 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400887
888 return rec;
889}
890
Steven Rostedt493762f2009-03-23 17:12:36 -0400891static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400892function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400893 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400894{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400895 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400896 struct ftrace_profile *rec;
897 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400898
899 if (!ftrace_profile_enabled)
900 return;
901
Steven Rostedt493762f2009-03-23 17:12:36 -0400902 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400903
904 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400905 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400906 goto out;
907
908 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400909 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400910 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400911 if (!rec)
912 goto out;
913 }
914
915 rec->counter++;
916 out:
917 local_irq_restore(flags);
918}
919
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400920#ifdef CONFIG_FUNCTION_GRAPH_TRACER
921static int profile_graph_entry(struct ftrace_graph_ent *trace)
922{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400923 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400924 return 1;
925}
926
927static void profile_graph_return(struct ftrace_graph_ret *trace)
928{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400929 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400930 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400931 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400932 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400933
934 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400935 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400936 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400937 goto out;
938
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400939 /* If the calltime was zero'd ignore it */
940 if (!trace->calltime)
941 goto out;
942
Steven Rostedta2a16d62009-03-24 23:17:58 -0400943 calltime = trace->rettime - trace->calltime;
944
945 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
946 int index;
947
948 index = trace->depth;
949
950 /* Append this call time to the parent time to subtract */
951 if (index)
952 current->ret_stack[index - 1].subtime += calltime;
953
954 if (current->ret_stack[index].subtime < calltime)
955 calltime -= current->ret_stack[index].subtime;
956 else
957 calltime = 0;
958 }
959
Steven Rostedtcafb1682009-03-24 20:50:39 -0400960 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400961 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400962 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400963 rec->time_squared += calltime * calltime;
964 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400965
Steven Rostedtcafb1682009-03-24 20:50:39 -0400966 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400967 local_irq_restore(flags);
968}
969
970static int register_ftrace_profiler(void)
971{
972 return register_ftrace_graph(&profile_graph_return,
973 &profile_graph_entry);
974}
975
976static void unregister_ftrace_profiler(void)
977{
978 unregister_ftrace_graph();
979}
980#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100981static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400982 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900983 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
984 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400985};
986
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400987static int register_ftrace_profiler(void)
988{
989 return register_ftrace_function(&ftrace_profile_ops);
990}
991
992static void unregister_ftrace_profiler(void)
993{
994 unregister_ftrace_function(&ftrace_profile_ops);
995}
996#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
997
Steven Rostedt493762f2009-03-23 17:12:36 -0400998static ssize_t
999ftrace_profile_write(struct file *filp, const char __user *ubuf,
1000 size_t cnt, loff_t *ppos)
1001{
1002 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -04001003 int ret;
1004
Peter Huewe22fe9b52011-06-07 21:58:27 +02001005 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1006 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -04001007 return ret;
1008
1009 val = !!val;
1010
1011 mutex_lock(&ftrace_profile_lock);
1012 if (ftrace_profile_enabled ^ val) {
1013 if (val) {
1014 ret = ftrace_profile_init();
1015 if (ret < 0) {
1016 cnt = ret;
1017 goto out;
1018 }
1019
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001020 ret = register_ftrace_profiler();
1021 if (ret < 0) {
1022 cnt = ret;
1023 goto out;
1024 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001025 ftrace_profile_enabled = 1;
1026 } else {
1027 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -04001028 /*
1029 * unregister_ftrace_profiler calls stop_machine
1030 * so this acts like an synchronize_sched.
1031 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001032 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -04001033 }
1034 }
1035 out:
1036 mutex_unlock(&ftrace_profile_lock);
1037
Jiri Olsacf8517c2009-10-23 19:36:16 -04001038 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001039
1040 return cnt;
1041}
1042
1043static ssize_t
1044ftrace_profile_read(struct file *filp, char __user *ubuf,
1045 size_t cnt, loff_t *ppos)
1046{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001047 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001048 int r;
1049
1050 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1051 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1052}
1053
1054static const struct file_operations ftrace_profile_fops = {
1055 .open = tracing_open_generic,
1056 .read = ftrace_profile_read,
1057 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001058 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001059};
1060
Steven Rostedtcafb1682009-03-24 20:50:39 -04001061/* used to initialize the real stat files */
1062static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001063 .name = "functions",
1064 .stat_start = function_stat_start,
1065 .stat_next = function_stat_next,
1066 .stat_cmp = function_stat_cmp,
1067 .stat_headers = function_stat_headers,
1068 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001069};
1070
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001071static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001072{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001073 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001074 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001075 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001076 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001077 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001078
Steven Rostedtcafb1682009-03-24 20:50:39 -04001079 for_each_possible_cpu(cpu) {
1080 stat = &per_cpu(ftrace_profile_stats, cpu);
1081
1082 /* allocate enough for function name + cpu number */
1083 name = kmalloc(32, GFP_KERNEL);
1084 if (!name) {
1085 /*
1086 * The files created are permanent, if something happens
1087 * we still do not free memory.
1088 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001089 WARN(1,
1090 "Could not allocate stat file for cpu %d\n",
1091 cpu);
1092 return;
1093 }
1094 stat->stat = function_stats;
1095 snprintf(name, 32, "function%d", cpu);
1096 stat->stat.name = name;
1097 ret = register_stat_tracer(&stat->stat);
1098 if (ret) {
1099 WARN(1,
1100 "Could not register function stat for cpu %d\n",
1101 cpu);
1102 kfree(name);
1103 return;
1104 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001105 }
1106
1107 entry = debugfs_create_file("function_profile_enabled", 0644,
1108 d_tracer, NULL, &ftrace_profile_fops);
1109 if (!entry)
1110 pr_warning("Could not create debugfs "
1111 "'function_profile_enabled' entry\n");
1112}
1113
1114#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001115static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001116{
1117}
1118#endif /* CONFIG_FUNCTION_PROFILER */
1119
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001120static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1121
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04001122loff_t
1123ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1124{
1125 loff_t ret;
1126
1127 if (file->f_mode & FMODE_READ)
1128 ret = seq_lseek(file, offset, whence);
1129 else
1130 file->f_pos = ret = 1;
1131
1132 return ret;
1133}
1134
Steven Rostedt3d083392008-05-12 21:20:42 +02001135#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001136
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001137#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001138# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001139#endif
1140
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001141static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1142
Steven Rostedtb6887d72009-02-17 12:32:04 -05001143struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001144 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001145 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001146 unsigned long flags;
1147 unsigned long ip;
1148 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001149 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001150};
1151
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001152struct ftrace_func_entry {
1153 struct hlist_node hlist;
1154 unsigned long ip;
1155};
1156
1157struct ftrace_hash {
1158 unsigned long size_bits;
1159 struct hlist_head *buckets;
1160 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001161 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001162};
1163
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001164/*
1165 * We make these constant because no one should touch them,
1166 * but they are used as the default "empty hash", to avoid allocating
1167 * it all the time. These are in a read only section such that if
1168 * anyone does try to modify it, it will cause an exception.
1169 */
1170static const struct hlist_head empty_buckets[1];
1171static const struct ftrace_hash empty_hash = {
1172 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001173};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001174#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001175
Steven Rostedt2b499382011-05-03 22:49:52 -04001176static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001177 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001178 .notrace_hash = EMPTY_HASH,
1179 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001180 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1181 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001182};
1183
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001184struct ftrace_page {
1185 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001186 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001187 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001188 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001189};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001190
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001191static struct ftrace_page *ftrace_new_pgs;
1192
Steven Rostedta7900872011-12-16 16:23:44 -05001193#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1194#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001195
1196/* estimate from running different kernels */
1197#define NR_TO_INIT 10000
1198
1199static struct ftrace_page *ftrace_pages_start;
1200static struct ftrace_page *ftrace_pages;
1201
Steven Rostedt06a51d92011-12-19 19:07:36 -05001202static bool ftrace_hash_empty(struct ftrace_hash *hash)
1203{
1204 return !hash || !hash->count;
1205}
1206
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001207static struct ftrace_func_entry *
1208ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1209{
1210 unsigned long key;
1211 struct ftrace_func_entry *entry;
1212 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001213
Steven Rostedt06a51d92011-12-19 19:07:36 -05001214 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001215 return NULL;
1216
1217 if (hash->size_bits > 0)
1218 key = hash_long(ip, hash->size_bits);
1219 else
1220 key = 0;
1221
1222 hhd = &hash->buckets[key];
1223
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001224 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001225 if (entry->ip == ip)
1226 return entry;
1227 }
1228 return NULL;
1229}
1230
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001231static void __add_hash_entry(struct ftrace_hash *hash,
1232 struct ftrace_func_entry *entry)
1233{
1234 struct hlist_head *hhd;
1235 unsigned long key;
1236
1237 if (hash->size_bits)
1238 key = hash_long(entry->ip, hash->size_bits);
1239 else
1240 key = 0;
1241
1242 hhd = &hash->buckets[key];
1243 hlist_add_head(&entry->hlist, hhd);
1244 hash->count++;
1245}
1246
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001247static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1248{
1249 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001250
1251 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1252 if (!entry)
1253 return -ENOMEM;
1254
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001255 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001256 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001257
1258 return 0;
1259}
1260
1261static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001262free_hash_entry(struct ftrace_hash *hash,
1263 struct ftrace_func_entry *entry)
1264{
1265 hlist_del(&entry->hlist);
1266 kfree(entry);
1267 hash->count--;
1268}
1269
1270static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001271remove_hash_entry(struct ftrace_hash *hash,
1272 struct ftrace_func_entry *entry)
1273{
1274 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001275 hash->count--;
1276}
1277
1278static void ftrace_hash_clear(struct ftrace_hash *hash)
1279{
1280 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001281 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001282 struct ftrace_func_entry *entry;
1283 int size = 1 << hash->size_bits;
1284 int i;
1285
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001286 if (!hash->count)
1287 return;
1288
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001289 for (i = 0; i < size; i++) {
1290 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001291 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001292 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001293 }
1294 FTRACE_WARN_ON(hash->count);
1295}
1296
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001297static void free_ftrace_hash(struct ftrace_hash *hash)
1298{
1299 if (!hash || hash == EMPTY_HASH)
1300 return;
1301 ftrace_hash_clear(hash);
1302 kfree(hash->buckets);
1303 kfree(hash);
1304}
1305
Steven Rostedt07fd5512011-05-05 18:03:47 -04001306static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1307{
1308 struct ftrace_hash *hash;
1309
1310 hash = container_of(rcu, struct ftrace_hash, rcu);
1311 free_ftrace_hash(hash);
1312}
1313
1314static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1315{
1316 if (!hash || hash == EMPTY_HASH)
1317 return;
1318 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1319}
1320
Jiri Olsa5500fa52012-02-15 15:51:54 +01001321void ftrace_free_filter(struct ftrace_ops *ops)
1322{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001323 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001324 free_ftrace_hash(ops->filter_hash);
1325 free_ftrace_hash(ops->notrace_hash);
1326}
1327
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1329{
1330 struct ftrace_hash *hash;
1331 int size;
1332
1333 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1334 if (!hash)
1335 return NULL;
1336
1337 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001338 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001339
1340 if (!hash->buckets) {
1341 kfree(hash);
1342 return NULL;
1343 }
1344
1345 hash->size_bits = size_bits;
1346
1347 return hash;
1348}
1349
1350static struct ftrace_hash *
1351alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1352{
1353 struct ftrace_func_entry *entry;
1354 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001355 int size;
1356 int ret;
1357 int i;
1358
1359 new_hash = alloc_ftrace_hash(size_bits);
1360 if (!new_hash)
1361 return NULL;
1362
1363 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001364 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001365 return new_hash;
1366
1367 size = 1 << hash->size_bits;
1368 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001369 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001370 ret = add_hash_entry(new_hash, entry->ip);
1371 if (ret < 0)
1372 goto free_hash;
1373 }
1374 }
1375
1376 FTRACE_WARN_ON(new_hash->count != hash->count);
1377
1378 return new_hash;
1379
1380 free_hash:
1381 free_ftrace_hash(new_hash);
1382 return NULL;
1383}
1384
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001385static void
1386ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1387static void
1388ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1389
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001390static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001391ftrace_hash_move(struct ftrace_ops *ops, int enable,
1392 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001393{
1394 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001395 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001396 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001397 struct ftrace_hash *old_hash;
1398 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001399 int size = src->count;
1400 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001401 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001402 int i;
1403
1404 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001405 * Remove the current set, update the hash and add
1406 * them back.
1407 */
1408 ftrace_hash_rec_disable(ops, enable);
1409
1410 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001411 * If the new source is empty, just free dst and assign it
1412 * the empty_hash.
1413 */
1414 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001415 free_ftrace_hash_rcu(*dst);
1416 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001417 /* still need to update the function records */
1418 ret = 0;
1419 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001420 }
1421
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001422 /*
1423 * Make the hash size about 1/2 the # found
1424 */
1425 for (size /= 2; size; size >>= 1)
1426 bits++;
1427
1428 /* Don't allocate too much */
1429 if (bits > FTRACE_HASH_MAX_BITS)
1430 bits = FTRACE_HASH_MAX_BITS;
1431
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001432 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001433 new_hash = alloc_ftrace_hash(bits);
1434 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001435 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001436
1437 size = 1 << src->size_bits;
1438 for (i = 0; i < size; i++) {
1439 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001440 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001441 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001442 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001443 }
1444 }
1445
Steven Rostedt07fd5512011-05-05 18:03:47 -04001446 old_hash = *dst;
1447 rcu_assign_pointer(*dst, new_hash);
1448 free_ftrace_hash_rcu(old_hash);
1449
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001450 ret = 0;
1451 out:
1452 /*
1453 * Enable regardless of ret:
1454 * On success, we enable the new hash.
1455 * On failure, we re-enable the original hash.
1456 */
1457 ftrace_hash_rec_enable(ops, enable);
1458
1459 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001460}
1461
Steven Rostedt265c8312009-02-13 12:43:56 -05001462/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001463 * Test the hashes for this ops to see if we want to call
1464 * the ops->func or not.
1465 *
1466 * It's a match if the ip is in the ops->filter_hash or
1467 * the filter_hash does not exist or is empty,
1468 * AND
1469 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001470 *
1471 * This needs to be called with preemption disabled as
1472 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001473 */
1474static int
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04001475ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001476{
1477 struct ftrace_hash *filter_hash;
1478 struct ftrace_hash *notrace_hash;
1479 int ret;
1480
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04001481#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1482 /*
1483 * There's a small race when adding ops that the ftrace handler
1484 * that wants regs, may be called without them. We can not
1485 * allow that handler to be called if regs is NULL.
1486 */
1487 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1488 return 0;
1489#endif
1490
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001491 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1492 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001493
Steven Rostedt06a51d92011-12-19 19:07:36 -05001494 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001495 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001496 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001497 !ftrace_lookup_ip(notrace_hash, ip)))
1498 ret = 1;
1499 else
1500 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001501
1502 return ret;
1503}
1504
1505/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001506 * This is a double for. Do not use 'break' to break out of the loop,
1507 * you must use a goto.
1508 */
1509#define do_for_each_ftrace_rec(pg, rec) \
1510 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1511 int _____i; \
1512 for (_____i = 0; _____i < pg->index; _____i++) { \
1513 rec = &pg->records[_____i];
1514
1515#define while_for_each_ftrace_rec() \
1516 } \
1517 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301518
Steven Rostedt5855fea2011-12-16 19:27:42 -05001519
1520static int ftrace_cmp_recs(const void *a, const void *b)
1521{
Steven Rostedta650e022012-04-25 13:48:13 -04001522 const struct dyn_ftrace *key = a;
1523 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001524
Steven Rostedta650e022012-04-25 13:48:13 -04001525 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001526 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001527 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1528 return 1;
1529 return 0;
1530}
1531
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001532static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001533{
1534 struct ftrace_page *pg;
1535 struct dyn_ftrace *rec;
1536 struct dyn_ftrace key;
1537
1538 key.ip = start;
1539 key.flags = end; /* overload flags, as it is unsigned long */
1540
1541 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1542 if (end < pg->records[0].ip ||
1543 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1544 continue;
1545 rec = bsearch(&key, pg->records, pg->index,
1546 sizeof(struct dyn_ftrace),
1547 ftrace_cmp_recs);
1548 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001549 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001550 }
1551
Steven Rostedt5855fea2011-12-16 19:27:42 -05001552 return 0;
1553}
1554
Steven Rostedtc88fd862011-08-16 09:53:39 -04001555/**
1556 * ftrace_location - return true if the ip giving is a traced location
1557 * @ip: the instruction pointer to check
1558 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001559 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001560 * That is, the instruction that is either a NOP or call to
1561 * the function tracer. It checks the ftrace internal tables to
1562 * determine if the address belongs or not.
1563 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001564unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001565{
Steven Rostedta650e022012-04-25 13:48:13 -04001566 return ftrace_location_range(ip, ip);
1567}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001568
Steven Rostedta650e022012-04-25 13:48:13 -04001569/**
1570 * ftrace_text_reserved - return true if range contains an ftrace location
1571 * @start: start of range to search
1572 * @end: end of range to search (inclusive). @end points to the last byte to check.
1573 *
1574 * Returns 1 if @start and @end contains a ftrace location.
1575 * That is, the instruction that is either a NOP or call to
1576 * the function tracer. It checks the ftrace internal tables to
1577 * determine if the address belongs or not.
1578 */
1579int ftrace_text_reserved(void *start, void *end)
1580{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001581 unsigned long ret;
1582
1583 ret = ftrace_location_range((unsigned long)start,
1584 (unsigned long)end);
1585
1586 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001587}
1588
Steven Rostedted926f92011-05-03 13:25:24 -04001589static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1590 int filter_hash,
1591 bool inc)
1592{
1593 struct ftrace_hash *hash;
1594 struct ftrace_hash *other_hash;
1595 struct ftrace_page *pg;
1596 struct dyn_ftrace *rec;
1597 int count = 0;
1598 int all = 0;
1599
1600 /* Only update if the ops has been registered */
1601 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1602 return;
1603
1604 /*
1605 * In the filter_hash case:
1606 * If the count is zero, we update all records.
1607 * Otherwise we just update the items in the hash.
1608 *
1609 * In the notrace_hash case:
1610 * We enable the update in the hash.
1611 * As disabling notrace means enabling the tracing,
1612 * and enabling notrace means disabling, the inc variable
1613 * gets inversed.
1614 */
1615 if (filter_hash) {
1616 hash = ops->filter_hash;
1617 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001618 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001619 all = 1;
1620 } else {
1621 inc = !inc;
1622 hash = ops->notrace_hash;
1623 other_hash = ops->filter_hash;
1624 /*
1625 * If the notrace hash has no items,
1626 * then there's nothing to do.
1627 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001628 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001629 return;
1630 }
1631
1632 do_for_each_ftrace_rec(pg, rec) {
1633 int in_other_hash = 0;
1634 int in_hash = 0;
1635 int match = 0;
1636
1637 if (all) {
1638 /*
1639 * Only the filter_hash affects all records.
1640 * Update if the record is not in the notrace hash.
1641 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001642 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001643 match = 1;
1644 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001645 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1646 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001647
1648 /*
1649 *
1650 */
1651 if (filter_hash && in_hash && !in_other_hash)
1652 match = 1;
1653 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001654 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001655 match = 1;
1656 }
1657 if (!match)
1658 continue;
1659
1660 if (inc) {
1661 rec->flags++;
1662 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1663 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001664 /*
1665 * If any ops wants regs saved for this function
1666 * then all ops will get saved regs.
1667 */
1668 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1669 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001670 } else {
1671 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1672 return;
1673 rec->flags--;
1674 }
1675 count++;
1676 /* Shortcut, if we handled all records, we are done. */
1677 if (!all && count == hash->count)
1678 return;
1679 } while_for_each_ftrace_rec();
1680}
1681
1682static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1683 int filter_hash)
1684{
1685 __ftrace_hash_rec_update(ops, filter_hash, 0);
1686}
1687
1688static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1689 int filter_hash)
1690{
1691 __ftrace_hash_rec_update(ops, filter_hash, 1);
1692}
1693
Steven Rostedt05736a42008-09-22 14:55:47 -07001694static void print_ip_ins(const char *fmt, unsigned char *p)
1695{
1696 int i;
1697
1698 printk(KERN_CONT "%s", fmt);
1699
1700 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1701 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1702}
1703
Steven Rostedtc88fd862011-08-16 09:53:39 -04001704/**
1705 * ftrace_bug - report and shutdown function tracer
1706 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1707 * @ip: The address that failed
1708 *
1709 * The arch code that enables or disables the function tracing
1710 * can call ftrace_bug() when it has detected a problem in
1711 * modifying the code. @failed should be one of either:
1712 * EFAULT - if the problem happens on reading the @ip address
1713 * EINVAL - if what is read at @ip is not what was expected
1714 * EPERM - if the problem happens on writting to the @ip address
1715 */
1716void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001717{
1718 switch (failed) {
1719 case -EFAULT:
1720 FTRACE_WARN_ON_ONCE(1);
1721 pr_info("ftrace faulted on modifying ");
1722 print_ip_sym(ip);
1723 break;
1724 case -EINVAL:
1725 FTRACE_WARN_ON_ONCE(1);
1726 pr_info("ftrace failed to modify ");
1727 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001728 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001729 printk(KERN_CONT "\n");
1730 break;
1731 case -EPERM:
1732 FTRACE_WARN_ON_ONCE(1);
1733 pr_info("ftrace faulted on writing ");
1734 print_ip_sym(ip);
1735 break;
1736 default:
1737 FTRACE_WARN_ON_ONCE(1);
1738 pr_info("ftrace faulted on unknown error ");
1739 print_ip_sym(ip);
1740 }
1741}
1742
Steven Rostedtc88fd862011-08-16 09:53:39 -04001743static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001744{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001745 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001746
Steven Rostedt982c3502008-11-15 16:31:41 -05001747 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001748 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001749 *
Steven Rostedted926f92011-05-03 13:25:24 -04001750 * If the record has a ref count, then we need to enable it
1751 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001752 *
Steven Rostedted926f92011-05-03 13:25:24 -04001753 * Otherwise we make sure its disabled.
1754 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001755 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001756 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001757 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001758 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001759 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001760
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001761 /*
1762 * If enabling and the REGS flag does not match the REGS_EN, then
1763 * do not ignore this record. Set flags to fail the compare against
1764 * ENABLED.
1765 */
1766 if (flag &&
1767 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1768 flag |= FTRACE_FL_REGS;
1769
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001770 /* If the state of this record hasn't changed, then do nothing */
1771 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001772 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001773
1774 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001775 /* Save off if rec is being enabled (for return value) */
1776 flag ^= rec->flags & FTRACE_FL_ENABLED;
1777
1778 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001779 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001780 if (flag & FTRACE_FL_REGS) {
1781 if (rec->flags & FTRACE_FL_REGS)
1782 rec->flags |= FTRACE_FL_REGS_EN;
1783 else
1784 rec->flags &= ~FTRACE_FL_REGS_EN;
1785 }
1786 }
1787
1788 /*
1789 * If this record is being updated from a nop, then
1790 * return UPDATE_MAKE_CALL.
1791 * Otherwise, if the EN flag is set, then return
1792 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1793 * from the non-save regs, to a save regs function.
1794 * Otherwise,
1795 * return UPDATE_MODIFY_CALL to tell the caller to convert
1796 * from the save regs, to a non-save regs function.
1797 */
1798 if (flag & FTRACE_FL_ENABLED)
1799 return FTRACE_UPDATE_MAKE_CALL;
1800 else if (rec->flags & FTRACE_FL_REGS_EN)
1801 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1802 else
1803 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001804 }
1805
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001806 if (update) {
1807 /* If there's no more users, clear all flags */
1808 if (!(rec->flags & ~FTRACE_FL_MASK))
1809 rec->flags = 0;
1810 else
1811 /* Just disable the record (keep REGS state) */
1812 rec->flags &= ~FTRACE_FL_ENABLED;
1813 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001814
1815 return FTRACE_UPDATE_MAKE_NOP;
1816}
1817
1818/**
1819 * ftrace_update_record, set a record that now is tracing or not
1820 * @rec: the record to update
1821 * @enable: set to 1 if the record is tracing, zero to force disable
1822 *
1823 * The records that represent all functions that can be traced need
1824 * to be updated when tracing has been enabled.
1825 */
1826int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1827{
1828 return ftrace_check_record(rec, enable, 1);
1829}
1830
1831/**
1832 * ftrace_test_record, check if the record has been enabled or not
1833 * @rec: the record to test
1834 * @enable: set to 1 to check if enabled, 0 if it is disabled
1835 *
1836 * The arch code may need to test if a record is already set to
1837 * tracing to determine how to modify the function code that it
1838 * represents.
1839 */
1840int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1841{
1842 return ftrace_check_record(rec, enable, 0);
1843}
1844
1845static int
1846__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1847{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001848 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001849 unsigned long ftrace_addr;
1850 int ret;
1851
Steven Rostedtc88fd862011-08-16 09:53:39 -04001852 ret = ftrace_update_record(rec, enable);
1853
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001854 if (rec->flags & FTRACE_FL_REGS)
1855 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1856 else
1857 ftrace_addr = (unsigned long)FTRACE_ADDR;
1858
Steven Rostedtc88fd862011-08-16 09:53:39 -04001859 switch (ret) {
1860 case FTRACE_UPDATE_IGNORE:
1861 return 0;
1862
1863 case FTRACE_UPDATE_MAKE_CALL:
1864 return ftrace_make_call(rec, ftrace_addr);
1865
1866 case FTRACE_UPDATE_MAKE_NOP:
1867 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001868
1869 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1870 case FTRACE_UPDATE_MODIFY_CALL:
1871 if (rec->flags & FTRACE_FL_REGS)
1872 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1873 else
1874 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1875
1876 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001877 }
1878
1879 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001880}
1881
Steven Rostedte4f5d542012-04-27 09:13:18 -04001882void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001883{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001884 struct dyn_ftrace *rec;
1885 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001886 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001887
Steven Rostedt45a4a232011-04-21 23:16:46 -04001888 if (unlikely(ftrace_disabled))
1889 return;
1890
Steven Rostedt265c8312009-02-13 12:43:56 -05001891 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001892 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001893 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001894 ftrace_bug(failed, rec->ip);
1895 /* Stop processing */
1896 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001897 }
1898 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001899}
1900
Steven Rostedtc88fd862011-08-16 09:53:39 -04001901struct ftrace_rec_iter {
1902 struct ftrace_page *pg;
1903 int index;
1904};
1905
1906/**
1907 * ftrace_rec_iter_start, start up iterating over traced functions
1908 *
1909 * Returns an iterator handle that is used to iterate over all
1910 * the records that represent address locations where functions
1911 * are traced.
1912 *
1913 * May return NULL if no records are available.
1914 */
1915struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1916{
1917 /*
1918 * We only use a single iterator.
1919 * Protected by the ftrace_lock mutex.
1920 */
1921 static struct ftrace_rec_iter ftrace_rec_iter;
1922 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1923
1924 iter->pg = ftrace_pages_start;
1925 iter->index = 0;
1926
1927 /* Could have empty pages */
1928 while (iter->pg && !iter->pg->index)
1929 iter->pg = iter->pg->next;
1930
1931 if (!iter->pg)
1932 return NULL;
1933
1934 return iter;
1935}
1936
1937/**
1938 * ftrace_rec_iter_next, get the next record to process.
1939 * @iter: The handle to the iterator.
1940 *
1941 * Returns the next iterator after the given iterator @iter.
1942 */
1943struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1944{
1945 iter->index++;
1946
1947 if (iter->index >= iter->pg->index) {
1948 iter->pg = iter->pg->next;
1949 iter->index = 0;
1950
1951 /* Could have empty pages */
1952 while (iter->pg && !iter->pg->index)
1953 iter->pg = iter->pg->next;
1954 }
1955
1956 if (!iter->pg)
1957 return NULL;
1958
1959 return iter;
1960}
1961
1962/**
1963 * ftrace_rec_iter_record, get the record at the iterator location
1964 * @iter: The current iterator location
1965 *
1966 * Returns the record that the current @iter is at.
1967 */
1968struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1969{
1970 return &iter->pg->records[iter->index];
1971}
1972
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301973static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001974ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001975{
1976 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001977 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001978
1979 ip = rec->ip;
1980
Steven Rostedt45a4a232011-04-21 23:16:46 -04001981 if (unlikely(ftrace_disabled))
1982 return 0;
1983
Shaohua Li25aac9d2009-01-09 11:29:40 +08001984 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001985 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001986 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301987 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001988 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301989 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001990}
1991
Steven Rostedt000ab692009-02-17 13:35:06 -05001992/*
1993 * archs can override this function if they must do something
1994 * before the modifying code is performed.
1995 */
1996int __weak ftrace_arch_code_modify_prepare(void)
1997{
1998 return 0;
1999}
2000
2001/*
2002 * archs can override this function if they must do something
2003 * after the modifying code is performed.
2004 */
2005int __weak ftrace_arch_code_modify_post_process(void)
2006{
2007 return 0;
2008}
2009
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002010void ftrace_modify_all_code(int command)
2011{
2012 if (command & FTRACE_UPDATE_CALLS)
2013 ftrace_replace_code(1);
2014 else if (command & FTRACE_DISABLE_CALLS)
2015 ftrace_replace_code(0);
2016
Steven Rostedta0d0a2a2014-02-07 14:41:17 -05002017 if (command & FTRACE_UPDATE_TRACE_FUNC) {
2018 function_trace_op = set_function_trace_op;
2019 smp_wmb();
2020 /* If irqs are disabled, we are in stop machine */
2021 if (!irqs_disabled())
2022 smp_call_function(ftrace_sync_ipi, NULL, 1);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002023 ftrace_update_ftrace_func(ftrace_trace_function);
Steven Rostedta0d0a2a2014-02-07 14:41:17 -05002024 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002025
2026 if (command & FTRACE_START_FUNC_RET)
2027 ftrace_enable_ftrace_graph_caller();
2028 else if (command & FTRACE_STOP_FUNC_RET)
2029 ftrace_disable_ftrace_graph_caller();
2030}
2031
Ingo Molnare309b412008-05-12 21:20:51 +02002032static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002033{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002034 int *command = data;
2035
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002036 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002037
Steven Rostedtc88fd862011-08-16 09:53:39 -04002038 return 0;
2039}
2040
2041/**
2042 * ftrace_run_stop_machine, go back to the stop machine method
2043 * @command: The command to tell ftrace what to do
2044 *
2045 * If an arch needs to fall back to the stop machine method, the
2046 * it can call this function.
2047 */
2048void ftrace_run_stop_machine(int command)
2049{
2050 stop_machine(__ftrace_modify_code, &command, NULL);
2051}
2052
2053/**
2054 * arch_ftrace_update_code, modify the code to trace or not trace
2055 * @command: The command that needs to be done
2056 *
2057 * Archs can override this function if it does not need to
2058 * run stop_machine() to modify code.
2059 */
2060void __weak arch_ftrace_update_code(int command)
2061{
2062 ftrace_run_stop_machine(command);
2063}
2064
2065static void ftrace_run_update_code(int command)
2066{
2067 int ret;
2068
2069 ret = ftrace_arch_code_modify_prepare();
2070 FTRACE_WARN_ON(ret);
2071 if (ret)
2072 return;
2073 /*
2074 * Do not call function tracer while we update the code.
2075 * We are in stop machine.
2076 */
2077 function_trace_stop++;
2078
2079 /*
2080 * By default we use stop_machine() to modify the code.
2081 * But archs can do what ever they want as long as it
2082 * is safe. The stop_machine() is the safest, but also
2083 * produces the most overhead.
2084 */
2085 arch_ftrace_update_code(command);
2086
Steven Rostedt6331c282011-07-13 15:11:02 -04002087 function_trace_stop--;
2088
Steven Rostedt000ab692009-02-17 13:35:06 -05002089 ret = ftrace_arch_code_modify_post_process();
2090 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002091}
2092
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002093static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002094static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002095static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002096
2097static void ftrace_startup_enable(int command)
2098{
2099 if (saved_ftrace_func != ftrace_trace_function) {
2100 saved_ftrace_func = ftrace_trace_function;
2101 command |= FTRACE_UPDATE_TRACE_FUNC;
2102 }
2103
2104 if (!command || !ftrace_enabled)
2105 return;
2106
2107 ftrace_run_update_code(command);
2108}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002109
Steven Rostedta1cd6172011-05-23 15:24:25 -04002110static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002111{
Steven Rostedtb8489142011-05-04 09:27:52 -04002112 bool hash_enable = true;
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002113 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002114
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002115 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002116 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002117
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002118 ret = __register_ftrace_function(ops);
2119 if (ret)
2120 return ret;
2121
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002122 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002123 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002124
Steven Rostedtb8489142011-05-04 09:27:52 -04002125 /* ops marked global share the filter hashes */
2126 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2127 ops = &global_ops;
2128 /* Don't update hash if global is already set */
2129 if (global_start_up)
2130 hash_enable = false;
2131 global_start_up++;
2132 }
2133
Steven Rostedted926f92011-05-03 13:25:24 -04002134 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002135 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002136 ftrace_hash_rec_enable(ops, 1);
2137
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002138 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002139
2140 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002141}
2142
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002143static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002144{
Steven Rostedtb8489142011-05-04 09:27:52 -04002145 bool hash_disable = true;
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002146 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002147
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002148 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002149 return -ENODEV;
2150
2151 ret = __unregister_ftrace_function(ops);
2152 if (ret)
2153 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002154
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002155 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002156 /*
2157 * Just warn in case of unbalance, no need to kill ftrace, it's not
2158 * critical but the ftrace_call callers may be never nopped again after
2159 * further ftrace uses.
2160 */
2161 WARN_ON_ONCE(ftrace_start_up < 0);
2162
Steven Rostedtb8489142011-05-04 09:27:52 -04002163 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2164 ops = &global_ops;
2165 global_start_up--;
2166 WARN_ON_ONCE(global_start_up < 0);
2167 /* Don't update hash if global still has users */
2168 if (global_start_up) {
2169 WARN_ON_ONCE(!ftrace_start_up);
2170 hash_disable = false;
2171 }
2172 }
2173
2174 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002175 ftrace_hash_rec_disable(ops, 1);
2176
Steven Rostedtb8489142011-05-04 09:27:52 -04002177 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002178 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002179
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002180 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002181
2182 if (saved_ftrace_func != ftrace_trace_function) {
2183 saved_ftrace_func = ftrace_trace_function;
2184 command |= FTRACE_UPDATE_TRACE_FUNC;
2185 }
2186
2187 if (!command || !ftrace_enabled)
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002188 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002189
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002190 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05002191 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002192}
2193
Ingo Molnare309b412008-05-12 21:20:51 +02002194static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002195{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002196 if (unlikely(ftrace_disabled))
2197 return;
2198
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002199 /* Force update next time */
2200 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002201 /* ftrace_start_up is true if we want ftrace running */
2202 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002203 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002204}
2205
Ingo Molnare309b412008-05-12 21:20:51 +02002206static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002207{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002208 if (unlikely(ftrace_disabled))
2209 return;
2210
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002211 /* ftrace_start_up is true if ftrace is running */
2212 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002213 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002214}
2215
Steven Rostedt3d083392008-05-12 21:20:42 +02002216static cycle_t ftrace_update_time;
2217static unsigned long ftrace_update_cnt;
2218unsigned long ftrace_update_tot_cnt;
2219
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002220static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002221{
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002222 /*
2223 * Filter_hash being empty will default to trace module.
2224 * But notrace hash requires a test of individual module functions.
2225 */
2226 return ftrace_hash_empty(ops->filter_hash) &&
2227 ftrace_hash_empty(ops->notrace_hash);
2228}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002229
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002230/*
2231 * Check if the current ops references the record.
2232 *
2233 * If the ops traces all functions, then it was already accounted for.
2234 * If the ops does not trace the current record function, skip it.
2235 * If the ops ignores the function via notrace filter, skip it.
2236 */
2237static inline bool
2238ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2239{
2240 /* If ops isn't enabled, ignore it */
2241 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2242 return 0;
2243
2244 /* If ops traces all mods, we already accounted for it */
2245 if (ops_traces_mod(ops))
2246 return 0;
2247
2248 /* The function must be in the filter */
2249 if (!ftrace_hash_empty(ops->filter_hash) &&
2250 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2251 return 0;
2252
2253 /* If in notrace hash, we ignore it too */
2254 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2255 return 0;
2256
2257 return 1;
2258}
2259
2260static int referenced_filters(struct dyn_ftrace *rec)
2261{
2262 struct ftrace_ops *ops;
2263 int cnt = 0;
2264
2265 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2266 if (ops_references_rec(ops, rec))
2267 cnt++;
2268 }
2269
2270 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002271}
2272
Steven Rostedt31e88902008-11-14 16:21:19 -08002273static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002274{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002275 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002276 struct dyn_ftrace *p;
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302277 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002278 unsigned long ref = 0;
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002279 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002280 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002281
2282 /*
2283 * When adding a module, we need to check if tracers are
2284 * currently enabled and if they are set to trace all functions.
2285 * If they are, we need to enable the module functions as well
2286 * as update the reference counts for those function records.
2287 */
2288 if (mod) {
2289 struct ftrace_ops *ops;
2290
2291 for (ops = ftrace_ops_list;
2292 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002293 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2294 if (ops_traces_mod(ops))
2295 ref++;
2296 else
2297 test = true;
2298 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002299 }
2300 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002301
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002302 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002303 ftrace_update_cnt = 0;
2304
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002305 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302306
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002307 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002308 int cnt = ref;
2309
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002310 /* If something went wrong, bail without enabling anything */
2311 if (unlikely(ftrace_disabled))
2312 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002313
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002314 p = &pg->records[i];
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002315 if (test)
2316 cnt += referenced_filters(p);
2317 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302318
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002319 /*
2320 * Do the initial record conversion from mcount jump
2321 * to the NOP instructions.
2322 */
2323 if (!ftrace_code_disable(mod, p))
2324 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002325
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002326 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002327
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002328 /*
2329 * If the tracing is enabled, go ahead and enable the record.
2330 *
2331 * The reason not to enable the record immediatelly is the
2332 * inherent check of ftrace_make_nop/ftrace_make_call for
2333 * correct previous instructions. Making first the NOP
2334 * conversion puts the module to the correct state, thus
2335 * passing the ftrace_make_call check.
2336 */
Steven Rostedt (Red Hat)f05e9992013-07-30 00:04:32 -04002337 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002338 int failed = __ftrace_replace_code(p, 1);
2339 if (failed)
2340 ftrace_bug(failed, p->ip);
2341 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002342 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002343 }
2344
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002345 ftrace_new_pgs = NULL;
2346
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002347 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002348 ftrace_update_time = stop - start;
2349 ftrace_update_tot_cnt += ftrace_update_cnt;
2350
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002351 return 0;
2352}
2353
Steven Rostedta7900872011-12-16 16:23:44 -05002354static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002355{
Steven Rostedta7900872011-12-16 16:23:44 -05002356 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002357 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002358
Steven Rostedta7900872011-12-16 16:23:44 -05002359 if (WARN_ON(!count))
2360 return -EINVAL;
2361
2362 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002363
2364 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002365 * We want to fill as much as possible. No more than a page
2366 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002367 */
Steven Rostedta7900872011-12-16 16:23:44 -05002368 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2369 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002370
Steven Rostedta7900872011-12-16 16:23:44 -05002371 again:
2372 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2373
2374 if (!pg->records) {
2375 /* if we can't allocate this size, try something smaller */
2376 if (!order)
2377 return -ENOMEM;
2378 order >>= 1;
2379 goto again;
2380 }
2381
2382 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2383 pg->size = cnt;
2384
2385 if (cnt > count)
2386 cnt = count;
2387
2388 return cnt;
2389}
2390
2391static struct ftrace_page *
2392ftrace_allocate_pages(unsigned long num_to_init)
2393{
2394 struct ftrace_page *start_pg;
2395 struct ftrace_page *pg;
2396 int order;
2397 int cnt;
2398
2399 if (!num_to_init)
2400 return 0;
2401
2402 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2403 if (!pg)
2404 return NULL;
2405
2406 /*
2407 * Try to allocate as much as possible in one continues
2408 * location that fills in all of the space. We want to
2409 * waste as little space as possible.
2410 */
2411 for (;;) {
2412 cnt = ftrace_allocate_records(pg, num_to_init);
2413 if (cnt < 0)
2414 goto free_pages;
2415
2416 num_to_init -= cnt;
2417 if (!num_to_init)
2418 break;
2419
2420 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2421 if (!pg->next)
2422 goto free_pages;
2423
2424 pg = pg->next;
2425 }
2426
2427 return start_pg;
2428
2429 free_pages:
2430 while (start_pg) {
2431 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2432 free_pages((unsigned long)pg->records, order);
2433 start_pg = pg->next;
2434 kfree(pg);
2435 pg = start_pg;
2436 }
2437 pr_info("ftrace: FAILED to allocate memory for functions\n");
2438 return NULL;
2439}
2440
2441static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2442{
2443 int cnt;
2444
2445 if (!num_to_init) {
2446 pr_info("ftrace: No functions to be traced?\n");
2447 return -1;
2448 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002449
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002450 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002451 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002452 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002453
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002454 return 0;
2455}
2456
Steven Rostedt5072c592008-05-12 21:20:43 +02002457#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2458
2459struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002460 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002461 loff_t func_pos;
2462 struct ftrace_page *pg;
2463 struct dyn_ftrace *func;
2464 struct ftrace_func_probe *probe;
2465 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002466 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002467 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002468 int hidx;
2469 int idx;
2470 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002471};
2472
Ingo Molnare309b412008-05-12 21:20:51 +02002473static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002474t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002475{
2476 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002477 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002478 struct hlist_head *hhd;
2479
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002480 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002481 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002482
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002483 if (iter->probe)
2484 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002485 retry:
2486 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2487 return NULL;
2488
2489 hhd = &ftrace_func_hash[iter->hidx];
2490
2491 if (hlist_empty(hhd)) {
2492 iter->hidx++;
2493 hnd = NULL;
2494 goto retry;
2495 }
2496
2497 if (!hnd)
2498 hnd = hhd->first;
2499 else {
2500 hnd = hnd->next;
2501 if (!hnd) {
2502 iter->hidx++;
2503 goto retry;
2504 }
2505 }
2506
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002507 if (WARN_ON_ONCE(!hnd))
2508 return NULL;
2509
2510 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2511
2512 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002513}
2514
2515static void *t_hash_start(struct seq_file *m, loff_t *pos)
2516{
2517 struct ftrace_iterator *iter = m->private;
2518 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002519 loff_t l;
2520
Steven Rostedt69a30832011-12-19 15:21:16 -05002521 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2522 return NULL;
2523
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002524 if (iter->func_pos > *pos)
2525 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002526
Li Zefand82d6242009-06-24 09:54:54 +08002527 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002528 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002529 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002530 if (!p)
2531 break;
2532 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002533 if (!p)
2534 return NULL;
2535
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002536 /* Only set this if we have an item */
2537 iter->flags |= FTRACE_ITER_HASH;
2538
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002539 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002540}
2541
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002542static int
2543t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002544{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002545 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002546
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002547 rec = iter->probe;
2548 if (WARN_ON_ONCE(!rec))
2549 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002550
Steven Rostedt809dcf22009-02-16 23:06:01 -05002551 if (rec->ops->print)
2552 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2553
Steven Rostedtb375a112009-09-17 00:05:58 -04002554 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002555
2556 if (rec->data)
2557 seq_printf(m, ":%p", rec->data);
2558 seq_putc(m, '\n');
2559
2560 return 0;
2561}
2562
2563static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002564t_next(struct seq_file *m, void *v, loff_t *pos)
2565{
2566 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002567 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002568 struct dyn_ftrace *rec = NULL;
2569
Steven Rostedt45a4a232011-04-21 23:16:46 -04002570 if (unlikely(ftrace_disabled))
2571 return NULL;
2572
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002573 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002574 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002575
Steven Rostedt5072c592008-05-12 21:20:43 +02002576 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002577 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002578
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002579 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002580 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002581
Steven Rostedt5072c592008-05-12 21:20:43 +02002582 retry:
2583 if (iter->idx >= iter->pg->index) {
2584 if (iter->pg->next) {
2585 iter->pg = iter->pg->next;
2586 iter->idx = 0;
2587 goto retry;
2588 }
2589 } else {
2590 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002591 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002592 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002593
Steven Rostedt41c52c02008-05-22 11:46:33 -04002594 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002595 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2596
2597 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002598 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002599
Steven Rostedt5072c592008-05-12 21:20:43 +02002600 rec = NULL;
2601 goto retry;
2602 }
2603 }
2604
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002605 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002606 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002607
2608 iter->func = rec;
2609
2610 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002611}
2612
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002613static void reset_iter_read(struct ftrace_iterator *iter)
2614{
2615 iter->pos = 0;
2616 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002617 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002618}
2619
2620static void *t_start(struct seq_file *m, loff_t *pos)
2621{
2622 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002623 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002624 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002625 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002626
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002627 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002628
2629 if (unlikely(ftrace_disabled))
2630 return NULL;
2631
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002632 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002633 * If an lseek was done, then reset and start from beginning.
2634 */
2635 if (*pos < iter->pos)
2636 reset_iter_read(iter);
2637
2638 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002639 * For set_ftrace_filter reading, if we have the filter
2640 * off, we can short cut and just print out that all
2641 * functions are enabled.
2642 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002643 if (iter->flags & FTRACE_ITER_FILTER &&
2644 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002645 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002646 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002647 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002648 /* reset in case of seek/pread */
2649 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002650 return iter;
2651 }
2652
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002653 if (iter->flags & FTRACE_ITER_HASH)
2654 return t_hash_start(m, pos);
2655
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002656 /*
2657 * Unfortunately, we need to restart at ftrace_pages_start
2658 * every time we let go of the ftrace_mutex. This is because
2659 * those pointers can change without the lock.
2660 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002661 iter->pg = ftrace_pages_start;
2662 iter->idx = 0;
2663 for (l = 0; l <= *pos; ) {
2664 p = t_next(m, p, &l);
2665 if (!p)
2666 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002667 }
walimis5821e1b2008-11-15 15:19:06 +08002668
Steven Rostedt69a30832011-12-19 15:21:16 -05002669 if (!p)
2670 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002671
2672 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002673}
2674
2675static void t_stop(struct seq_file *m, void *p)
2676{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002677 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002678}
2679
2680static int t_show(struct seq_file *m, void *v)
2681{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002682 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002683 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002684
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002685 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002686 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002687
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002688 if (iter->flags & FTRACE_ITER_PRINTALL) {
2689 seq_printf(m, "#### all functions enabled ####\n");
2690 return 0;
2691 }
2692
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002693 rec = iter->func;
2694
Steven Rostedt5072c592008-05-12 21:20:43 +02002695 if (!rec)
2696 return 0;
2697
Steven Rostedt647bcd02011-05-03 14:39:21 -04002698 seq_printf(m, "%ps", (void *)rec->ip);
2699 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002700 seq_printf(m, " (%ld)%s",
2701 rec->flags & ~FTRACE_FL_MASK,
2702 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002703 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002704
2705 return 0;
2706}
2707
James Morris88e9d342009-09-22 16:43:43 -07002708static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002709 .start = t_start,
2710 .next = t_next,
2711 .stop = t_stop,
2712 .show = t_show,
2713};
2714
Ingo Molnare309b412008-05-12 21:20:51 +02002715static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002716ftrace_avail_open(struct inode *inode, struct file *file)
2717{
2718 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002719
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002720 if (unlikely(ftrace_disabled))
2721 return -ENODEV;
2722
Jiri Olsa50e18b92012-04-25 10:23:39 +02002723 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2724 if (iter) {
2725 iter->pg = ftrace_pages_start;
2726 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002727 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002728
Jiri Olsa50e18b92012-04-25 10:23:39 +02002729 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002730}
2731
Steven Rostedt647bcd02011-05-03 14:39:21 -04002732static int
2733ftrace_enabled_open(struct inode *inode, struct file *file)
2734{
2735 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002736
2737 if (unlikely(ftrace_disabled))
2738 return -ENODEV;
2739
Jiri Olsa50e18b92012-04-25 10:23:39 +02002740 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2741 if (iter) {
2742 iter->pg = ftrace_pages_start;
2743 iter->flags = FTRACE_ITER_ENABLED;
2744 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002745 }
2746
Jiri Olsa50e18b92012-04-25 10:23:39 +02002747 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002748}
2749
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002750static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002751{
Steven Rostedt52baf112009-02-14 01:15:39 -05002752 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002753 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002754 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002755}
2756
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002757/**
2758 * ftrace_regex_open - initialize function tracer filter files
2759 * @ops: The ftrace_ops that hold the hash filters
2760 * @flag: The type of filter to process
2761 * @inode: The inode, usually passed in to your open routine
2762 * @file: The file, usually passed in to your open routine
2763 *
2764 * ftrace_regex_open() initializes the filter files for the
2765 * @ops. Depending on @flag it may process the filter hash or
2766 * the notrace hash of @ops. With this called from the open
2767 * routine, you can use ftrace_filter_write() for the write
2768 * routine if @flag has FTRACE_ITER_FILTER set, or
2769 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04002770 * ftrace_filter_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002771 * release must call ftrace_regex_release().
2772 */
2773int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002774ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002775 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002776{
2777 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002778 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002779 int ret = 0;
2780
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002781 ftrace_ops_init(ops);
2782
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002783 if (unlikely(ftrace_disabled))
2784 return -ENODEV;
2785
Steven Rostedt5072c592008-05-12 21:20:43 +02002786 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2787 if (!iter)
2788 return -ENOMEM;
2789
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002790 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2791 kfree(iter);
2792 return -ENOMEM;
2793 }
2794
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002795 iter->ops = ops;
2796 iter->flags = flag;
2797
2798 mutex_lock(&ops->regex_lock);
2799
Steven Rostedtf45948e2011-05-02 12:29:25 -04002800 if (flag & FTRACE_ITER_NOTRACE)
2801 hash = ops->notrace_hash;
2802 else
2803 hash = ops->filter_hash;
2804
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002805 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002806 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002807 if (!iter->hash) {
2808 trace_parser_put(&iter->parser);
2809 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002810 ret = -ENOMEM;
2811 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002812 }
2813 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002814
Steven Rostedt5072c592008-05-12 21:20:43 +02002815 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002816 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002817 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002818
2819 if (file->f_mode & FMODE_READ) {
2820 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002821
2822 ret = seq_open(file, &show_ftrace_seq_ops);
2823 if (!ret) {
2824 struct seq_file *m = file->private_data;
2825 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002826 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002827 /* Failed */
2828 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002829 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002830 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002831 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002832 } else
2833 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002834
2835 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002836 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002837
2838 return ret;
2839}
2840
Steven Rostedt41c52c02008-05-22 11:46:33 -04002841static int
2842ftrace_filter_open(struct inode *inode, struct file *file)
2843{
Steven Rostedt69a30832011-12-19 15:21:16 -05002844 return ftrace_regex_open(&global_ops,
2845 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2846 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002847}
2848
2849static int
2850ftrace_notrace_open(struct inode *inode, struct file *file)
2851{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002852 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002853 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002854}
2855
Steven Rostedt64e7c442009-02-13 17:08:48 -05002856static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002857{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002858 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002859 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002860
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002861 switch (type) {
2862 case MATCH_FULL:
2863 if (strcmp(str, regex) == 0)
2864 matched = 1;
2865 break;
2866 case MATCH_FRONT_ONLY:
2867 if (strncmp(str, regex, len) == 0)
2868 matched = 1;
2869 break;
2870 case MATCH_MIDDLE_ONLY:
2871 if (strstr(str, regex))
2872 matched = 1;
2873 break;
2874 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002875 slen = strlen(str);
2876 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002877 matched = 1;
2878 break;
2879 }
2880
2881 return matched;
2882}
2883
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002884static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002885enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002886{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002887 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002888 int ret = 0;
2889
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002890 entry = ftrace_lookup_ip(hash, rec->ip);
2891 if (not) {
2892 /* Do nothing if it doesn't exist */
2893 if (!entry)
2894 return 0;
2895
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002896 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002897 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002898 /* Do nothing if it exists */
2899 if (entry)
2900 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002901
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002902 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002903 }
2904 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002905}
2906
Steven Rostedt64e7c442009-02-13 17:08:48 -05002907static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002908ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2909 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002910{
2911 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002912 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002913
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002914 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2915
2916 if (mod) {
2917 /* module lookup requires matching the module */
2918 if (!modname || strcmp(modname, mod))
2919 return 0;
2920
2921 /* blank search means to match all funcs in the mod */
2922 if (!len)
2923 return 1;
2924 }
2925
Steven Rostedt64e7c442009-02-13 17:08:48 -05002926 return ftrace_match(str, regex, len, type);
2927}
2928
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002929static int
2930match_records(struct ftrace_hash *hash, char *buff,
2931 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002932{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002933 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002934 struct ftrace_page *pg;
2935 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002936 int type = MATCH_FULL;
2937 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002938 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002939 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002940
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002941 if (len) {
2942 type = filter_parse_regex(buff, len, &search, &not);
2943 search_len = strlen(search);
2944 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002945
Steven Rostedt52baf112009-02-14 01:15:39 -05002946 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002947
2948 if (unlikely(ftrace_disabled))
2949 goto out_unlock;
2950
Steven Rostedt265c8312009-02-13 12:43:56 -05002951 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002952 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002953 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002954 if (ret < 0) {
2955 found = ret;
2956 goto out_unlock;
2957 }
Li Zefan311d16d2009-12-08 11:15:11 +08002958 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002959 }
2960 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002961 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002962 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002963
2964 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002965}
2966
Steven Rostedt64e7c442009-02-13 17:08:48 -05002967static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002968ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002969{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002970 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002971}
2972
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002973static int
2974ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002975{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002976 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002977
Steven Rostedt64e7c442009-02-13 17:08:48 -05002978 /* blank or '*' mean the same */
2979 if (strcmp(buff, "*") == 0)
2980 buff[0] = 0;
2981
2982 /* handle the case of 'dont filter this module' */
2983 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2984 buff[0] = 0;
2985 not = 1;
2986 }
2987
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002988 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002989}
2990
Steven Rostedtf6180772009-02-14 00:40:25 -05002991/*
2992 * We register the module command as a template to show others how
2993 * to register the a command as well.
2994 */
2995
2996static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002997ftrace_mod_callback(struct ftrace_hash *hash,
2998 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002999{
3000 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003001 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003002
3003 /*
3004 * cmd == 'mod' because we only registered this func
3005 * for the 'mod' ftrace_func_command.
3006 * But if you register one func with multiple commands,
3007 * you can tell which command was used by the cmd
3008 * parameter.
3009 */
3010
3011 /* we must have a module name */
3012 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003013 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003014
3015 mod = strsep(&param, ":");
3016 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003017 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003018
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003019 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003020 if (!ret)
3021 ret = -EINVAL;
3022 if (ret < 0)
3023 return ret;
3024
3025 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003026}
3027
3028static struct ftrace_func_command ftrace_mod_cmd = {
3029 .name = "mod",
3030 .func = ftrace_mod_callback,
3031};
3032
3033static int __init ftrace_mod_cmd_init(void)
3034{
3035 return register_ftrace_command(&ftrace_mod_cmd);
3036}
Steven Rostedt6f415672012-10-05 12:13:07 -04003037core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003038
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003039static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003040 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003041{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003042 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003043 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003044 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003045
3046 key = hash_long(ip, FTRACE_HASH_BITS);
3047
3048 hhd = &ftrace_func_hash[key];
3049
3050 if (hlist_empty(hhd))
3051 return;
3052
3053 /*
3054 * Disable preemption for these calls to prevent a RCU grace
3055 * period. This syncs the hash iteration and freeing of items
3056 * on the hash. rcu_read_lock is too dangerous here.
3057 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003058 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003059 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003060 if (entry->ip == ip)
3061 entry->ops->func(ip, parent_ip, &entry->data);
3062 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003063 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003064}
3065
Steven Rostedtb6887d72009-02-17 12:32:04 -05003066static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003067{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003068 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003069 .flags = FTRACE_OPS_FL_INITIALIZED,
3070 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003071};
3072
Steven Rostedtb6887d72009-02-17 12:32:04 -05003073static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003074
Steven Rostedtb6887d72009-02-17 12:32:04 -05003075static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003076{
Steven Rostedtb8489142011-05-04 09:27:52 -04003077 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003078 int i;
3079
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003080 if (ftrace_probe_registered) {
3081 /* still need to update the function call sites */
3082 if (ftrace_enabled)
3083 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003084 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003085 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003086
3087 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3088 struct hlist_head *hhd = &ftrace_func_hash[i];
3089 if (hhd->first)
3090 break;
3091 }
3092 /* Nothing registered? */
3093 if (i == FTRACE_FUNC_HASHSIZE)
3094 return;
3095
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05003096 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003097
Steven Rostedtb6887d72009-02-17 12:32:04 -05003098 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003099}
3100
Steven Rostedtb6887d72009-02-17 12:32:04 -05003101static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003102{
3103 int i;
3104
Steven Rostedtb6887d72009-02-17 12:32:04 -05003105 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003106 return;
3107
3108 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3109 struct hlist_head *hhd = &ftrace_func_hash[i];
3110 if (hhd->first)
3111 return;
3112 }
3113
3114 /* no more funcs left */
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05003115 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003116
Steven Rostedtb6887d72009-02-17 12:32:04 -05003117 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003118}
3119
3120
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003121static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003122{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003123 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003124 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003125 kfree(entry);
3126}
3127
Steven Rostedt59df055f2009-02-14 15:29:06 -05003128int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003129register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003130 void *data)
3131{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003132 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003133 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3134 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003135 struct ftrace_page *pg;
3136 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003137 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003138 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003139 int count = 0;
3140 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003141 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003142
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003143 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003144 len = strlen(search);
3145
Steven Rostedtb6887d72009-02-17 12:32:04 -05003146 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003147 if (WARN_ON(not))
3148 return -EINVAL;
3149
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003150 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003151
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003152 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3153 if (!hash) {
3154 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003155 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003156 }
3157
3158 if (unlikely(ftrace_disabled)) {
3159 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003160 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003161 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003162
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003163 mutex_lock(&ftrace_lock);
3164
Steven Rostedt59df055f2009-02-14 15:29:06 -05003165 do_for_each_ftrace_rec(pg, rec) {
3166
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003167 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003168 continue;
3169
3170 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3171 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003172 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003173 if (!count)
3174 count = -ENOMEM;
3175 goto out_unlock;
3176 }
3177
3178 count++;
3179
3180 entry->data = data;
3181
3182 /*
3183 * The caller might want to do something special
3184 * for each function we find. We call the callback
3185 * to give the caller an opportunity to do so.
3186 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003187 if (ops->init) {
3188 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003189 /* caller does not like this func */
3190 kfree(entry);
3191 continue;
3192 }
3193 }
3194
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003195 ret = enter_record(hash, rec, 0);
3196 if (ret < 0) {
3197 kfree(entry);
3198 count = ret;
3199 goto out_unlock;
3200 }
3201
Steven Rostedt59df055f2009-02-14 15:29:06 -05003202 entry->ops = ops;
3203 entry->ip = rec->ip;
3204
3205 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3206 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3207
3208 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003209
3210 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3211 if (ret < 0)
3212 count = ret;
3213
Steven Rostedtb6887d72009-02-17 12:32:04 -05003214 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003215
3216 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003217 mutex_unlock(&ftrace_lock);
3218 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003219 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003220 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003221
3222 return count;
3223}
3224
3225enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003226 PROBE_TEST_FUNC = 1,
3227 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003228};
3229
3230static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003231__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003232 void *data, int flags)
3233{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003234 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003235 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003236 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003237 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003238 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003239 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003240 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003241 char str[KSYM_SYMBOL_LEN];
3242 int type = MATCH_FULL;
3243 int i, len = 0;
3244 char *search;
3245
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003246 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003247 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003248 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003249 int not;
3250
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003251 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003252 len = strlen(search);
3253
Steven Rostedtb6887d72009-02-17 12:32:04 -05003254 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003255 if (WARN_ON(not))
3256 return;
3257 }
3258
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003259 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003260
3261 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3262 if (!hash)
3263 /* Hmm, should report this somehow */
3264 goto out_unlock;
3265
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003266 INIT_LIST_HEAD(&free_list);
3267
Steven Rostedt59df055f2009-02-14 15:29:06 -05003268 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3269 struct hlist_head *hhd = &ftrace_func_hash[i];
3270
Sasha Levinb67bfe02013-02-27 17:06:00 -08003271 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003272
3273 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003274 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003275 continue;
3276
Steven Rostedtb6887d72009-02-17 12:32:04 -05003277 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003278 continue;
3279
3280 /* do this last, since it is the most expensive */
3281 if (glob) {
3282 kallsyms_lookup(entry->ip, NULL, NULL,
3283 NULL, str);
3284 if (!ftrace_match(str, glob, len, type))
3285 continue;
3286 }
3287
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003288 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3289 /* It is possible more than one entry had this ip */
3290 if (rec_entry)
3291 free_hash_entry(hash, rec_entry);
3292
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003293 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003294 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003295 }
3296 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003297 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003298 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003299 /*
3300 * Remove after the disable is called. Otherwise, if the last
3301 * probe is removed, a null hash means *all enabled*.
3302 */
3303 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003304 synchronize_sched();
3305 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3306 list_del(&entry->free_list);
3307 ftrace_free_entry(entry);
3308 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003309 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003310
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003311 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003312 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003313 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003314}
3315
3316void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003317unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003318 void *data)
3319{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003320 __unregister_ftrace_function_probe(glob, ops, data,
3321 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003322}
3323
3324void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003325unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003326{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003327 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003328}
3329
Steven Rostedtb6887d72009-02-17 12:32:04 -05003330void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003331{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003332 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003333}
3334
Steven Rostedtf6180772009-02-14 00:40:25 -05003335static LIST_HEAD(ftrace_commands);
3336static DEFINE_MUTEX(ftrace_cmd_mutex);
3337
3338int register_ftrace_command(struct ftrace_func_command *cmd)
3339{
3340 struct ftrace_func_command *p;
3341 int ret = 0;
3342
3343 mutex_lock(&ftrace_cmd_mutex);
3344 list_for_each_entry(p, &ftrace_commands, list) {
3345 if (strcmp(cmd->name, p->name) == 0) {
3346 ret = -EBUSY;
3347 goto out_unlock;
3348 }
3349 }
3350 list_add(&cmd->list, &ftrace_commands);
3351 out_unlock:
3352 mutex_unlock(&ftrace_cmd_mutex);
3353
3354 return ret;
3355}
3356
3357int unregister_ftrace_command(struct ftrace_func_command *cmd)
3358{
3359 struct ftrace_func_command *p, *n;
3360 int ret = -ENODEV;
3361
3362 mutex_lock(&ftrace_cmd_mutex);
3363 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3364 if (strcmp(cmd->name, p->name) == 0) {
3365 ret = 0;
3366 list_del_init(&p->list);
3367 goto out_unlock;
3368 }
3369 }
3370 out_unlock:
3371 mutex_unlock(&ftrace_cmd_mutex);
3372
3373 return ret;
3374}
3375
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003376static int ftrace_process_regex(struct ftrace_hash *hash,
3377 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003378{
Steven Rostedtf6180772009-02-14 00:40:25 -05003379 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003380 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003381 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003382
3383 func = strsep(&next, ":");
3384
3385 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003386 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003387 if (!ret)
3388 ret = -EINVAL;
3389 if (ret < 0)
3390 return ret;
3391 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003392 }
3393
Steven Rostedtf6180772009-02-14 00:40:25 -05003394 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003395
3396 command = strsep(&next, ":");
3397
Steven Rostedtf6180772009-02-14 00:40:25 -05003398 mutex_lock(&ftrace_cmd_mutex);
3399 list_for_each_entry(p, &ftrace_commands, list) {
3400 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003401 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003402 goto out_unlock;
3403 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003404 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003405 out_unlock:
3406 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003407
Steven Rostedtf6180772009-02-14 00:40:25 -05003408 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003409}
3410
Ingo Molnare309b412008-05-12 21:20:51 +02003411static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003412ftrace_regex_write(struct file *file, const char __user *ubuf,
3413 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003414{
3415 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003416 struct trace_parser *parser;
3417 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003418
Li Zefan4ba79782009-09-22 13:52:20 +08003419 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003420 return 0;
3421
Steven Rostedt5072c592008-05-12 21:20:43 +02003422 if (file->f_mode & FMODE_READ) {
3423 struct seq_file *m = file->private_data;
3424 iter = m->private;
3425 } else
3426 iter = file->private_data;
3427
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003428 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003429 return -ENODEV;
3430
3431 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003432
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003433 parser = &iter->parser;
3434 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003435
Li Zefan4ba79782009-09-22 13:52:20 +08003436 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003437 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003438 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003439 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003440 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003441 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003442 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003443 }
3444
Steven Rostedt5072c592008-05-12 21:20:43 +02003445 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003446 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003447 return ret;
3448}
3449
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003450ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003451ftrace_filter_write(struct file *file, const char __user *ubuf,
3452 size_t cnt, loff_t *ppos)
3453{
3454 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3455}
3456
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003457ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003458ftrace_notrace_write(struct file *file, const char __user *ubuf,
3459 size_t cnt, loff_t *ppos)
3460{
3461 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3462}
3463
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003464static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003465ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3466{
3467 struct ftrace_func_entry *entry;
3468
3469 if (!ftrace_location(ip))
3470 return -EINVAL;
3471
3472 if (remove) {
3473 entry = ftrace_lookup_ip(hash, ip);
3474 if (!entry)
3475 return -ENOENT;
3476 free_hash_entry(hash, entry);
3477 return 0;
3478 }
3479
3480 return add_hash_entry(hash, ip);
3481}
3482
3483static int
3484ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3485 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003486{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003487 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003488 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003489 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003490
Steven Rostedt936e0742011-05-05 22:54:01 -04003491 /* All global ops uses the global ops filters */
3492 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3493 ops = &global_ops;
3494
Steven Rostedt41c52c02008-05-22 11:46:33 -04003495 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003496 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003497
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003498 mutex_lock(&ops->regex_lock);
3499
Steven Rostedtf45948e2011-05-02 12:29:25 -04003500 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003501 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003502 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003503 orig_hash = &ops->notrace_hash;
3504
3505 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003506 if (!hash) {
3507 ret = -ENOMEM;
3508 goto out_regex_unlock;
3509 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003510
Steven Rostedt41c52c02008-05-22 11:46:33 -04003511 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003512 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003513 if (buf && !ftrace_match_records(hash, buf, len)) {
3514 ret = -EINVAL;
3515 goto out_regex_unlock;
3516 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003517 if (ip) {
3518 ret = ftrace_match_addr(hash, ip, remove);
3519 if (ret < 0)
3520 goto out_regex_unlock;
3521 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003522
3523 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003524 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003525 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3526 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003527 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003528
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003529 mutex_unlock(&ftrace_lock);
3530
Jiri Olsaac483c42012-01-02 10:04:14 +01003531 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003532 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003533
3534 free_ftrace_hash(hash);
3535 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003536}
3537
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003538static int
3539ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3540 int reset, int enable)
3541{
3542 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3543}
3544
3545/**
3546 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3547 * @ops - the ops to set the filter with
3548 * @ip - the address to add to or remove from the filter.
3549 * @remove - non zero to remove the ip from the filter
3550 * @reset - non zero to reset all filters before applying this filter.
3551 *
3552 * Filters denote which functions should be enabled when tracing is enabled
3553 * If @ip is NULL, it failes to update filter.
3554 */
3555int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3556 int remove, int reset)
3557{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003558 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003559 return ftrace_set_addr(ops, ip, remove, reset, 1);
3560}
3561EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3562
3563static int
3564ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3565 int reset, int enable)
3566{
3567 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3568}
3569
Steven Rostedt77a2b372008-05-12 21:20:45 +02003570/**
3571 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003572 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003573 * @buf - the string that holds the function filter text.
3574 * @len - the length of the string.
3575 * @reset - non zero to reset all filters before applying this filter.
3576 *
3577 * Filters denote which functions should be enabled when tracing is enabled.
3578 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3579 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003580int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003581 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003582{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003583 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003584 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003585}
Steven Rostedt936e0742011-05-05 22:54:01 -04003586EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003587
Steven Rostedt41c52c02008-05-22 11:46:33 -04003588/**
3589 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003590 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003591 * @buf - the string that holds the function notrace text.
3592 * @len - the length of the string.
3593 * @reset - non zero to reset all filters before applying this filter.
3594 *
3595 * Notrace Filters denote which functions should not be enabled when tracing
3596 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3597 * for tracing.
3598 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003599int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003600 int len, int reset)
3601{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003602 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003603 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003604}
3605EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3606/**
3607 * ftrace_set_filter - set a function to filter on in ftrace
3608 * @ops - the ops to set the filter with
3609 * @buf - the string that holds the function filter text.
3610 * @len - the length of the string.
3611 * @reset - non zero to reset all filters before applying this filter.
3612 *
3613 * Filters denote which functions should be enabled when tracing is enabled.
3614 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3615 */
3616void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3617{
3618 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3619}
3620EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3621
3622/**
3623 * ftrace_set_notrace - set a function to not trace in ftrace
3624 * @ops - the ops to set the notrace filter with
3625 * @buf - the string that holds the function notrace text.
3626 * @len - the length of the string.
3627 * @reset - non zero to reset all filters before applying this filter.
3628 *
3629 * Notrace Filters denote which functions should not be enabled when tracing
3630 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3631 * for tracing.
3632 */
3633void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003634{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003635 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003636}
Steven Rostedt936e0742011-05-05 22:54:01 -04003637EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003638
Steven Rostedt2af15d62009-05-28 13:37:24 -04003639/*
3640 * command line interface to allow users to set filters on boot up.
3641 */
3642#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3643static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3644static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3645
3646static int __init set_ftrace_notrace(char *str)
3647{
Chen Gang75761cc2013-04-08 12:12:39 +08003648 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003649 return 1;
3650}
3651__setup("ftrace_notrace=", set_ftrace_notrace);
3652
3653static int __init set_ftrace_filter(char *str)
3654{
Chen Gang75761cc2013-04-08 12:12:39 +08003655 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003656 return 1;
3657}
3658__setup("ftrace_filter=", set_ftrace_filter);
3659
Stefan Assmann369bc182009-10-12 22:17:21 +02003660#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003661static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003662static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3663
Stefan Assmann369bc182009-10-12 22:17:21 +02003664static int __init set_graph_function(char *str)
3665{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003666 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003667 return 1;
3668}
3669__setup("ftrace_graph_filter=", set_graph_function);
3670
3671static void __init set_ftrace_early_graph(char *buf)
3672{
3673 int ret;
3674 char *func;
3675
3676 while (buf) {
3677 func = strsep(&buf, ",");
3678 /* we allow only one expression at a time */
3679 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3680 func);
3681 if (ret)
3682 printk(KERN_DEBUG "ftrace: function %s not "
3683 "traceable\n", func);
3684 }
3685}
3686#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3687
Steven Rostedt2a85a372011-12-19 21:57:44 -05003688void __init
3689ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003690{
3691 char *func;
3692
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003693 ftrace_ops_init(ops);
3694
Steven Rostedt2af15d62009-05-28 13:37:24 -04003695 while (buf) {
3696 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003697 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003698 }
3699}
3700
3701static void __init set_ftrace_early_filters(void)
3702{
3703 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003704 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003705 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003706 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003707#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3708 if (ftrace_graph_buf[0])
3709 set_ftrace_early_graph(ftrace_graph_buf);
3710#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003711}
3712
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003713int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003714{
3715 struct seq_file *m = (struct seq_file *)file->private_data;
3716 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003717 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003718 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003719 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003720 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003721
Steven Rostedt5072c592008-05-12 21:20:43 +02003722 if (file->f_mode & FMODE_READ) {
3723 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003724 seq_release(inode, file);
3725 } else
3726 iter = file->private_data;
3727
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003728 parser = &iter->parser;
3729 if (trace_parser_loaded(parser)) {
3730 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003731 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003732 }
3733
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003734 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003735
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003736 mutex_lock(&iter->ops->regex_lock);
3737
Steven Rostedt058e2972011-04-29 22:35:33 -04003738 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003739 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3740
3741 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003742 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003743 else
3744 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003745
Steven Rostedt058e2972011-04-29 22:35:33 -04003746 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003747 ret = ftrace_hash_move(iter->ops, filter_hash,
3748 orig_hash, iter->hash);
3749 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3750 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003751 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003752
Steven Rostedt058e2972011-04-29 22:35:33 -04003753 mutex_unlock(&ftrace_lock);
3754 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003755
3756 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003757 free_ftrace_hash(iter->hash);
3758 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003759
Steven Rostedt5072c592008-05-12 21:20:43 +02003760 return 0;
3761}
3762
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003763static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003764 .open = ftrace_avail_open,
3765 .read = seq_read,
3766 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003767 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003768};
3769
Steven Rostedt647bcd02011-05-03 14:39:21 -04003770static const struct file_operations ftrace_enabled_fops = {
3771 .open = ftrace_enabled_open,
3772 .read = seq_read,
3773 .llseek = seq_lseek,
3774 .release = seq_release_private,
3775};
3776
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003777static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003778 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003779 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003780 .write = ftrace_filter_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003781 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003782 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003783};
3784
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003785static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003786 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003787 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003788 .write = ftrace_notrace_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003789 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003790 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003791};
3792
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003793#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3794
3795static DEFINE_MUTEX(graph_lock);
3796
3797int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003798int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003799unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3800
3801static void *
Li Zefan85951842009-06-24 09:54:00 +08003802__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003803{
Li Zefan85951842009-06-24 09:54:00 +08003804 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003805 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003806 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003807}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003808
Li Zefan85951842009-06-24 09:54:00 +08003809static void *
3810g_next(struct seq_file *m, void *v, loff_t *pos)
3811{
3812 (*pos)++;
3813 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003814}
3815
3816static void *g_start(struct seq_file *m, loff_t *pos)
3817{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003818 mutex_lock(&graph_lock);
3819
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003820 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003821 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003822 return (void *)1;
3823
Li Zefan85951842009-06-24 09:54:00 +08003824 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003825}
3826
3827static void g_stop(struct seq_file *m, void *p)
3828{
3829 mutex_unlock(&graph_lock);
3830}
3831
3832static int g_show(struct seq_file *m, void *v)
3833{
3834 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003835
3836 if (!ptr)
3837 return 0;
3838
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003839 if (ptr == (unsigned long *)1) {
3840 seq_printf(m, "#### all functions enabled ####\n");
3841 return 0;
3842 }
3843
Steven Rostedtb375a112009-09-17 00:05:58 -04003844 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003845
3846 return 0;
3847}
3848
James Morris88e9d342009-09-22 16:43:43 -07003849static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003850 .start = g_start,
3851 .next = g_next,
3852 .stop = g_stop,
3853 .show = g_show,
3854};
3855
3856static int
3857ftrace_graph_open(struct inode *inode, struct file *file)
3858{
3859 int ret = 0;
3860
3861 if (unlikely(ftrace_disabled))
3862 return -ENODEV;
3863
3864 mutex_lock(&graph_lock);
3865 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003866 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003867 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003868 ftrace_graph_count = 0;
3869 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3870 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003871 mutex_unlock(&graph_lock);
3872
Li Zefana4ec5e02009-09-18 14:06:28 +08003873 if (file->f_mode & FMODE_READ)
3874 ret = seq_open(file, &ftrace_graph_seq_ops);
3875
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003876 return ret;
3877}
3878
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003879static int
Li Zefan87827112009-07-23 11:29:11 +08003880ftrace_graph_release(struct inode *inode, struct file *file)
3881{
3882 if (file->f_mode & FMODE_READ)
3883 seq_release(inode, file);
3884 return 0;
3885}
3886
3887static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003888ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003889{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003890 struct dyn_ftrace *rec;
3891 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003892 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003893 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003894 int type, not;
3895 char *search;
3896 bool exists;
3897 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003898
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003899 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003900 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003901 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3902 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003903
3904 search_len = strlen(search);
3905
Steven Rostedt52baf112009-02-14 01:15:39 -05003906 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003907
3908 if (unlikely(ftrace_disabled)) {
3909 mutex_unlock(&ftrace_lock);
3910 return -ENODEV;
3911 }
3912
Steven Rostedt265c8312009-02-13 12:43:56 -05003913 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003914
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003915 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003916 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003917 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003918 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003919 if (array[i] == rec->ip) {
3920 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003921 break;
3922 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003923 }
3924
3925 if (!not) {
3926 fail = 0;
3927 if (!exists) {
3928 array[(*idx)++] = rec->ip;
3929 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3930 goto out;
3931 }
3932 } else {
3933 if (exists) {
3934 array[i] = array[--(*idx)];
3935 array[*idx] = 0;
3936 fail = 0;
3937 }
3938 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003939 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003940 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003941out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003942 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003943
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003944 if (fail)
3945 return -EINVAL;
3946
Namhyung Kim9f50afc2013-04-11 16:01:38 +09003947 ftrace_graph_filter_enabled = !!(*idx);
3948
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003949 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003950}
3951
3952static ssize_t
3953ftrace_graph_write(struct file *file, const char __user *ubuf,
3954 size_t cnt, loff_t *ppos)
3955{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003956 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003957 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003958
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003959 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003960 return 0;
3961
3962 mutex_lock(&graph_lock);
3963
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003964 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3965 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003966 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003967 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003968
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003969 read = trace_get_user(&parser, ubuf, cnt, ppos);
3970
Li Zefan4ba79782009-09-22 13:52:20 +08003971 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003972 parser.buffer[parser.idx] = 0;
3973
3974 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003975 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003976 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003977 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003978 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003979 }
3980
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003981 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003982
3983out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003984 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003985out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003986 mutex_unlock(&graph_lock);
3987
3988 return ret;
3989}
3990
3991static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003992 .open = ftrace_graph_open,
3993 .read = seq_read,
3994 .write = ftrace_graph_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003995 .llseek = ftrace_filter_lseek,
Li Zefan87827112009-07-23 11:29:11 +08003996 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003997};
3998#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3999
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004000static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004001{
Steven Rostedt5072c592008-05-12 21:20:43 +02004002
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004003 trace_create_file("available_filter_functions", 0444,
4004 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004005
Steven Rostedt647bcd02011-05-03 14:39:21 -04004006 trace_create_file("enabled_functions", 0444,
4007 d_tracer, NULL, &ftrace_enabled_fops);
4008
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004009 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4010 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004011
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004012 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004013 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004014
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004015#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004016 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004017 NULL,
4018 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004019#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4020
Steven Rostedt5072c592008-05-12 21:20:43 +02004021 return 0;
4022}
4023
Steven Rostedt9fd49322012-04-24 22:32:06 -04004024static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004025{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004026 const unsigned long *ipa = a;
4027 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004028
Steven Rostedt9fd49322012-04-24 22:32:06 -04004029 if (*ipa > *ipb)
4030 return 1;
4031 if (*ipa < *ipb)
4032 return -1;
4033 return 0;
4034}
4035
4036static void ftrace_swap_ips(void *a, void *b, int size)
4037{
4038 unsigned long *ipa = a;
4039 unsigned long *ipb = b;
4040 unsigned long t;
4041
4042 t = *ipa;
4043 *ipa = *ipb;
4044 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004045}
4046
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004047static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004048 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004049 unsigned long *end)
4050{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004051 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004052 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004053 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004054 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004055 unsigned long *p;
4056 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004057 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004058 int ret = -ENOMEM;
4059
4060 count = end - start;
4061
4062 if (!count)
4063 return 0;
4064
Steven Rostedt9fd49322012-04-24 22:32:06 -04004065 sort(start, count, sizeof(*start),
4066 ftrace_cmp_ips, ftrace_swap_ips);
4067
Steven Rostedt706c81f2012-04-24 23:45:26 -04004068 start_pg = ftrace_allocate_pages(count);
4069 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004070 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004071
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004072 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004073
Steven Rostedt32082302011-12-16 14:42:37 -05004074 /*
4075 * Core and each module needs their own pages, as
4076 * modules will free them when they are removed.
4077 * Force a new page to be allocated for modules.
4078 */
Steven Rostedta7900872011-12-16 16:23:44 -05004079 if (!mod) {
4080 WARN_ON(ftrace_pages || ftrace_pages_start);
4081 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004082 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004083 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004084 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004085 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004086
Steven Rostedta7900872011-12-16 16:23:44 -05004087 if (WARN_ON(ftrace_pages->next)) {
4088 /* Hmm, we have free pages? */
4089 while (ftrace_pages->next)
4090 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004091 }
Steven Rostedta7900872011-12-16 16:23:44 -05004092
Steven Rostedt706c81f2012-04-24 23:45:26 -04004093 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004094 }
4095
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004096 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004097 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004098 while (p < end) {
4099 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004100 /*
4101 * Some architecture linkers will pad between
4102 * the different mcount_loc sections of different
4103 * object files to satisfy alignments.
4104 * Skip any NULL pointers.
4105 */
4106 if (!addr)
4107 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004108
4109 if (pg->index == pg->size) {
4110 /* We should have allocated enough */
4111 if (WARN_ON(!pg->next))
4112 break;
4113 pg = pg->next;
4114 }
4115
4116 rec = &pg->records[pg->index++];
4117 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004118 }
4119
Steven Rostedt706c81f2012-04-24 23:45:26 -04004120 /* We should have used all pages */
4121 WARN_ON(pg->next);
4122
4123 /* Assign the last page to ftrace_pages */
4124 ftrace_pages = pg;
4125
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004126 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004127 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004128
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004129 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004130 * We only need to disable interrupts on start up
4131 * because we are modifying code that an interrupt
4132 * may execute, and the modification is not atomic.
4133 * But for modules, nothing runs the code we modify
4134 * until we are finished with it, and there's no
4135 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004136 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004137 if (!mod)
4138 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08004139 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004140 if (!mod)
4141 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004142 ret = 0;
4143 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004144 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004145
Steven Rostedta7900872011-12-16 16:23:44 -05004146 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004147}
4148
Steven Rostedt93eb6772009-04-15 13:24:06 -04004149#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004150
4151#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4152
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004153void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004154{
4155 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004156 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004157 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004158 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004159
Steven Rostedt93eb6772009-04-15 13:24:06 -04004160 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004161
4162 if (ftrace_disabled)
4163 goto out_unlock;
4164
Steven Rostedt32082302011-12-16 14:42:37 -05004165 /*
4166 * Each module has its own ftrace_pages, remove
4167 * them from the list.
4168 */
4169 last_pg = &ftrace_pages_start;
4170 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4171 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004172 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004173 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004174 * As core pages are first, the first
4175 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004176 */
Steven Rostedt32082302011-12-16 14:42:37 -05004177 if (WARN_ON(pg == ftrace_pages_start))
4178 goto out_unlock;
4179
4180 /* Check if we are deleting the last page */
4181 if (pg == ftrace_pages)
4182 ftrace_pages = next_to_ftrace_page(last_pg);
4183
4184 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004185 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4186 free_pages((unsigned long)pg->records, order);
4187 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004188 } else
4189 last_pg = &pg->next;
4190 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004191 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004192 mutex_unlock(&ftrace_lock);
4193}
4194
4195static void ftrace_init_module(struct module *mod,
4196 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004197{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004198 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004199 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004200 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004201}
4202
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004203static int ftrace_module_notify_enter(struct notifier_block *self,
4204 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004205{
4206 struct module *mod = data;
4207
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004208 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004209 ftrace_init_module(mod, mod->ftrace_callsites,
4210 mod->ftrace_callsites +
4211 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004212 return 0;
4213}
4214
4215static int ftrace_module_notify_exit(struct notifier_block *self,
4216 unsigned long val, void *data)
4217{
4218 struct module *mod = data;
4219
4220 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004221 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004222
4223 return 0;
4224}
4225#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004226static int ftrace_module_notify_enter(struct notifier_block *self,
4227 unsigned long val, void *data)
4228{
4229 return 0;
4230}
4231static int ftrace_module_notify_exit(struct notifier_block *self,
4232 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004233{
4234 return 0;
4235}
4236#endif /* CONFIG_MODULES */
4237
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004238struct notifier_block ftrace_module_enter_nb = {
4239 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004240 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004241};
4242
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004243struct notifier_block ftrace_module_exit_nb = {
4244 .notifier_call = ftrace_module_notify_exit,
4245 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4246};
4247
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004248extern unsigned long __start_mcount_loc[];
4249extern unsigned long __stop_mcount_loc[];
4250
4251void __init ftrace_init(void)
4252{
4253 unsigned long count, addr, flags;
4254 int ret;
4255
4256 /* Keep the ftrace pointer to the stub */
4257 addr = (unsigned long)ftrace_stub;
4258
4259 local_irq_save(flags);
4260 ftrace_dyn_arch_init(&addr);
4261 local_irq_restore(flags);
4262
4263 /* ftrace_dyn_arch_init places the return code in addr */
4264 if (addr)
4265 goto failed;
4266
4267 count = __stop_mcount_loc - __start_mcount_loc;
4268
4269 ret = ftrace_dyn_table_alloc(count);
4270 if (ret)
4271 goto failed;
4272
4273 last_ftrace_enabled = ftrace_enabled = 1;
4274
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004275 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004276 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004277 __stop_mcount_loc);
4278
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004279 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004280 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004281 pr_warning("Failed to register trace ftrace module enter notifier\n");
4282
4283 ret = register_module_notifier(&ftrace_module_exit_nb);
4284 if (ret)
4285 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004286
Steven Rostedt2af15d62009-05-28 13:37:24 -04004287 set_ftrace_early_filters();
4288
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004289 return;
4290 failed:
4291 ftrace_disabled = 1;
4292}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004293
Steven Rostedt3d083392008-05-12 21:20:42 +02004294#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004295
Steven Rostedt2b499382011-05-03 22:49:52 -04004296static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004297 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004298 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4299 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004300};
4301
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004302static int __init ftrace_nodyn_init(void)
4303{
4304 ftrace_enabled = 1;
4305 return 0;
4306}
Steven Rostedt6f415672012-10-05 12:13:07 -04004307core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004308
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004309static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4310static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004311/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004312# define ftrace_startup(ops, command) \
4313 ({ \
4314 int ___ret = __register_ftrace_function(ops); \
4315 if (!___ret) \
4316 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4317 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004318 })
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004319# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4320
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004321# define ftrace_startup_sysctl() do { } while (0)
4322# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004323
4324static inline int
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04004325ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004326{
4327 return 1;
4328}
4329
Steven Rostedt3d083392008-05-12 21:20:42 +02004330#endif /* CONFIG_DYNAMIC_FTRACE */
4331
Steven Rostedtb8489142011-05-04 09:27:52 -04004332static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004333ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004334 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004335{
Jiri Olsae2484912012-02-15 15:51:48 +01004336 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4337 return;
4338
4339 /*
4340 * Some of the ops may be dynamically allocated,
4341 * they must be freed after a synchronize_sched().
4342 */
4343 preempt_disable_notrace();
4344 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt0a016402012-11-02 17:03:03 -04004345 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004346 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4347 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04004348 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004349 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004350 } while_for_each_ftrace_op(op);
Jiri Olsae2484912012-02-15 15:51:48 +01004351 trace_recursion_clear(TRACE_CONTROL_BIT);
4352 preempt_enable_notrace();
4353}
4354
4355static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004356 .func = ftrace_ops_control_func,
4357 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4358 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004359};
4360
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004361static inline void
4362__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004363 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004364{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004365 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004366 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004367
Steven Rostedtccf36722012-06-05 09:44:25 -04004368 if (function_trace_stop)
4369 return;
4370
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004371 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4372 if (bit < 0)
4373 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004374
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004375 /*
4376 * Some of the ops may be dynamically allocated,
4377 * they must be freed after a synchronize_sched().
4378 */
4379 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004380 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)29632b12013-07-23 22:06:15 -04004381 if (ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004382 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004383 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004384 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004385 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004386}
4387
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004388/*
4389 * Some archs only support passing ip and parent_ip. Even though
4390 * the list function ignores the op parameter, we do not want any
4391 * C side effects, where a function is called without the caller
4392 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004393 * Archs are to support both the regs and ftrace_ops at the same time.
4394 * If they support ftrace_ops, it is assumed they support regs.
4395 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004396 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4397 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004398 * An architecture can pass partial regs with ftrace_ops and still
4399 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004400 */
4401#if ARCH_SUPPORTS_FTRACE_OPS
4402static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004403 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004404{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004405 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004406}
4407#else
4408static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4409{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004410 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004411}
4412#endif
4413
Steven Rostedte32d8952008-12-04 00:26:41 -05004414static void clear_ftrace_swapper(void)
4415{
4416 struct task_struct *p;
4417 int cpu;
4418
4419 get_online_cpus();
4420 for_each_online_cpu(cpu) {
4421 p = idle_task(cpu);
4422 clear_tsk_trace_trace(p);
4423 }
4424 put_online_cpus();
4425}
4426
4427static void set_ftrace_swapper(void)
4428{
4429 struct task_struct *p;
4430 int cpu;
4431
4432 get_online_cpus();
4433 for_each_online_cpu(cpu) {
4434 p = idle_task(cpu);
4435 set_tsk_trace_trace(p);
4436 }
4437 put_online_cpus();
4438}
4439
4440static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004441{
4442 struct task_struct *p;
4443
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004444 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004445 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004446 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004447 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004448 rcu_read_unlock();
4449
Steven Rostedte32d8952008-12-04 00:26:41 -05004450 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004451}
4452
Steven Rostedte32d8952008-12-04 00:26:41 -05004453static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004454{
4455 struct task_struct *p;
4456
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004457 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004458 do_each_pid_task(pid, PIDTYPE_PID, p) {
4459 set_tsk_trace_trace(p);
4460 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004461 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004462}
4463
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004464static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004465{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004466 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004467 clear_ftrace_swapper();
4468 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004469 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004470}
4471
4472static void set_ftrace_pid_task(struct pid *pid)
4473{
4474 if (pid == ftrace_swapper_pid)
4475 set_ftrace_swapper();
4476 else
4477 set_ftrace_pid(pid);
4478}
4479
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004480static int ftrace_pid_add(int p)
4481{
4482 struct pid *pid;
4483 struct ftrace_pid *fpid;
4484 int ret = -EINVAL;
4485
4486 mutex_lock(&ftrace_lock);
4487
4488 if (!p)
4489 pid = ftrace_swapper_pid;
4490 else
4491 pid = find_get_pid(p);
4492
4493 if (!pid)
4494 goto out;
4495
4496 ret = 0;
4497
4498 list_for_each_entry(fpid, &ftrace_pids, list)
4499 if (fpid->pid == pid)
4500 goto out_put;
4501
4502 ret = -ENOMEM;
4503
4504 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4505 if (!fpid)
4506 goto out_put;
4507
4508 list_add(&fpid->list, &ftrace_pids);
4509 fpid->pid = pid;
4510
4511 set_ftrace_pid_task(pid);
4512
4513 ftrace_update_pid_func();
4514 ftrace_startup_enable(0);
4515
4516 mutex_unlock(&ftrace_lock);
4517 return 0;
4518
4519out_put:
4520 if (pid != ftrace_swapper_pid)
4521 put_pid(pid);
4522
4523out:
4524 mutex_unlock(&ftrace_lock);
4525 return ret;
4526}
4527
4528static void ftrace_pid_reset(void)
4529{
4530 struct ftrace_pid *fpid, *safe;
4531
4532 mutex_lock(&ftrace_lock);
4533 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4534 struct pid *pid = fpid->pid;
4535
4536 clear_ftrace_pid_task(pid);
4537
4538 list_del(&fpid->list);
4539 kfree(fpid);
4540 }
4541
4542 ftrace_update_pid_func();
4543 ftrace_startup_enable(0);
4544
4545 mutex_unlock(&ftrace_lock);
4546}
4547
4548static void *fpid_start(struct seq_file *m, loff_t *pos)
4549{
4550 mutex_lock(&ftrace_lock);
4551
4552 if (list_empty(&ftrace_pids) && (!*pos))
4553 return (void *) 1;
4554
4555 return seq_list_start(&ftrace_pids, *pos);
4556}
4557
4558static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4559{
4560 if (v == (void *)1)
4561 return NULL;
4562
4563 return seq_list_next(v, &ftrace_pids, pos);
4564}
4565
4566static void fpid_stop(struct seq_file *m, void *p)
4567{
4568 mutex_unlock(&ftrace_lock);
4569}
4570
4571static int fpid_show(struct seq_file *m, void *v)
4572{
4573 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4574
4575 if (v == (void *)1) {
4576 seq_printf(m, "no pid\n");
4577 return 0;
4578 }
4579
4580 if (fpid->pid == ftrace_swapper_pid)
4581 seq_printf(m, "swapper tasks\n");
4582 else
4583 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4584
4585 return 0;
4586}
4587
4588static const struct seq_operations ftrace_pid_sops = {
4589 .start = fpid_start,
4590 .next = fpid_next,
4591 .stop = fpid_stop,
4592 .show = fpid_show,
4593};
4594
4595static int
4596ftrace_pid_open(struct inode *inode, struct file *file)
4597{
4598 int ret = 0;
4599
4600 if ((file->f_mode & FMODE_WRITE) &&
4601 (file->f_flags & O_TRUNC))
4602 ftrace_pid_reset();
4603
4604 if (file->f_mode & FMODE_READ)
4605 ret = seq_open(file, &ftrace_pid_sops);
4606
4607 return ret;
4608}
4609
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004610static ssize_t
4611ftrace_pid_write(struct file *filp, const char __user *ubuf,
4612 size_t cnt, loff_t *ppos)
4613{
Ingo Molnar457dc922009-11-23 11:03:28 +01004614 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004615 long val;
4616 int ret;
4617
4618 if (cnt >= sizeof(buf))
4619 return -EINVAL;
4620
4621 if (copy_from_user(&buf, ubuf, cnt))
4622 return -EFAULT;
4623
4624 buf[cnt] = 0;
4625
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004626 /*
4627 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4628 * to clean the filter quietly.
4629 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004630 tmp = strstrip(buf);
4631 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004632 return 1;
4633
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004634 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004635 if (ret < 0)
4636 return ret;
4637
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004638 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004639
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004640 return ret ? ret : cnt;
4641}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004642
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004643static int
4644ftrace_pid_release(struct inode *inode, struct file *file)
4645{
4646 if (file->f_mode & FMODE_READ)
4647 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004648
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004649 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004650}
4651
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004652static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004653 .open = ftrace_pid_open,
4654 .write = ftrace_pid_write,
4655 .read = seq_read,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09004656 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004657 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004658};
4659
4660static __init int ftrace_init_debugfs(void)
4661{
4662 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004663
4664 d_tracer = tracing_init_dentry();
4665 if (!d_tracer)
4666 return 0;
4667
4668 ftrace_init_dyn_debugfs(d_tracer);
4669
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004670 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4671 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004672
4673 ftrace_profile_debugfs(d_tracer);
4674
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004675 return 0;
4676}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004677fs_initcall(ftrace_init_debugfs);
4678
Steven Rostedt3d083392008-05-12 21:20:42 +02004679/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004680 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004681 *
4682 * This function should be used by panic code. It stops ftrace
4683 * but in a not so nice way. If you need to simply kill ftrace
4684 * from a non-atomic section, use ftrace_kill.
4685 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004686void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004687{
4688 ftrace_disabled = 1;
4689 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004690 clear_ftrace_function();
4691}
4692
4693/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004694 * Test if ftrace is dead or not.
4695 */
4696int ftrace_is_dead(void)
4697{
4698 return ftrace_disabled;
4699}
4700
4701/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004702 * register_ftrace_function - register a function for profiling
4703 * @ops - ops structure that holds the function for profiling.
4704 *
4705 * Register a function to be called by all functions in the
4706 * kernel.
4707 *
4708 * Note: @ops->func and all the functions it calls must be labeled
4709 * with "notrace", otherwise it will go into a
4710 * recursive loop.
4711 */
4712int register_ftrace_function(struct ftrace_ops *ops)
4713{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004714 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004715
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004716 ftrace_ops_init(ops);
4717
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004718 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004719
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004720 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004721
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004722 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004723
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004724 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004725}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004726EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004727
4728/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004729 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004730 * @ops - ops structure that holds the function to unregister
4731 *
4732 * Unregister a function that was added to be called by ftrace profiling.
4733 */
4734int unregister_ftrace_function(struct ftrace_ops *ops)
4735{
4736 int ret;
4737
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004738 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004739 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004740 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004741
4742 return ret;
4743}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004744EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004745
Ingo Molnare309b412008-05-12 21:20:51 +02004746int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004747ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004748 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004749 loff_t *ppos)
4750{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004751 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004752
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004753 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004754
Steven Rostedt45a4a232011-04-21 23:16:46 -04004755 if (unlikely(ftrace_disabled))
4756 goto out;
4757
4758 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004759
Li Zefana32c7762009-06-26 16:55:51 +08004760 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004761 goto out;
4762
Li Zefana32c7762009-06-26 16:55:51 +08004763 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004764
4765 if (ftrace_enabled) {
4766
4767 ftrace_startup_sysctl();
4768
4769 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004770 if (ftrace_ops_list != &ftrace_list_end)
4771 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004772
4773 } else {
4774 /* stopping ftrace calls (just send to ftrace_stub) */
4775 ftrace_trace_function = ftrace_stub;
4776
4777 ftrace_shutdown_sysctl();
4778 }
4779
4780 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004781 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004782 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004783}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004784
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004785#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004786
Steven Rostedt597af812009-04-03 15:24:12 -04004787static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004788static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004789
Steven Rostedte49dc192008-12-02 23:50:05 -05004790int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4791{
4792 return 0;
4793}
4794
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004795/* The callbacks that hook a function */
4796trace_func_graph_ret_t ftrace_graph_return =
4797 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004798trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004799
4800/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4801static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4802{
4803 int i;
4804 int ret = 0;
4805 unsigned long flags;
4806 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4807 struct task_struct *g, *t;
4808
4809 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4810 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4811 * sizeof(struct ftrace_ret_stack),
4812 GFP_KERNEL);
4813 if (!ret_stack_list[i]) {
4814 start = 0;
4815 end = i;
4816 ret = -ENOMEM;
4817 goto free;
4818 }
4819 }
4820
4821 read_lock_irqsave(&tasklist_lock, flags);
4822 do_each_thread(g, t) {
4823 if (start == end) {
4824 ret = -EAGAIN;
4825 goto unlock;
4826 }
4827
4828 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004829 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004830 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004831 t->curr_ret_stack = -1;
4832 /* Make sure the tasks see the -1 first: */
4833 smp_wmb();
4834 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004835 }
4836 } while_each_thread(g, t);
4837
4838unlock:
4839 read_unlock_irqrestore(&tasklist_lock, flags);
4840free:
4841 for (i = start; i < end; i++)
4842 kfree(ret_stack_list[i]);
4843 return ret;
4844}
4845
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004846static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004847ftrace_graph_probe_sched_switch(void *ignore,
4848 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004849{
4850 unsigned long long timestamp;
4851 int index;
4852
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004853 /*
4854 * Does the user want to count the time a function was asleep.
4855 * If so, do not update the time stamps.
4856 */
4857 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4858 return;
4859
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004860 timestamp = trace_clock_local();
4861
4862 prev->ftrace_timestamp = timestamp;
4863
4864 /* only process tasks that we timestamped */
4865 if (!next->ftrace_timestamp)
4866 return;
4867
4868 /*
4869 * Update all the counters in next to make up for the
4870 * time next was sleeping.
4871 */
4872 timestamp -= next->ftrace_timestamp;
4873
4874 for (index = next->curr_ret_stack; index >= 0; index--)
4875 next->ret_stack[index].calltime += timestamp;
4876}
4877
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004878/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004879static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004880{
4881 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004882 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004883
4884 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4885 sizeof(struct ftrace_ret_stack *),
4886 GFP_KERNEL);
4887
4888 if (!ret_stack_list)
4889 return -ENOMEM;
4890
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004891 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004892 for_each_online_cpu(cpu) {
4893 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004894 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004895 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004896
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004897 do {
4898 ret = alloc_retstack_tasklist(ret_stack_list);
4899 } while (ret == -EAGAIN);
4900
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004901 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004902 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004903 if (ret)
4904 pr_info("ftrace_graph: Couldn't activate tracepoint"
4905 " probe to kernel_sched_switch\n");
4906 }
4907
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004908 kfree(ret_stack_list);
4909 return ret;
4910}
4911
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004912/*
4913 * Hibernation protection.
4914 * The state of the current task is too much unstable during
4915 * suspend/restore to disk. We want to protect against that.
4916 */
4917static int
4918ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4919 void *unused)
4920{
4921 switch (state) {
4922 case PM_HIBERNATION_PREPARE:
4923 pause_graph_tracing();
4924 break;
4925
4926 case PM_POST_HIBERNATION:
4927 unpause_graph_tracing();
4928 break;
4929 }
4930 return NOTIFY_DONE;
4931}
4932
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004933/* Just a place holder for function graph */
4934static struct ftrace_ops fgraph_ops __read_mostly = {
4935 .func = ftrace_stub,
4936 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
4937 FTRACE_OPS_FL_RECURSION_SAFE,
4938};
4939
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004940int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4941 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004942{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004943 int ret = 0;
4944
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004945 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004946
Steven Rostedt05ce5812009-03-24 00:18:31 -04004947 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004948 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004949 ret = -EBUSY;
4950 goto out;
4951 }
4952
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004953 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4954 register_pm_notifier(&ftrace_suspend_notifier);
4955
Steven Rostedt597af812009-04-03 15:24:12 -04004956 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004957 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004958 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004959 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004960 goto out;
4961 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004962
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004963 ftrace_graph_return = retfunc;
4964 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004965
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004966 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004967
4968out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004969 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004970 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004971}
4972
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004973void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004974{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004975 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004976
Steven Rostedt597af812009-04-03 15:24:12 -04004977 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004978 goto out;
4979
Steven Rostedt597af812009-04-03 15:24:12 -04004980 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004981 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004982 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)2940c252013-11-25 20:59:46 -05004983 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004984 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004985 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004986
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004987 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004988 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004989}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004990
Steven Rostedt868baf02011-02-10 21:26:13 -05004991static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4992
4993static void
4994graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4995{
4996 atomic_set(&t->tracing_graph_pause, 0);
4997 atomic_set(&t->trace_overrun, 0);
4998 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004999 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005000 smp_wmb();
5001 t->ret_stack = ret_stack;
5002}
5003
5004/*
5005 * Allocate a return stack for the idle task. May be the first
5006 * time through, or it may be done by CPU hotplug online.
5007 */
5008void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5009{
5010 t->curr_ret_stack = -1;
5011 /*
5012 * The idle task has no parent, it either has its own
5013 * stack or no stack at all.
5014 */
5015 if (t->ret_stack)
5016 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5017
5018 if (ftrace_graph_active) {
5019 struct ftrace_ret_stack *ret_stack;
5020
5021 ret_stack = per_cpu(idle_ret_stack, cpu);
5022 if (!ret_stack) {
5023 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5024 * sizeof(struct ftrace_ret_stack),
5025 GFP_KERNEL);
5026 if (!ret_stack)
5027 return;
5028 per_cpu(idle_ret_stack, cpu) = ret_stack;
5029 }
5030 graph_init_task(t, ret_stack);
5031 }
5032}
5033
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005034/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005035void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005036{
Steven Rostedt84047e32009-06-02 16:51:55 -04005037 /* Make sure we do not use the parent ret_stack */
5038 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005039 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005040
Steven Rostedt597af812009-04-03 15:24:12 -04005041 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005042 struct ftrace_ret_stack *ret_stack;
5043
5044 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005045 * sizeof(struct ftrace_ret_stack),
5046 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005047 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005048 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005049 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005050 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005051}
5052
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005053void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005054{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005055 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5056
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005057 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005058 /* NULL must become visible to IRQs before we free it: */
5059 barrier();
5060
5061 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005062}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005063
5064void ftrace_graph_stop(void)
5065{
5066 ftrace_stop();
5067}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005068#endif