blob: 5313c1100d30602c4a5edffac6404849f8539986 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt6912896e2008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Jiri Olsae2484912012-02-15 15:51:48 +010065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
68#define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
70#else
71#define INIT_REGEX_LOCK(opsname)
72#endif
73
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040074static struct ftrace_ops ftrace_list_end __read_mostly = {
75 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040076 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077};
78
Steven Rostedt4eebcc82008-05-12 21:20:48 +020079/* ftrace_enabled is a method to turn ftrace on or off */
80int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020081static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020082
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050083/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040084int function_trace_stop __read_mostly;
85
86/* Current function tracing op */
87struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -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{
Steven Rostedt (Red Hat)e6435e92014-01-10 14:31:31 -0500247 ftrace_func_t func = ftrace_global_list_func;
248 void *private = NULL;
249
250 /* The list has its own recursion protection. */
251 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400252
253 /*
254 * If there's only one function registered, then call that
255 * function directly. Otherwise, we need to iterate over the
256 * registered callers.
257 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400258 if (ftrace_global_list == &ftrace_list_end ||
Steven Rostedt63503792012-11-02 16:58:56 -0400259 ftrace_global_list->next == &ftrace_list_end) {
Steven Rostedtb8489142011-05-04 09:27:52 -0400260 func = ftrace_global_list->func;
Steven Rostedt (Red Hat)e6435e92014-01-10 14:31:31 -0500261 private = ftrace_global_list->private;
Steven Rostedt63503792012-11-02 16:58:56 -0400262 /*
263 * As we are calling the function directly.
264 * If it does not have recursion protection,
265 * the function_trace_op needs to be updated
266 * accordingly.
267 */
Steven Rostedt (Red Hat)e6435e92014-01-10 14:31:31 -0500268 if (!(ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE))
Steven Rostedt63503792012-11-02 16:58:56 -0400269 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
Steven Rostedt63503792012-11-02 16:58:56 -0400270 }
271
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400272 /* If we filter on pids, update to use the pid function */
273 if (!list_empty(&ftrace_pids)) {
274 set_ftrace_pid_function(func);
275 func = ftrace_pid_func;
276 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400277
278 global_ops.func = func;
Steven Rostedt (Red Hat)e6435e92014-01-10 14:31:31 -0500279 global_ops.private = private;
Steven Rostedt2b499382011-05-03 22:49:52 -0400280}
281
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500282static void ftrace_sync(struct work_struct *work)
283{
284 /*
285 * This function is just a stub to implement a hard force
286 * of synchronize_sched(). This requires synchronizing
287 * tasks even in userspace and idle.
288 *
289 * Yes, function tracing is rude.
290 */
291}
292
293static void ftrace_sync_ipi(void *data)
294{
295 /* Probably not needed, but do it anyway */
296 smp_rmb();
297}
298
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500299#ifdef CONFIG_FUNCTION_GRAPH_TRACER
300static void update_function_graph_func(void);
301#else
302static inline void update_function_graph_func(void) { }
303#endif
304
Steven Rostedt2b499382011-05-03 22:49:52 -0400305static void update_ftrace_function(void)
306{
307 ftrace_func_t func;
308
309 update_global_ops();
310
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400311 /*
312 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400313 * recursion safe and not dynamic and the arch supports passing ops,
314 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400315 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400316 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400317 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400318 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400319 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400320 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400321 /* Set the ftrace_ops that the arch callback uses */
322 if (ftrace_ops_list == &global_ops)
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500323 set_function_trace_op = ftrace_global_list;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400324 else
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500325 set_function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400326 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400327 } else {
328 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500329 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400330 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400331 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400332
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500333 /* If there's no change, then do nothing more here */
334 if (ftrace_trace_function == func)
335 return;
336
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500337 update_function_graph_func();
338
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500339 /*
340 * If we are using the list function, it doesn't care
341 * about the function_trace_ops.
342 */
343 if (func == ftrace_ops_list_func) {
344 ftrace_trace_function = func;
345 /*
346 * Don't even bother setting function_trace_ops,
347 * it would be racy to do so anyway.
348 */
349 return;
350 }
351
352#ifndef CONFIG_DYNAMIC_FTRACE
353 /*
354 * For static tracing, we need to be a bit more careful.
355 * The function change takes affect immediately. Thus,
356 * we need to coorditate the setting of the function_trace_ops
357 * with the setting of the ftrace_trace_function.
358 *
359 * Set the function to the list ops, which will call the
360 * function we want, albeit indirectly, but it handles the
361 * ftrace_ops and doesn't depend on function_trace_op.
362 */
363 ftrace_trace_function = ftrace_ops_list_func;
364 /*
365 * Make sure all CPUs see this. Yes this is slow, but static
366 * tracing is slow and nasty to have enabled.
367 */
368 schedule_on_each_cpu(ftrace_sync);
369 /* Now all cpus are using the list ops. */
370 function_trace_op = set_function_trace_op;
371 /* Make sure the function_trace_op is visible on all CPUs */
372 smp_wmb();
373 /* Nasty way to force a rmb on all cpus */
374 smp_call_function(ftrace_sync_ipi, NULL, 1);
375 /* OK, we are all set to update the ftrace_trace_function now! */
376#endif /* !CONFIG_DYNAMIC_FTRACE */
377
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400378 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400379}
380
Steven Rostedt2b499382011-05-03 22:49:52 -0400381static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200382{
Steven Rostedt2b499382011-05-03 22:49:52 -0400383 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200384 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400385 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200386 * CPU might be walking that list. We need to make sure
387 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400388 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200389 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400390 rcu_assign_pointer(*list, ops);
391}
Steven Rostedt3d083392008-05-12 21:20:42 +0200392
Steven Rostedt2b499382011-05-03 22:49:52 -0400393static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
394{
395 struct ftrace_ops **p;
396
397 /*
398 * If we are removing the last function, then simply point
399 * to the ftrace_stub.
400 */
401 if (*list == ops && ops->next == &ftrace_list_end) {
402 *list = &ftrace_list_end;
403 return 0;
404 }
405
406 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
407 if (*p == ops)
408 break;
409
410 if (*p != ops)
411 return -1;
412
413 *p = (*p)->next;
414 return 0;
415}
416
Jiri Olsae2484912012-02-15 15:51:48 +0100417static void add_ftrace_list_ops(struct ftrace_ops **list,
418 struct ftrace_ops *main_ops,
419 struct ftrace_ops *ops)
420{
421 int first = *list == &ftrace_list_end;
422 add_ftrace_ops(list, ops);
423 if (first)
424 add_ftrace_ops(&ftrace_ops_list, main_ops);
425}
426
427static int remove_ftrace_list_ops(struct ftrace_ops **list,
428 struct ftrace_ops *main_ops,
429 struct ftrace_ops *ops)
430{
431 int ret = remove_ftrace_ops(list, ops);
432 if (!ret && *list == &ftrace_list_end)
433 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
434 return ret;
435}
436
Steven Rostedt2b499382011-05-03 22:49:52 -0400437static int __register_ftrace_function(struct ftrace_ops *ops)
438{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500439 if (ops->flags & FTRACE_OPS_FL_DELETED)
440 return -EINVAL;
441
Steven Rostedt2b499382011-05-03 22:49:52 -0400442 if (FTRACE_WARN_ON(ops == &global_ops))
443 return -EINVAL;
444
Steven Rostedtb8489142011-05-04 09:27:52 -0400445 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
446 return -EBUSY;
447
Jiri Olsae2484912012-02-15 15:51:48 +0100448 /* We don't support both control and global flags set. */
449 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
450 return -EINVAL;
451
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900452#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400453 /*
454 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
455 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
456 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
457 */
458 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
459 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
460 return -EINVAL;
461
462 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
463 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
464#endif
465
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400466 if (!core_kernel_data((unsigned long)ops))
467 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
468
Steven Rostedtb8489142011-05-04 09:27:52 -0400469 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100470 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400471 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100472 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
473 if (control_ops_alloc(ops))
474 return -ENOMEM;
475 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400476 } else
477 add_ftrace_ops(&ftrace_ops_list, ops);
478
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400479 if (ftrace_enabled)
480 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200481
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200482 return 0;
483}
484
Ingo Molnare309b412008-05-12 21:20:51 +0200485static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200486{
Steven Rostedt2b499382011-05-03 22:49:52 -0400487 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200488
Steven Rostedtb8489142011-05-04 09:27:52 -0400489 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
490 return -EBUSY;
491
Steven Rostedt2b499382011-05-03 22:49:52 -0400492 if (FTRACE_WARN_ON(ops == &global_ops))
493 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200494
Steven Rostedtb8489142011-05-04 09:27:52 -0400495 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100496 ret = remove_ftrace_list_ops(&ftrace_global_list,
497 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400498 if (!ret)
499 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100500 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
501 ret = remove_ftrace_list_ops(&ftrace_control_list,
502 &control_ops, ops);
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 Rostedte6ea44e2009-02-14 01:42:44 -0500512 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200513}
514
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500515static void ftrace_update_pid_func(void)
516{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400517 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500518 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900519 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500520
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400521 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500522}
523
Steven Rostedt493762f2009-03-23 17:12:36 -0400524#ifdef CONFIG_FUNCTION_PROFILER
525struct ftrace_profile {
526 struct hlist_node node;
527 unsigned long ip;
528 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400529#ifdef CONFIG_FUNCTION_GRAPH_TRACER
530 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400531 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400532#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400533};
534
535struct ftrace_profile_page {
536 struct ftrace_profile_page *next;
537 unsigned long index;
538 struct ftrace_profile records[];
539};
540
Steven Rostedtcafb1682009-03-24 20:50:39 -0400541struct ftrace_profile_stat {
542 atomic_t disabled;
543 struct hlist_head *hash;
544 struct ftrace_profile_page *pages;
545 struct ftrace_profile_page *start;
546 struct tracer_stat stat;
547};
548
Steven Rostedt493762f2009-03-23 17:12:36 -0400549#define PROFILE_RECORDS_SIZE \
550 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
551
552#define PROFILES_PER_PAGE \
553 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
554
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400555static int ftrace_profile_enabled __read_mostly;
556
557/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400558static DEFINE_MUTEX(ftrace_profile_lock);
559
Steven Rostedtcafb1682009-03-24 20:50:39 -0400560static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400561
Namhyung Kim20079eb2013-04-10 08:55:50 +0900562#define FTRACE_PROFILE_HASH_BITS 10
563#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400564
Steven Rostedt493762f2009-03-23 17:12:36 -0400565static void *
566function_stat_next(void *v, int idx)
567{
568 struct ftrace_profile *rec = v;
569 struct ftrace_profile_page *pg;
570
571 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
572
573 again:
Li Zefan0296e422009-06-26 11:15:37 +0800574 if (idx != 0)
575 rec++;
576
Steven Rostedt493762f2009-03-23 17:12:36 -0400577 if ((void *)rec >= (void *)&pg->records[pg->index]) {
578 pg = pg->next;
579 if (!pg)
580 return NULL;
581 rec = &pg->records[0];
582 if (!rec->counter)
583 goto again;
584 }
585
586 return rec;
587}
588
589static void *function_stat_start(struct tracer_stat *trace)
590{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400591 struct ftrace_profile_stat *stat =
592 container_of(trace, struct ftrace_profile_stat, stat);
593
594 if (!stat || !stat->start)
595 return NULL;
596
597 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400598}
599
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400600#ifdef CONFIG_FUNCTION_GRAPH_TRACER
601/* function graph compares on total time */
602static int function_stat_cmp(void *p1, void *p2)
603{
604 struct ftrace_profile *a = p1;
605 struct ftrace_profile *b = p2;
606
607 if (a->time < b->time)
608 return -1;
609 if (a->time > b->time)
610 return 1;
611 else
612 return 0;
613}
614#else
615/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400616static int function_stat_cmp(void *p1, void *p2)
617{
618 struct ftrace_profile *a = p1;
619 struct ftrace_profile *b = p2;
620
621 if (a->counter < b->counter)
622 return -1;
623 if (a->counter > b->counter)
624 return 1;
625 else
626 return 0;
627}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400628#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400629
630static int function_stat_headers(struct seq_file *m)
631{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400632#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400633 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400634 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400635 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400636 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400637#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400638 seq_printf(m, " Function Hit\n"
639 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400640#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400641 return 0;
642}
643
644static int function_stat_show(struct seq_file *m, void *v)
645{
646 struct ftrace_profile *rec = v;
647 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800648 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400649#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400650 static struct trace_seq s;
651 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400652 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400653#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800654 mutex_lock(&ftrace_profile_lock);
655
656 /* we raced with function_profile_reset() */
657 if (unlikely(rec->counter == 0)) {
658 ret = -EBUSY;
659 goto out;
660 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400661
662 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400663 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400664
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400665#ifdef CONFIG_FUNCTION_GRAPH_TRACER
666 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400667 avg = rec->time;
668 do_div(avg, rec->counter);
669
Chase Douglase330b3b2010-04-26 14:02:05 -0400670 /* Sample standard deviation (s^2) */
671 if (rec->counter <= 1)
672 stddev = 0;
673 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200674 /*
675 * Apply Welford's method:
676 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
677 */
678 stddev = rec->counter * rec->time_squared -
679 rec->time * rec->time;
680
Chase Douglase330b3b2010-04-26 14:02:05 -0400681 /*
682 * Divide only 1000 for ns^2 -> us^2 conversion.
683 * trace_print_graph_duration will divide 1000 again.
684 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200685 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400686 }
687
Steven Rostedt34886c82009-03-25 21:00:47 -0400688 trace_seq_init(&s);
689 trace_print_graph_duration(rec->time, &s);
690 trace_seq_puts(&s, " ");
691 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400692 trace_seq_puts(&s, " ");
693 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400694 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400695#endif
696 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800697out:
698 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400699
Li Zefan3aaba202010-08-23 16:50:12 +0800700 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400701}
702
Steven Rostedtcafb1682009-03-24 20:50:39 -0400703static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400704{
705 struct ftrace_profile_page *pg;
706
Steven Rostedtcafb1682009-03-24 20:50:39 -0400707 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400708
709 while (pg) {
710 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
711 pg->index = 0;
712 pg = pg->next;
713 }
714
Steven Rostedtcafb1682009-03-24 20:50:39 -0400715 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400716 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
717}
718
Steven Rostedtcafb1682009-03-24 20:50:39 -0400719int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400720{
721 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400722 int functions;
723 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400724 int i;
725
726 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400727 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400728 return 0;
729
Steven Rostedtcafb1682009-03-24 20:50:39 -0400730 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
731 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400732 return -ENOMEM;
733
Steven Rostedt318e0a72009-03-25 20:06:34 -0400734#ifdef CONFIG_DYNAMIC_FTRACE
735 functions = ftrace_update_tot_cnt;
736#else
737 /*
738 * We do not know the number of functions that exist because
739 * dynamic tracing is what counts them. With past experience
740 * we have around 20K functions. That should be more than enough.
741 * It is highly unlikely we will execute every function in
742 * the kernel.
743 */
744 functions = 20000;
745#endif
746
Steven Rostedtcafb1682009-03-24 20:50:39 -0400747 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400748
Steven Rostedt318e0a72009-03-25 20:06:34 -0400749 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
750
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900751 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400752 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400753 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400754 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400755 pg = pg->next;
756 }
757
758 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400759
760 out_free:
761 pg = stat->start;
762 while (pg) {
763 unsigned long tmp = (unsigned long)pg;
764
765 pg = pg->next;
766 free_page(tmp);
767 }
768
Steven Rostedt318e0a72009-03-25 20:06:34 -0400769 stat->pages = NULL;
770 stat->start = NULL;
771
772 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400773}
774
Steven Rostedtcafb1682009-03-24 20:50:39 -0400775static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400776{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400777 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400778 int size;
779
Steven Rostedtcafb1682009-03-24 20:50:39 -0400780 stat = &per_cpu(ftrace_profile_stats, cpu);
781
782 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400783 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400784 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400785 return 0;
786 }
787
788 /*
789 * We are profiling all functions, but usually only a few thousand
790 * functions are hit. We'll make a hash of 1024 items.
791 */
792 size = FTRACE_PROFILE_HASH_SIZE;
793
Steven Rostedtcafb1682009-03-24 20:50:39 -0400794 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400795
Steven Rostedtcafb1682009-03-24 20:50:39 -0400796 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400797 return -ENOMEM;
798
Steven Rostedt318e0a72009-03-25 20:06:34 -0400799 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400800 if (ftrace_profile_pages_init(stat) < 0) {
801 kfree(stat->hash);
802 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400803 return -ENOMEM;
804 }
805
806 return 0;
807}
808
Steven Rostedtcafb1682009-03-24 20:50:39 -0400809static int ftrace_profile_init(void)
810{
811 int cpu;
812 int ret = 0;
813
Miao Xiec4602c12013-12-16 15:20:01 +0800814 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400815 ret = ftrace_profile_init_cpu(cpu);
816 if (ret)
817 break;
818 }
819
820 return ret;
821}
822
Steven Rostedt493762f2009-03-23 17:12:36 -0400823/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824static struct ftrace_profile *
825ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400826{
827 struct ftrace_profile *rec;
828 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400829 unsigned long key;
830
Namhyung Kim20079eb2013-04-10 08:55:50 +0900831 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400832 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400833
834 if (hlist_empty(hhd))
835 return NULL;
836
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400837 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400838 if (rec->ip == ip)
839 return rec;
840 }
841
842 return NULL;
843}
844
Steven Rostedtcafb1682009-03-24 20:50:39 -0400845static void ftrace_add_profile(struct ftrace_profile_stat *stat,
846 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400847{
848 unsigned long key;
849
Namhyung Kim20079eb2013-04-10 08:55:50 +0900850 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400851 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400852}
853
Steven Rostedt318e0a72009-03-25 20:06:34 -0400854/*
855 * The memory is already allocated, this simply finds a new record to use.
856 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400857static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400858ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400859{
860 struct ftrace_profile *rec = NULL;
861
Steven Rostedt318e0a72009-03-25 20:06:34 -0400862 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400863 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400864 goto out;
865
Steven Rostedt493762f2009-03-23 17:12:36 -0400866 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400867 * Try to find the function again since an NMI
868 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400869 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400870 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400871 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400872 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400873
Steven Rostedtcafb1682009-03-24 20:50:39 -0400874 if (stat->pages->index == PROFILES_PER_PAGE) {
875 if (!stat->pages->next)
876 goto out;
877 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400878 }
879
Steven Rostedtcafb1682009-03-24 20:50:39 -0400880 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400881 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400882 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400883
Steven Rostedt493762f2009-03-23 17:12:36 -0400884 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400885 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400886
887 return rec;
888}
889
Steven Rostedt493762f2009-03-23 17:12:36 -0400890static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400891function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400892 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400893{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400894 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400895 struct ftrace_profile *rec;
896 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400897
898 if (!ftrace_profile_enabled)
899 return;
900
Steven Rostedt493762f2009-03-23 17:12:36 -0400901 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400902
903 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400904 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400905 goto out;
906
907 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400908 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400909 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400910 if (!rec)
911 goto out;
912 }
913
914 rec->counter++;
915 out:
916 local_irq_restore(flags);
917}
918
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400919#ifdef CONFIG_FUNCTION_GRAPH_TRACER
920static int profile_graph_entry(struct ftrace_graph_ent *trace)
921{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400922 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400923 return 1;
924}
925
926static void profile_graph_return(struct ftrace_graph_ret *trace)
927{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400928 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400929 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400930 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400931 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400932
933 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400934 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400935 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400936 goto out;
937
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400938 /* If the calltime was zero'd ignore it */
939 if (!trace->calltime)
940 goto out;
941
Steven Rostedta2a16d62009-03-24 23:17:58 -0400942 calltime = trace->rettime - trace->calltime;
943
944 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
945 int index;
946
947 index = trace->depth;
948
949 /* Append this call time to the parent time to subtract */
950 if (index)
951 current->ret_stack[index - 1].subtime += calltime;
952
953 if (current->ret_stack[index].subtime < calltime)
954 calltime -= current->ret_stack[index].subtime;
955 else
956 calltime = 0;
957 }
958
Steven Rostedtcafb1682009-03-24 20:50:39 -0400959 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400960 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400961 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400962 rec->time_squared += calltime * calltime;
963 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400964
Steven Rostedtcafb1682009-03-24 20:50:39 -0400965 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400966 local_irq_restore(flags);
967}
968
969static int register_ftrace_profiler(void)
970{
971 return register_ftrace_graph(&profile_graph_return,
972 &profile_graph_entry);
973}
974
975static void unregister_ftrace_profiler(void)
976{
977 unregister_ftrace_graph();
978}
979#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100980static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400981 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900982 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
983 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400984};
985
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400986static int register_ftrace_profiler(void)
987{
988 return register_ftrace_function(&ftrace_profile_ops);
989}
990
991static void unregister_ftrace_profiler(void)
992{
993 unregister_ftrace_function(&ftrace_profile_ops);
994}
995#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
996
Steven Rostedt493762f2009-03-23 17:12:36 -0400997static ssize_t
998ftrace_profile_write(struct file *filp, const char __user *ubuf,
999 size_t cnt, loff_t *ppos)
1000{
1001 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -04001002 int ret;
1003
Peter Huewe22fe9b52011-06-07 21:58:27 +02001004 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1005 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -04001006 return ret;
1007
1008 val = !!val;
1009
1010 mutex_lock(&ftrace_profile_lock);
1011 if (ftrace_profile_enabled ^ val) {
1012 if (val) {
1013 ret = ftrace_profile_init();
1014 if (ret < 0) {
1015 cnt = ret;
1016 goto out;
1017 }
1018
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001019 ret = register_ftrace_profiler();
1020 if (ret < 0) {
1021 cnt = ret;
1022 goto out;
1023 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001024 ftrace_profile_enabled = 1;
1025 } else {
1026 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -04001027 /*
1028 * unregister_ftrace_profiler calls stop_machine
1029 * so this acts like an synchronize_sched.
1030 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001031 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -04001032 }
1033 }
1034 out:
1035 mutex_unlock(&ftrace_profile_lock);
1036
Jiri Olsacf8517c2009-10-23 19:36:16 -04001037 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001038
1039 return cnt;
1040}
1041
1042static ssize_t
1043ftrace_profile_read(struct file *filp, char __user *ubuf,
1044 size_t cnt, loff_t *ppos)
1045{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001046 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001047 int r;
1048
1049 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1050 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1051}
1052
1053static const struct file_operations ftrace_profile_fops = {
1054 .open = tracing_open_generic,
1055 .read = ftrace_profile_read,
1056 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001057 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001058};
1059
Steven Rostedtcafb1682009-03-24 20:50:39 -04001060/* used to initialize the real stat files */
1061static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001062 .name = "functions",
1063 .stat_start = function_stat_start,
1064 .stat_next = function_stat_next,
1065 .stat_cmp = function_stat_cmp,
1066 .stat_headers = function_stat_headers,
1067 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001068};
1069
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001070static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001071{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001072 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001073 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001074 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001075 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001076 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001077
Steven Rostedtcafb1682009-03-24 20:50:39 -04001078 for_each_possible_cpu(cpu) {
1079 stat = &per_cpu(ftrace_profile_stats, cpu);
1080
1081 /* allocate enough for function name + cpu number */
1082 name = kmalloc(32, GFP_KERNEL);
1083 if (!name) {
1084 /*
1085 * The files created are permanent, if something happens
1086 * we still do not free memory.
1087 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001088 WARN(1,
1089 "Could not allocate stat file for cpu %d\n",
1090 cpu);
1091 return;
1092 }
1093 stat->stat = function_stats;
1094 snprintf(name, 32, "function%d", cpu);
1095 stat->stat.name = name;
1096 ret = register_stat_tracer(&stat->stat);
1097 if (ret) {
1098 WARN(1,
1099 "Could not register function stat for cpu %d\n",
1100 cpu);
1101 kfree(name);
1102 return;
1103 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001104 }
1105
1106 entry = debugfs_create_file("function_profile_enabled", 0644,
1107 d_tracer, NULL, &ftrace_profile_fops);
1108 if (!entry)
1109 pr_warning("Could not create debugfs "
1110 "'function_profile_enabled' entry\n");
1111}
1112
1113#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001114static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001115{
1116}
1117#endif /* CONFIG_FUNCTION_PROFILER */
1118
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001119static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1120
Steven Rostedt3d083392008-05-12 21:20:42 +02001121#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001122
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001123#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001124# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001125#endif
1126
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001127static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1128
Steven Rostedtb6887d72009-02-17 12:32:04 -05001129struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001130 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001131 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001132 unsigned long flags;
1133 unsigned long ip;
1134 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001135 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001136};
1137
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001138struct ftrace_func_entry {
1139 struct hlist_node hlist;
1140 unsigned long ip;
1141};
1142
1143struct ftrace_hash {
1144 unsigned long size_bits;
1145 struct hlist_head *buckets;
1146 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001147 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001148};
1149
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001150/*
1151 * We make these constant because no one should touch them,
1152 * but they are used as the default "empty hash", to avoid allocating
1153 * it all the time. These are in a read only section such that if
1154 * anyone does try to modify it, it will cause an exception.
1155 */
1156static const struct hlist_head empty_buckets[1];
1157static const struct ftrace_hash empty_hash = {
1158 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001159};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001160#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001161
Steven Rostedt2b499382011-05-03 22:49:52 -04001162static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001163 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001164 .notrace_hash = EMPTY_HASH,
1165 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001166 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1167 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001168};
1169
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001170struct ftrace_page {
1171 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001172 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001173 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001174 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001175};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001176
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001177static struct ftrace_page *ftrace_new_pgs;
1178
Steven Rostedta7900872011-12-16 16:23:44 -05001179#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1180#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001181
1182/* estimate from running different kernels */
1183#define NR_TO_INIT 10000
1184
1185static struct ftrace_page *ftrace_pages_start;
1186static struct ftrace_page *ftrace_pages;
1187
Steven Rostedt06a51d92011-12-19 19:07:36 -05001188static bool ftrace_hash_empty(struct ftrace_hash *hash)
1189{
1190 return !hash || !hash->count;
1191}
1192
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001193static struct ftrace_func_entry *
1194ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1195{
1196 unsigned long key;
1197 struct ftrace_func_entry *entry;
1198 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001199
Steven Rostedt06a51d92011-12-19 19:07:36 -05001200 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001201 return NULL;
1202
1203 if (hash->size_bits > 0)
1204 key = hash_long(ip, hash->size_bits);
1205 else
1206 key = 0;
1207
1208 hhd = &hash->buckets[key];
1209
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001210 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001211 if (entry->ip == ip)
1212 return entry;
1213 }
1214 return NULL;
1215}
1216
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001217static void __add_hash_entry(struct ftrace_hash *hash,
1218 struct ftrace_func_entry *entry)
1219{
1220 struct hlist_head *hhd;
1221 unsigned long key;
1222
1223 if (hash->size_bits)
1224 key = hash_long(entry->ip, hash->size_bits);
1225 else
1226 key = 0;
1227
1228 hhd = &hash->buckets[key];
1229 hlist_add_head(&entry->hlist, hhd);
1230 hash->count++;
1231}
1232
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001233static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1234{
1235 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001236
1237 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1238 if (!entry)
1239 return -ENOMEM;
1240
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001241 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001242 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001243
1244 return 0;
1245}
1246
1247static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001248free_hash_entry(struct ftrace_hash *hash,
1249 struct ftrace_func_entry *entry)
1250{
1251 hlist_del(&entry->hlist);
1252 kfree(entry);
1253 hash->count--;
1254}
1255
1256static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001257remove_hash_entry(struct ftrace_hash *hash,
1258 struct ftrace_func_entry *entry)
1259{
1260 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001261 hash->count--;
1262}
1263
1264static void ftrace_hash_clear(struct ftrace_hash *hash)
1265{
1266 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001267 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001268 struct ftrace_func_entry *entry;
1269 int size = 1 << hash->size_bits;
1270 int i;
1271
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001272 if (!hash->count)
1273 return;
1274
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001275 for (i = 0; i < size; i++) {
1276 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001277 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001278 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001279 }
1280 FTRACE_WARN_ON(hash->count);
1281}
1282
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001283static void free_ftrace_hash(struct ftrace_hash *hash)
1284{
1285 if (!hash || hash == EMPTY_HASH)
1286 return;
1287 ftrace_hash_clear(hash);
1288 kfree(hash->buckets);
1289 kfree(hash);
1290}
1291
Steven Rostedt07fd5512011-05-05 18:03:47 -04001292static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1293{
1294 struct ftrace_hash *hash;
1295
1296 hash = container_of(rcu, struct ftrace_hash, rcu);
1297 free_ftrace_hash(hash);
1298}
1299
1300static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1301{
1302 if (!hash || hash == EMPTY_HASH)
1303 return;
1304 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1305}
1306
Jiri Olsa5500fa52012-02-15 15:51:54 +01001307void ftrace_free_filter(struct ftrace_ops *ops)
1308{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001309 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001310 free_ftrace_hash(ops->filter_hash);
1311 free_ftrace_hash(ops->notrace_hash);
1312}
1313
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001314static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1315{
1316 struct ftrace_hash *hash;
1317 int size;
1318
1319 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1320 if (!hash)
1321 return NULL;
1322
1323 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001324 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001325
1326 if (!hash->buckets) {
1327 kfree(hash);
1328 return NULL;
1329 }
1330
1331 hash->size_bits = size_bits;
1332
1333 return hash;
1334}
1335
1336static struct ftrace_hash *
1337alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1338{
1339 struct ftrace_func_entry *entry;
1340 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001341 int size;
1342 int ret;
1343 int i;
1344
1345 new_hash = alloc_ftrace_hash(size_bits);
1346 if (!new_hash)
1347 return NULL;
1348
1349 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001350 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001351 return new_hash;
1352
1353 size = 1 << hash->size_bits;
1354 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001355 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001356 ret = add_hash_entry(new_hash, entry->ip);
1357 if (ret < 0)
1358 goto free_hash;
1359 }
1360 }
1361
1362 FTRACE_WARN_ON(new_hash->count != hash->count);
1363
1364 return new_hash;
1365
1366 free_hash:
1367 free_ftrace_hash(new_hash);
1368 return NULL;
1369}
1370
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001371static void
1372ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1373static void
1374ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1375
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001376static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001377ftrace_hash_move(struct ftrace_ops *ops, int enable,
1378 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001379{
1380 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001381 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001382 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001383 struct ftrace_hash *old_hash;
1384 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001385 int size = src->count;
1386 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001387 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001388 int i;
1389
1390 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001391 * Remove the current set, update the hash and add
1392 * them back.
1393 */
1394 ftrace_hash_rec_disable(ops, enable);
1395
1396 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001397 * If the new source is empty, just free dst and assign it
1398 * the empty_hash.
1399 */
1400 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001401 free_ftrace_hash_rcu(*dst);
1402 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001403 /* still need to update the function records */
1404 ret = 0;
1405 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001406 }
1407
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001408 /*
1409 * Make the hash size about 1/2 the # found
1410 */
1411 for (size /= 2; size; size >>= 1)
1412 bits++;
1413
1414 /* Don't allocate too much */
1415 if (bits > FTRACE_HASH_MAX_BITS)
1416 bits = FTRACE_HASH_MAX_BITS;
1417
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001418 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001419 new_hash = alloc_ftrace_hash(bits);
1420 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001421 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001422
1423 size = 1 << src->size_bits;
1424 for (i = 0; i < size; i++) {
1425 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001426 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001427 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001428 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001429 }
1430 }
1431
Steven Rostedt07fd5512011-05-05 18:03:47 -04001432 old_hash = *dst;
1433 rcu_assign_pointer(*dst, new_hash);
1434 free_ftrace_hash_rcu(old_hash);
1435
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001436 ret = 0;
1437 out:
1438 /*
1439 * Enable regardless of ret:
1440 * On success, we enable the new hash.
1441 * On failure, we re-enable the original hash.
1442 */
1443 ftrace_hash_rec_enable(ops, enable);
1444
1445 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001446}
1447
Steven Rostedt265c8312009-02-13 12:43:56 -05001448/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001449 * Test the hashes for this ops to see if we want to call
1450 * the ops->func or not.
1451 *
1452 * It's a match if the ip is in the ops->filter_hash or
1453 * the filter_hash does not exist or is empty,
1454 * AND
1455 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001456 *
1457 * This needs to be called with preemption disabled as
1458 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001459 */
1460static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001461ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001462{
1463 struct ftrace_hash *filter_hash;
1464 struct ftrace_hash *notrace_hash;
1465 int ret;
1466
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001467#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1468 /*
1469 * There's a small race when adding ops that the ftrace handler
1470 * that wants regs, may be called without them. We can not
1471 * allow that handler to be called if regs is NULL.
1472 */
1473 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1474 return 0;
1475#endif
1476
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001477 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1478 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001479
Steven Rostedt06a51d92011-12-19 19:07:36 -05001480 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001481 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001482 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001483 !ftrace_lookup_ip(notrace_hash, ip)))
1484 ret = 1;
1485 else
1486 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001487
1488 return ret;
1489}
1490
1491/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001492 * This is a double for. Do not use 'break' to break out of the loop,
1493 * you must use a goto.
1494 */
1495#define do_for_each_ftrace_rec(pg, rec) \
1496 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1497 int _____i; \
1498 for (_____i = 0; _____i < pg->index; _____i++) { \
1499 rec = &pg->records[_____i];
1500
1501#define while_for_each_ftrace_rec() \
1502 } \
1503 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301504
Steven Rostedt5855fea2011-12-16 19:27:42 -05001505
1506static int ftrace_cmp_recs(const void *a, const void *b)
1507{
Steven Rostedta650e022012-04-25 13:48:13 -04001508 const struct dyn_ftrace *key = a;
1509 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001510
Steven Rostedta650e022012-04-25 13:48:13 -04001511 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001512 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001513 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1514 return 1;
1515 return 0;
1516}
1517
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001518static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001519{
1520 struct ftrace_page *pg;
1521 struct dyn_ftrace *rec;
1522 struct dyn_ftrace key;
1523
1524 key.ip = start;
1525 key.flags = end; /* overload flags, as it is unsigned long */
1526
1527 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1528 if (end < pg->records[0].ip ||
1529 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1530 continue;
1531 rec = bsearch(&key, pg->records, pg->index,
1532 sizeof(struct dyn_ftrace),
1533 ftrace_cmp_recs);
1534 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001535 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001536 }
1537
Steven Rostedt5855fea2011-12-16 19:27:42 -05001538 return 0;
1539}
1540
Steven Rostedtc88fd862011-08-16 09:53:39 -04001541/**
1542 * ftrace_location - return true if the ip giving is a traced location
1543 * @ip: the instruction pointer to check
1544 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001545 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001546 * That is, the instruction that is either a NOP or call to
1547 * the function tracer. It checks the ftrace internal tables to
1548 * determine if the address belongs or not.
1549 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001550unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001551{
Steven Rostedta650e022012-04-25 13:48:13 -04001552 return ftrace_location_range(ip, ip);
1553}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001554
Steven Rostedta650e022012-04-25 13:48:13 -04001555/**
1556 * ftrace_text_reserved - return true if range contains an ftrace location
1557 * @start: start of range to search
1558 * @end: end of range to search (inclusive). @end points to the last byte to check.
1559 *
1560 * Returns 1 if @start and @end contains a ftrace location.
1561 * That is, the instruction that is either a NOP or call to
1562 * the function tracer. It checks the ftrace internal tables to
1563 * determine if the address belongs or not.
1564 */
1565int ftrace_text_reserved(void *start, void *end)
1566{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001567 unsigned long ret;
1568
1569 ret = ftrace_location_range((unsigned long)start,
1570 (unsigned long)end);
1571
1572 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001573}
1574
Steven Rostedted926f92011-05-03 13:25:24 -04001575static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1576 int filter_hash,
1577 bool inc)
1578{
1579 struct ftrace_hash *hash;
1580 struct ftrace_hash *other_hash;
1581 struct ftrace_page *pg;
1582 struct dyn_ftrace *rec;
1583 int count = 0;
1584 int all = 0;
1585
1586 /* Only update if the ops has been registered */
1587 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1588 return;
1589
1590 /*
1591 * In the filter_hash case:
1592 * If the count is zero, we update all records.
1593 * Otherwise we just update the items in the hash.
1594 *
1595 * In the notrace_hash case:
1596 * We enable the update in the hash.
1597 * As disabling notrace means enabling the tracing,
1598 * and enabling notrace means disabling, the inc variable
1599 * gets inversed.
1600 */
1601 if (filter_hash) {
1602 hash = ops->filter_hash;
1603 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001604 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001605 all = 1;
1606 } else {
1607 inc = !inc;
1608 hash = ops->notrace_hash;
1609 other_hash = ops->filter_hash;
1610 /*
1611 * If the notrace hash has no items,
1612 * then there's nothing to do.
1613 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001614 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001615 return;
1616 }
1617
1618 do_for_each_ftrace_rec(pg, rec) {
1619 int in_other_hash = 0;
1620 int in_hash = 0;
1621 int match = 0;
1622
1623 if (all) {
1624 /*
1625 * Only the filter_hash affects all records.
1626 * Update if the record is not in the notrace hash.
1627 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001628 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001629 match = 1;
1630 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001631 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1632 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001633
1634 /*
1635 *
1636 */
1637 if (filter_hash && in_hash && !in_other_hash)
1638 match = 1;
1639 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001640 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001641 match = 1;
1642 }
1643 if (!match)
1644 continue;
1645
1646 if (inc) {
1647 rec->flags++;
1648 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1649 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001650 /*
1651 * If any ops wants regs saved for this function
1652 * then all ops will get saved regs.
1653 */
1654 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1655 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001656 } else {
1657 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1658 return;
1659 rec->flags--;
1660 }
1661 count++;
1662 /* Shortcut, if we handled all records, we are done. */
1663 if (!all && count == hash->count)
1664 return;
1665 } while_for_each_ftrace_rec();
1666}
1667
1668static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1669 int filter_hash)
1670{
1671 __ftrace_hash_rec_update(ops, filter_hash, 0);
1672}
1673
1674static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1675 int filter_hash)
1676{
1677 __ftrace_hash_rec_update(ops, filter_hash, 1);
1678}
1679
Steven Rostedt05736a42008-09-22 14:55:47 -07001680static void print_ip_ins(const char *fmt, unsigned char *p)
1681{
1682 int i;
1683
1684 printk(KERN_CONT "%s", fmt);
1685
1686 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1687 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1688}
1689
Steven Rostedtc88fd862011-08-16 09:53:39 -04001690/**
1691 * ftrace_bug - report and shutdown function tracer
1692 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1693 * @ip: The address that failed
1694 *
1695 * The arch code that enables or disables the function tracing
1696 * can call ftrace_bug() when it has detected a problem in
1697 * modifying the code. @failed should be one of either:
1698 * EFAULT - if the problem happens on reading the @ip address
1699 * EINVAL - if what is read at @ip is not what was expected
1700 * EPERM - if the problem happens on writting to the @ip address
1701 */
1702void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001703{
1704 switch (failed) {
1705 case -EFAULT:
1706 FTRACE_WARN_ON_ONCE(1);
1707 pr_info("ftrace faulted on modifying ");
1708 print_ip_sym(ip);
1709 break;
1710 case -EINVAL:
1711 FTRACE_WARN_ON_ONCE(1);
1712 pr_info("ftrace failed to modify ");
1713 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001714 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001715 printk(KERN_CONT "\n");
1716 break;
1717 case -EPERM:
1718 FTRACE_WARN_ON_ONCE(1);
1719 pr_info("ftrace faulted on writing ");
1720 print_ip_sym(ip);
1721 break;
1722 default:
1723 FTRACE_WARN_ON_ONCE(1);
1724 pr_info("ftrace faulted on unknown error ");
1725 print_ip_sym(ip);
1726 }
1727}
1728
Steven Rostedtc88fd862011-08-16 09:53:39 -04001729static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001730{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001731 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001732
Steven Rostedt982c3502008-11-15 16:31:41 -05001733 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001734 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001735 *
Steven Rostedted926f92011-05-03 13:25:24 -04001736 * If the record has a ref count, then we need to enable it
1737 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001738 *
Steven Rostedted926f92011-05-03 13:25:24 -04001739 * Otherwise we make sure its disabled.
1740 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001741 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001742 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001743 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001744 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001745 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001746
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001747 /*
1748 * If enabling and the REGS flag does not match the REGS_EN, then
1749 * do not ignore this record. Set flags to fail the compare against
1750 * ENABLED.
1751 */
1752 if (flag &&
1753 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1754 flag |= FTRACE_FL_REGS;
1755
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001756 /* If the state of this record hasn't changed, then do nothing */
1757 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001758 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001759
1760 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001761 /* Save off if rec is being enabled (for return value) */
1762 flag ^= rec->flags & FTRACE_FL_ENABLED;
1763
1764 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001765 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001766 if (flag & FTRACE_FL_REGS) {
1767 if (rec->flags & FTRACE_FL_REGS)
1768 rec->flags |= FTRACE_FL_REGS_EN;
1769 else
1770 rec->flags &= ~FTRACE_FL_REGS_EN;
1771 }
1772 }
1773
1774 /*
1775 * If this record is being updated from a nop, then
1776 * return UPDATE_MAKE_CALL.
1777 * Otherwise, if the EN flag is set, then return
1778 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1779 * from the non-save regs, to a save regs function.
1780 * Otherwise,
1781 * return UPDATE_MODIFY_CALL to tell the caller to convert
1782 * from the save regs, to a non-save regs function.
1783 */
1784 if (flag & FTRACE_FL_ENABLED)
1785 return FTRACE_UPDATE_MAKE_CALL;
1786 else if (rec->flags & FTRACE_FL_REGS_EN)
1787 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1788 else
1789 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001790 }
1791
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001792 if (update) {
1793 /* If there's no more users, clear all flags */
1794 if (!(rec->flags & ~FTRACE_FL_MASK))
1795 rec->flags = 0;
1796 else
1797 /* Just disable the record (keep REGS state) */
1798 rec->flags &= ~FTRACE_FL_ENABLED;
1799 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001800
1801 return FTRACE_UPDATE_MAKE_NOP;
1802}
1803
1804/**
1805 * ftrace_update_record, set a record that now is tracing or not
1806 * @rec: the record to update
1807 * @enable: set to 1 if the record is tracing, zero to force disable
1808 *
1809 * The records that represent all functions that can be traced need
1810 * to be updated when tracing has been enabled.
1811 */
1812int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1813{
1814 return ftrace_check_record(rec, enable, 1);
1815}
1816
1817/**
1818 * ftrace_test_record, check if the record has been enabled or not
1819 * @rec: the record to test
1820 * @enable: set to 1 to check if enabled, 0 if it is disabled
1821 *
1822 * The arch code may need to test if a record is already set to
1823 * tracing to determine how to modify the function code that it
1824 * represents.
1825 */
1826int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1827{
1828 return ftrace_check_record(rec, enable, 0);
1829}
1830
1831static int
1832__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1833{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001834 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001835 unsigned long ftrace_addr;
1836 int ret;
1837
Steven Rostedtc88fd862011-08-16 09:53:39 -04001838 ret = ftrace_update_record(rec, enable);
1839
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001840 if (rec->flags & FTRACE_FL_REGS)
1841 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1842 else
1843 ftrace_addr = (unsigned long)FTRACE_ADDR;
1844
Steven Rostedtc88fd862011-08-16 09:53:39 -04001845 switch (ret) {
1846 case FTRACE_UPDATE_IGNORE:
1847 return 0;
1848
1849 case FTRACE_UPDATE_MAKE_CALL:
1850 return ftrace_make_call(rec, ftrace_addr);
1851
1852 case FTRACE_UPDATE_MAKE_NOP:
1853 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001854
1855 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1856 case FTRACE_UPDATE_MODIFY_CALL:
1857 if (rec->flags & FTRACE_FL_REGS)
1858 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1859 else
1860 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1861
1862 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001863 }
1864
1865 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001866}
1867
Steven Rostedte4f5d542012-04-27 09:13:18 -04001868void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001869{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001870 struct dyn_ftrace *rec;
1871 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001872 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001873
Steven Rostedt45a4a232011-04-21 23:16:46 -04001874 if (unlikely(ftrace_disabled))
1875 return;
1876
Steven Rostedt265c8312009-02-13 12:43:56 -05001877 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001878 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001879 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001880 ftrace_bug(failed, rec->ip);
1881 /* Stop processing */
1882 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001883 }
1884 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001885}
1886
Steven Rostedtc88fd862011-08-16 09:53:39 -04001887struct ftrace_rec_iter {
1888 struct ftrace_page *pg;
1889 int index;
1890};
1891
1892/**
1893 * ftrace_rec_iter_start, start up iterating over traced functions
1894 *
1895 * Returns an iterator handle that is used to iterate over all
1896 * the records that represent address locations where functions
1897 * are traced.
1898 *
1899 * May return NULL if no records are available.
1900 */
1901struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1902{
1903 /*
1904 * We only use a single iterator.
1905 * Protected by the ftrace_lock mutex.
1906 */
1907 static struct ftrace_rec_iter ftrace_rec_iter;
1908 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1909
1910 iter->pg = ftrace_pages_start;
1911 iter->index = 0;
1912
1913 /* Could have empty pages */
1914 while (iter->pg && !iter->pg->index)
1915 iter->pg = iter->pg->next;
1916
1917 if (!iter->pg)
1918 return NULL;
1919
1920 return iter;
1921}
1922
1923/**
1924 * ftrace_rec_iter_next, get the next record to process.
1925 * @iter: The handle to the iterator.
1926 *
1927 * Returns the next iterator after the given iterator @iter.
1928 */
1929struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1930{
1931 iter->index++;
1932
1933 if (iter->index >= iter->pg->index) {
1934 iter->pg = iter->pg->next;
1935 iter->index = 0;
1936
1937 /* Could have empty pages */
1938 while (iter->pg && !iter->pg->index)
1939 iter->pg = iter->pg->next;
1940 }
1941
1942 if (!iter->pg)
1943 return NULL;
1944
1945 return iter;
1946}
1947
1948/**
1949 * ftrace_rec_iter_record, get the record at the iterator location
1950 * @iter: The current iterator location
1951 *
1952 * Returns the record that the current @iter is at.
1953 */
1954struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1955{
1956 return &iter->pg->records[iter->index];
1957}
1958
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301959static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001960ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001961{
1962 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001963 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001964
1965 ip = rec->ip;
1966
Steven Rostedt45a4a232011-04-21 23:16:46 -04001967 if (unlikely(ftrace_disabled))
1968 return 0;
1969
Shaohua Li25aac9d2009-01-09 11:29:40 +08001970 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001971 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001972 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301973 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001974 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301975 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001976}
1977
Steven Rostedt000ab692009-02-17 13:35:06 -05001978/*
1979 * archs can override this function if they must do something
1980 * before the modifying code is performed.
1981 */
1982int __weak ftrace_arch_code_modify_prepare(void)
1983{
1984 return 0;
1985}
1986
1987/*
1988 * archs can override this function if they must do something
1989 * after the modifying code is performed.
1990 */
1991int __weak ftrace_arch_code_modify_post_process(void)
1992{
1993 return 0;
1994}
1995
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001996void ftrace_modify_all_code(int command)
1997{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001998 int update = command & FTRACE_UPDATE_TRACE_FUNC;
1999
2000 /*
2001 * If the ftrace_caller calls a ftrace_ops func directly,
2002 * we need to make sure that it only traces functions it
2003 * expects to trace. When doing the switch of functions,
2004 * we need to update to the ftrace_ops_list_func first
2005 * before the transition between old and new calls are set,
2006 * as the ftrace_ops_list_func will check the ops hashes
2007 * to make sure the ops are having the right functions
2008 * traced.
2009 */
2010 if (update)
2011 ftrace_update_ftrace_func(ftrace_ops_list_func);
2012
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002013 if (command & FTRACE_UPDATE_CALLS)
2014 ftrace_replace_code(1);
2015 else if (command & FTRACE_DISABLE_CALLS)
2016 ftrace_replace_code(0);
2017
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002018 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2019 function_trace_op = set_function_trace_op;
2020 smp_wmb();
2021 /* If irqs are disabled, we are in stop machine */
2022 if (!irqs_disabled())
2023 smp_call_function(ftrace_sync_ipi, NULL, 1);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002024 ftrace_update_ftrace_func(ftrace_trace_function);
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002025 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002026
2027 if (command & FTRACE_START_FUNC_RET)
2028 ftrace_enable_ftrace_graph_caller();
2029 else if (command & FTRACE_STOP_FUNC_RET)
2030 ftrace_disable_ftrace_graph_caller();
2031}
2032
Ingo Molnare309b412008-05-12 21:20:51 +02002033static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002034{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002035 int *command = data;
2036
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002037 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002038
Steven Rostedtc88fd862011-08-16 09:53:39 -04002039 return 0;
2040}
2041
2042/**
2043 * ftrace_run_stop_machine, go back to the stop machine method
2044 * @command: The command to tell ftrace what to do
2045 *
2046 * If an arch needs to fall back to the stop machine method, the
2047 * it can call this function.
2048 */
2049void ftrace_run_stop_machine(int command)
2050{
2051 stop_machine(__ftrace_modify_code, &command, NULL);
2052}
2053
2054/**
2055 * arch_ftrace_update_code, modify the code to trace or not trace
2056 * @command: The command that needs to be done
2057 *
2058 * Archs can override this function if it does not need to
2059 * run stop_machine() to modify code.
2060 */
2061void __weak arch_ftrace_update_code(int command)
2062{
2063 ftrace_run_stop_machine(command);
2064}
2065
2066static void ftrace_run_update_code(int command)
2067{
2068 int ret;
2069
2070 ret = ftrace_arch_code_modify_prepare();
2071 FTRACE_WARN_ON(ret);
2072 if (ret)
2073 return;
2074 /*
2075 * Do not call function tracer while we update the code.
2076 * We are in stop machine.
2077 */
2078 function_trace_stop++;
2079
2080 /*
2081 * By default we use stop_machine() to modify the code.
2082 * But archs can do what ever they want as long as it
2083 * is safe. The stop_machine() is the safest, but also
2084 * produces the most overhead.
2085 */
2086 arch_ftrace_update_code(command);
2087
Steven Rostedt6331c282011-07-13 15:11:02 -04002088 function_trace_stop--;
2089
Steven Rostedt000ab692009-02-17 13:35:06 -05002090 ret = ftrace_arch_code_modify_post_process();
2091 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002092}
2093
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002094static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002095static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002096static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002097
2098static void ftrace_startup_enable(int command)
2099{
2100 if (saved_ftrace_func != ftrace_trace_function) {
2101 saved_ftrace_func = ftrace_trace_function;
2102 command |= FTRACE_UPDATE_TRACE_FUNC;
2103 }
2104
2105 if (!command || !ftrace_enabled)
2106 return;
2107
2108 ftrace_run_update_code(command);
2109}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002110
Steven Rostedta1cd6172011-05-23 15:24:25 -04002111static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002112{
Steven Rostedtb8489142011-05-04 09:27:52 -04002113 bool hash_enable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002114 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002115
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002116 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002117 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002118
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002119 ret = __register_ftrace_function(ops);
2120 if (ret)
2121 return ret;
2122
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002123 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002124 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002125
Steven Rostedtb8489142011-05-04 09:27:52 -04002126 /* ops marked global share the filter hashes */
2127 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2128 ops = &global_ops;
2129 /* Don't update hash if global is already set */
2130 if (global_start_up)
2131 hash_enable = false;
2132 global_start_up++;
2133 }
2134
Steven Rostedted926f92011-05-03 13:25:24 -04002135 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002136 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002137 ftrace_hash_rec_enable(ops, 1);
2138
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002139 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002140
2141 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002142}
2143
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002144static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002145{
Steven Rostedtb8489142011-05-04 09:27:52 -04002146 bool hash_disable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002147 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002148
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002149 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002150 return -ENODEV;
2151
2152 ret = __unregister_ftrace_function(ops);
2153 if (ret)
2154 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002155
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002156 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002157 /*
2158 * Just warn in case of unbalance, no need to kill ftrace, it's not
2159 * critical but the ftrace_call callers may be never nopped again after
2160 * further ftrace uses.
2161 */
2162 WARN_ON_ONCE(ftrace_start_up < 0);
2163
Steven Rostedtb8489142011-05-04 09:27:52 -04002164 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2165 ops = &global_ops;
2166 global_start_up--;
2167 WARN_ON_ONCE(global_start_up < 0);
2168 /* Don't update hash if global still has users */
2169 if (global_start_up) {
2170 WARN_ON_ONCE(!ftrace_start_up);
2171 hash_disable = false;
2172 }
2173 }
2174
2175 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002176 ftrace_hash_rec_disable(ops, 1);
2177
Steven Rostedtb8489142011-05-04 09:27:52 -04002178 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002179 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002180
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002181 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002182
2183 if (saved_ftrace_func != ftrace_trace_function) {
2184 saved_ftrace_func = ftrace_trace_function;
2185 command |= FTRACE_UPDATE_TRACE_FUNC;
2186 }
2187
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002188 if (!command || !ftrace_enabled) {
2189 /*
2190 * If these are control ops, they still need their
2191 * per_cpu field freed. Since, function tracing is
2192 * not currently active, we can just free them
2193 * without synchronizing all CPUs.
2194 */
2195 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2196 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002197 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002198 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002199
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002200 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002201
2202 /*
2203 * Dynamic ops may be freed, we must make sure that all
2204 * callers are done before leaving this function.
2205 * The same goes for freeing the per_cpu data of the control
2206 * ops.
2207 *
2208 * Again, normal synchronize_sched() is not good enough.
2209 * We need to do a hard force of sched synchronization.
2210 * This is because we use preempt_disable() to do RCU, but
2211 * the function tracers can be called where RCU is not watching
2212 * (like before user_exit()). We can not rely on the RCU
2213 * infrastructure to do the synchronization, thus we must do it
2214 * ourselves.
2215 */
2216 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2217 schedule_on_each_cpu(ftrace_sync);
2218
2219 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2220 control_ops_free(ops);
2221 }
2222
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002223 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002224}
2225
Ingo Molnare309b412008-05-12 21:20:51 +02002226static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002227{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002228 if (unlikely(ftrace_disabled))
2229 return;
2230
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002231 /* Force update next time */
2232 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002233 /* ftrace_start_up is true if we want ftrace running */
2234 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002235 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002236}
2237
Ingo Molnare309b412008-05-12 21:20:51 +02002238static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002239{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002240 if (unlikely(ftrace_disabled))
2241 return;
2242
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002243 /* ftrace_start_up is true if ftrace is running */
2244 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002245 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002246}
2247
Steven Rostedt3d083392008-05-12 21:20:42 +02002248static cycle_t ftrace_update_time;
2249static unsigned long ftrace_update_cnt;
2250unsigned long ftrace_update_tot_cnt;
2251
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002252static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002253{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002254 /*
2255 * Filter_hash being empty will default to trace module.
2256 * But notrace hash requires a test of individual module functions.
2257 */
2258 return ftrace_hash_empty(ops->filter_hash) &&
2259 ftrace_hash_empty(ops->notrace_hash);
2260}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002261
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002262/*
2263 * Check if the current ops references the record.
2264 *
2265 * If the ops traces all functions, then it was already accounted for.
2266 * If the ops does not trace the current record function, skip it.
2267 * If the ops ignores the function via notrace filter, skip it.
2268 */
2269static inline bool
2270ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2271{
2272 /* If ops isn't enabled, ignore it */
2273 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2274 return 0;
2275
2276 /* If ops traces all mods, we already accounted for it */
2277 if (ops_traces_mod(ops))
2278 return 0;
2279
2280 /* The function must be in the filter */
2281 if (!ftrace_hash_empty(ops->filter_hash) &&
2282 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2283 return 0;
2284
2285 /* If in notrace hash, we ignore it too */
2286 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2287 return 0;
2288
2289 return 1;
2290}
2291
2292static int referenced_filters(struct dyn_ftrace *rec)
2293{
2294 struct ftrace_ops *ops;
2295 int cnt = 0;
2296
2297 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2298 if (ops_references_rec(ops, rec))
2299 cnt++;
2300 }
2301
2302 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002303}
2304
Steven Rostedt31e88902008-11-14 16:21:19 -08002305static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002306{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002307 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002308 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302309 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002310 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002311 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002312 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002313
2314 /*
2315 * When adding a module, we need to check if tracers are
2316 * currently enabled and if they are set to trace all functions.
2317 * If they are, we need to enable the module functions as well
2318 * as update the reference counts for those function records.
2319 */
2320 if (mod) {
2321 struct ftrace_ops *ops;
2322
2323 for (ops = ftrace_ops_list;
2324 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002325 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2326 if (ops_traces_mod(ops))
2327 ref++;
2328 else
2329 test = true;
2330 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002331 }
2332 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002333
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002334 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002335 ftrace_update_cnt = 0;
2336
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002337 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302338
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002339 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002340 int cnt = ref;
2341
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002342 /* If something went wrong, bail without enabling anything */
2343 if (unlikely(ftrace_disabled))
2344 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002345
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002346 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002347 if (test)
2348 cnt += referenced_filters(p);
2349 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302350
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002351 /*
2352 * Do the initial record conversion from mcount jump
2353 * to the NOP instructions.
2354 */
2355 if (!ftrace_code_disable(mod, p))
2356 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002357
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002358 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002359
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002360 /*
2361 * If the tracing is enabled, go ahead and enable the record.
2362 *
2363 * The reason not to enable the record immediatelly is the
2364 * inherent check of ftrace_make_nop/ftrace_make_call for
2365 * correct previous instructions. Making first the NOP
2366 * conversion puts the module to the correct state, thus
2367 * passing the ftrace_make_call check.
2368 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002369 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002370 int failed = __ftrace_replace_code(p, 1);
2371 if (failed)
2372 ftrace_bug(failed, p->ip);
2373 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002374 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002375 }
2376
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002377 ftrace_new_pgs = NULL;
2378
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002379 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002380 ftrace_update_time = stop - start;
2381 ftrace_update_tot_cnt += ftrace_update_cnt;
2382
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002383 return 0;
2384}
2385
Steven Rostedta7900872011-12-16 16:23:44 -05002386static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002387{
Steven Rostedta7900872011-12-16 16:23:44 -05002388 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002389 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002390
Steven Rostedta7900872011-12-16 16:23:44 -05002391 if (WARN_ON(!count))
2392 return -EINVAL;
2393
2394 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002395
2396 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002397 * We want to fill as much as possible. No more than a page
2398 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002399 */
Steven Rostedta7900872011-12-16 16:23:44 -05002400 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2401 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002402
Steven Rostedta7900872011-12-16 16:23:44 -05002403 again:
2404 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2405
2406 if (!pg->records) {
2407 /* if we can't allocate this size, try something smaller */
2408 if (!order)
2409 return -ENOMEM;
2410 order >>= 1;
2411 goto again;
2412 }
2413
2414 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2415 pg->size = cnt;
2416
2417 if (cnt > count)
2418 cnt = count;
2419
2420 return cnt;
2421}
2422
2423static struct ftrace_page *
2424ftrace_allocate_pages(unsigned long num_to_init)
2425{
2426 struct ftrace_page *start_pg;
2427 struct ftrace_page *pg;
2428 int order;
2429 int cnt;
2430
2431 if (!num_to_init)
2432 return 0;
2433
2434 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2435 if (!pg)
2436 return NULL;
2437
2438 /*
2439 * Try to allocate as much as possible in one continues
2440 * location that fills in all of the space. We want to
2441 * waste as little space as possible.
2442 */
2443 for (;;) {
2444 cnt = ftrace_allocate_records(pg, num_to_init);
2445 if (cnt < 0)
2446 goto free_pages;
2447
2448 num_to_init -= cnt;
2449 if (!num_to_init)
2450 break;
2451
2452 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2453 if (!pg->next)
2454 goto free_pages;
2455
2456 pg = pg->next;
2457 }
2458
2459 return start_pg;
2460
2461 free_pages:
2462 while (start_pg) {
2463 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2464 free_pages((unsigned long)pg->records, order);
2465 start_pg = pg->next;
2466 kfree(pg);
2467 pg = start_pg;
2468 }
2469 pr_info("ftrace: FAILED to allocate memory for functions\n");
2470 return NULL;
2471}
2472
2473static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2474{
2475 int cnt;
2476
2477 if (!num_to_init) {
2478 pr_info("ftrace: No functions to be traced?\n");
2479 return -1;
2480 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002481
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002482 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002483 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002484 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002485
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002486 return 0;
2487}
2488
Steven Rostedt5072c592008-05-12 21:20:43 +02002489#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2490
2491struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002492 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002493 loff_t func_pos;
2494 struct ftrace_page *pg;
2495 struct dyn_ftrace *func;
2496 struct ftrace_func_probe *probe;
2497 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002498 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002499 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002500 int hidx;
2501 int idx;
2502 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002503};
2504
Ingo Molnare309b412008-05-12 21:20:51 +02002505static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002506t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002507{
2508 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002509 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002510 struct hlist_head *hhd;
2511
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002512 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002513 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002514
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002515 if (iter->probe)
2516 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002517 retry:
2518 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2519 return NULL;
2520
2521 hhd = &ftrace_func_hash[iter->hidx];
2522
2523 if (hlist_empty(hhd)) {
2524 iter->hidx++;
2525 hnd = NULL;
2526 goto retry;
2527 }
2528
2529 if (!hnd)
2530 hnd = hhd->first;
2531 else {
2532 hnd = hnd->next;
2533 if (!hnd) {
2534 iter->hidx++;
2535 goto retry;
2536 }
2537 }
2538
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002539 if (WARN_ON_ONCE(!hnd))
2540 return NULL;
2541
2542 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2543
2544 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002545}
2546
2547static void *t_hash_start(struct seq_file *m, loff_t *pos)
2548{
2549 struct ftrace_iterator *iter = m->private;
2550 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002551 loff_t l;
2552
Steven Rostedt69a30832011-12-19 15:21:16 -05002553 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2554 return NULL;
2555
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002556 if (iter->func_pos > *pos)
2557 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002558
Li Zefand82d6242009-06-24 09:54:54 +08002559 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002560 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002561 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002562 if (!p)
2563 break;
2564 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002565 if (!p)
2566 return NULL;
2567
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002568 /* Only set this if we have an item */
2569 iter->flags |= FTRACE_ITER_HASH;
2570
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002571 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002572}
2573
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002574static int
2575t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002576{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002577 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002578
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002579 rec = iter->probe;
2580 if (WARN_ON_ONCE(!rec))
2581 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002582
Steven Rostedt809dcf22009-02-16 23:06:01 -05002583 if (rec->ops->print)
2584 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2585
Steven Rostedtb375a112009-09-17 00:05:58 -04002586 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002587
2588 if (rec->data)
2589 seq_printf(m, ":%p", rec->data);
2590 seq_putc(m, '\n');
2591
2592 return 0;
2593}
2594
2595static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002596t_next(struct seq_file *m, void *v, loff_t *pos)
2597{
2598 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002599 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002600 struct dyn_ftrace *rec = NULL;
2601
Steven Rostedt45a4a232011-04-21 23:16:46 -04002602 if (unlikely(ftrace_disabled))
2603 return NULL;
2604
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002605 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002606 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002607
Steven Rostedt5072c592008-05-12 21:20:43 +02002608 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002609 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002610
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002611 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002612 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002613
Steven Rostedt5072c592008-05-12 21:20:43 +02002614 retry:
2615 if (iter->idx >= iter->pg->index) {
2616 if (iter->pg->next) {
2617 iter->pg = iter->pg->next;
2618 iter->idx = 0;
2619 goto retry;
2620 }
2621 } else {
2622 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05002623 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002624 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05002625
Steven Rostedt41c52c02008-05-22 11:46:33 -04002626 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002627 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2628
2629 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002630 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002631
Steven Rostedt5072c592008-05-12 21:20:43 +02002632 rec = NULL;
2633 goto retry;
2634 }
2635 }
2636
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002637 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002638 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002639
2640 iter->func = rec;
2641
2642 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002643}
2644
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002645static void reset_iter_read(struct ftrace_iterator *iter)
2646{
2647 iter->pos = 0;
2648 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03002649 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002650}
2651
2652static void *t_start(struct seq_file *m, loff_t *pos)
2653{
2654 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002655 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002656 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002657 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002658
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002659 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002660
2661 if (unlikely(ftrace_disabled))
2662 return NULL;
2663
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002664 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002665 * If an lseek was done, then reset and start from beginning.
2666 */
2667 if (*pos < iter->pos)
2668 reset_iter_read(iter);
2669
2670 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002671 * For set_ftrace_filter reading, if we have the filter
2672 * off, we can short cut and just print out that all
2673 * functions are enabled.
2674 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002675 if (iter->flags & FTRACE_ITER_FILTER &&
2676 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002677 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002678 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002679 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002680 /* reset in case of seek/pread */
2681 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002682 return iter;
2683 }
2684
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002685 if (iter->flags & FTRACE_ITER_HASH)
2686 return t_hash_start(m, pos);
2687
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002688 /*
2689 * Unfortunately, we need to restart at ftrace_pages_start
2690 * every time we let go of the ftrace_mutex. This is because
2691 * those pointers can change without the lock.
2692 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002693 iter->pg = ftrace_pages_start;
2694 iter->idx = 0;
2695 for (l = 0; l <= *pos; ) {
2696 p = t_next(m, p, &l);
2697 if (!p)
2698 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002699 }
walimis5821e1b2008-11-15 15:19:06 +08002700
Steven Rostedt69a30832011-12-19 15:21:16 -05002701 if (!p)
2702 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002703
2704 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002705}
2706
2707static void t_stop(struct seq_file *m, void *p)
2708{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002709 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002710}
2711
2712static int t_show(struct seq_file *m, void *v)
2713{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002714 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002715 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002716
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002717 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002718 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002719
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002720 if (iter->flags & FTRACE_ITER_PRINTALL) {
2721 seq_printf(m, "#### all functions enabled ####\n");
2722 return 0;
2723 }
2724
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002725 rec = iter->func;
2726
Steven Rostedt5072c592008-05-12 21:20:43 +02002727 if (!rec)
2728 return 0;
2729
Steven Rostedt647bcd02011-05-03 14:39:21 -04002730 seq_printf(m, "%ps", (void *)rec->ip);
2731 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002732 seq_printf(m, " (%ld)%s",
2733 rec->flags & ~FTRACE_FL_MASK,
2734 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002735 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002736
2737 return 0;
2738}
2739
James Morris88e9d342009-09-22 16:43:43 -07002740static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002741 .start = t_start,
2742 .next = t_next,
2743 .stop = t_stop,
2744 .show = t_show,
2745};
2746
Ingo Molnare309b412008-05-12 21:20:51 +02002747static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002748ftrace_avail_open(struct inode *inode, struct file *file)
2749{
2750 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002751
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002752 if (unlikely(ftrace_disabled))
2753 return -ENODEV;
2754
Jiri Olsa50e18b92012-04-25 10:23:39 +02002755 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2756 if (iter) {
2757 iter->pg = ftrace_pages_start;
2758 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002759 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002760
Jiri Olsa50e18b92012-04-25 10:23:39 +02002761 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002762}
2763
Steven Rostedt647bcd02011-05-03 14:39:21 -04002764static int
2765ftrace_enabled_open(struct inode *inode, struct file *file)
2766{
2767 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002768
2769 if (unlikely(ftrace_disabled))
2770 return -ENODEV;
2771
Jiri Olsa50e18b92012-04-25 10:23:39 +02002772 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2773 if (iter) {
2774 iter->pg = ftrace_pages_start;
2775 iter->flags = FTRACE_ITER_ENABLED;
2776 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002777 }
2778
Jiri Olsa50e18b92012-04-25 10:23:39 +02002779 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002780}
2781
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002782static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002783{
Steven Rostedt52baf112009-02-14 01:15:39 -05002784 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002785 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002786 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002787}
2788
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002789/**
2790 * ftrace_regex_open - initialize function tracer filter files
2791 * @ops: The ftrace_ops that hold the hash filters
2792 * @flag: The type of filter to process
2793 * @inode: The inode, usually passed in to your open routine
2794 * @file: The file, usually passed in to your open routine
2795 *
2796 * ftrace_regex_open() initializes the filter files for the
2797 * @ops. Depending on @flag it may process the filter hash or
2798 * the notrace hash of @ops. With this called from the open
2799 * routine, you can use ftrace_filter_write() for the write
2800 * routine if @flag has FTRACE_ITER_FILTER set, or
2801 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05002802 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002803 * release must call ftrace_regex_release().
2804 */
2805int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002806ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002807 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002808{
2809 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002810 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002811 int ret = 0;
2812
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002813 ftrace_ops_init(ops);
2814
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002815 if (unlikely(ftrace_disabled))
2816 return -ENODEV;
2817
Steven Rostedt5072c592008-05-12 21:20:43 +02002818 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2819 if (!iter)
2820 return -ENOMEM;
2821
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002822 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2823 kfree(iter);
2824 return -ENOMEM;
2825 }
2826
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002827 iter->ops = ops;
2828 iter->flags = flag;
2829
2830 mutex_lock(&ops->regex_lock);
2831
Steven Rostedtf45948e2011-05-02 12:29:25 -04002832 if (flag & FTRACE_ITER_NOTRACE)
2833 hash = ops->notrace_hash;
2834 else
2835 hash = ops->filter_hash;
2836
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002837 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002838 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002839 if (!iter->hash) {
2840 trace_parser_put(&iter->parser);
2841 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002842 ret = -ENOMEM;
2843 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002844 }
2845 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002846
Steven Rostedt5072c592008-05-12 21:20:43 +02002847 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002848 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002849 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002850
2851 if (file->f_mode & FMODE_READ) {
2852 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002853
2854 ret = seq_open(file, &show_ftrace_seq_ops);
2855 if (!ret) {
2856 struct seq_file *m = file->private_data;
2857 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002858 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002859 /* Failed */
2860 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002861 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002862 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002863 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002864 } else
2865 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002866
2867 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002868 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002869
2870 return ret;
2871}
2872
Steven Rostedt41c52c02008-05-22 11:46:33 -04002873static int
2874ftrace_filter_open(struct inode *inode, struct file *file)
2875{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05002876 struct ftrace_ops *ops = inode->i_private;
2877
2878 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05002879 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2880 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002881}
2882
2883static int
2884ftrace_notrace_open(struct inode *inode, struct file *file)
2885{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05002886 struct ftrace_ops *ops = inode->i_private;
2887
2888 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002889 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002890}
2891
Steven Rostedt64e7c442009-02-13 17:08:48 -05002892static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002893{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002894 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002895 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002896
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002897 switch (type) {
2898 case MATCH_FULL:
2899 if (strcmp(str, regex) == 0)
2900 matched = 1;
2901 break;
2902 case MATCH_FRONT_ONLY:
2903 if (strncmp(str, regex, len) == 0)
2904 matched = 1;
2905 break;
2906 case MATCH_MIDDLE_ONLY:
2907 if (strstr(str, regex))
2908 matched = 1;
2909 break;
2910 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002911 slen = strlen(str);
2912 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002913 matched = 1;
2914 break;
2915 }
2916
2917 return matched;
2918}
2919
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002920static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002921enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002922{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002923 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002924 int ret = 0;
2925
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002926 entry = ftrace_lookup_ip(hash, rec->ip);
2927 if (not) {
2928 /* Do nothing if it doesn't exist */
2929 if (!entry)
2930 return 0;
2931
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002932 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002933 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002934 /* Do nothing if it exists */
2935 if (entry)
2936 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002937
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002938 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002939 }
2940 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002941}
2942
Steven Rostedt64e7c442009-02-13 17:08:48 -05002943static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002944ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2945 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002946{
2947 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002948 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002949
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002950 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2951
2952 if (mod) {
2953 /* module lookup requires matching the module */
2954 if (!modname || strcmp(modname, mod))
2955 return 0;
2956
2957 /* blank search means to match all funcs in the mod */
2958 if (!len)
2959 return 1;
2960 }
2961
Steven Rostedt64e7c442009-02-13 17:08:48 -05002962 return ftrace_match(str, regex, len, type);
2963}
2964
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002965static int
2966match_records(struct ftrace_hash *hash, char *buff,
2967 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002968{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002969 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002970 struct ftrace_page *pg;
2971 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002972 int type = MATCH_FULL;
2973 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002974 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002975 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002976
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002977 if (len) {
2978 type = filter_parse_regex(buff, len, &search, &not);
2979 search_len = strlen(search);
2980 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002981
Steven Rostedt52baf112009-02-14 01:15:39 -05002982 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002983
2984 if (unlikely(ftrace_disabled))
2985 goto out_unlock;
2986
Steven Rostedt265c8312009-02-13 12:43:56 -05002987 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002988 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002989 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002990 if (ret < 0) {
2991 found = ret;
2992 goto out_unlock;
2993 }
Li Zefan311d16d2009-12-08 11:15:11 +08002994 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002995 }
2996 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002997 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002998 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002999
3000 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003001}
3002
Steven Rostedt64e7c442009-02-13 17:08:48 -05003003static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003004ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003005{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003006 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003007}
3008
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003009static int
3010ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003011{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003012 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003013
Steven Rostedt64e7c442009-02-13 17:08:48 -05003014 /* blank or '*' mean the same */
3015 if (strcmp(buff, "*") == 0)
3016 buff[0] = 0;
3017
3018 /* handle the case of 'dont filter this module' */
3019 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3020 buff[0] = 0;
3021 not = 1;
3022 }
3023
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003024 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003025}
3026
Steven Rostedtf6180772009-02-14 00:40:25 -05003027/*
3028 * We register the module command as a template to show others how
3029 * to register the a command as well.
3030 */
3031
3032static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003033ftrace_mod_callback(struct ftrace_hash *hash,
3034 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003035{
3036 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003037 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003038
3039 /*
3040 * cmd == 'mod' because we only registered this func
3041 * for the 'mod' ftrace_func_command.
3042 * But if you register one func with multiple commands,
3043 * you can tell which command was used by the cmd
3044 * parameter.
3045 */
3046
3047 /* we must have a module name */
3048 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003049 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003050
3051 mod = strsep(&param, ":");
3052 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003053 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003054
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003055 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003056 if (!ret)
3057 ret = -EINVAL;
3058 if (ret < 0)
3059 return ret;
3060
3061 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003062}
3063
3064static struct ftrace_func_command ftrace_mod_cmd = {
3065 .name = "mod",
3066 .func = ftrace_mod_callback,
3067};
3068
3069static int __init ftrace_mod_cmd_init(void)
3070{
3071 return register_ftrace_command(&ftrace_mod_cmd);
3072}
Steven Rostedt6f415672012-10-05 12:13:07 -04003073core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003074
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003075static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003076 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003077{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003078 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003079 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003080 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003081
3082 key = hash_long(ip, FTRACE_HASH_BITS);
3083
3084 hhd = &ftrace_func_hash[key];
3085
3086 if (hlist_empty(hhd))
3087 return;
3088
3089 /*
3090 * Disable preemption for these calls to prevent a RCU grace
3091 * period. This syncs the hash iteration and freeing of items
3092 * on the hash. rcu_read_lock is too dangerous here.
3093 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003094 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003095 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003096 if (entry->ip == ip)
3097 entry->ops->func(ip, parent_ip, &entry->data);
3098 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003099 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003100}
3101
Steven Rostedtb6887d72009-02-17 12:32:04 -05003102static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003103{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003104 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003105 .flags = FTRACE_OPS_FL_INITIALIZED,
3106 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003107};
3108
Steven Rostedtb6887d72009-02-17 12:32:04 -05003109static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003110
Steven Rostedtb6887d72009-02-17 12:32:04 -05003111static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003112{
Steven Rostedtb8489142011-05-04 09:27:52 -04003113 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003114 int i;
3115
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003116 if (ftrace_probe_registered) {
3117 /* still need to update the function call sites */
3118 if (ftrace_enabled)
3119 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003120 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003121 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003122
3123 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3124 struct hlist_head *hhd = &ftrace_func_hash[i];
3125 if (hhd->first)
3126 break;
3127 }
3128 /* Nothing registered? */
3129 if (i == FTRACE_FUNC_HASHSIZE)
3130 return;
3131
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003132 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003133
Steven Rostedtb6887d72009-02-17 12:32:04 -05003134 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003135}
3136
Steven Rostedtb6887d72009-02-17 12:32:04 -05003137static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003138{
3139 int i;
3140
Steven Rostedtb6887d72009-02-17 12:32:04 -05003141 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003142 return;
3143
3144 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3145 struct hlist_head *hhd = &ftrace_func_hash[i];
3146 if (hhd->first)
3147 return;
3148 }
3149
3150 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003151 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003152
Steven Rostedtb6887d72009-02-17 12:32:04 -05003153 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003154}
3155
3156
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003157static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003158{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003159 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003160 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003161 kfree(entry);
3162}
3163
Steven Rostedt59df055f2009-02-14 15:29:06 -05003164int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003165register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003166 void *data)
3167{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003168 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003169 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3170 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003171 struct ftrace_page *pg;
3172 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003173 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003174 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003175 int count = 0;
3176 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003177 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003178
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003179 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003180 len = strlen(search);
3181
Steven Rostedtb6887d72009-02-17 12:32:04 -05003182 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003183 if (WARN_ON(not))
3184 return -EINVAL;
3185
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003186 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003187
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003188 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3189 if (!hash) {
3190 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003191 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003192 }
3193
3194 if (unlikely(ftrace_disabled)) {
3195 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003196 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003197 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003198
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003199 mutex_lock(&ftrace_lock);
3200
Steven Rostedt59df055f2009-02-14 15:29:06 -05003201 do_for_each_ftrace_rec(pg, rec) {
3202
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003203 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003204 continue;
3205
3206 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3207 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003208 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003209 if (!count)
3210 count = -ENOMEM;
3211 goto out_unlock;
3212 }
3213
3214 count++;
3215
3216 entry->data = data;
3217
3218 /*
3219 * The caller might want to do something special
3220 * for each function we find. We call the callback
3221 * to give the caller an opportunity to do so.
3222 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003223 if (ops->init) {
3224 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003225 /* caller does not like this func */
3226 kfree(entry);
3227 continue;
3228 }
3229 }
3230
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003231 ret = enter_record(hash, rec, 0);
3232 if (ret < 0) {
3233 kfree(entry);
3234 count = ret;
3235 goto out_unlock;
3236 }
3237
Steven Rostedt59df055f2009-02-14 15:29:06 -05003238 entry->ops = ops;
3239 entry->ip = rec->ip;
3240
3241 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3242 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3243
3244 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003245
3246 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3247 if (ret < 0)
3248 count = ret;
3249
Steven Rostedtb6887d72009-02-17 12:32:04 -05003250 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003251
3252 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003253 mutex_unlock(&ftrace_lock);
3254 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003255 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003256 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003257
3258 return count;
3259}
3260
3261enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003262 PROBE_TEST_FUNC = 1,
3263 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003264};
3265
3266static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003267__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003268 void *data, int flags)
3269{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003270 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003271 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003272 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003273 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003274 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003275 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003276 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003277 char str[KSYM_SYMBOL_LEN];
3278 int type = MATCH_FULL;
3279 int i, len = 0;
3280 char *search;
3281
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003282 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003283 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003284 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003285 int not;
3286
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003287 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003288 len = strlen(search);
3289
Steven Rostedtb6887d72009-02-17 12:32:04 -05003290 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003291 if (WARN_ON(not))
3292 return;
3293 }
3294
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003295 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003296
3297 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3298 if (!hash)
3299 /* Hmm, should report this somehow */
3300 goto out_unlock;
3301
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003302 INIT_LIST_HEAD(&free_list);
3303
Steven Rostedt59df055f2009-02-14 15:29:06 -05003304 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3305 struct hlist_head *hhd = &ftrace_func_hash[i];
3306
Sasha Levinb67bfe02013-02-27 17:06:00 -08003307 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003308
3309 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003310 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003311 continue;
3312
Steven Rostedtb6887d72009-02-17 12:32:04 -05003313 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003314 continue;
3315
3316 /* do this last, since it is the most expensive */
3317 if (glob) {
3318 kallsyms_lookup(entry->ip, NULL, NULL,
3319 NULL, str);
3320 if (!ftrace_match(str, glob, len, type))
3321 continue;
3322 }
3323
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003324 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3325 /* It is possible more than one entry had this ip */
3326 if (rec_entry)
3327 free_hash_entry(hash, rec_entry);
3328
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003329 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003330 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003331 }
3332 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003333 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003334 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003335 /*
3336 * Remove after the disable is called. Otherwise, if the last
3337 * probe is removed, a null hash means *all enabled*.
3338 */
3339 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003340 synchronize_sched();
3341 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3342 list_del(&entry->free_list);
3343 ftrace_free_entry(entry);
3344 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003345 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003346
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003347 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003348 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003349 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003350}
3351
3352void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003353unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003354 void *data)
3355{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003356 __unregister_ftrace_function_probe(glob, ops, data,
3357 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003358}
3359
3360void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003361unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003362{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003363 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003364}
3365
Steven Rostedtb6887d72009-02-17 12:32:04 -05003366void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003367{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003368 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003369}
3370
Steven Rostedtf6180772009-02-14 00:40:25 -05003371static LIST_HEAD(ftrace_commands);
3372static DEFINE_MUTEX(ftrace_cmd_mutex);
3373
Tom Zanussi38de93a2013-10-24 08:34:18 -05003374/*
3375 * Currently we only register ftrace commands from __init, so mark this
3376 * __init too.
3377 */
3378__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003379{
3380 struct ftrace_func_command *p;
3381 int ret = 0;
3382
3383 mutex_lock(&ftrace_cmd_mutex);
3384 list_for_each_entry(p, &ftrace_commands, list) {
3385 if (strcmp(cmd->name, p->name) == 0) {
3386 ret = -EBUSY;
3387 goto out_unlock;
3388 }
3389 }
3390 list_add(&cmd->list, &ftrace_commands);
3391 out_unlock:
3392 mutex_unlock(&ftrace_cmd_mutex);
3393
3394 return ret;
3395}
3396
Tom Zanussi38de93a2013-10-24 08:34:18 -05003397/*
3398 * Currently we only unregister ftrace commands from __init, so mark
3399 * this __init too.
3400 */
3401__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003402{
3403 struct ftrace_func_command *p, *n;
3404 int ret = -ENODEV;
3405
3406 mutex_lock(&ftrace_cmd_mutex);
3407 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3408 if (strcmp(cmd->name, p->name) == 0) {
3409 ret = 0;
3410 list_del_init(&p->list);
3411 goto out_unlock;
3412 }
3413 }
3414 out_unlock:
3415 mutex_unlock(&ftrace_cmd_mutex);
3416
3417 return ret;
3418}
3419
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003420static int ftrace_process_regex(struct ftrace_hash *hash,
3421 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003422{
Steven Rostedtf6180772009-02-14 00:40:25 -05003423 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003424 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003425 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003426
3427 func = strsep(&next, ":");
3428
3429 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003430 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003431 if (!ret)
3432 ret = -EINVAL;
3433 if (ret < 0)
3434 return ret;
3435 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003436 }
3437
Steven Rostedtf6180772009-02-14 00:40:25 -05003438 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003439
3440 command = strsep(&next, ":");
3441
Steven Rostedtf6180772009-02-14 00:40:25 -05003442 mutex_lock(&ftrace_cmd_mutex);
3443 list_for_each_entry(p, &ftrace_commands, list) {
3444 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003445 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003446 goto out_unlock;
3447 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003448 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003449 out_unlock:
3450 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003451
Steven Rostedtf6180772009-02-14 00:40:25 -05003452 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003453}
3454
Ingo Molnare309b412008-05-12 21:20:51 +02003455static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003456ftrace_regex_write(struct file *file, const char __user *ubuf,
3457 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003458{
3459 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003460 struct trace_parser *parser;
3461 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003462
Li Zefan4ba79782009-09-22 13:52:20 +08003463 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003464 return 0;
3465
Steven Rostedt5072c592008-05-12 21:20:43 +02003466 if (file->f_mode & FMODE_READ) {
3467 struct seq_file *m = file->private_data;
3468 iter = m->private;
3469 } else
3470 iter = file->private_data;
3471
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003472 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003473 return -ENODEV;
3474
3475 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003476
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003477 parser = &iter->parser;
3478 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003479
Li Zefan4ba79782009-09-22 13:52:20 +08003480 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003481 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003482 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003483 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003484 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003485 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003486 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003487 }
3488
Steven Rostedt5072c592008-05-12 21:20:43 +02003489 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003490 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003491 return ret;
3492}
3493
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003494ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003495ftrace_filter_write(struct file *file, const char __user *ubuf,
3496 size_t cnt, loff_t *ppos)
3497{
3498 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3499}
3500
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003501ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003502ftrace_notrace_write(struct file *file, const char __user *ubuf,
3503 size_t cnt, loff_t *ppos)
3504{
3505 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3506}
3507
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003508static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003509ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3510{
3511 struct ftrace_func_entry *entry;
3512
3513 if (!ftrace_location(ip))
3514 return -EINVAL;
3515
3516 if (remove) {
3517 entry = ftrace_lookup_ip(hash, ip);
3518 if (!entry)
3519 return -ENOENT;
3520 free_hash_entry(hash, entry);
3521 return 0;
3522 }
3523
3524 return add_hash_entry(hash, ip);
3525}
3526
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003527static void ftrace_ops_update_code(struct ftrace_ops *ops)
3528{
3529 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3530 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3531}
3532
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003533static int
3534ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3535 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003536{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003537 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003538 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003539 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003540
Steven Rostedt936e0742011-05-05 22:54:01 -04003541 /* All global ops uses the global ops filters */
3542 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3543 ops = &global_ops;
3544
Steven Rostedt41c52c02008-05-22 11:46:33 -04003545 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003546 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003547
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003548 mutex_lock(&ops->regex_lock);
3549
Steven Rostedtf45948e2011-05-02 12:29:25 -04003550 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003551 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003552 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003553 orig_hash = &ops->notrace_hash;
3554
3555 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003556 if (!hash) {
3557 ret = -ENOMEM;
3558 goto out_regex_unlock;
3559 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003560
Steven Rostedt41c52c02008-05-22 11:46:33 -04003561 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003562 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003563 if (buf && !ftrace_match_records(hash, buf, len)) {
3564 ret = -EINVAL;
3565 goto out_regex_unlock;
3566 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003567 if (ip) {
3568 ret = ftrace_match_addr(hash, ip, remove);
3569 if (ret < 0)
3570 goto out_regex_unlock;
3571 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003572
3573 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003574 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003575 if (!ret)
3576 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003577
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003578 mutex_unlock(&ftrace_lock);
3579
Jiri Olsaac483c42012-01-02 10:04:14 +01003580 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003581 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003582
3583 free_ftrace_hash(hash);
3584 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003585}
3586
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003587static int
3588ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3589 int reset, int enable)
3590{
3591 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3592}
3593
3594/**
3595 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3596 * @ops - the ops to set the filter with
3597 * @ip - the address to add to or remove from the filter.
3598 * @remove - non zero to remove the ip from the filter
3599 * @reset - non zero to reset all filters before applying this filter.
3600 *
3601 * Filters denote which functions should be enabled when tracing is enabled
3602 * If @ip is NULL, it failes to update filter.
3603 */
3604int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3605 int remove, int reset)
3606{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003607 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003608 return ftrace_set_addr(ops, ip, remove, reset, 1);
3609}
3610EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3611
3612static int
3613ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3614 int reset, int enable)
3615{
3616 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3617}
3618
Steven Rostedt77a2b372008-05-12 21:20:45 +02003619/**
3620 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003621 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003622 * @buf - the string that holds the function filter text.
3623 * @len - the length of the string.
3624 * @reset - non zero to reset all filters before applying this filter.
3625 *
3626 * Filters denote which functions should be enabled when tracing is enabled.
3627 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3628 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003629int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003630 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003631{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003632 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003633 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003634}
Steven Rostedt936e0742011-05-05 22:54:01 -04003635EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003636
Steven Rostedt41c52c02008-05-22 11:46:33 -04003637/**
3638 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003639 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003640 * @buf - the string that holds the function notrace text.
3641 * @len - the length of the string.
3642 * @reset - non zero to reset all filters before applying this filter.
3643 *
3644 * Notrace Filters denote which functions should not be enabled when tracing
3645 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3646 * for tracing.
3647 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003648int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003649 int len, int reset)
3650{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003651 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003652 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003653}
3654EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3655/**
3656 * ftrace_set_filter - set a function to filter on in ftrace
3657 * @ops - the ops to set the filter with
3658 * @buf - the string that holds the function filter text.
3659 * @len - the length of the string.
3660 * @reset - non zero to reset all filters before applying this filter.
3661 *
3662 * Filters denote which functions should be enabled when tracing is enabled.
3663 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3664 */
3665void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3666{
3667 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3668}
3669EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3670
3671/**
3672 * ftrace_set_notrace - set a function to not trace in ftrace
3673 * @ops - the ops to set the notrace filter with
3674 * @buf - the string that holds the function notrace text.
3675 * @len - the length of the string.
3676 * @reset - non zero to reset all filters before applying this filter.
3677 *
3678 * Notrace Filters denote which functions should not be enabled when tracing
3679 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3680 * for tracing.
3681 */
3682void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003683{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003684 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003685}
Steven Rostedt936e0742011-05-05 22:54:01 -04003686EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003687
Steven Rostedt2af15d62009-05-28 13:37:24 -04003688/*
3689 * command line interface to allow users to set filters on boot up.
3690 */
3691#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3692static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3693static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3694
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003695/* Used by function selftest to not test if filter is set */
3696bool ftrace_filter_param __initdata;
3697
Steven Rostedt2af15d62009-05-28 13:37:24 -04003698static int __init set_ftrace_notrace(char *str)
3699{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003700 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003701 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003702 return 1;
3703}
3704__setup("ftrace_notrace=", set_ftrace_notrace);
3705
3706static int __init set_ftrace_filter(char *str)
3707{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003708 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003709 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003710 return 1;
3711}
3712__setup("ftrace_filter=", set_ftrace_filter);
3713
Stefan Assmann369bc182009-10-12 22:17:21 +02003714#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003715static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003716static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003717
Stefan Assmann369bc182009-10-12 22:17:21 +02003718static int __init set_graph_function(char *str)
3719{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003720 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003721 return 1;
3722}
3723__setup("ftrace_graph_filter=", set_graph_function);
3724
3725static void __init set_ftrace_early_graph(char *buf)
3726{
3727 int ret;
3728 char *func;
3729
3730 while (buf) {
3731 func = strsep(&buf, ",");
3732 /* we allow only one expression at a time */
3733 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003734 FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003735 if (ret)
3736 printk(KERN_DEBUG "ftrace: function %s not "
3737 "traceable\n", func);
3738 }
3739}
3740#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3741
Steven Rostedt2a85a372011-12-19 21:57:44 -05003742void __init
3743ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003744{
3745 char *func;
3746
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003747 ftrace_ops_init(ops);
3748
Steven Rostedt2af15d62009-05-28 13:37:24 -04003749 while (buf) {
3750 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003751 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003752 }
3753}
3754
3755static void __init set_ftrace_early_filters(void)
3756{
3757 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003758 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003759 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003760 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003761#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3762 if (ftrace_graph_buf[0])
3763 set_ftrace_early_graph(ftrace_graph_buf);
3764#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003765}
3766
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003767int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003768{
3769 struct seq_file *m = (struct seq_file *)file->private_data;
3770 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003771 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003772 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003773 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003774 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003775
Steven Rostedt5072c592008-05-12 21:20:43 +02003776 if (file->f_mode & FMODE_READ) {
3777 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003778 seq_release(inode, file);
3779 } else
3780 iter = file->private_data;
3781
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003782 parser = &iter->parser;
3783 if (trace_parser_loaded(parser)) {
3784 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003785 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003786 }
3787
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003788 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003789
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003790 mutex_lock(&iter->ops->regex_lock);
3791
Steven Rostedt058e2972011-04-29 22:35:33 -04003792 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003793 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3794
3795 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003796 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003797 else
3798 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003799
Steven Rostedt058e2972011-04-29 22:35:33 -04003800 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003801 ret = ftrace_hash_move(iter->ops, filter_hash,
3802 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003803 if (!ret)
3804 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003805
Steven Rostedt058e2972011-04-29 22:35:33 -04003806 mutex_unlock(&ftrace_lock);
3807 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003808
3809 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003810 free_ftrace_hash(iter->hash);
3811 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003812
Steven Rostedt5072c592008-05-12 21:20:43 +02003813 return 0;
3814}
3815
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003816static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003817 .open = ftrace_avail_open,
3818 .read = seq_read,
3819 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003820 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003821};
3822
Steven Rostedt647bcd02011-05-03 14:39:21 -04003823static const struct file_operations ftrace_enabled_fops = {
3824 .open = ftrace_enabled_open,
3825 .read = seq_read,
3826 .llseek = seq_lseek,
3827 .release = seq_release_private,
3828};
3829
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003830static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003831 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003832 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003833 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003834 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003835 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003836};
3837
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003838static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003839 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003840 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003841 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003842 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003843 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003844};
3845
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003846#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3847
3848static DEFINE_MUTEX(graph_lock);
3849
3850int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003851int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003852unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003853unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003854
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003855struct ftrace_graph_data {
3856 unsigned long *table;
3857 size_t size;
3858 int *count;
3859 const struct seq_operations *seq_ops;
3860};
3861
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003862static void *
Li Zefan85951842009-06-24 09:54:00 +08003863__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003864{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003865 struct ftrace_graph_data *fgd = m->private;
3866
3867 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003868 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003869 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003870}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003871
Li Zefan85951842009-06-24 09:54:00 +08003872static void *
3873g_next(struct seq_file *m, void *v, loff_t *pos)
3874{
3875 (*pos)++;
3876 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003877}
3878
3879static void *g_start(struct seq_file *m, loff_t *pos)
3880{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003881 struct ftrace_graph_data *fgd = m->private;
3882
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003883 mutex_lock(&graph_lock);
3884
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003885 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003886 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003887 return (void *)1;
3888
Li Zefan85951842009-06-24 09:54:00 +08003889 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003890}
3891
3892static void g_stop(struct seq_file *m, void *p)
3893{
3894 mutex_unlock(&graph_lock);
3895}
3896
3897static int g_show(struct seq_file *m, void *v)
3898{
3899 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003900
3901 if (!ptr)
3902 return 0;
3903
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003904 if (ptr == (unsigned long *)1) {
3905 seq_printf(m, "#### all functions enabled ####\n");
3906 return 0;
3907 }
3908
Steven Rostedtb375a112009-09-17 00:05:58 -04003909 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003910
3911 return 0;
3912}
3913
James Morris88e9d342009-09-22 16:43:43 -07003914static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003915 .start = g_start,
3916 .next = g_next,
3917 .stop = g_stop,
3918 .show = g_show,
3919};
3920
3921static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003922__ftrace_graph_open(struct inode *inode, struct file *file,
3923 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003924{
3925 int ret = 0;
3926
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003927 mutex_lock(&graph_lock);
3928 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003929 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003930 *fgd->count = 0;
3931 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003932 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003933 mutex_unlock(&graph_lock);
3934
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003935 if (file->f_mode & FMODE_READ) {
3936 ret = seq_open(file, fgd->seq_ops);
3937 if (!ret) {
3938 struct seq_file *m = file->private_data;
3939 m->private = fgd;
3940 }
3941 } else
3942 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08003943
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003944 return ret;
3945}
3946
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003947static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003948ftrace_graph_open(struct inode *inode, struct file *file)
3949{
3950 struct ftrace_graph_data *fgd;
3951
3952 if (unlikely(ftrace_disabled))
3953 return -ENODEV;
3954
3955 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3956 if (fgd == NULL)
3957 return -ENOMEM;
3958
3959 fgd->table = ftrace_graph_funcs;
3960 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3961 fgd->count = &ftrace_graph_count;
3962 fgd->seq_ops = &ftrace_graph_seq_ops;
3963
3964 return __ftrace_graph_open(inode, file, fgd);
3965}
3966
3967static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003968ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3969{
3970 struct ftrace_graph_data *fgd;
3971
3972 if (unlikely(ftrace_disabled))
3973 return -ENODEV;
3974
3975 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3976 if (fgd == NULL)
3977 return -ENOMEM;
3978
3979 fgd->table = ftrace_graph_notrace_funcs;
3980 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3981 fgd->count = &ftrace_graph_notrace_count;
3982 fgd->seq_ops = &ftrace_graph_seq_ops;
3983
3984 return __ftrace_graph_open(inode, file, fgd);
3985}
3986
3987static int
Li Zefan87827112009-07-23 11:29:11 +08003988ftrace_graph_release(struct inode *inode, struct file *file)
3989{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003990 if (file->f_mode & FMODE_READ) {
3991 struct seq_file *m = file->private_data;
3992
3993 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08003994 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003995 } else {
3996 kfree(file->private_data);
3997 }
3998
Li Zefan87827112009-07-23 11:29:11 +08003999 return 0;
4000}
4001
4002static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004003ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004004{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004005 struct dyn_ftrace *rec;
4006 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004007 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004008 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004009 int type, not;
4010 char *search;
4011 bool exists;
4012 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004013
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004014 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004015 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004016 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004017 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004018
4019 search_len = strlen(search);
4020
Steven Rostedt52baf112009-02-14 01:15:39 -05004021 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004022
4023 if (unlikely(ftrace_disabled)) {
4024 mutex_unlock(&ftrace_lock);
4025 return -ENODEV;
4026 }
4027
Steven Rostedt265c8312009-02-13 12:43:56 -05004028 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004029
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004030 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004031 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004032 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004033 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004034 if (array[i] == rec->ip) {
4035 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004036 break;
4037 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004038 }
4039
4040 if (!not) {
4041 fail = 0;
4042 if (!exists) {
4043 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004044 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004045 goto out;
4046 }
4047 } else {
4048 if (exists) {
4049 array[i] = array[--(*idx)];
4050 array[*idx] = 0;
4051 fail = 0;
4052 }
4053 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004054 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004055 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004056out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004057 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004058
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004059 if (fail)
4060 return -EINVAL;
4061
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004062 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004063}
4064
4065static ssize_t
4066ftrace_graph_write(struct file *file, const char __user *ubuf,
4067 size_t cnt, loff_t *ppos)
4068{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004069 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004070 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004071 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004072
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004073 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004074 return 0;
4075
Namhyung Kim6a101082013-10-14 17:24:25 +09004076 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4077 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004078
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004079 read = trace_get_user(&parser, ubuf, cnt, ppos);
4080
Li Zefan4ba79782009-09-22 13:52:20 +08004081 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004082 parser.buffer[parser.idx] = 0;
4083
Namhyung Kim6a101082013-10-14 17:24:25 +09004084 mutex_lock(&graph_lock);
4085
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004086 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004087 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4088 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004089
4090 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004091 }
4092
Namhyung Kim6a101082013-10-14 17:24:25 +09004093 if (!ret)
4094 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004095
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004096 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004097
4098 return ret;
4099}
4100
4101static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004102 .open = ftrace_graph_open,
4103 .read = seq_read,
4104 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004105 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004106 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004107};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004108
4109static const struct file_operations ftrace_graph_notrace_fops = {
4110 .open = ftrace_graph_notrace_open,
4111 .read = seq_read,
4112 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004113 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004114 .release = ftrace_graph_release,
4115};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004116#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4117
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004118void ftrace_create_filter_files(struct ftrace_ops *ops,
4119 struct dentry *parent)
4120{
4121
4122 trace_create_file("set_ftrace_filter", 0644, parent,
4123 ops, &ftrace_filter_fops);
4124
4125 trace_create_file("set_ftrace_notrace", 0644, parent,
4126 ops, &ftrace_notrace_fops);
4127}
4128
4129/*
4130 * The name "destroy_filter_files" is really a misnomer. Although
4131 * in the future, it may actualy delete the files, but this is
4132 * really intended to make sure the ops passed in are disabled
4133 * and that when this function returns, the caller is free to
4134 * free the ops.
4135 *
4136 * The "destroy" name is only to match the "create" name that this
4137 * should be paired with.
4138 */
4139void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4140{
4141 mutex_lock(&ftrace_lock);
4142 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4143 ftrace_shutdown(ops, 0);
4144 ops->flags |= FTRACE_OPS_FL_DELETED;
4145 mutex_unlock(&ftrace_lock);
4146}
4147
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004148static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004149{
Steven Rostedt5072c592008-05-12 21:20:43 +02004150
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004151 trace_create_file("available_filter_functions", 0444,
4152 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004153
Steven Rostedt647bcd02011-05-03 14:39:21 -04004154 trace_create_file("enabled_functions", 0444,
4155 d_tracer, NULL, &ftrace_enabled_fops);
4156
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004157 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004158
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004159#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004160 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004161 NULL,
4162 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004163 trace_create_file("set_graph_notrace", 0444, d_tracer,
4164 NULL,
4165 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004166#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4167
Steven Rostedt5072c592008-05-12 21:20:43 +02004168 return 0;
4169}
4170
Steven Rostedt9fd49322012-04-24 22:32:06 -04004171static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004172{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004173 const unsigned long *ipa = a;
4174 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004175
Steven Rostedt9fd49322012-04-24 22:32:06 -04004176 if (*ipa > *ipb)
4177 return 1;
4178 if (*ipa < *ipb)
4179 return -1;
4180 return 0;
4181}
4182
4183static void ftrace_swap_ips(void *a, void *b, int size)
4184{
4185 unsigned long *ipa = a;
4186 unsigned long *ipb = b;
4187 unsigned long t;
4188
4189 t = *ipa;
4190 *ipa = *ipb;
4191 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004192}
4193
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004194static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004195 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004196 unsigned long *end)
4197{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004198 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004199 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004200 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004201 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004202 unsigned long *p;
4203 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004204 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004205 int ret = -ENOMEM;
4206
4207 count = end - start;
4208
4209 if (!count)
4210 return 0;
4211
Steven Rostedt9fd49322012-04-24 22:32:06 -04004212 sort(start, count, sizeof(*start),
4213 ftrace_cmp_ips, ftrace_swap_ips);
4214
Steven Rostedt706c81f2012-04-24 23:45:26 -04004215 start_pg = ftrace_allocate_pages(count);
4216 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004217 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004218
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004219 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004220
Steven Rostedt320823092011-12-16 14:42:37 -05004221 /*
4222 * Core and each module needs their own pages, as
4223 * modules will free them when they are removed.
4224 * Force a new page to be allocated for modules.
4225 */
Steven Rostedta7900872011-12-16 16:23:44 -05004226 if (!mod) {
4227 WARN_ON(ftrace_pages || ftrace_pages_start);
4228 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004229 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004230 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05004231 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004232 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05004233
Steven Rostedta7900872011-12-16 16:23:44 -05004234 if (WARN_ON(ftrace_pages->next)) {
4235 /* Hmm, we have free pages? */
4236 while (ftrace_pages->next)
4237 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05004238 }
Steven Rostedta7900872011-12-16 16:23:44 -05004239
Steven Rostedt706c81f2012-04-24 23:45:26 -04004240 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05004241 }
4242
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004243 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004244 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004245 while (p < end) {
4246 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004247 /*
4248 * Some architecture linkers will pad between
4249 * the different mcount_loc sections of different
4250 * object files to satisfy alignments.
4251 * Skip any NULL pointers.
4252 */
4253 if (!addr)
4254 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004255
4256 if (pg->index == pg->size) {
4257 /* We should have allocated enough */
4258 if (WARN_ON(!pg->next))
4259 break;
4260 pg = pg->next;
4261 }
4262
4263 rec = &pg->records[pg->index++];
4264 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004265 }
4266
Steven Rostedt706c81f2012-04-24 23:45:26 -04004267 /* We should have used all pages */
4268 WARN_ON(pg->next);
4269
4270 /* Assign the last page to ftrace_pages */
4271 ftrace_pages = pg;
4272
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004273 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004274 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004275
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004276 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004277 * We only need to disable interrupts on start up
4278 * because we are modifying code that an interrupt
4279 * may execute, and the modification is not atomic.
4280 * But for modules, nothing runs the code we modify
4281 * until we are finished with it, and there's no
4282 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004283 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004284 if (!mod)
4285 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08004286 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004287 if (!mod)
4288 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004289 ret = 0;
4290 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004291 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004292
Steven Rostedta7900872011-12-16 16:23:44 -05004293 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004294}
4295
Steven Rostedt93eb6772009-04-15 13:24:06 -04004296#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05004297
4298#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4299
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004300void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004301{
4302 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05004303 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004304 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004305 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004306
Steven Rostedt93eb6772009-04-15 13:24:06 -04004307 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004308
4309 if (ftrace_disabled)
4310 goto out_unlock;
4311
Steven Rostedt320823092011-12-16 14:42:37 -05004312 /*
4313 * Each module has its own ftrace_pages, remove
4314 * them from the list.
4315 */
4316 last_pg = &ftrace_pages_start;
4317 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4318 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004319 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004320 /*
Steven Rostedt320823092011-12-16 14:42:37 -05004321 * As core pages are first, the first
4322 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004323 */
Steven Rostedt320823092011-12-16 14:42:37 -05004324 if (WARN_ON(pg == ftrace_pages_start))
4325 goto out_unlock;
4326
4327 /* Check if we are deleting the last page */
4328 if (pg == ftrace_pages)
4329 ftrace_pages = next_to_ftrace_page(last_pg);
4330
4331 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004332 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4333 free_pages((unsigned long)pg->records, order);
4334 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05004335 } else
4336 last_pg = &pg->next;
4337 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004338 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004339 mutex_unlock(&ftrace_lock);
4340}
4341
4342static void ftrace_init_module(struct module *mod,
4343 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004344{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004345 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004346 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004347 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004348}
4349
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004350static int ftrace_module_notify_enter(struct notifier_block *self,
4351 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004352{
4353 struct module *mod = data;
4354
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004355 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004356 ftrace_init_module(mod, mod->ftrace_callsites,
4357 mod->ftrace_callsites +
4358 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004359 return 0;
4360}
4361
4362static int ftrace_module_notify_exit(struct notifier_block *self,
4363 unsigned long val, void *data)
4364{
4365 struct module *mod = data;
4366
4367 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004368 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004369
4370 return 0;
4371}
4372#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004373static int ftrace_module_notify_enter(struct notifier_block *self,
4374 unsigned long val, void *data)
4375{
4376 return 0;
4377}
4378static int ftrace_module_notify_exit(struct notifier_block *self,
4379 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004380{
4381 return 0;
4382}
4383#endif /* CONFIG_MODULES */
4384
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004385struct notifier_block ftrace_module_enter_nb = {
4386 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004387 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004388};
4389
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004390struct notifier_block ftrace_module_exit_nb = {
4391 .notifier_call = ftrace_module_notify_exit,
4392 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4393};
4394
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004395extern unsigned long __start_mcount_loc[];
4396extern unsigned long __stop_mcount_loc[];
4397
4398void __init ftrace_init(void)
4399{
4400 unsigned long count, addr, flags;
4401 int ret;
4402
4403 /* Keep the ftrace pointer to the stub */
4404 addr = (unsigned long)ftrace_stub;
4405
4406 local_irq_save(flags);
4407 ftrace_dyn_arch_init(&addr);
4408 local_irq_restore(flags);
4409
4410 /* ftrace_dyn_arch_init places the return code in addr */
4411 if (addr)
4412 goto failed;
4413
4414 count = __stop_mcount_loc - __start_mcount_loc;
4415
4416 ret = ftrace_dyn_table_alloc(count);
4417 if (ret)
4418 goto failed;
4419
4420 last_ftrace_enabled = ftrace_enabled = 1;
4421
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004422 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004423 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004424 __stop_mcount_loc);
4425
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004426 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004427 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004428 pr_warning("Failed to register trace ftrace module enter notifier\n");
4429
4430 ret = register_module_notifier(&ftrace_module_exit_nb);
4431 if (ret)
4432 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004433
Steven Rostedt2af15d62009-05-28 13:37:24 -04004434 set_ftrace_early_filters();
4435
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004436 return;
4437 failed:
4438 ftrace_disabled = 1;
4439}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004440
Steven Rostedt3d083392008-05-12 21:20:42 +02004441#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004442
Steven Rostedt2b499382011-05-03 22:49:52 -04004443static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004444 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004445 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4446 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004447};
4448
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004449static int __init ftrace_nodyn_init(void)
4450{
4451 ftrace_enabled = 1;
4452 return 0;
4453}
Steven Rostedt6f415672012-10-05 12:13:07 -04004454core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004455
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004456static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4457static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004458/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004459# define ftrace_startup(ops, command) \
4460 ({ \
4461 int ___ret = __register_ftrace_function(ops); \
4462 if (!___ret) \
4463 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4464 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004465 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004466# define ftrace_shutdown(ops, command) \
4467 ({ \
4468 int ___ret = __unregister_ftrace_function(ops); \
4469 if (!___ret) \
4470 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4471 ___ret; \
4472 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004473
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004474# define ftrace_startup_sysctl() do { } while (0)
4475# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004476
4477static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004478ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004479{
4480 return 1;
4481}
4482
Steven Rostedt3d083392008-05-12 21:20:42 +02004483#endif /* CONFIG_DYNAMIC_FTRACE */
4484
Steven Rostedtb8489142011-05-04 09:27:52 -04004485static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004486ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004487 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004488{
Jiri Olsae2484912012-02-15 15:51:48 +01004489 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4490 return;
4491
4492 /*
4493 * Some of the ops may be dynamically allocated,
4494 * they must be freed after a synchronize_sched().
4495 */
4496 preempt_disable_notrace();
4497 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004498
4499 /*
4500 * Control funcs (perf) uses RCU. Only trace if
4501 * RCU is currently active.
4502 */
4503 if (!rcu_is_watching())
4504 goto out;
4505
Steven Rostedt0a016402012-11-02 17:03:03 -04004506 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004507 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4508 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004509 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004510 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004511 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004512 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004513 trace_recursion_clear(TRACE_CONTROL_BIT);
4514 preempt_enable_notrace();
4515}
4516
4517static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004518 .func = ftrace_ops_control_func,
4519 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4520 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004521};
4522
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004523static inline void
4524__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004525 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004526{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004527 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004528 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004529
Steven Rostedtccf36722012-06-05 09:44:25 -04004530 if (function_trace_stop)
4531 return;
4532
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004533 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4534 if (bit < 0)
4535 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004536
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004537 /*
4538 * Some of the ops may be dynamically allocated,
4539 * they must be freed after a synchronize_sched().
4540 */
4541 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004542 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004543 if (ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004544 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004545 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004546 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004547 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004548}
4549
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004550/*
4551 * Some archs only support passing ip and parent_ip. Even though
4552 * the list function ignores the op parameter, we do not want any
4553 * C side effects, where a function is called without the caller
4554 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004555 * Archs are to support both the regs and ftrace_ops at the same time.
4556 * If they support ftrace_ops, it is assumed they support regs.
4557 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004558 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4559 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004560 * An architecture can pass partial regs with ftrace_ops and still
4561 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004562 */
4563#if ARCH_SUPPORTS_FTRACE_OPS
4564static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004565 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004566{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004567 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004568}
4569#else
4570static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4571{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004572 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004573}
4574#endif
4575
Steven Rostedte32d8952008-12-04 00:26:41 -05004576static void clear_ftrace_swapper(void)
4577{
4578 struct task_struct *p;
4579 int cpu;
4580
4581 get_online_cpus();
4582 for_each_online_cpu(cpu) {
4583 p = idle_task(cpu);
4584 clear_tsk_trace_trace(p);
4585 }
4586 put_online_cpus();
4587}
4588
4589static void set_ftrace_swapper(void)
4590{
4591 struct task_struct *p;
4592 int cpu;
4593
4594 get_online_cpus();
4595 for_each_online_cpu(cpu) {
4596 p = idle_task(cpu);
4597 set_tsk_trace_trace(p);
4598 }
4599 put_online_cpus();
4600}
4601
4602static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004603{
4604 struct task_struct *p;
4605
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004606 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004607 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004608 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004609 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004610 rcu_read_unlock();
4611
Steven Rostedte32d8952008-12-04 00:26:41 -05004612 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004613}
4614
Steven Rostedte32d8952008-12-04 00:26:41 -05004615static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004616{
4617 struct task_struct *p;
4618
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004619 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004620 do_each_pid_task(pid, PIDTYPE_PID, p) {
4621 set_tsk_trace_trace(p);
4622 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004623 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004624}
4625
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004626static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004627{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004628 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004629 clear_ftrace_swapper();
4630 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004631 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004632}
4633
4634static void set_ftrace_pid_task(struct pid *pid)
4635{
4636 if (pid == ftrace_swapper_pid)
4637 set_ftrace_swapper();
4638 else
4639 set_ftrace_pid(pid);
4640}
4641
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004642static int ftrace_pid_add(int p)
4643{
4644 struct pid *pid;
4645 struct ftrace_pid *fpid;
4646 int ret = -EINVAL;
4647
4648 mutex_lock(&ftrace_lock);
4649
4650 if (!p)
4651 pid = ftrace_swapper_pid;
4652 else
4653 pid = find_get_pid(p);
4654
4655 if (!pid)
4656 goto out;
4657
4658 ret = 0;
4659
4660 list_for_each_entry(fpid, &ftrace_pids, list)
4661 if (fpid->pid == pid)
4662 goto out_put;
4663
4664 ret = -ENOMEM;
4665
4666 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4667 if (!fpid)
4668 goto out_put;
4669
4670 list_add(&fpid->list, &ftrace_pids);
4671 fpid->pid = pid;
4672
4673 set_ftrace_pid_task(pid);
4674
4675 ftrace_update_pid_func();
4676 ftrace_startup_enable(0);
4677
4678 mutex_unlock(&ftrace_lock);
4679 return 0;
4680
4681out_put:
4682 if (pid != ftrace_swapper_pid)
4683 put_pid(pid);
4684
4685out:
4686 mutex_unlock(&ftrace_lock);
4687 return ret;
4688}
4689
4690static void ftrace_pid_reset(void)
4691{
4692 struct ftrace_pid *fpid, *safe;
4693
4694 mutex_lock(&ftrace_lock);
4695 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4696 struct pid *pid = fpid->pid;
4697
4698 clear_ftrace_pid_task(pid);
4699
4700 list_del(&fpid->list);
4701 kfree(fpid);
4702 }
4703
4704 ftrace_update_pid_func();
4705 ftrace_startup_enable(0);
4706
4707 mutex_unlock(&ftrace_lock);
4708}
4709
4710static void *fpid_start(struct seq_file *m, loff_t *pos)
4711{
4712 mutex_lock(&ftrace_lock);
4713
4714 if (list_empty(&ftrace_pids) && (!*pos))
4715 return (void *) 1;
4716
4717 return seq_list_start(&ftrace_pids, *pos);
4718}
4719
4720static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4721{
4722 if (v == (void *)1)
4723 return NULL;
4724
4725 return seq_list_next(v, &ftrace_pids, pos);
4726}
4727
4728static void fpid_stop(struct seq_file *m, void *p)
4729{
4730 mutex_unlock(&ftrace_lock);
4731}
4732
4733static int fpid_show(struct seq_file *m, void *v)
4734{
4735 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4736
4737 if (v == (void *)1) {
4738 seq_printf(m, "no pid\n");
4739 return 0;
4740 }
4741
4742 if (fpid->pid == ftrace_swapper_pid)
4743 seq_printf(m, "swapper tasks\n");
4744 else
4745 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4746
4747 return 0;
4748}
4749
4750static const struct seq_operations ftrace_pid_sops = {
4751 .start = fpid_start,
4752 .next = fpid_next,
4753 .stop = fpid_stop,
4754 .show = fpid_show,
4755};
4756
4757static int
4758ftrace_pid_open(struct inode *inode, struct file *file)
4759{
4760 int ret = 0;
4761
4762 if ((file->f_mode & FMODE_WRITE) &&
4763 (file->f_flags & O_TRUNC))
4764 ftrace_pid_reset();
4765
4766 if (file->f_mode & FMODE_READ)
4767 ret = seq_open(file, &ftrace_pid_sops);
4768
4769 return ret;
4770}
4771
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004772static ssize_t
4773ftrace_pid_write(struct file *filp, const char __user *ubuf,
4774 size_t cnt, loff_t *ppos)
4775{
Ingo Molnar457dc922009-11-23 11:03:28 +01004776 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004777 long val;
4778 int ret;
4779
4780 if (cnt >= sizeof(buf))
4781 return -EINVAL;
4782
4783 if (copy_from_user(&buf, ubuf, cnt))
4784 return -EFAULT;
4785
4786 buf[cnt] = 0;
4787
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004788 /*
4789 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4790 * to clean the filter quietly.
4791 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004792 tmp = strstrip(buf);
4793 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004794 return 1;
4795
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004796 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004797 if (ret < 0)
4798 return ret;
4799
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004800 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004801
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004802 return ret ? ret : cnt;
4803}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004804
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004805static int
4806ftrace_pid_release(struct inode *inode, struct file *file)
4807{
4808 if (file->f_mode & FMODE_READ)
4809 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004810
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004811 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004812}
4813
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004814static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004815 .open = ftrace_pid_open,
4816 .write = ftrace_pid_write,
4817 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004818 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004819 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004820};
4821
4822static __init int ftrace_init_debugfs(void)
4823{
4824 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004825
4826 d_tracer = tracing_init_dentry();
4827 if (!d_tracer)
4828 return 0;
4829
4830 ftrace_init_dyn_debugfs(d_tracer);
4831
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004832 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4833 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004834
4835 ftrace_profile_debugfs(d_tracer);
4836
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004837 return 0;
4838}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004839fs_initcall(ftrace_init_debugfs);
4840
Steven Rostedt3d083392008-05-12 21:20:42 +02004841/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004842 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004843 *
4844 * This function should be used by panic code. It stops ftrace
4845 * but in a not so nice way. If you need to simply kill ftrace
4846 * from a non-atomic section, use ftrace_kill.
4847 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004848void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004849{
4850 ftrace_disabled = 1;
4851 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004852 clear_ftrace_function();
4853}
4854
4855/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004856 * Test if ftrace is dead or not.
4857 */
4858int ftrace_is_dead(void)
4859{
4860 return ftrace_disabled;
4861}
4862
4863/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004864 * register_ftrace_function - register a function for profiling
4865 * @ops - ops structure that holds the function for profiling.
4866 *
4867 * Register a function to be called by all functions in the
4868 * kernel.
4869 *
4870 * Note: @ops->func and all the functions it calls must be labeled
4871 * with "notrace", otherwise it will go into a
4872 * recursive loop.
4873 */
4874int register_ftrace_function(struct ftrace_ops *ops)
4875{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004876 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004877
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004878 ftrace_ops_init(ops);
4879
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004880 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004881
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004882 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004883
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004884 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004885
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004886 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004887}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004888EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004889
4890/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004891 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004892 * @ops - ops structure that holds the function to unregister
4893 *
4894 * Unregister a function that was added to be called by ftrace profiling.
4895 */
4896int unregister_ftrace_function(struct ftrace_ops *ops)
4897{
4898 int ret;
4899
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004900 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004901 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004902 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004903
4904 return ret;
4905}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004906EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004907
Ingo Molnare309b412008-05-12 21:20:51 +02004908int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004909ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004910 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004911 loff_t *ppos)
4912{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004913 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004914
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004915 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004916
Steven Rostedt45a4a232011-04-21 23:16:46 -04004917 if (unlikely(ftrace_disabled))
4918 goto out;
4919
4920 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004921
Li Zefana32c7762009-06-26 16:55:51 +08004922 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004923 goto out;
4924
Li Zefana32c7762009-06-26 16:55:51 +08004925 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004926
4927 if (ftrace_enabled) {
4928
4929 ftrace_startup_sysctl();
4930
4931 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004932 if (ftrace_ops_list != &ftrace_list_end)
4933 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004934
4935 } else {
4936 /* stopping ftrace calls (just send to ftrace_stub) */
4937 ftrace_trace_function = ftrace_stub;
4938
4939 ftrace_shutdown_sysctl();
4940 }
4941
4942 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004943 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004944 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004945}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004946
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004947#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004948
Steven Rostedt597af812009-04-03 15:24:12 -04004949static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004950static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004951
Steven Rostedte49dc192008-12-02 23:50:05 -05004952int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4953{
4954 return 0;
4955}
4956
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004957/* The callbacks that hook a function */
4958trace_func_graph_ret_t ftrace_graph_return =
4959 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004960trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05004961static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004962
4963/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4964static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4965{
4966 int i;
4967 int ret = 0;
4968 unsigned long flags;
4969 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4970 struct task_struct *g, *t;
4971
4972 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4973 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4974 * sizeof(struct ftrace_ret_stack),
4975 GFP_KERNEL);
4976 if (!ret_stack_list[i]) {
4977 start = 0;
4978 end = i;
4979 ret = -ENOMEM;
4980 goto free;
4981 }
4982 }
4983
4984 read_lock_irqsave(&tasklist_lock, flags);
4985 do_each_thread(g, t) {
4986 if (start == end) {
4987 ret = -EAGAIN;
4988 goto unlock;
4989 }
4990
4991 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004992 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004993 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004994 t->curr_ret_stack = -1;
4995 /* Make sure the tasks see the -1 first: */
4996 smp_wmb();
4997 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004998 }
4999 } while_each_thread(g, t);
5000
5001unlock:
5002 read_unlock_irqrestore(&tasklist_lock, flags);
5003free:
5004 for (i = start; i < end; i++)
5005 kfree(ret_stack_list[i]);
5006 return ret;
5007}
5008
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005009static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005010ftrace_graph_probe_sched_switch(void *ignore,
5011 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005012{
5013 unsigned long long timestamp;
5014 int index;
5015
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005016 /*
5017 * Does the user want to count the time a function was asleep.
5018 * If so, do not update the time stamps.
5019 */
5020 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5021 return;
5022
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005023 timestamp = trace_clock_local();
5024
5025 prev->ftrace_timestamp = timestamp;
5026
5027 /* only process tasks that we timestamped */
5028 if (!next->ftrace_timestamp)
5029 return;
5030
5031 /*
5032 * Update all the counters in next to make up for the
5033 * time next was sleeping.
5034 */
5035 timestamp -= next->ftrace_timestamp;
5036
5037 for (index = next->curr_ret_stack; index >= 0; index--)
5038 next->ret_stack[index].calltime += timestamp;
5039}
5040
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005041/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005042static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005043{
5044 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005045 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005046
5047 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5048 sizeof(struct ftrace_ret_stack *),
5049 GFP_KERNEL);
5050
5051 if (!ret_stack_list)
5052 return -ENOMEM;
5053
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005054 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005055 for_each_online_cpu(cpu) {
5056 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005057 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005058 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005059
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005060 do {
5061 ret = alloc_retstack_tasklist(ret_stack_list);
5062 } while (ret == -EAGAIN);
5063
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005064 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005065 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005066 if (ret)
5067 pr_info("ftrace_graph: Couldn't activate tracepoint"
5068 " probe to kernel_sched_switch\n");
5069 }
5070
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005071 kfree(ret_stack_list);
5072 return ret;
5073}
5074
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005075/*
5076 * Hibernation protection.
5077 * The state of the current task is too much unstable during
5078 * suspend/restore to disk. We want to protect against that.
5079 */
5080static int
5081ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5082 void *unused)
5083{
5084 switch (state) {
5085 case PM_HIBERNATION_PREPARE:
5086 pause_graph_tracing();
5087 break;
5088
5089 case PM_POST_HIBERNATION:
5090 unpause_graph_tracing();
5091 break;
5092 }
5093 return NOTIFY_DONE;
5094}
5095
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005096/* Just a place holder for function graph */
5097static struct ftrace_ops fgraph_ops __read_mostly = {
5098 .func = ftrace_stub,
5099 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
5100 FTRACE_OPS_FL_RECURSION_SAFE,
5101};
5102
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005103static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5104{
5105 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5106 return 0;
5107 return __ftrace_graph_entry(trace);
5108}
5109
5110/*
5111 * The function graph tracer should only trace the functions defined
5112 * by set_ftrace_filter and set_ftrace_notrace. If another function
5113 * tracer ops is registered, the graph tracer requires testing the
5114 * function against the global ops, and not just trace any function
5115 * that any ftrace_ops registered.
5116 */
5117static void update_function_graph_func(void)
5118{
5119 if (ftrace_ops_list == &ftrace_list_end ||
5120 (ftrace_ops_list == &global_ops &&
5121 global_ops.next == &ftrace_list_end))
5122 ftrace_graph_entry = __ftrace_graph_entry;
5123 else
5124 ftrace_graph_entry = ftrace_graph_entry_test;
5125}
5126
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005127int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5128 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005129{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005130 int ret = 0;
5131
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005132 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005133
Steven Rostedt05ce5812009-03-24 00:18:31 -04005134 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005135 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005136 ret = -EBUSY;
5137 goto out;
5138 }
5139
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005140 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5141 register_pm_notifier(&ftrace_suspend_notifier);
5142
Steven Rostedt597af812009-04-03 15:24:12 -04005143 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005144 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005145 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005146 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005147 goto out;
5148 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005149
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005150 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005151
5152 /*
5153 * Update the indirect function to the entryfunc, and the
5154 * function that gets called to the entry_test first. Then
5155 * call the update fgraph entry function to determine if
5156 * the entryfunc should be called directly or not.
5157 */
5158 __ftrace_graph_entry = entryfunc;
5159 ftrace_graph_entry = ftrace_graph_entry_test;
5160 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005161
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005162 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005163
5164out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005165 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005166 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005167}
5168
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005169void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005170{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005171 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005172
Steven Rostedt597af812009-04-03 15:24:12 -04005173 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005174 goto out;
5175
Steven Rostedt597af812009-04-03 15:24:12 -04005176 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005177 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005178 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005179 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005180 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005181 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005182 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005183
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005184 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005185 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005186}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005187
Steven Rostedt868baf02011-02-10 21:26:13 -05005188static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5189
5190static void
5191graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5192{
5193 atomic_set(&t->tracing_graph_pause, 0);
5194 atomic_set(&t->trace_overrun, 0);
5195 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005196 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005197 smp_wmb();
5198 t->ret_stack = ret_stack;
5199}
5200
5201/*
5202 * Allocate a return stack for the idle task. May be the first
5203 * time through, or it may be done by CPU hotplug online.
5204 */
5205void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5206{
5207 t->curr_ret_stack = -1;
5208 /*
5209 * The idle task has no parent, it either has its own
5210 * stack or no stack at all.
5211 */
5212 if (t->ret_stack)
5213 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5214
5215 if (ftrace_graph_active) {
5216 struct ftrace_ret_stack *ret_stack;
5217
5218 ret_stack = per_cpu(idle_ret_stack, cpu);
5219 if (!ret_stack) {
5220 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5221 * sizeof(struct ftrace_ret_stack),
5222 GFP_KERNEL);
5223 if (!ret_stack)
5224 return;
5225 per_cpu(idle_ret_stack, cpu) = ret_stack;
5226 }
5227 graph_init_task(t, ret_stack);
5228 }
5229}
5230
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005231/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005232void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005233{
Steven Rostedt84047e32009-06-02 16:51:55 -04005234 /* Make sure we do not use the parent ret_stack */
5235 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005236 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005237
Steven Rostedt597af812009-04-03 15:24:12 -04005238 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005239 struct ftrace_ret_stack *ret_stack;
5240
5241 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005242 * sizeof(struct ftrace_ret_stack),
5243 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005244 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005245 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005246 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005247 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005248}
5249
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005250void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005251{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005252 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5253
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005254 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005255 /* NULL must become visible to IRQs before we free it: */
5256 barrier();
5257
5258 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005259}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005260
5261void ftrace_graph_stop(void)
5262{
5263 ftrace_stop();
5264}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005265#endif