blob: b4531b228180831e3dcc8fddb3fd4e15b91aa5aa [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
Steven Rostedt2b499382011-05-03 22:49:52 -0400240static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400241{
Steven Rostedt (Red Hat)e6435e92014-01-10 14:31:31 -0500242 ftrace_func_t func = ftrace_global_list_func;
243 void *private = NULL;
244
245 /* The list has its own recursion protection. */
246 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400247
248 /*
249 * If there's only one function registered, then call that
250 * function directly. Otherwise, we need to iterate over the
251 * registered callers.
252 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400253 if (ftrace_global_list == &ftrace_list_end ||
Steven Rostedt63503792012-11-02 16:58:56 -0400254 ftrace_global_list->next == &ftrace_list_end) {
Steven Rostedtb8489142011-05-04 09:27:52 -0400255 func = ftrace_global_list->func;
Steven Rostedt (Red Hat)e6435e92014-01-10 14:31:31 -0500256 private = ftrace_global_list->private;
Steven Rostedt63503792012-11-02 16:58:56 -0400257 /*
258 * As we are calling the function directly.
259 * If it does not have recursion protection,
260 * the function_trace_op needs to be updated
261 * accordingly.
262 */
Steven Rostedt (Red Hat)e6435e92014-01-10 14:31:31 -0500263 if (!(ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE))
Steven Rostedt63503792012-11-02 16:58:56 -0400264 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
Steven Rostedt63503792012-11-02 16:58:56 -0400265 }
266
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400267 /* If we filter on pids, update to use the pid function */
268 if (!list_empty(&ftrace_pids)) {
269 set_ftrace_pid_function(func);
270 func = ftrace_pid_func;
271 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400272
273 global_ops.func = func;
Steven Rostedt (Red Hat)e6435e92014-01-10 14:31:31 -0500274 global_ops.private = private;
Steven Rostedt2b499382011-05-03 22:49:52 -0400275}
276
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500277static void ftrace_sync(struct work_struct *work)
278{
279 /*
280 * This function is just a stub to implement a hard force
281 * of synchronize_sched(). This requires synchronizing
282 * tasks even in userspace and idle.
283 *
284 * Yes, function tracing is rude.
285 */
286}
287
288static void ftrace_sync_ipi(void *data)
289{
290 /* Probably not needed, but do it anyway */
291 smp_rmb();
292}
293
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500294#ifdef CONFIG_FUNCTION_GRAPH_TRACER
295static void update_function_graph_func(void);
296#else
297static inline void update_function_graph_func(void) { }
298#endif
299
Steven Rostedt2b499382011-05-03 22:49:52 -0400300static void update_ftrace_function(void)
301{
302 ftrace_func_t func;
303
304 update_global_ops();
305
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400306 /*
307 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400308 * recursion safe and not dynamic and the arch supports passing ops,
309 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400310 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400311 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400312 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400313 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400314 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400315 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400316 /* Set the ftrace_ops that the arch callback uses */
317 if (ftrace_ops_list == &global_ops)
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500318 set_function_trace_op = ftrace_global_list;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400319 else
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500320 set_function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400321 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400322 } else {
323 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500324 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400325 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400326 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400327
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500328 /* If there's no change, then do nothing more here */
329 if (ftrace_trace_function == func)
330 return;
331
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500332 update_function_graph_func();
333
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500334 /*
335 * If we are using the list function, it doesn't care
336 * about the function_trace_ops.
337 */
338 if (func == ftrace_ops_list_func) {
339 ftrace_trace_function = func;
340 /*
341 * Don't even bother setting function_trace_ops,
342 * it would be racy to do so anyway.
343 */
344 return;
345 }
346
347#ifndef CONFIG_DYNAMIC_FTRACE
348 /*
349 * For static tracing, we need to be a bit more careful.
350 * The function change takes affect immediately. Thus,
351 * we need to coorditate the setting of the function_trace_ops
352 * with the setting of the ftrace_trace_function.
353 *
354 * Set the function to the list ops, which will call the
355 * function we want, albeit indirectly, but it handles the
356 * ftrace_ops and doesn't depend on function_trace_op.
357 */
358 ftrace_trace_function = ftrace_ops_list_func;
359 /*
360 * Make sure all CPUs see this. Yes this is slow, but static
361 * tracing is slow and nasty to have enabled.
362 */
363 schedule_on_each_cpu(ftrace_sync);
364 /* Now all cpus are using the list ops. */
365 function_trace_op = set_function_trace_op;
366 /* Make sure the function_trace_op is visible on all CPUs */
367 smp_wmb();
368 /* Nasty way to force a rmb on all cpus */
369 smp_call_function(ftrace_sync_ipi, NULL, 1);
370 /* OK, we are all set to update the ftrace_trace_function now! */
371#endif /* !CONFIG_DYNAMIC_FTRACE */
372
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400373 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400374}
375
Steven Rostedt2b499382011-05-03 22:49:52 -0400376static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200377{
Steven Rostedt2b499382011-05-03 22:49:52 -0400378 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200379 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400380 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200381 * CPU might be walking that list. We need to make sure
382 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400383 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200384 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400385 rcu_assign_pointer(*list, ops);
386}
Steven Rostedt3d083392008-05-12 21:20:42 +0200387
Steven Rostedt2b499382011-05-03 22:49:52 -0400388static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
389{
390 struct ftrace_ops **p;
391
392 /*
393 * If we are removing the last function, then simply point
394 * to the ftrace_stub.
395 */
396 if (*list == ops && ops->next == &ftrace_list_end) {
397 *list = &ftrace_list_end;
398 return 0;
399 }
400
401 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
402 if (*p == ops)
403 break;
404
405 if (*p != ops)
406 return -1;
407
408 *p = (*p)->next;
409 return 0;
410}
411
Jiri Olsae2484912012-02-15 15:51:48 +0100412static void add_ftrace_list_ops(struct ftrace_ops **list,
413 struct ftrace_ops *main_ops,
414 struct ftrace_ops *ops)
415{
416 int first = *list == &ftrace_list_end;
417 add_ftrace_ops(list, ops);
418 if (first)
419 add_ftrace_ops(&ftrace_ops_list, main_ops);
420}
421
422static int remove_ftrace_list_ops(struct ftrace_ops **list,
423 struct ftrace_ops *main_ops,
424 struct ftrace_ops *ops)
425{
426 int ret = remove_ftrace_ops(list, ops);
427 if (!ret && *list == &ftrace_list_end)
428 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
429 return ret;
430}
431
Steven Rostedt2b499382011-05-03 22:49:52 -0400432static int __register_ftrace_function(struct ftrace_ops *ops)
433{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500434 if (ops->flags & FTRACE_OPS_FL_DELETED)
435 return -EINVAL;
436
Steven Rostedt2b499382011-05-03 22:49:52 -0400437 if (FTRACE_WARN_ON(ops == &global_ops))
438 return -EINVAL;
439
Steven Rostedtb8489142011-05-04 09:27:52 -0400440 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
441 return -EBUSY;
442
Jiri Olsae2484912012-02-15 15:51:48 +0100443 /* We don't support both control and global flags set. */
444 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
445 return -EINVAL;
446
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900447#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400448 /*
449 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
450 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
451 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
452 */
453 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
454 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
455 return -EINVAL;
456
457 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
458 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
459#endif
460
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400461 if (!core_kernel_data((unsigned long)ops))
462 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
463
Steven Rostedtb8489142011-05-04 09:27:52 -0400464 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100465 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400466 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100467 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
468 if (control_ops_alloc(ops))
469 return -ENOMEM;
470 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400471 } else
472 add_ftrace_ops(&ftrace_ops_list, ops);
473
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400474 if (ftrace_enabled)
475 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200476
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200477 return 0;
478}
479
Ingo Molnare309b412008-05-12 21:20:51 +0200480static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200481{
Steven Rostedt2b499382011-05-03 22:49:52 -0400482 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200483
Steven Rostedtb8489142011-05-04 09:27:52 -0400484 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
485 return -EBUSY;
486
Steven Rostedt2b499382011-05-03 22:49:52 -0400487 if (FTRACE_WARN_ON(ops == &global_ops))
488 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200489
Steven Rostedtb8489142011-05-04 09:27:52 -0400490 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100491 ret = remove_ftrace_list_ops(&ftrace_global_list,
492 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400493 if (!ret)
494 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100495 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
496 ret = remove_ftrace_list_ops(&ftrace_control_list,
497 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400498 } else
499 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
500
Steven Rostedt2b499382011-05-03 22:49:52 -0400501 if (ret < 0)
502 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400503
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400504 if (ftrace_enabled)
505 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200506
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500507 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200508}
509
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500510static void ftrace_update_pid_func(void)
511{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400512 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500513 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900514 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500515
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400516 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500517}
518
Steven Rostedt493762f2009-03-23 17:12:36 -0400519#ifdef CONFIG_FUNCTION_PROFILER
520struct ftrace_profile {
521 struct hlist_node node;
522 unsigned long ip;
523 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400524#ifdef CONFIG_FUNCTION_GRAPH_TRACER
525 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400526 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400527#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400528};
529
530struct ftrace_profile_page {
531 struct ftrace_profile_page *next;
532 unsigned long index;
533 struct ftrace_profile records[];
534};
535
Steven Rostedtcafb1682009-03-24 20:50:39 -0400536struct ftrace_profile_stat {
537 atomic_t disabled;
538 struct hlist_head *hash;
539 struct ftrace_profile_page *pages;
540 struct ftrace_profile_page *start;
541 struct tracer_stat stat;
542};
543
Steven Rostedt493762f2009-03-23 17:12:36 -0400544#define PROFILE_RECORDS_SIZE \
545 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
546
547#define PROFILES_PER_PAGE \
548 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
549
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400550static int ftrace_profile_enabled __read_mostly;
551
552/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400553static DEFINE_MUTEX(ftrace_profile_lock);
554
Steven Rostedtcafb1682009-03-24 20:50:39 -0400555static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400556
Namhyung Kim20079eb2013-04-10 08:55:50 +0900557#define FTRACE_PROFILE_HASH_BITS 10
558#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400559
Steven Rostedt493762f2009-03-23 17:12:36 -0400560static void *
561function_stat_next(void *v, int idx)
562{
563 struct ftrace_profile *rec = v;
564 struct ftrace_profile_page *pg;
565
566 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
567
568 again:
Li Zefan0296e422009-06-26 11:15:37 +0800569 if (idx != 0)
570 rec++;
571
Steven Rostedt493762f2009-03-23 17:12:36 -0400572 if ((void *)rec >= (void *)&pg->records[pg->index]) {
573 pg = pg->next;
574 if (!pg)
575 return NULL;
576 rec = &pg->records[0];
577 if (!rec->counter)
578 goto again;
579 }
580
581 return rec;
582}
583
584static void *function_stat_start(struct tracer_stat *trace)
585{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400586 struct ftrace_profile_stat *stat =
587 container_of(trace, struct ftrace_profile_stat, stat);
588
589 if (!stat || !stat->start)
590 return NULL;
591
592 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400593}
594
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400595#ifdef CONFIG_FUNCTION_GRAPH_TRACER
596/* function graph compares on total time */
597static int function_stat_cmp(void *p1, void *p2)
598{
599 struct ftrace_profile *a = p1;
600 struct ftrace_profile *b = p2;
601
602 if (a->time < b->time)
603 return -1;
604 if (a->time > b->time)
605 return 1;
606 else
607 return 0;
608}
609#else
610/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400611static int function_stat_cmp(void *p1, void *p2)
612{
613 struct ftrace_profile *a = p1;
614 struct ftrace_profile *b = p2;
615
616 if (a->counter < b->counter)
617 return -1;
618 if (a->counter > b->counter)
619 return 1;
620 else
621 return 0;
622}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400623#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400624
625static int function_stat_headers(struct seq_file *m)
626{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400627#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400628 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400629 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400630 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400631 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400632#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400633 seq_printf(m, " Function Hit\n"
634 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400635#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400636 return 0;
637}
638
639static int function_stat_show(struct seq_file *m, void *v)
640{
641 struct ftrace_profile *rec = v;
642 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800643 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400644#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400645 static struct trace_seq s;
646 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400647 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400648#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800649 mutex_lock(&ftrace_profile_lock);
650
651 /* we raced with function_profile_reset() */
652 if (unlikely(rec->counter == 0)) {
653 ret = -EBUSY;
654 goto out;
655 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400656
657 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400658 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400659
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400660#ifdef CONFIG_FUNCTION_GRAPH_TRACER
661 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400662 avg = rec->time;
663 do_div(avg, rec->counter);
664
Chase Douglase330b3b2010-04-26 14:02:05 -0400665 /* Sample standard deviation (s^2) */
666 if (rec->counter <= 1)
667 stddev = 0;
668 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200669 /*
670 * Apply Welford's method:
671 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
672 */
673 stddev = rec->counter * rec->time_squared -
674 rec->time * rec->time;
675
Chase Douglase330b3b2010-04-26 14:02:05 -0400676 /*
677 * Divide only 1000 for ns^2 -> us^2 conversion.
678 * trace_print_graph_duration will divide 1000 again.
679 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200680 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400681 }
682
Steven Rostedt34886c82009-03-25 21:00:47 -0400683 trace_seq_init(&s);
684 trace_print_graph_duration(rec->time, &s);
685 trace_seq_puts(&s, " ");
686 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400687 trace_seq_puts(&s, " ");
688 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400689 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400690#endif
691 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800692out:
693 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400694
Li Zefan3aaba202010-08-23 16:50:12 +0800695 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400696}
697
Steven Rostedtcafb1682009-03-24 20:50:39 -0400698static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400699{
700 struct ftrace_profile_page *pg;
701
Steven Rostedtcafb1682009-03-24 20:50:39 -0400702 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400703
704 while (pg) {
705 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
706 pg->index = 0;
707 pg = pg->next;
708 }
709
Steven Rostedtcafb1682009-03-24 20:50:39 -0400710 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400711 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
712}
713
Steven Rostedtcafb1682009-03-24 20:50:39 -0400714int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400715{
716 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400717 int functions;
718 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400719 int i;
720
721 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400722 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400723 return 0;
724
Steven Rostedtcafb1682009-03-24 20:50:39 -0400725 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
726 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400727 return -ENOMEM;
728
Steven Rostedt318e0a72009-03-25 20:06:34 -0400729#ifdef CONFIG_DYNAMIC_FTRACE
730 functions = ftrace_update_tot_cnt;
731#else
732 /*
733 * We do not know the number of functions that exist because
734 * dynamic tracing is what counts them. With past experience
735 * we have around 20K functions. That should be more than enough.
736 * It is highly unlikely we will execute every function in
737 * the kernel.
738 */
739 functions = 20000;
740#endif
741
Steven Rostedtcafb1682009-03-24 20:50:39 -0400742 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400743
Steven Rostedt318e0a72009-03-25 20:06:34 -0400744 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
745
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900746 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400747 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400748 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400749 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400750 pg = pg->next;
751 }
752
753 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400754
755 out_free:
756 pg = stat->start;
757 while (pg) {
758 unsigned long tmp = (unsigned long)pg;
759
760 pg = pg->next;
761 free_page(tmp);
762 }
763
Steven Rostedt318e0a72009-03-25 20:06:34 -0400764 stat->pages = NULL;
765 stat->start = NULL;
766
767 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400768}
769
Steven Rostedtcafb1682009-03-24 20:50:39 -0400770static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400771{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400772 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400773 int size;
774
Steven Rostedtcafb1682009-03-24 20:50:39 -0400775 stat = &per_cpu(ftrace_profile_stats, cpu);
776
777 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400778 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400779 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400780 return 0;
781 }
782
783 /*
784 * We are profiling all functions, but usually only a few thousand
785 * functions are hit. We'll make a hash of 1024 items.
786 */
787 size = FTRACE_PROFILE_HASH_SIZE;
788
Steven Rostedtcafb1682009-03-24 20:50:39 -0400789 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400790
Steven Rostedtcafb1682009-03-24 20:50:39 -0400791 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400792 return -ENOMEM;
793
Steven Rostedt318e0a72009-03-25 20:06:34 -0400794 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400795 if (ftrace_profile_pages_init(stat) < 0) {
796 kfree(stat->hash);
797 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400798 return -ENOMEM;
799 }
800
801 return 0;
802}
803
Steven Rostedtcafb1682009-03-24 20:50:39 -0400804static int ftrace_profile_init(void)
805{
806 int cpu;
807 int ret = 0;
808
Miao Xiec4602c12013-12-16 15:20:01 +0800809 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400810 ret = ftrace_profile_init_cpu(cpu);
811 if (ret)
812 break;
813 }
814
815 return ret;
816}
817
Steven Rostedt493762f2009-03-23 17:12:36 -0400818/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819static struct ftrace_profile *
820ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400821{
822 struct ftrace_profile *rec;
823 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400824 unsigned long key;
825
Namhyung Kim20079eb2013-04-10 08:55:50 +0900826 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400827 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400828
829 if (hlist_empty(hhd))
830 return NULL;
831
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400832 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400833 if (rec->ip == ip)
834 return rec;
835 }
836
837 return NULL;
838}
839
Steven Rostedtcafb1682009-03-24 20:50:39 -0400840static void ftrace_add_profile(struct ftrace_profile_stat *stat,
841 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400842{
843 unsigned long key;
844
Namhyung Kim20079eb2013-04-10 08:55:50 +0900845 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400846 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400847}
848
Steven Rostedt318e0a72009-03-25 20:06:34 -0400849/*
850 * The memory is already allocated, this simply finds a new record to use.
851 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400852static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400853ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400854{
855 struct ftrace_profile *rec = NULL;
856
Steven Rostedt318e0a72009-03-25 20:06:34 -0400857 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400858 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400859 goto out;
860
Steven Rostedt493762f2009-03-23 17:12:36 -0400861 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400862 * Try to find the function again since an NMI
863 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400864 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400865 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400866 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400867 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400868
Steven Rostedtcafb1682009-03-24 20:50:39 -0400869 if (stat->pages->index == PROFILES_PER_PAGE) {
870 if (!stat->pages->next)
871 goto out;
872 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400873 }
874
Steven Rostedtcafb1682009-03-24 20:50:39 -0400875 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400876 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400877 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400878
Steven Rostedt493762f2009-03-23 17:12:36 -0400879 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400880 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400881
882 return rec;
883}
884
Steven Rostedt493762f2009-03-23 17:12:36 -0400885static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400886function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400887 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400888{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400889 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400890 struct ftrace_profile *rec;
891 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400892
893 if (!ftrace_profile_enabled)
894 return;
895
Steven Rostedt493762f2009-03-23 17:12:36 -0400896 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400897
898 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400899 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400900 goto out;
901
902 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400903 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400904 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400905 if (!rec)
906 goto out;
907 }
908
909 rec->counter++;
910 out:
911 local_irq_restore(flags);
912}
913
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400914#ifdef CONFIG_FUNCTION_GRAPH_TRACER
915static int profile_graph_entry(struct ftrace_graph_ent *trace)
916{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400917 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400918 return 1;
919}
920
921static void profile_graph_return(struct ftrace_graph_ret *trace)
922{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400923 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400924 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400925 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400926 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400927
928 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400929 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400930 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400931 goto out;
932
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400933 /* If the calltime was zero'd ignore it */
934 if (!trace->calltime)
935 goto out;
936
Steven Rostedta2a16d62009-03-24 23:17:58 -0400937 calltime = trace->rettime - trace->calltime;
938
939 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
940 int index;
941
942 index = trace->depth;
943
944 /* Append this call time to the parent time to subtract */
945 if (index)
946 current->ret_stack[index - 1].subtime += calltime;
947
948 if (current->ret_stack[index].subtime < calltime)
949 calltime -= current->ret_stack[index].subtime;
950 else
951 calltime = 0;
952 }
953
Steven Rostedtcafb1682009-03-24 20:50:39 -0400954 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400955 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400956 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400957 rec->time_squared += calltime * calltime;
958 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400959
Steven Rostedtcafb1682009-03-24 20:50:39 -0400960 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400961 local_irq_restore(flags);
962}
963
964static int register_ftrace_profiler(void)
965{
966 return register_ftrace_graph(&profile_graph_return,
967 &profile_graph_entry);
968}
969
970static void unregister_ftrace_profiler(void)
971{
972 unregister_ftrace_graph();
973}
974#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100975static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400976 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900977 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
978 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400979};
980
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400981static int register_ftrace_profiler(void)
982{
983 return register_ftrace_function(&ftrace_profile_ops);
984}
985
986static void unregister_ftrace_profiler(void)
987{
988 unregister_ftrace_function(&ftrace_profile_ops);
989}
990#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
991
Steven Rostedt493762f2009-03-23 17:12:36 -0400992static ssize_t
993ftrace_profile_write(struct file *filp, const char __user *ubuf,
994 size_t cnt, loff_t *ppos)
995{
996 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400997 int ret;
998
Peter Huewe22fe9b52011-06-07 21:58:27 +0200999 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1000 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -04001001 return ret;
1002
1003 val = !!val;
1004
1005 mutex_lock(&ftrace_profile_lock);
1006 if (ftrace_profile_enabled ^ val) {
1007 if (val) {
1008 ret = ftrace_profile_init();
1009 if (ret < 0) {
1010 cnt = ret;
1011 goto out;
1012 }
1013
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001014 ret = register_ftrace_profiler();
1015 if (ret < 0) {
1016 cnt = ret;
1017 goto out;
1018 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001019 ftrace_profile_enabled = 1;
1020 } else {
1021 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -04001022 /*
1023 * unregister_ftrace_profiler calls stop_machine
1024 * so this acts like an synchronize_sched.
1025 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001026 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -04001027 }
1028 }
1029 out:
1030 mutex_unlock(&ftrace_profile_lock);
1031
Jiri Olsacf8517c2009-10-23 19:36:16 -04001032 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001033
1034 return cnt;
1035}
1036
1037static ssize_t
1038ftrace_profile_read(struct file *filp, char __user *ubuf,
1039 size_t cnt, loff_t *ppos)
1040{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001041 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001042 int r;
1043
1044 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1045 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1046}
1047
1048static const struct file_operations ftrace_profile_fops = {
1049 .open = tracing_open_generic,
1050 .read = ftrace_profile_read,
1051 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001052 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001053};
1054
Steven Rostedtcafb1682009-03-24 20:50:39 -04001055/* used to initialize the real stat files */
1056static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001057 .name = "functions",
1058 .stat_start = function_stat_start,
1059 .stat_next = function_stat_next,
1060 .stat_cmp = function_stat_cmp,
1061 .stat_headers = function_stat_headers,
1062 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001063};
1064
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001065static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001066{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001067 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001068 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001069 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001070 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001071 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001072
Steven Rostedtcafb1682009-03-24 20:50:39 -04001073 for_each_possible_cpu(cpu) {
1074 stat = &per_cpu(ftrace_profile_stats, cpu);
1075
1076 /* allocate enough for function name + cpu number */
1077 name = kmalloc(32, GFP_KERNEL);
1078 if (!name) {
1079 /*
1080 * The files created are permanent, if something happens
1081 * we still do not free memory.
1082 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001083 WARN(1,
1084 "Could not allocate stat file for cpu %d\n",
1085 cpu);
1086 return;
1087 }
1088 stat->stat = function_stats;
1089 snprintf(name, 32, "function%d", cpu);
1090 stat->stat.name = name;
1091 ret = register_stat_tracer(&stat->stat);
1092 if (ret) {
1093 WARN(1,
1094 "Could not register function stat for cpu %d\n",
1095 cpu);
1096 kfree(name);
1097 return;
1098 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001099 }
1100
1101 entry = debugfs_create_file("function_profile_enabled", 0644,
1102 d_tracer, NULL, &ftrace_profile_fops);
1103 if (!entry)
1104 pr_warning("Could not create debugfs "
1105 "'function_profile_enabled' entry\n");
1106}
1107
1108#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001109static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001110{
1111}
1112#endif /* CONFIG_FUNCTION_PROFILER */
1113
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001114static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1115
Steven Rostedt3d083392008-05-12 21:20:42 +02001116#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001117
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001118#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001119# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001120#endif
1121
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001122static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1123
Steven Rostedtb6887d72009-02-17 12:32:04 -05001124struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001125 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001126 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001127 unsigned long flags;
1128 unsigned long ip;
1129 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001130 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001131};
1132
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001133struct ftrace_func_entry {
1134 struct hlist_node hlist;
1135 unsigned long ip;
1136};
1137
1138struct ftrace_hash {
1139 unsigned long size_bits;
1140 struct hlist_head *buckets;
1141 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001142 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001143};
1144
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001145/*
1146 * We make these constant because no one should touch them,
1147 * but they are used as the default "empty hash", to avoid allocating
1148 * it all the time. These are in a read only section such that if
1149 * anyone does try to modify it, it will cause an exception.
1150 */
1151static const struct hlist_head empty_buckets[1];
1152static const struct ftrace_hash empty_hash = {
1153 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001154};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001155#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001156
Steven Rostedt2b499382011-05-03 22:49:52 -04001157static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001158 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001159 .notrace_hash = EMPTY_HASH,
1160 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001161 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1162 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001163};
1164
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001165struct ftrace_page {
1166 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001167 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001168 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001169 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001170};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001171
Steven Rostedta7900872011-12-16 16:23:44 -05001172#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1173#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001174
1175/* estimate from running different kernels */
1176#define NR_TO_INIT 10000
1177
1178static struct ftrace_page *ftrace_pages_start;
1179static struct ftrace_page *ftrace_pages;
1180
Steven Rostedt06a51d92011-12-19 19:07:36 -05001181static bool ftrace_hash_empty(struct ftrace_hash *hash)
1182{
1183 return !hash || !hash->count;
1184}
1185
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001186static struct ftrace_func_entry *
1187ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1188{
1189 unsigned long key;
1190 struct ftrace_func_entry *entry;
1191 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001192
Steven Rostedt06a51d92011-12-19 19:07:36 -05001193 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001194 return NULL;
1195
1196 if (hash->size_bits > 0)
1197 key = hash_long(ip, hash->size_bits);
1198 else
1199 key = 0;
1200
1201 hhd = &hash->buckets[key];
1202
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001203 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001204 if (entry->ip == ip)
1205 return entry;
1206 }
1207 return NULL;
1208}
1209
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001210static void __add_hash_entry(struct ftrace_hash *hash,
1211 struct ftrace_func_entry *entry)
1212{
1213 struct hlist_head *hhd;
1214 unsigned long key;
1215
1216 if (hash->size_bits)
1217 key = hash_long(entry->ip, hash->size_bits);
1218 else
1219 key = 0;
1220
1221 hhd = &hash->buckets[key];
1222 hlist_add_head(&entry->hlist, hhd);
1223 hash->count++;
1224}
1225
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001226static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1227{
1228 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001229
1230 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1231 if (!entry)
1232 return -ENOMEM;
1233
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001234 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001235 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001236
1237 return 0;
1238}
1239
1240static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001241free_hash_entry(struct ftrace_hash *hash,
1242 struct ftrace_func_entry *entry)
1243{
1244 hlist_del(&entry->hlist);
1245 kfree(entry);
1246 hash->count--;
1247}
1248
1249static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001250remove_hash_entry(struct ftrace_hash *hash,
1251 struct ftrace_func_entry *entry)
1252{
1253 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001254 hash->count--;
1255}
1256
1257static void ftrace_hash_clear(struct ftrace_hash *hash)
1258{
1259 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001260 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001261 struct ftrace_func_entry *entry;
1262 int size = 1 << hash->size_bits;
1263 int i;
1264
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001265 if (!hash->count)
1266 return;
1267
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001268 for (i = 0; i < size; i++) {
1269 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001270 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001271 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001272 }
1273 FTRACE_WARN_ON(hash->count);
1274}
1275
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001276static void free_ftrace_hash(struct ftrace_hash *hash)
1277{
1278 if (!hash || hash == EMPTY_HASH)
1279 return;
1280 ftrace_hash_clear(hash);
1281 kfree(hash->buckets);
1282 kfree(hash);
1283}
1284
Steven Rostedt07fd5512011-05-05 18:03:47 -04001285static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1286{
1287 struct ftrace_hash *hash;
1288
1289 hash = container_of(rcu, struct ftrace_hash, rcu);
1290 free_ftrace_hash(hash);
1291}
1292
1293static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1294{
1295 if (!hash || hash == EMPTY_HASH)
1296 return;
1297 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1298}
1299
Jiri Olsa5500fa52012-02-15 15:51:54 +01001300void ftrace_free_filter(struct ftrace_ops *ops)
1301{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001302 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001303 free_ftrace_hash(ops->filter_hash);
1304 free_ftrace_hash(ops->notrace_hash);
1305}
1306
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001307static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1308{
1309 struct ftrace_hash *hash;
1310 int size;
1311
1312 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1313 if (!hash)
1314 return NULL;
1315
1316 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001317 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001318
1319 if (!hash->buckets) {
1320 kfree(hash);
1321 return NULL;
1322 }
1323
1324 hash->size_bits = size_bits;
1325
1326 return hash;
1327}
1328
1329static struct ftrace_hash *
1330alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1331{
1332 struct ftrace_func_entry *entry;
1333 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001334 int size;
1335 int ret;
1336 int i;
1337
1338 new_hash = alloc_ftrace_hash(size_bits);
1339 if (!new_hash)
1340 return NULL;
1341
1342 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001343 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001344 return new_hash;
1345
1346 size = 1 << hash->size_bits;
1347 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001348 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001349 ret = add_hash_entry(new_hash, entry->ip);
1350 if (ret < 0)
1351 goto free_hash;
1352 }
1353 }
1354
1355 FTRACE_WARN_ON(new_hash->count != hash->count);
1356
1357 return new_hash;
1358
1359 free_hash:
1360 free_ftrace_hash(new_hash);
1361 return NULL;
1362}
1363
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001364static void
1365ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1366static void
1367ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1368
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001369static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001370ftrace_hash_move(struct ftrace_ops *ops, int enable,
1371 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001372{
1373 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001374 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001375 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001376 struct ftrace_hash *old_hash;
1377 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001378 int size = src->count;
1379 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001380 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001381 int i;
1382
1383 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001384 * Remove the current set, update the hash and add
1385 * them back.
1386 */
1387 ftrace_hash_rec_disable(ops, enable);
1388
1389 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001390 * If the new source is empty, just free dst and assign it
1391 * the empty_hash.
1392 */
1393 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001394 free_ftrace_hash_rcu(*dst);
1395 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b982011-11-04 20:32:39 -04001396 /* still need to update the function records */
1397 ret = 0;
1398 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001399 }
1400
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001401 /*
1402 * Make the hash size about 1/2 the # found
1403 */
1404 for (size /= 2; size; size >>= 1)
1405 bits++;
1406
1407 /* Don't allocate too much */
1408 if (bits > FTRACE_HASH_MAX_BITS)
1409 bits = FTRACE_HASH_MAX_BITS;
1410
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001411 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001412 new_hash = alloc_ftrace_hash(bits);
1413 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001414 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001415
1416 size = 1 << src->size_bits;
1417 for (i = 0; i < size; i++) {
1418 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001419 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001420 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001421 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001422 }
1423 }
1424
Steven Rostedt07fd5512011-05-05 18:03:47 -04001425 old_hash = *dst;
1426 rcu_assign_pointer(*dst, new_hash);
1427 free_ftrace_hash_rcu(old_hash);
1428
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001429 ret = 0;
1430 out:
1431 /*
1432 * Enable regardless of ret:
1433 * On success, we enable the new hash.
1434 * On failure, we re-enable the original hash.
1435 */
1436 ftrace_hash_rec_enable(ops, enable);
1437
1438 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001439}
1440
Steven Rostedt265c8312009-02-13 12:43:56 -05001441/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001442 * Test the hashes for this ops to see if we want to call
1443 * the ops->func or not.
1444 *
1445 * It's a match if the ip is in the ops->filter_hash or
1446 * the filter_hash does not exist or is empty,
1447 * AND
1448 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001449 *
1450 * This needs to be called with preemption disabled as
1451 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001452 */
1453static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001454ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001455{
1456 struct ftrace_hash *filter_hash;
1457 struct ftrace_hash *notrace_hash;
1458 int ret;
1459
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001460#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1461 /*
1462 * There's a small race when adding ops that the ftrace handler
1463 * that wants regs, may be called without them. We can not
1464 * allow that handler to be called if regs is NULL.
1465 */
1466 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1467 return 0;
1468#endif
1469
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001470 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1471 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001472
Steven Rostedt06a51d92011-12-19 19:07:36 -05001473 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001474 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001475 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001476 !ftrace_lookup_ip(notrace_hash, ip)))
1477 ret = 1;
1478 else
1479 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001480
1481 return ret;
1482}
1483
1484/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001485 * This is a double for. Do not use 'break' to break out of the loop,
1486 * you must use a goto.
1487 */
1488#define do_for_each_ftrace_rec(pg, rec) \
1489 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1490 int _____i; \
1491 for (_____i = 0; _____i < pg->index; _____i++) { \
1492 rec = &pg->records[_____i];
1493
1494#define while_for_each_ftrace_rec() \
1495 } \
1496 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301497
Steven Rostedt5855fea2011-12-16 19:27:42 -05001498
1499static int ftrace_cmp_recs(const void *a, const void *b)
1500{
Steven Rostedta650e022012-04-25 13:48:13 -04001501 const struct dyn_ftrace *key = a;
1502 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001503
Steven Rostedta650e022012-04-25 13:48:13 -04001504 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001505 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001506 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1507 return 1;
1508 return 0;
1509}
1510
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001511static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001512{
1513 struct ftrace_page *pg;
1514 struct dyn_ftrace *rec;
1515 struct dyn_ftrace key;
1516
1517 key.ip = start;
1518 key.flags = end; /* overload flags, as it is unsigned long */
1519
1520 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1521 if (end < pg->records[0].ip ||
1522 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1523 continue;
1524 rec = bsearch(&key, pg->records, pg->index,
1525 sizeof(struct dyn_ftrace),
1526 ftrace_cmp_recs);
1527 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001528 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001529 }
1530
Steven Rostedt5855fea2011-12-16 19:27:42 -05001531 return 0;
1532}
1533
Steven Rostedtc88fd862011-08-16 09:53:39 -04001534/**
1535 * ftrace_location - return true if the ip giving is a traced location
1536 * @ip: the instruction pointer to check
1537 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001538 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001539 * That is, the instruction that is either a NOP or call to
1540 * the function tracer. It checks the ftrace internal tables to
1541 * determine if the address belongs or not.
1542 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001543unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001544{
Steven Rostedta650e022012-04-25 13:48:13 -04001545 return ftrace_location_range(ip, ip);
1546}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001547
Steven Rostedta650e022012-04-25 13:48:13 -04001548/**
1549 * ftrace_text_reserved - return true if range contains an ftrace location
1550 * @start: start of range to search
1551 * @end: end of range to search (inclusive). @end points to the last byte to check.
1552 *
1553 * Returns 1 if @start and @end contains a ftrace location.
1554 * That is, the instruction that is either a NOP or call to
1555 * the function tracer. It checks the ftrace internal tables to
1556 * determine if the address belongs or not.
1557 */
1558int ftrace_text_reserved(void *start, void *end)
1559{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001560 unsigned long ret;
1561
1562 ret = ftrace_location_range((unsigned long)start,
1563 (unsigned long)end);
1564
1565 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001566}
1567
Steven Rostedted926f92011-05-03 13:25:24 -04001568static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1569 int filter_hash,
1570 bool inc)
1571{
1572 struct ftrace_hash *hash;
1573 struct ftrace_hash *other_hash;
1574 struct ftrace_page *pg;
1575 struct dyn_ftrace *rec;
1576 int count = 0;
1577 int all = 0;
1578
1579 /* Only update if the ops has been registered */
1580 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1581 return;
1582
1583 /*
1584 * In the filter_hash case:
1585 * If the count is zero, we update all records.
1586 * Otherwise we just update the items in the hash.
1587 *
1588 * In the notrace_hash case:
1589 * We enable the update in the hash.
1590 * As disabling notrace means enabling the tracing,
1591 * and enabling notrace means disabling, the inc variable
1592 * gets inversed.
1593 */
1594 if (filter_hash) {
1595 hash = ops->filter_hash;
1596 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001597 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001598 all = 1;
1599 } else {
1600 inc = !inc;
1601 hash = ops->notrace_hash;
1602 other_hash = ops->filter_hash;
1603 /*
1604 * If the notrace hash has no items,
1605 * then there's nothing to do.
1606 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001607 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001608 return;
1609 }
1610
1611 do_for_each_ftrace_rec(pg, rec) {
1612 int in_other_hash = 0;
1613 int in_hash = 0;
1614 int match = 0;
1615
1616 if (all) {
1617 /*
1618 * Only the filter_hash affects all records.
1619 * Update if the record is not in the notrace hash.
1620 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001621 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001622 match = 1;
1623 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001624 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1625 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001626
1627 /*
1628 *
1629 */
1630 if (filter_hash && in_hash && !in_other_hash)
1631 match = 1;
1632 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001633 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001634 match = 1;
1635 }
1636 if (!match)
1637 continue;
1638
1639 if (inc) {
1640 rec->flags++;
1641 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1642 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001643 /*
1644 * If any ops wants regs saved for this function
1645 * then all ops will get saved regs.
1646 */
1647 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1648 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001649 } else {
1650 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1651 return;
1652 rec->flags--;
1653 }
1654 count++;
1655 /* Shortcut, if we handled all records, we are done. */
1656 if (!all && count == hash->count)
1657 return;
1658 } while_for_each_ftrace_rec();
1659}
1660
1661static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1662 int filter_hash)
1663{
1664 __ftrace_hash_rec_update(ops, filter_hash, 0);
1665}
1666
1667static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1668 int filter_hash)
1669{
1670 __ftrace_hash_rec_update(ops, filter_hash, 1);
1671}
1672
Steven Rostedt05736a42008-09-22 14:55:47 -07001673static void print_ip_ins(const char *fmt, unsigned char *p)
1674{
1675 int i;
1676
1677 printk(KERN_CONT "%s", fmt);
1678
1679 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1680 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1681}
1682
Steven Rostedtc88fd862011-08-16 09:53:39 -04001683/**
1684 * ftrace_bug - report and shutdown function tracer
1685 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1686 * @ip: The address that failed
1687 *
1688 * The arch code that enables or disables the function tracing
1689 * can call ftrace_bug() when it has detected a problem in
1690 * modifying the code. @failed should be one of either:
1691 * EFAULT - if the problem happens on reading the @ip address
1692 * EINVAL - if what is read at @ip is not what was expected
1693 * EPERM - if the problem happens on writting to the @ip address
1694 */
1695void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001696{
1697 switch (failed) {
1698 case -EFAULT:
1699 FTRACE_WARN_ON_ONCE(1);
1700 pr_info("ftrace faulted on modifying ");
1701 print_ip_sym(ip);
1702 break;
1703 case -EINVAL:
1704 FTRACE_WARN_ON_ONCE(1);
1705 pr_info("ftrace failed to modify ");
1706 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001707 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001708 printk(KERN_CONT "\n");
1709 break;
1710 case -EPERM:
1711 FTRACE_WARN_ON_ONCE(1);
1712 pr_info("ftrace faulted on writing ");
1713 print_ip_sym(ip);
1714 break;
1715 default:
1716 FTRACE_WARN_ON_ONCE(1);
1717 pr_info("ftrace faulted on unknown error ");
1718 print_ip_sym(ip);
1719 }
1720}
1721
Steven Rostedtc88fd862011-08-16 09:53:39 -04001722static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001723{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001724 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001725
Steven Rostedt982c3502008-11-15 16:31:41 -05001726 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001727 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001728 *
Steven Rostedted926f92011-05-03 13:25:24 -04001729 * If the record has a ref count, then we need to enable it
1730 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001731 *
Steven Rostedted926f92011-05-03 13:25:24 -04001732 * Otherwise we make sure its disabled.
1733 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001734 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001735 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001736 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001737 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001738 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001739
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001740 /*
1741 * If enabling and the REGS flag does not match the REGS_EN, then
1742 * do not ignore this record. Set flags to fail the compare against
1743 * ENABLED.
1744 */
1745 if (flag &&
1746 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1747 flag |= FTRACE_FL_REGS;
1748
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001749 /* If the state of this record hasn't changed, then do nothing */
1750 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001751 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001752
1753 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001754 /* Save off if rec is being enabled (for return value) */
1755 flag ^= rec->flags & FTRACE_FL_ENABLED;
1756
1757 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001758 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001759 if (flag & FTRACE_FL_REGS) {
1760 if (rec->flags & FTRACE_FL_REGS)
1761 rec->flags |= FTRACE_FL_REGS_EN;
1762 else
1763 rec->flags &= ~FTRACE_FL_REGS_EN;
1764 }
1765 }
1766
1767 /*
1768 * If this record is being updated from a nop, then
1769 * return UPDATE_MAKE_CALL.
1770 * Otherwise, if the EN flag is set, then return
1771 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1772 * from the non-save regs, to a save regs function.
1773 * Otherwise,
1774 * return UPDATE_MODIFY_CALL to tell the caller to convert
1775 * from the save regs, to a non-save regs function.
1776 */
1777 if (flag & FTRACE_FL_ENABLED)
1778 return FTRACE_UPDATE_MAKE_CALL;
1779 else if (rec->flags & FTRACE_FL_REGS_EN)
1780 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1781 else
1782 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001783 }
1784
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001785 if (update) {
1786 /* If there's no more users, clear all flags */
1787 if (!(rec->flags & ~FTRACE_FL_MASK))
1788 rec->flags = 0;
1789 else
1790 /* Just disable the record (keep REGS state) */
1791 rec->flags &= ~FTRACE_FL_ENABLED;
1792 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001793
1794 return FTRACE_UPDATE_MAKE_NOP;
1795}
1796
1797/**
1798 * ftrace_update_record, set a record that now is tracing or not
1799 * @rec: the record to update
1800 * @enable: set to 1 if the record is tracing, zero to force disable
1801 *
1802 * The records that represent all functions that can be traced need
1803 * to be updated when tracing has been enabled.
1804 */
1805int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1806{
1807 return ftrace_check_record(rec, enable, 1);
1808}
1809
1810/**
1811 * ftrace_test_record, check if the record has been enabled or not
1812 * @rec: the record to test
1813 * @enable: set to 1 to check if enabled, 0 if it is disabled
1814 *
1815 * The arch code may need to test if a record is already set to
1816 * tracing to determine how to modify the function code that it
1817 * represents.
1818 */
1819int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1820{
1821 return ftrace_check_record(rec, enable, 0);
1822}
1823
1824static int
1825__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1826{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001827 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001828 unsigned long ftrace_addr;
1829 int ret;
1830
Steven Rostedtc88fd862011-08-16 09:53:39 -04001831 ret = ftrace_update_record(rec, enable);
1832
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001833 if (rec->flags & FTRACE_FL_REGS)
1834 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1835 else
1836 ftrace_addr = (unsigned long)FTRACE_ADDR;
1837
Steven Rostedtc88fd862011-08-16 09:53:39 -04001838 switch (ret) {
1839 case FTRACE_UPDATE_IGNORE:
1840 return 0;
1841
1842 case FTRACE_UPDATE_MAKE_CALL:
1843 return ftrace_make_call(rec, ftrace_addr);
1844
1845 case FTRACE_UPDATE_MAKE_NOP:
1846 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001847
1848 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1849 case FTRACE_UPDATE_MODIFY_CALL:
1850 if (rec->flags & FTRACE_FL_REGS)
1851 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1852 else
1853 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1854
1855 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001856 }
1857
1858 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001859}
1860
Steven Rostedte4f5d542012-04-27 09:13:18 -04001861void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001862{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001863 struct dyn_ftrace *rec;
1864 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001865 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001866
Steven Rostedt45a4a232011-04-21 23:16:46 -04001867 if (unlikely(ftrace_disabled))
1868 return;
1869
Steven Rostedt265c8312009-02-13 12:43:56 -05001870 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001871 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001872 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001873 ftrace_bug(failed, rec->ip);
1874 /* Stop processing */
1875 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001876 }
1877 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001878}
1879
Steven Rostedtc88fd862011-08-16 09:53:39 -04001880struct ftrace_rec_iter {
1881 struct ftrace_page *pg;
1882 int index;
1883};
1884
1885/**
1886 * ftrace_rec_iter_start, start up iterating over traced functions
1887 *
1888 * Returns an iterator handle that is used to iterate over all
1889 * the records that represent address locations where functions
1890 * are traced.
1891 *
1892 * May return NULL if no records are available.
1893 */
1894struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1895{
1896 /*
1897 * We only use a single iterator.
1898 * Protected by the ftrace_lock mutex.
1899 */
1900 static struct ftrace_rec_iter ftrace_rec_iter;
1901 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1902
1903 iter->pg = ftrace_pages_start;
1904 iter->index = 0;
1905
1906 /* Could have empty pages */
1907 while (iter->pg && !iter->pg->index)
1908 iter->pg = iter->pg->next;
1909
1910 if (!iter->pg)
1911 return NULL;
1912
1913 return iter;
1914}
1915
1916/**
1917 * ftrace_rec_iter_next, get the next record to process.
1918 * @iter: The handle to the iterator.
1919 *
1920 * Returns the next iterator after the given iterator @iter.
1921 */
1922struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1923{
1924 iter->index++;
1925
1926 if (iter->index >= iter->pg->index) {
1927 iter->pg = iter->pg->next;
1928 iter->index = 0;
1929
1930 /* Could have empty pages */
1931 while (iter->pg && !iter->pg->index)
1932 iter->pg = iter->pg->next;
1933 }
1934
1935 if (!iter->pg)
1936 return NULL;
1937
1938 return iter;
1939}
1940
1941/**
1942 * ftrace_rec_iter_record, get the record at the iterator location
1943 * @iter: The current iterator location
1944 *
1945 * Returns the record that the current @iter is at.
1946 */
1947struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1948{
1949 return &iter->pg->records[iter->index];
1950}
1951
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301952static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001953ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001954{
1955 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001956 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001957
1958 ip = rec->ip;
1959
Steven Rostedt45a4a232011-04-21 23:16:46 -04001960 if (unlikely(ftrace_disabled))
1961 return 0;
1962
Shaohua Li25aac9d2009-01-09 11:29:40 +08001963 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001964 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001965 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301966 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001967 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301968 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001969}
1970
Steven Rostedt000ab692009-02-17 13:35:06 -05001971/*
1972 * archs can override this function if they must do something
1973 * before the modifying code is performed.
1974 */
1975int __weak ftrace_arch_code_modify_prepare(void)
1976{
1977 return 0;
1978}
1979
1980/*
1981 * archs can override this function if they must do something
1982 * after the modifying code is performed.
1983 */
1984int __weak ftrace_arch_code_modify_post_process(void)
1985{
1986 return 0;
1987}
1988
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001989void ftrace_modify_all_code(int command)
1990{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001991 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01001992 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001993
1994 /*
1995 * If the ftrace_caller calls a ftrace_ops func directly,
1996 * we need to make sure that it only traces functions it
1997 * expects to trace. When doing the switch of functions,
1998 * we need to update to the ftrace_ops_list_func first
1999 * before the transition between old and new calls are set,
2000 * as the ftrace_ops_list_func will check the ops hashes
2001 * to make sure the ops are having the right functions
2002 * traced.
2003 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002004 if (update) {
2005 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2006 if (FTRACE_WARN_ON(err))
2007 return;
2008 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002009
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002010 if (command & FTRACE_UPDATE_CALLS)
2011 ftrace_replace_code(1);
2012 else if (command & FTRACE_DISABLE_CALLS)
2013 ftrace_replace_code(0);
2014
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002015 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2016 function_trace_op = set_function_trace_op;
2017 smp_wmb();
2018 /* If irqs are disabled, we are in stop machine */
2019 if (!irqs_disabled())
2020 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002021 err = ftrace_update_ftrace_func(ftrace_trace_function);
2022 if (FTRACE_WARN_ON(err))
2023 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002024 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002025
2026 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002027 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002028 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002029 err = ftrace_disable_ftrace_graph_caller();
2030 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002031}
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
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002098static void control_ops_free(struct ftrace_ops *ops)
2099{
2100 free_percpu(ops->disabled);
2101}
2102
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002103static void ftrace_startup_enable(int command)
2104{
2105 if (saved_ftrace_func != ftrace_trace_function) {
2106 saved_ftrace_func = ftrace_trace_function;
2107 command |= FTRACE_UPDATE_TRACE_FUNC;
2108 }
2109
2110 if (!command || !ftrace_enabled)
2111 return;
2112
2113 ftrace_run_update_code(command);
2114}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002115
Steven Rostedta1cd6172011-05-23 15:24:25 -04002116static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002117{
Steven Rostedtb8489142011-05-04 09:27:52 -04002118 bool hash_enable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002119 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002120
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002121 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002122 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002123
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002124 ret = __register_ftrace_function(ops);
2125 if (ret)
2126 return ret;
2127
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002128 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002129 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002130
Steven Rostedtb8489142011-05-04 09:27:52 -04002131 /* ops marked global share the filter hashes */
2132 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2133 ops = &global_ops;
2134 /* Don't update hash if global is already set */
2135 if (global_start_up)
2136 hash_enable = false;
2137 global_start_up++;
2138 }
2139
Steven Rostedted926f92011-05-03 13:25:24 -04002140 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002141 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002142 ftrace_hash_rec_enable(ops, 1);
2143
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002144 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002145
2146 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002147}
2148
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002149static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002150{
Steven Rostedtb8489142011-05-04 09:27:52 -04002151 bool hash_disable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002152 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002153
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002154 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002155 return -ENODEV;
2156
2157 ret = __unregister_ftrace_function(ops);
2158 if (ret)
2159 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002160
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002161 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002162 /*
2163 * Just warn in case of unbalance, no need to kill ftrace, it's not
2164 * critical but the ftrace_call callers may be never nopped again after
2165 * further ftrace uses.
2166 */
2167 WARN_ON_ONCE(ftrace_start_up < 0);
2168
Steven Rostedtb8489142011-05-04 09:27:52 -04002169 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2170 ops = &global_ops;
2171 global_start_up--;
2172 WARN_ON_ONCE(global_start_up < 0);
2173 /* Don't update hash if global still has users */
2174 if (global_start_up) {
2175 WARN_ON_ONCE(!ftrace_start_up);
2176 hash_disable = false;
2177 }
2178 }
2179
2180 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002181 ftrace_hash_rec_disable(ops, 1);
2182
Steven Rostedtb8489142011-05-04 09:27:52 -04002183 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002184 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002185
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002186 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002187
2188 if (saved_ftrace_func != ftrace_trace_function) {
2189 saved_ftrace_func = ftrace_trace_function;
2190 command |= FTRACE_UPDATE_TRACE_FUNC;
2191 }
2192
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002193 if (!command || !ftrace_enabled) {
2194 /*
2195 * If these are control ops, they still need their
2196 * per_cpu field freed. Since, function tracing is
2197 * not currently active, we can just free them
2198 * without synchronizing all CPUs.
2199 */
2200 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2201 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002202 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002203 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002204
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002205 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002206
2207 /*
2208 * Dynamic ops may be freed, we must make sure that all
2209 * callers are done before leaving this function.
2210 * The same goes for freeing the per_cpu data of the control
2211 * ops.
2212 *
2213 * Again, normal synchronize_sched() is not good enough.
2214 * We need to do a hard force of sched synchronization.
2215 * This is because we use preempt_disable() to do RCU, but
2216 * the function tracers can be called where RCU is not watching
2217 * (like before user_exit()). We can not rely on the RCU
2218 * infrastructure to do the synchronization, thus we must do it
2219 * ourselves.
2220 */
2221 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2222 schedule_on_each_cpu(ftrace_sync);
2223
2224 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2225 control_ops_free(ops);
2226 }
2227
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002228 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002229}
2230
Ingo Molnare309b412008-05-12 21:20:51 +02002231static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002232{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002233 if (unlikely(ftrace_disabled))
2234 return;
2235
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002236 /* Force update next time */
2237 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002238 /* ftrace_start_up is true if we want ftrace running */
2239 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002240 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002241}
2242
Ingo Molnare309b412008-05-12 21:20:51 +02002243static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002244{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002245 if (unlikely(ftrace_disabled))
2246 return;
2247
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002248 /* ftrace_start_up is true if ftrace is running */
2249 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002250 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002251}
2252
Steven Rostedt3d083392008-05-12 21:20:42 +02002253static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002254unsigned long ftrace_update_tot_cnt;
2255
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002256static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002257{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002258 /*
2259 * Filter_hash being empty will default to trace module.
2260 * But notrace hash requires a test of individual module functions.
2261 */
2262 return ftrace_hash_empty(ops->filter_hash) &&
2263 ftrace_hash_empty(ops->notrace_hash);
2264}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002265
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002266/*
2267 * Check if the current ops references the record.
2268 *
2269 * If the ops traces all functions, then it was already accounted for.
2270 * If the ops does not trace the current record function, skip it.
2271 * If the ops ignores the function via notrace filter, skip it.
2272 */
2273static inline bool
2274ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2275{
2276 /* If ops isn't enabled, ignore it */
2277 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2278 return 0;
2279
2280 /* If ops traces all mods, we already accounted for it */
2281 if (ops_traces_mod(ops))
2282 return 0;
2283
2284 /* The function must be in the filter */
2285 if (!ftrace_hash_empty(ops->filter_hash) &&
2286 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2287 return 0;
2288
2289 /* If in notrace hash, we ignore it too */
2290 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2291 return 0;
2292
2293 return 1;
2294}
2295
2296static int referenced_filters(struct dyn_ftrace *rec)
2297{
2298 struct ftrace_ops *ops;
2299 int cnt = 0;
2300
2301 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2302 if (ops_references_rec(ops, rec))
2303 cnt++;
2304 }
2305
2306 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002307}
2308
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002309static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002310{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002311 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002312 struct dyn_ftrace *p;
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302313 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002314 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002315 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002316 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002317 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002318
2319 /*
2320 * When adding a module, we need to check if tracers are
2321 * currently enabled and if they are set to trace all functions.
2322 * If they are, we need to enable the module functions as well
2323 * as update the reference counts for those function records.
2324 */
2325 if (mod) {
2326 struct ftrace_ops *ops;
2327
2328 for (ops = ftrace_ops_list;
2329 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002330 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2331 if (ops_traces_mod(ops))
2332 ref++;
2333 else
2334 test = true;
2335 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002336 }
2337 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002338
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002339 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002340
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002341 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302342
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002343 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002344 int cnt = ref;
2345
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002346 /* If something went wrong, bail without enabling anything */
2347 if (unlikely(ftrace_disabled))
2348 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002349
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002350 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002351 if (test)
2352 cnt += referenced_filters(p);
2353 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302354
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002355 /*
2356 * Do the initial record conversion from mcount jump
2357 * to the NOP instructions.
2358 */
2359 if (!ftrace_code_disable(mod, p))
2360 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002361
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002362 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002363
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002364 /*
2365 * If the tracing is enabled, go ahead and enable the record.
2366 *
2367 * The reason not to enable the record immediatelly is the
2368 * inherent check of ftrace_make_nop/ftrace_make_call for
2369 * correct previous instructions. Making first the NOP
2370 * conversion puts the module to the correct state, thus
2371 * passing the ftrace_make_call check.
2372 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002373 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002374 int failed = __ftrace_replace_code(p, 1);
2375 if (failed)
2376 ftrace_bug(failed, p->ip);
2377 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002378 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002379 }
2380
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002381 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002382 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002383 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002384
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002385 return 0;
2386}
2387
Steven Rostedta7900872011-12-16 16:23:44 -05002388static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002389{
Steven Rostedta7900872011-12-16 16:23:44 -05002390 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002391 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002392
Steven Rostedta7900872011-12-16 16:23:44 -05002393 if (WARN_ON(!count))
2394 return -EINVAL;
2395
2396 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002397
2398 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002399 * We want to fill as much as possible. No more than a page
2400 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002401 */
Steven Rostedta7900872011-12-16 16:23:44 -05002402 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2403 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002404
Steven Rostedta7900872011-12-16 16:23:44 -05002405 again:
2406 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2407
2408 if (!pg->records) {
2409 /* if we can't allocate this size, try something smaller */
2410 if (!order)
2411 return -ENOMEM;
2412 order >>= 1;
2413 goto again;
2414 }
2415
2416 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2417 pg->size = cnt;
2418
2419 if (cnt > count)
2420 cnt = count;
2421
2422 return cnt;
2423}
2424
2425static struct ftrace_page *
2426ftrace_allocate_pages(unsigned long num_to_init)
2427{
2428 struct ftrace_page *start_pg;
2429 struct ftrace_page *pg;
2430 int order;
2431 int cnt;
2432
2433 if (!num_to_init)
2434 return 0;
2435
2436 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2437 if (!pg)
2438 return NULL;
2439
2440 /*
2441 * Try to allocate as much as possible in one continues
2442 * location that fills in all of the space. We want to
2443 * waste as little space as possible.
2444 */
2445 for (;;) {
2446 cnt = ftrace_allocate_records(pg, num_to_init);
2447 if (cnt < 0)
2448 goto free_pages;
2449
2450 num_to_init -= cnt;
2451 if (!num_to_init)
2452 break;
2453
2454 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2455 if (!pg->next)
2456 goto free_pages;
2457
2458 pg = pg->next;
2459 }
2460
2461 return start_pg;
2462
2463 free_pages:
2464 while (start_pg) {
2465 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2466 free_pages((unsigned long)pg->records, order);
2467 start_pg = pg->next;
2468 kfree(pg);
2469 pg = start_pg;
2470 }
2471 pr_info("ftrace: FAILED to allocate memory for functions\n");
2472 return NULL;
2473}
2474
Steven Rostedt5072c592008-05-12 21:20:43 +02002475#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2476
2477struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002478 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002479 loff_t func_pos;
2480 struct ftrace_page *pg;
2481 struct dyn_ftrace *func;
2482 struct ftrace_func_probe *probe;
2483 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002484 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002485 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002486 int hidx;
2487 int idx;
2488 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002489};
2490
Ingo Molnare309b412008-05-12 21:20:51 +02002491static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002492t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002493{
2494 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002495 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002496 struct hlist_head *hhd;
2497
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002498 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002499 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002500
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002501 if (iter->probe)
2502 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002503 retry:
2504 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2505 return NULL;
2506
2507 hhd = &ftrace_func_hash[iter->hidx];
2508
2509 if (hlist_empty(hhd)) {
2510 iter->hidx++;
2511 hnd = NULL;
2512 goto retry;
2513 }
2514
2515 if (!hnd)
2516 hnd = hhd->first;
2517 else {
2518 hnd = hnd->next;
2519 if (!hnd) {
2520 iter->hidx++;
2521 goto retry;
2522 }
2523 }
2524
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002525 if (WARN_ON_ONCE(!hnd))
2526 return NULL;
2527
2528 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2529
2530 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002531}
2532
2533static void *t_hash_start(struct seq_file *m, loff_t *pos)
2534{
2535 struct ftrace_iterator *iter = m->private;
2536 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002537 loff_t l;
2538
Steven Rostedt69a30832011-12-19 15:21:16 -05002539 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2540 return NULL;
2541
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002542 if (iter->func_pos > *pos)
2543 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002544
Li Zefand82d6242009-06-24 09:54:54 +08002545 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002546 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002547 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002548 if (!p)
2549 break;
2550 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002551 if (!p)
2552 return NULL;
2553
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002554 /* Only set this if we have an item */
2555 iter->flags |= FTRACE_ITER_HASH;
2556
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002557 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002558}
2559
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002560static int
2561t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002562{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002563 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002564
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002565 rec = iter->probe;
2566 if (WARN_ON_ONCE(!rec))
2567 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002568
Steven Rostedt809dcf22009-02-16 23:06:01 -05002569 if (rec->ops->print)
2570 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2571
Steven Rostedtb375a112009-09-17 00:05:58 -04002572 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002573
2574 if (rec->data)
2575 seq_printf(m, ":%p", rec->data);
2576 seq_putc(m, '\n');
2577
2578 return 0;
2579}
2580
2581static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002582t_next(struct seq_file *m, void *v, loff_t *pos)
2583{
2584 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002585 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002586 struct dyn_ftrace *rec = NULL;
2587
Steven Rostedt45a4a232011-04-21 23:16:46 -04002588 if (unlikely(ftrace_disabled))
2589 return NULL;
2590
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002591 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002592 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002593
Steven Rostedt5072c592008-05-12 21:20:43 +02002594 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002595 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002596
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002597 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002598 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002599
Steven Rostedt5072c592008-05-12 21:20:43 +02002600 retry:
2601 if (iter->idx >= iter->pg->index) {
2602 if (iter->pg->next) {
2603 iter->pg = iter->pg->next;
2604 iter->idx = 0;
2605 goto retry;
2606 }
2607 } else {
2608 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05002609 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002610 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05002611
Steven Rostedt41c52c02008-05-22 11:46:33 -04002612 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002613 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2614
2615 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002616 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002617
Steven Rostedt5072c592008-05-12 21:20:43 +02002618 rec = NULL;
2619 goto retry;
2620 }
2621 }
2622
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002623 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002624 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002625
2626 iter->func = rec;
2627
2628 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002629}
2630
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002631static void reset_iter_read(struct ftrace_iterator *iter)
2632{
2633 iter->pos = 0;
2634 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03002635 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002636}
2637
2638static void *t_start(struct seq_file *m, loff_t *pos)
2639{
2640 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002641 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002642 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002643 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002644
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002645 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002646
2647 if (unlikely(ftrace_disabled))
2648 return NULL;
2649
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002650 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002651 * If an lseek was done, then reset and start from beginning.
2652 */
2653 if (*pos < iter->pos)
2654 reset_iter_read(iter);
2655
2656 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002657 * For set_ftrace_filter reading, if we have the filter
2658 * off, we can short cut and just print out that all
2659 * functions are enabled.
2660 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002661 if (iter->flags & FTRACE_ITER_FILTER &&
2662 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002663 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002664 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002665 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002666 /* reset in case of seek/pread */
2667 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002668 return iter;
2669 }
2670
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002671 if (iter->flags & FTRACE_ITER_HASH)
2672 return t_hash_start(m, pos);
2673
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002674 /*
2675 * Unfortunately, we need to restart at ftrace_pages_start
2676 * every time we let go of the ftrace_mutex. This is because
2677 * those pointers can change without the lock.
2678 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002679 iter->pg = ftrace_pages_start;
2680 iter->idx = 0;
2681 for (l = 0; l <= *pos; ) {
2682 p = t_next(m, p, &l);
2683 if (!p)
2684 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002685 }
walimis5821e1b2008-11-15 15:19:06 +08002686
Steven Rostedt69a30832011-12-19 15:21:16 -05002687 if (!p)
2688 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002689
2690 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002691}
2692
2693static void t_stop(struct seq_file *m, void *p)
2694{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002695 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002696}
2697
2698static int t_show(struct seq_file *m, void *v)
2699{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002700 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002701 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002702
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002703 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002704 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002705
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002706 if (iter->flags & FTRACE_ITER_PRINTALL) {
2707 seq_printf(m, "#### all functions enabled ####\n");
2708 return 0;
2709 }
2710
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002711 rec = iter->func;
2712
Steven Rostedt5072c592008-05-12 21:20:43 +02002713 if (!rec)
2714 return 0;
2715
Steven Rostedt647bcd02011-05-03 14:39:21 -04002716 seq_printf(m, "%ps", (void *)rec->ip);
2717 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002718 seq_printf(m, " (%ld)%s",
2719 rec->flags & ~FTRACE_FL_MASK,
2720 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002721 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002722
2723 return 0;
2724}
2725
James Morris88e9d342009-09-22 16:43:43 -07002726static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002727 .start = t_start,
2728 .next = t_next,
2729 .stop = t_stop,
2730 .show = t_show,
2731};
2732
Ingo Molnare309b412008-05-12 21:20:51 +02002733static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002734ftrace_avail_open(struct inode *inode, struct file *file)
2735{
2736 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002737
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002738 if (unlikely(ftrace_disabled))
2739 return -ENODEV;
2740
Jiri Olsa50e18b92012-04-25 10:23:39 +02002741 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2742 if (iter) {
2743 iter->pg = ftrace_pages_start;
2744 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002745 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002746
Jiri Olsa50e18b92012-04-25 10:23:39 +02002747 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002748}
2749
Steven Rostedt647bcd02011-05-03 14:39:21 -04002750static int
2751ftrace_enabled_open(struct inode *inode, struct file *file)
2752{
2753 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002754
2755 if (unlikely(ftrace_disabled))
2756 return -ENODEV;
2757
Jiri Olsa50e18b92012-04-25 10:23:39 +02002758 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2759 if (iter) {
2760 iter->pg = ftrace_pages_start;
2761 iter->flags = FTRACE_ITER_ENABLED;
2762 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002763 }
2764
Jiri Olsa50e18b92012-04-25 10:23:39 +02002765 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002766}
2767
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002768static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002769{
Steven Rostedt52baf112009-02-14 01:15:39 -05002770 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002771 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002772 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002773}
2774
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002775/**
2776 * ftrace_regex_open - initialize function tracer filter files
2777 * @ops: The ftrace_ops that hold the hash filters
2778 * @flag: The type of filter to process
2779 * @inode: The inode, usually passed in to your open routine
2780 * @file: The file, usually passed in to your open routine
2781 *
2782 * ftrace_regex_open() initializes the filter files for the
2783 * @ops. Depending on @flag it may process the filter hash or
2784 * the notrace hash of @ops. With this called from the open
2785 * routine, you can use ftrace_filter_write() for the write
2786 * routine if @flag has FTRACE_ITER_FILTER set, or
2787 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05002788 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002789 * release must call ftrace_regex_release().
2790 */
2791int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002792ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002793 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002794{
2795 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002796 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002797 int ret = 0;
2798
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002799 ftrace_ops_init(ops);
2800
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002801 if (unlikely(ftrace_disabled))
2802 return -ENODEV;
2803
Steven Rostedt5072c592008-05-12 21:20:43 +02002804 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2805 if (!iter)
2806 return -ENOMEM;
2807
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002808 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2809 kfree(iter);
2810 return -ENOMEM;
2811 }
2812
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002813 iter->ops = ops;
2814 iter->flags = flag;
2815
2816 mutex_lock(&ops->regex_lock);
2817
Steven Rostedtf45948e2011-05-02 12:29:25 -04002818 if (flag & FTRACE_ITER_NOTRACE)
2819 hash = ops->notrace_hash;
2820 else
2821 hash = ops->filter_hash;
2822
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002823 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002824 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002825 if (!iter->hash) {
2826 trace_parser_put(&iter->parser);
2827 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002828 ret = -ENOMEM;
2829 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002830 }
2831 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002832
Steven Rostedt5072c592008-05-12 21:20:43 +02002833 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002834 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002835 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002836
2837 if (file->f_mode & FMODE_READ) {
2838 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002839
2840 ret = seq_open(file, &show_ftrace_seq_ops);
2841 if (!ret) {
2842 struct seq_file *m = file->private_data;
2843 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002844 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002845 /* Failed */
2846 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002847 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002848 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002849 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002850 } else
2851 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002852
2853 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002854 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002855
2856 return ret;
2857}
2858
Steven Rostedt41c52c02008-05-22 11:46:33 -04002859static int
2860ftrace_filter_open(struct inode *inode, struct file *file)
2861{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05002862 struct ftrace_ops *ops = inode->i_private;
2863
2864 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05002865 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2866 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002867}
2868
2869static int
2870ftrace_notrace_open(struct inode *inode, struct file *file)
2871{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05002872 struct ftrace_ops *ops = inode->i_private;
2873
2874 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002875 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002876}
2877
Steven Rostedt64e7c442009-02-13 17:08:48 -05002878static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002879{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002880 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002881 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002882
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002883 switch (type) {
2884 case MATCH_FULL:
2885 if (strcmp(str, regex) == 0)
2886 matched = 1;
2887 break;
2888 case MATCH_FRONT_ONLY:
2889 if (strncmp(str, regex, len) == 0)
2890 matched = 1;
2891 break;
2892 case MATCH_MIDDLE_ONLY:
2893 if (strstr(str, regex))
2894 matched = 1;
2895 break;
2896 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002897 slen = strlen(str);
2898 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002899 matched = 1;
2900 break;
2901 }
2902
2903 return matched;
2904}
2905
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002906static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002907enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002908{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002909 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002910 int ret = 0;
2911
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002912 entry = ftrace_lookup_ip(hash, rec->ip);
2913 if (not) {
2914 /* Do nothing if it doesn't exist */
2915 if (!entry)
2916 return 0;
2917
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002918 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002919 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002920 /* Do nothing if it exists */
2921 if (entry)
2922 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002923
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002924 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002925 }
2926 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002927}
2928
Steven Rostedt64e7c442009-02-13 17:08:48 -05002929static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002930ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2931 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002932{
2933 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002934 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002935
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002936 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2937
2938 if (mod) {
2939 /* module lookup requires matching the module */
2940 if (!modname || strcmp(modname, mod))
2941 return 0;
2942
2943 /* blank search means to match all funcs in the mod */
2944 if (!len)
2945 return 1;
2946 }
2947
Steven Rostedt64e7c442009-02-13 17:08:48 -05002948 return ftrace_match(str, regex, len, type);
2949}
2950
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002951static int
2952match_records(struct ftrace_hash *hash, char *buff,
2953 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002954{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002955 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002956 struct ftrace_page *pg;
2957 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002958 int type = MATCH_FULL;
2959 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002960 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002961 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002962
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002963 if (len) {
2964 type = filter_parse_regex(buff, len, &search, &not);
2965 search_len = strlen(search);
2966 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002967
Steven Rostedt52baf112009-02-14 01:15:39 -05002968 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002969
2970 if (unlikely(ftrace_disabled))
2971 goto out_unlock;
2972
Steven Rostedt265c8312009-02-13 12:43:56 -05002973 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002974 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002975 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002976 if (ret < 0) {
2977 found = ret;
2978 goto out_unlock;
2979 }
Li Zefan311d16d2009-12-08 11:15:11 +08002980 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002981 }
2982 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002983 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002984 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002985
2986 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002987}
2988
Steven Rostedt64e7c442009-02-13 17:08:48 -05002989static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002990ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002991{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002992 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002993}
2994
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002995static int
2996ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002997{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002998 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002999
Steven Rostedt64e7c442009-02-13 17:08:48 -05003000 /* blank or '*' mean the same */
3001 if (strcmp(buff, "*") == 0)
3002 buff[0] = 0;
3003
3004 /* handle the case of 'dont filter this module' */
3005 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3006 buff[0] = 0;
3007 not = 1;
3008 }
3009
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003010 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003011}
3012
Steven Rostedtf6180772009-02-14 00:40:25 -05003013/*
3014 * We register the module command as a template to show others how
3015 * to register the a command as well.
3016 */
3017
3018static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003019ftrace_mod_callback(struct ftrace_hash *hash,
3020 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003021{
3022 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003023 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003024
3025 /*
3026 * cmd == 'mod' because we only registered this func
3027 * for the 'mod' ftrace_func_command.
3028 * But if you register one func with multiple commands,
3029 * you can tell which command was used by the cmd
3030 * parameter.
3031 */
3032
3033 /* we must have a module name */
3034 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003035 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003036
3037 mod = strsep(&param, ":");
3038 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003039 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003040
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003041 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003042 if (!ret)
3043 ret = -EINVAL;
3044 if (ret < 0)
3045 return ret;
3046
3047 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003048}
3049
3050static struct ftrace_func_command ftrace_mod_cmd = {
3051 .name = "mod",
3052 .func = ftrace_mod_callback,
3053};
3054
3055static int __init ftrace_mod_cmd_init(void)
3056{
3057 return register_ftrace_command(&ftrace_mod_cmd);
3058}
Steven Rostedt6f415672012-10-05 12:13:07 -04003059core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003060
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003061static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003062 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003063{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003064 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003065 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003066 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003067
3068 key = hash_long(ip, FTRACE_HASH_BITS);
3069
3070 hhd = &ftrace_func_hash[key];
3071
3072 if (hlist_empty(hhd))
3073 return;
3074
3075 /*
3076 * Disable preemption for these calls to prevent a RCU grace
3077 * period. This syncs the hash iteration and freeing of items
3078 * on the hash. rcu_read_lock is too dangerous here.
3079 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003080 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003081 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003082 if (entry->ip == ip)
3083 entry->ops->func(ip, parent_ip, &entry->data);
3084 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003085 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003086}
3087
Steven Rostedtb6887d72009-02-17 12:32:04 -05003088static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003089{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003090 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003091 .flags = FTRACE_OPS_FL_INITIALIZED,
3092 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003093};
3094
Steven Rostedtb6887d72009-02-17 12:32:04 -05003095static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003096
Steven Rostedtb6887d72009-02-17 12:32:04 -05003097static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003098{
Steven Rostedtb8489142011-05-04 09:27:52 -04003099 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003100 int i;
3101
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003102 if (ftrace_probe_registered) {
3103 /* still need to update the function call sites */
3104 if (ftrace_enabled)
3105 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003106 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003107 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003108
3109 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3110 struct hlist_head *hhd = &ftrace_func_hash[i];
3111 if (hhd->first)
3112 break;
3113 }
3114 /* Nothing registered? */
3115 if (i == FTRACE_FUNC_HASHSIZE)
3116 return;
3117
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003118 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003119
Steven Rostedtb6887d72009-02-17 12:32:04 -05003120 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003121}
3122
Steven Rostedtb6887d72009-02-17 12:32:04 -05003123static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003124{
3125 int i;
3126
Steven Rostedtb6887d72009-02-17 12:32:04 -05003127 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003128 return;
3129
3130 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3131 struct hlist_head *hhd = &ftrace_func_hash[i];
3132 if (hhd->first)
3133 return;
3134 }
3135
3136 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003137 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003138
Steven Rostedtb6887d72009-02-17 12:32:04 -05003139 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003140}
3141
3142
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003143static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003144{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003145 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003146 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003147 kfree(entry);
3148}
3149
Steven Rostedt59df055f2009-02-14 15:29:06 -05003150int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003151register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003152 void *data)
3153{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003154 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003155 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3156 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003157 struct ftrace_page *pg;
3158 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003159 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003160 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003161 int count = 0;
3162 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003163 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003164
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003165 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003166 len = strlen(search);
3167
Steven Rostedtb6887d72009-02-17 12:32:04 -05003168 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003169 if (WARN_ON(not))
3170 return -EINVAL;
3171
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003172 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003173
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003174 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3175 if (!hash) {
3176 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003177 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003178 }
3179
3180 if (unlikely(ftrace_disabled)) {
3181 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003182 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003183 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003184
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003185 mutex_lock(&ftrace_lock);
3186
Steven Rostedt59df055f2009-02-14 15:29:06 -05003187 do_for_each_ftrace_rec(pg, rec) {
3188
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003189 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003190 continue;
3191
3192 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3193 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003194 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003195 if (!count)
3196 count = -ENOMEM;
3197 goto out_unlock;
3198 }
3199
3200 count++;
3201
3202 entry->data = data;
3203
3204 /*
3205 * The caller might want to do something special
3206 * for each function we find. We call the callback
3207 * to give the caller an opportunity to do so.
3208 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003209 if (ops->init) {
3210 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003211 /* caller does not like this func */
3212 kfree(entry);
3213 continue;
3214 }
3215 }
3216
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003217 ret = enter_record(hash, rec, 0);
3218 if (ret < 0) {
3219 kfree(entry);
3220 count = ret;
3221 goto out_unlock;
3222 }
3223
Steven Rostedt59df055f2009-02-14 15:29:06 -05003224 entry->ops = ops;
3225 entry->ip = rec->ip;
3226
3227 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3228 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3229
3230 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003231
3232 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3233 if (ret < 0)
3234 count = ret;
3235
Steven Rostedtb6887d72009-02-17 12:32:04 -05003236 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003237
3238 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003239 mutex_unlock(&ftrace_lock);
3240 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003241 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003242 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003243
3244 return count;
3245}
3246
3247enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003248 PROBE_TEST_FUNC = 1,
3249 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003250};
3251
3252static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003253__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003254 void *data, int flags)
3255{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003256 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003257 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003258 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003259 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003260 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003261 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003262 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003263 char str[KSYM_SYMBOL_LEN];
3264 int type = MATCH_FULL;
3265 int i, len = 0;
3266 char *search;
3267
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003268 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003269 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003270 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003271 int not;
3272
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003273 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003274 len = strlen(search);
3275
Steven Rostedtb6887d72009-02-17 12:32:04 -05003276 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003277 if (WARN_ON(not))
3278 return;
3279 }
3280
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003281 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003282
3283 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3284 if (!hash)
3285 /* Hmm, should report this somehow */
3286 goto out_unlock;
3287
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003288 INIT_LIST_HEAD(&free_list);
3289
Steven Rostedt59df055f2009-02-14 15:29:06 -05003290 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3291 struct hlist_head *hhd = &ftrace_func_hash[i];
3292
Sasha Levinb67bfe02013-02-27 17:06:00 -08003293 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003294
3295 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003296 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003297 continue;
3298
Steven Rostedtb6887d72009-02-17 12:32:04 -05003299 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003300 continue;
3301
3302 /* do this last, since it is the most expensive */
3303 if (glob) {
3304 kallsyms_lookup(entry->ip, NULL, NULL,
3305 NULL, str);
3306 if (!ftrace_match(str, glob, len, type))
3307 continue;
3308 }
3309
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003310 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3311 /* It is possible more than one entry had this ip */
3312 if (rec_entry)
3313 free_hash_entry(hash, rec_entry);
3314
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003315 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003316 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003317 }
3318 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003319 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003320 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003321 /*
3322 * Remove after the disable is called. Otherwise, if the last
3323 * probe is removed, a null hash means *all enabled*.
3324 */
3325 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003326 synchronize_sched();
3327 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3328 list_del(&entry->free_list);
3329 ftrace_free_entry(entry);
3330 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003331 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003332
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003333 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003334 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003335 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003336}
3337
3338void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003339unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003340 void *data)
3341{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003342 __unregister_ftrace_function_probe(glob, ops, data,
3343 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003344}
3345
3346void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003347unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003348{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003349 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003350}
3351
Steven Rostedtb6887d72009-02-17 12:32:04 -05003352void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003353{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003354 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003355}
3356
Steven Rostedtf6180772009-02-14 00:40:25 -05003357static LIST_HEAD(ftrace_commands);
3358static DEFINE_MUTEX(ftrace_cmd_mutex);
3359
Tom Zanussi38de93a2013-10-24 08:34:18 -05003360/*
3361 * Currently we only register ftrace commands from __init, so mark this
3362 * __init too.
3363 */
3364__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003365{
3366 struct ftrace_func_command *p;
3367 int ret = 0;
3368
3369 mutex_lock(&ftrace_cmd_mutex);
3370 list_for_each_entry(p, &ftrace_commands, list) {
3371 if (strcmp(cmd->name, p->name) == 0) {
3372 ret = -EBUSY;
3373 goto out_unlock;
3374 }
3375 }
3376 list_add(&cmd->list, &ftrace_commands);
3377 out_unlock:
3378 mutex_unlock(&ftrace_cmd_mutex);
3379
3380 return ret;
3381}
3382
Tom Zanussi38de93a2013-10-24 08:34:18 -05003383/*
3384 * Currently we only unregister ftrace commands from __init, so mark
3385 * this __init too.
3386 */
3387__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003388{
3389 struct ftrace_func_command *p, *n;
3390 int ret = -ENODEV;
3391
3392 mutex_lock(&ftrace_cmd_mutex);
3393 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3394 if (strcmp(cmd->name, p->name) == 0) {
3395 ret = 0;
3396 list_del_init(&p->list);
3397 goto out_unlock;
3398 }
3399 }
3400 out_unlock:
3401 mutex_unlock(&ftrace_cmd_mutex);
3402
3403 return ret;
3404}
3405
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003406static int ftrace_process_regex(struct ftrace_hash *hash,
3407 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003408{
Steven Rostedtf6180772009-02-14 00:40:25 -05003409 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003410 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003411 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003412
3413 func = strsep(&next, ":");
3414
3415 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003416 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003417 if (!ret)
3418 ret = -EINVAL;
3419 if (ret < 0)
3420 return ret;
3421 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003422 }
3423
Steven Rostedtf6180772009-02-14 00:40:25 -05003424 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003425
3426 command = strsep(&next, ":");
3427
Steven Rostedtf6180772009-02-14 00:40:25 -05003428 mutex_lock(&ftrace_cmd_mutex);
3429 list_for_each_entry(p, &ftrace_commands, list) {
3430 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003431 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003432 goto out_unlock;
3433 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003434 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003435 out_unlock:
3436 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003437
Steven Rostedtf6180772009-02-14 00:40:25 -05003438 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003439}
3440
Ingo Molnare309b412008-05-12 21:20:51 +02003441static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003442ftrace_regex_write(struct file *file, const char __user *ubuf,
3443 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003444{
3445 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003446 struct trace_parser *parser;
3447 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003448
Li Zefan4ba79782009-09-22 13:52:20 +08003449 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003450 return 0;
3451
Steven Rostedt5072c592008-05-12 21:20:43 +02003452 if (file->f_mode & FMODE_READ) {
3453 struct seq_file *m = file->private_data;
3454 iter = m->private;
3455 } else
3456 iter = file->private_data;
3457
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003458 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003459 return -ENODEV;
3460
3461 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003462
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003463 parser = &iter->parser;
3464 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003465
Li Zefan4ba79782009-09-22 13:52:20 +08003466 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003467 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003468 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003469 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003470 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003471 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003472 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003473 }
3474
Steven Rostedt5072c592008-05-12 21:20:43 +02003475 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003476 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003477 return ret;
3478}
3479
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003480ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003481ftrace_filter_write(struct file *file, const char __user *ubuf,
3482 size_t cnt, loff_t *ppos)
3483{
3484 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3485}
3486
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003487ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003488ftrace_notrace_write(struct file *file, const char __user *ubuf,
3489 size_t cnt, loff_t *ppos)
3490{
3491 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3492}
3493
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003494static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003495ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3496{
3497 struct ftrace_func_entry *entry;
3498
3499 if (!ftrace_location(ip))
3500 return -EINVAL;
3501
3502 if (remove) {
3503 entry = ftrace_lookup_ip(hash, ip);
3504 if (!entry)
3505 return -ENOENT;
3506 free_hash_entry(hash, entry);
3507 return 0;
3508 }
3509
3510 return add_hash_entry(hash, ip);
3511}
3512
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003513static void ftrace_ops_update_code(struct ftrace_ops *ops)
3514{
3515 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3516 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3517}
3518
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003519static int
3520ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3521 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003522{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003523 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003524 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003525 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003526
Steven Rostedt936e0742011-05-05 22:54:01 -04003527 /* All global ops uses the global ops filters */
3528 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3529 ops = &global_ops;
3530
Steven Rostedt41c52c02008-05-22 11:46:33 -04003531 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003532 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003533
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003534 mutex_lock(&ops->regex_lock);
3535
Steven Rostedtf45948e2011-05-02 12:29:25 -04003536 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003537 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003538 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003539 orig_hash = &ops->notrace_hash;
3540
3541 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003542 if (!hash) {
3543 ret = -ENOMEM;
3544 goto out_regex_unlock;
3545 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003546
Steven Rostedt41c52c02008-05-22 11:46:33 -04003547 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003548 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003549 if (buf && !ftrace_match_records(hash, buf, len)) {
3550 ret = -EINVAL;
3551 goto out_regex_unlock;
3552 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003553 if (ip) {
3554 ret = ftrace_match_addr(hash, ip, remove);
3555 if (ret < 0)
3556 goto out_regex_unlock;
3557 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003558
3559 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003560 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003561 if (!ret)
3562 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003563
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003564 mutex_unlock(&ftrace_lock);
3565
Jiri Olsaac483c42012-01-02 10:04:14 +01003566 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003567 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003568
3569 free_ftrace_hash(hash);
3570 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003571}
3572
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003573static int
3574ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3575 int reset, int enable)
3576{
3577 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3578}
3579
3580/**
3581 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3582 * @ops - the ops to set the filter with
3583 * @ip - the address to add to or remove from the filter.
3584 * @remove - non zero to remove the ip from the filter
3585 * @reset - non zero to reset all filters before applying this filter.
3586 *
3587 * Filters denote which functions should be enabled when tracing is enabled
3588 * If @ip is NULL, it failes to update filter.
3589 */
3590int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3591 int remove, int reset)
3592{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003593 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003594 return ftrace_set_addr(ops, ip, remove, reset, 1);
3595}
3596EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3597
3598static int
3599ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3600 int reset, int enable)
3601{
3602 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3603}
3604
Steven Rostedt77a2b372008-05-12 21:20:45 +02003605/**
3606 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003607 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003608 * @buf - the string that holds the function filter text.
3609 * @len - the length of the string.
3610 * @reset - non zero to reset all filters before applying this filter.
3611 *
3612 * Filters denote which functions should be enabled when tracing is enabled.
3613 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3614 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003615int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003616 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003617{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003618 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003619 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003620}
Steven Rostedt936e0742011-05-05 22:54:01 -04003621EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003622
Steven Rostedt41c52c02008-05-22 11:46:33 -04003623/**
3624 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003625 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003626 * @buf - the string that holds the function notrace text.
3627 * @len - the length of the string.
3628 * @reset - non zero to reset all filters before applying this filter.
3629 *
3630 * Notrace Filters denote which functions should not be enabled when tracing
3631 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3632 * for tracing.
3633 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003634int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003635 int len, int reset)
3636{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003637 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003638 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003639}
3640EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3641/**
3642 * ftrace_set_filter - set a function to filter on in ftrace
3643 * @ops - the ops to set the filter with
3644 * @buf - the string that holds the function filter text.
3645 * @len - the length of the string.
3646 * @reset - non zero to reset all filters before applying this filter.
3647 *
3648 * Filters denote which functions should be enabled when tracing is enabled.
3649 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3650 */
3651void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3652{
3653 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3654}
3655EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3656
3657/**
3658 * ftrace_set_notrace - set a function to not trace in ftrace
3659 * @ops - the ops to set the notrace filter with
3660 * @buf - the string that holds the function notrace text.
3661 * @len - the length of the string.
3662 * @reset - non zero to reset all filters before applying this filter.
3663 *
3664 * Notrace Filters denote which functions should not be enabled when tracing
3665 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3666 * for tracing.
3667 */
3668void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003669{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003670 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003671}
Steven Rostedt936e0742011-05-05 22:54:01 -04003672EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003673
Steven Rostedt2af15d62009-05-28 13:37:24 -04003674/*
3675 * command line interface to allow users to set filters on boot up.
3676 */
3677#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3678static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3679static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3680
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003681/* Used by function selftest to not test if filter is set */
3682bool ftrace_filter_param __initdata;
3683
Steven Rostedt2af15d62009-05-28 13:37:24 -04003684static int __init set_ftrace_notrace(char *str)
3685{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003686 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003687 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003688 return 1;
3689}
3690__setup("ftrace_notrace=", set_ftrace_notrace);
3691
3692static int __init set_ftrace_filter(char *str)
3693{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003694 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003695 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003696 return 1;
3697}
3698__setup("ftrace_filter=", set_ftrace_filter);
3699
Stefan Assmann369bc182009-10-12 22:17:21 +02003700#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003701static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003702static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003703
Stefan Assmann369bc182009-10-12 22:17:21 +02003704static int __init set_graph_function(char *str)
3705{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003706 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003707 return 1;
3708}
3709__setup("ftrace_graph_filter=", set_graph_function);
3710
3711static void __init set_ftrace_early_graph(char *buf)
3712{
3713 int ret;
3714 char *func;
3715
3716 while (buf) {
3717 func = strsep(&buf, ",");
3718 /* we allow only one expression at a time */
3719 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003720 FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003721 if (ret)
3722 printk(KERN_DEBUG "ftrace: function %s not "
3723 "traceable\n", func);
3724 }
3725}
3726#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3727
Steven Rostedt2a85a372011-12-19 21:57:44 -05003728void __init
3729ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003730{
3731 char *func;
3732
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003733 ftrace_ops_init(ops);
3734
Steven Rostedt2af15d62009-05-28 13:37:24 -04003735 while (buf) {
3736 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003737 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003738 }
3739}
3740
3741static void __init set_ftrace_early_filters(void)
3742{
3743 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003744 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003745 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003746 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003747#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3748 if (ftrace_graph_buf[0])
3749 set_ftrace_early_graph(ftrace_graph_buf);
3750#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003751}
3752
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003753int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003754{
3755 struct seq_file *m = (struct seq_file *)file->private_data;
3756 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003757 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003758 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003759 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003760 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003761
Steven Rostedt5072c592008-05-12 21:20:43 +02003762 if (file->f_mode & FMODE_READ) {
3763 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003764 seq_release(inode, file);
3765 } else
3766 iter = file->private_data;
3767
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003768 parser = &iter->parser;
3769 if (trace_parser_loaded(parser)) {
3770 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003771 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003772 }
3773
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003774 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003775
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003776 mutex_lock(&iter->ops->regex_lock);
3777
Steven Rostedt058e2972011-04-29 22:35:33 -04003778 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003779 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3780
3781 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003782 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003783 else
3784 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003785
Steven Rostedt058e2972011-04-29 22:35:33 -04003786 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003787 ret = ftrace_hash_move(iter->ops, filter_hash,
3788 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003789 if (!ret)
3790 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003791
Steven Rostedt058e2972011-04-29 22:35:33 -04003792 mutex_unlock(&ftrace_lock);
3793 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003794
3795 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003796 free_ftrace_hash(iter->hash);
3797 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003798
Steven Rostedt5072c592008-05-12 21:20:43 +02003799 return 0;
3800}
3801
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003802static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003803 .open = ftrace_avail_open,
3804 .read = seq_read,
3805 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003806 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003807};
3808
Steven Rostedt647bcd02011-05-03 14:39:21 -04003809static const struct file_operations ftrace_enabled_fops = {
3810 .open = ftrace_enabled_open,
3811 .read = seq_read,
3812 .llseek = seq_lseek,
3813 .release = seq_release_private,
3814};
3815
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003816static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003817 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003818 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003819 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003820 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003821 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003822};
3823
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003824static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003825 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003826 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003827 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003828 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003829 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003830};
3831
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003832#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3833
3834static DEFINE_MUTEX(graph_lock);
3835
3836int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003837int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003838unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003839unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003840
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003841struct ftrace_graph_data {
3842 unsigned long *table;
3843 size_t size;
3844 int *count;
3845 const struct seq_operations *seq_ops;
3846};
3847
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003848static void *
Li Zefan85951842009-06-24 09:54:00 +08003849__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003850{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003851 struct ftrace_graph_data *fgd = m->private;
3852
3853 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003854 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003855 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003856}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003857
Li Zefan85951842009-06-24 09:54:00 +08003858static void *
3859g_next(struct seq_file *m, void *v, loff_t *pos)
3860{
3861 (*pos)++;
3862 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003863}
3864
3865static void *g_start(struct seq_file *m, loff_t *pos)
3866{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003867 struct ftrace_graph_data *fgd = m->private;
3868
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003869 mutex_lock(&graph_lock);
3870
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003871 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003872 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003873 return (void *)1;
3874
Li Zefan85951842009-06-24 09:54:00 +08003875 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003876}
3877
3878static void g_stop(struct seq_file *m, void *p)
3879{
3880 mutex_unlock(&graph_lock);
3881}
3882
3883static int g_show(struct seq_file *m, void *v)
3884{
3885 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003886
3887 if (!ptr)
3888 return 0;
3889
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003890 if (ptr == (unsigned long *)1) {
3891 seq_printf(m, "#### all functions enabled ####\n");
3892 return 0;
3893 }
3894
Steven Rostedtb375a112009-09-17 00:05:58 -04003895 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003896
3897 return 0;
3898}
3899
James Morris88e9d342009-09-22 16:43:43 -07003900static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003901 .start = g_start,
3902 .next = g_next,
3903 .stop = g_stop,
3904 .show = g_show,
3905};
3906
3907static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003908__ftrace_graph_open(struct inode *inode, struct file *file,
3909 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003910{
3911 int ret = 0;
3912
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003913 mutex_lock(&graph_lock);
3914 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003915 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003916 *fgd->count = 0;
3917 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003918 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003919 mutex_unlock(&graph_lock);
3920
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003921 if (file->f_mode & FMODE_READ) {
3922 ret = seq_open(file, fgd->seq_ops);
3923 if (!ret) {
3924 struct seq_file *m = file->private_data;
3925 m->private = fgd;
3926 }
3927 } else
3928 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08003929
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003930 return ret;
3931}
3932
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003933static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003934ftrace_graph_open(struct inode *inode, struct file *file)
3935{
3936 struct ftrace_graph_data *fgd;
3937
3938 if (unlikely(ftrace_disabled))
3939 return -ENODEV;
3940
3941 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3942 if (fgd == NULL)
3943 return -ENOMEM;
3944
3945 fgd->table = ftrace_graph_funcs;
3946 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3947 fgd->count = &ftrace_graph_count;
3948 fgd->seq_ops = &ftrace_graph_seq_ops;
3949
3950 return __ftrace_graph_open(inode, file, fgd);
3951}
3952
3953static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003954ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3955{
3956 struct ftrace_graph_data *fgd;
3957
3958 if (unlikely(ftrace_disabled))
3959 return -ENODEV;
3960
3961 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3962 if (fgd == NULL)
3963 return -ENOMEM;
3964
3965 fgd->table = ftrace_graph_notrace_funcs;
3966 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3967 fgd->count = &ftrace_graph_notrace_count;
3968 fgd->seq_ops = &ftrace_graph_seq_ops;
3969
3970 return __ftrace_graph_open(inode, file, fgd);
3971}
3972
3973static int
Li Zefan87827112009-07-23 11:29:11 +08003974ftrace_graph_release(struct inode *inode, struct file *file)
3975{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003976 if (file->f_mode & FMODE_READ) {
3977 struct seq_file *m = file->private_data;
3978
3979 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08003980 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003981 } else {
3982 kfree(file->private_data);
3983 }
3984
Li Zefan87827112009-07-23 11:29:11 +08003985 return 0;
3986}
3987
3988static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003989ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003990{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003991 struct dyn_ftrace *rec;
3992 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003993 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003994 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003995 int type, not;
3996 char *search;
3997 bool exists;
3998 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003999
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004000 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004001 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004002 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004003 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004004
4005 search_len = strlen(search);
4006
Steven Rostedt52baf112009-02-14 01:15:39 -05004007 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004008
4009 if (unlikely(ftrace_disabled)) {
4010 mutex_unlock(&ftrace_lock);
4011 return -ENODEV;
4012 }
4013
Steven Rostedt265c8312009-02-13 12:43:56 -05004014 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004015
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004016 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004017 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004018 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004019 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004020 if (array[i] == rec->ip) {
4021 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004022 break;
4023 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004024 }
4025
4026 if (!not) {
4027 fail = 0;
4028 if (!exists) {
4029 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004030 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004031 goto out;
4032 }
4033 } else {
4034 if (exists) {
4035 array[i] = array[--(*idx)];
4036 array[*idx] = 0;
4037 fail = 0;
4038 }
4039 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004040 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004041 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004042out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004043 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004044
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004045 if (fail)
4046 return -EINVAL;
4047
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004048 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004049}
4050
4051static ssize_t
4052ftrace_graph_write(struct file *file, const char __user *ubuf,
4053 size_t cnt, loff_t *ppos)
4054{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004055 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004056 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004057 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004058
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004059 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004060 return 0;
4061
Namhyung Kim6a101082013-10-14 17:24:25 +09004062 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4063 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004064
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004065 read = trace_get_user(&parser, ubuf, cnt, ppos);
4066
Li Zefan4ba79782009-09-22 13:52:20 +08004067 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004068 parser.buffer[parser.idx] = 0;
4069
Namhyung Kim6a101082013-10-14 17:24:25 +09004070 mutex_lock(&graph_lock);
4071
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004072 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004073 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4074 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004075
4076 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004077 }
4078
Namhyung Kim6a101082013-10-14 17:24:25 +09004079 if (!ret)
4080 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004081
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004082 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004083
4084 return ret;
4085}
4086
4087static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004088 .open = ftrace_graph_open,
4089 .read = seq_read,
4090 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004091 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004092 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004093};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004094
4095static const struct file_operations ftrace_graph_notrace_fops = {
4096 .open = ftrace_graph_notrace_open,
4097 .read = seq_read,
4098 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004099 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004100 .release = ftrace_graph_release,
4101};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004102#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4103
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004104void ftrace_create_filter_files(struct ftrace_ops *ops,
4105 struct dentry *parent)
4106{
4107
4108 trace_create_file("set_ftrace_filter", 0644, parent,
4109 ops, &ftrace_filter_fops);
4110
4111 trace_create_file("set_ftrace_notrace", 0644, parent,
4112 ops, &ftrace_notrace_fops);
4113}
4114
4115/*
4116 * The name "destroy_filter_files" is really a misnomer. Although
4117 * in the future, it may actualy delete the files, but this is
4118 * really intended to make sure the ops passed in are disabled
4119 * and that when this function returns, the caller is free to
4120 * free the ops.
4121 *
4122 * The "destroy" name is only to match the "create" name that this
4123 * should be paired with.
4124 */
4125void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4126{
4127 mutex_lock(&ftrace_lock);
4128 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4129 ftrace_shutdown(ops, 0);
4130 ops->flags |= FTRACE_OPS_FL_DELETED;
4131 mutex_unlock(&ftrace_lock);
4132}
4133
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004134static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004135{
Steven Rostedt5072c592008-05-12 21:20:43 +02004136
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004137 trace_create_file("available_filter_functions", 0444,
4138 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004139
Steven Rostedt647bcd02011-05-03 14:39:21 -04004140 trace_create_file("enabled_functions", 0444,
4141 d_tracer, NULL, &ftrace_enabled_fops);
4142
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004143 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004144
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004145#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004146 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004147 NULL,
4148 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004149 trace_create_file("set_graph_notrace", 0444, d_tracer,
4150 NULL,
4151 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004152#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4153
Steven Rostedt5072c592008-05-12 21:20:43 +02004154 return 0;
4155}
4156
Steven Rostedt9fd49322012-04-24 22:32:06 -04004157static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004158{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004159 const unsigned long *ipa = a;
4160 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004161
Steven Rostedt9fd49322012-04-24 22:32:06 -04004162 if (*ipa > *ipb)
4163 return 1;
4164 if (*ipa < *ipb)
4165 return -1;
4166 return 0;
4167}
4168
4169static void ftrace_swap_ips(void *a, void *b, int size)
4170{
4171 unsigned long *ipa = a;
4172 unsigned long *ipb = b;
4173 unsigned long t;
4174
4175 t = *ipa;
4176 *ipa = *ipb;
4177 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004178}
4179
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004180static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004181 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004182 unsigned long *end)
4183{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004184 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004185 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004186 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004187 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004188 unsigned long *p;
4189 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004190 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004191 int ret = -ENOMEM;
4192
4193 count = end - start;
4194
4195 if (!count)
4196 return 0;
4197
Steven Rostedt9fd49322012-04-24 22:32:06 -04004198 sort(start, count, sizeof(*start),
4199 ftrace_cmp_ips, ftrace_swap_ips);
4200
Steven Rostedt706c81f2012-04-24 23:45:26 -04004201 start_pg = ftrace_allocate_pages(count);
4202 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004203 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004204
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004205 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004206
Steven Rostedt320823092011-12-16 14:42:37 -05004207 /*
4208 * Core and each module needs their own pages, as
4209 * modules will free them when they are removed.
4210 * Force a new page to be allocated for modules.
4211 */
Steven Rostedta7900872011-12-16 16:23:44 -05004212 if (!mod) {
4213 WARN_ON(ftrace_pages || ftrace_pages_start);
4214 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004215 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004216 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05004217 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004218 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05004219
Steven Rostedta7900872011-12-16 16:23:44 -05004220 if (WARN_ON(ftrace_pages->next)) {
4221 /* Hmm, we have free pages? */
4222 while (ftrace_pages->next)
4223 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05004224 }
Steven Rostedta7900872011-12-16 16:23:44 -05004225
Steven Rostedt706c81f2012-04-24 23:45:26 -04004226 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05004227 }
4228
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004229 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004230 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004231 while (p < end) {
4232 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004233 /*
4234 * Some architecture linkers will pad between
4235 * the different mcount_loc sections of different
4236 * object files to satisfy alignments.
4237 * Skip any NULL pointers.
4238 */
4239 if (!addr)
4240 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004241
4242 if (pg->index == pg->size) {
4243 /* We should have allocated enough */
4244 if (WARN_ON(!pg->next))
4245 break;
4246 pg = pg->next;
4247 }
4248
4249 rec = &pg->records[pg->index++];
4250 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004251 }
4252
Steven Rostedt706c81f2012-04-24 23:45:26 -04004253 /* We should have used all pages */
4254 WARN_ON(pg->next);
4255
4256 /* Assign the last page to ftrace_pages */
4257 ftrace_pages = pg;
4258
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004259 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004260 * We only need to disable interrupts on start up
4261 * because we are modifying code that an interrupt
4262 * may execute, and the modification is not atomic.
4263 * But for modules, nothing runs the code we modify
4264 * until we are finished with it, and there's no
4265 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004266 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004267 if (!mod)
4268 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004269 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004270 if (!mod)
4271 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004272 ret = 0;
4273 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004274 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004275
Steven Rostedta7900872011-12-16 16:23:44 -05004276 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004277}
4278
Steven Rostedt93eb6772009-04-15 13:24:06 -04004279#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05004280
4281#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4282
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004283void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004284{
4285 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05004286 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004287 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004288 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004289
Steven Rostedt93eb6772009-04-15 13:24:06 -04004290 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004291
4292 if (ftrace_disabled)
4293 goto out_unlock;
4294
Steven Rostedt320823092011-12-16 14:42:37 -05004295 /*
4296 * Each module has its own ftrace_pages, remove
4297 * them from the list.
4298 */
4299 last_pg = &ftrace_pages_start;
4300 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4301 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004302 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004303 /*
Steven Rostedt320823092011-12-16 14:42:37 -05004304 * As core pages are first, the first
4305 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004306 */
Steven Rostedt320823092011-12-16 14:42:37 -05004307 if (WARN_ON(pg == ftrace_pages_start))
4308 goto out_unlock;
4309
4310 /* Check if we are deleting the last page */
4311 if (pg == ftrace_pages)
4312 ftrace_pages = next_to_ftrace_page(last_pg);
4313
4314 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004315 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4316 free_pages((unsigned long)pg->records, order);
4317 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05004318 } else
4319 last_pg = &pg->next;
4320 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004321 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004322 mutex_unlock(&ftrace_lock);
4323}
4324
4325static void ftrace_init_module(struct module *mod,
4326 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004327{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004328 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004329 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004330 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004331}
4332
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004333static int ftrace_module_notify_enter(struct notifier_block *self,
4334 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004335{
4336 struct module *mod = data;
4337
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004338 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004339 ftrace_init_module(mod, mod->ftrace_callsites,
4340 mod->ftrace_callsites +
4341 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004342 return 0;
4343}
4344
4345static int ftrace_module_notify_exit(struct notifier_block *self,
4346 unsigned long val, void *data)
4347{
4348 struct module *mod = data;
4349
4350 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004351 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004352
4353 return 0;
4354}
4355#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004356static int ftrace_module_notify_enter(struct notifier_block *self,
4357 unsigned long val, void *data)
4358{
4359 return 0;
4360}
4361static int ftrace_module_notify_exit(struct notifier_block *self,
4362 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004363{
4364 return 0;
4365}
4366#endif /* CONFIG_MODULES */
4367
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004368struct notifier_block ftrace_module_enter_nb = {
4369 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004370 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004371};
4372
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004373struct notifier_block ftrace_module_exit_nb = {
4374 .notifier_call = ftrace_module_notify_exit,
4375 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4376};
4377
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004378void __init ftrace_init(void)
4379{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004380 extern unsigned long __start_mcount_loc[];
4381 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004382 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004383 int ret;
4384
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004385 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004386 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004387 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004388 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004389 goto failed;
4390
4391 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004392 if (!count) {
4393 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004394 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004395 }
4396
4397 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4398 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004399
4400 last_ftrace_enabled = ftrace_enabled = 1;
4401
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004402 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004403 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004404 __stop_mcount_loc);
4405
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004406 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004407 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004408 pr_warning("Failed to register trace ftrace module enter notifier\n");
4409
4410 ret = register_module_notifier(&ftrace_module_exit_nb);
4411 if (ret)
4412 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004413
Steven Rostedt2af15d62009-05-28 13:37:24 -04004414 set_ftrace_early_filters();
4415
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004416 return;
4417 failed:
4418 ftrace_disabled = 1;
4419}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004420
Steven Rostedt3d083392008-05-12 21:20:42 +02004421#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004422
Steven Rostedt2b499382011-05-03 22:49:52 -04004423static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004424 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004425 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4426 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004427};
4428
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004429static int __init ftrace_nodyn_init(void)
4430{
4431 ftrace_enabled = 1;
4432 return 0;
4433}
Steven Rostedt6f415672012-10-05 12:13:07 -04004434core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004435
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004436static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4437static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004438/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004439# define ftrace_startup(ops, command) \
4440 ({ \
4441 int ___ret = __register_ftrace_function(ops); \
4442 if (!___ret) \
4443 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4444 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004445 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004446# define ftrace_shutdown(ops, command) \
4447 ({ \
4448 int ___ret = __unregister_ftrace_function(ops); \
4449 if (!___ret) \
4450 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4451 ___ret; \
4452 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004453
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004454# define ftrace_startup_sysctl() do { } while (0)
4455# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004456
4457static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004458ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004459{
4460 return 1;
4461}
4462
Steven Rostedt3d083392008-05-12 21:20:42 +02004463#endif /* CONFIG_DYNAMIC_FTRACE */
4464
Steven Rostedtb8489142011-05-04 09:27:52 -04004465static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004466ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004467 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004468{
Jiri Olsae2484912012-02-15 15:51:48 +01004469 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4470 return;
4471
4472 /*
4473 * Some of the ops may be dynamically allocated,
4474 * they must be freed after a synchronize_sched().
4475 */
4476 preempt_disable_notrace();
4477 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004478
4479 /*
4480 * Control funcs (perf) uses RCU. Only trace if
4481 * RCU is currently active.
4482 */
4483 if (!rcu_is_watching())
4484 goto out;
4485
Steven Rostedt0a016402012-11-02 17:03:03 -04004486 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004487 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4488 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004489 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004490 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004491 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004492 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004493 trace_recursion_clear(TRACE_CONTROL_BIT);
4494 preempt_enable_notrace();
4495}
4496
4497static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004498 .func = ftrace_ops_control_func,
4499 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4500 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004501};
4502
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004503static inline void
4504__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004505 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004506{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004507 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004508 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004509
Steven Rostedtccf36722012-06-05 09:44:25 -04004510 if (function_trace_stop)
4511 return;
4512
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004513 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4514 if (bit < 0)
4515 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004516
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004517 /*
4518 * Some of the ops may be dynamically allocated,
4519 * they must be freed after a synchronize_sched().
4520 */
4521 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004522 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004523 if (ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004524 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004525 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004526 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004527 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004528}
4529
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004530/*
4531 * Some archs only support passing ip and parent_ip. Even though
4532 * the list function ignores the op parameter, we do not want any
4533 * C side effects, where a function is called without the caller
4534 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004535 * Archs are to support both the regs and ftrace_ops at the same time.
4536 * If they support ftrace_ops, it is assumed they support regs.
4537 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004538 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4539 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004540 * An architecture can pass partial regs with ftrace_ops and still
4541 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004542 */
4543#if ARCH_SUPPORTS_FTRACE_OPS
4544static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004545 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004546{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004547 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004548}
4549#else
4550static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4551{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004552 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004553}
4554#endif
4555
Steven Rostedte32d8952008-12-04 00:26:41 -05004556static void clear_ftrace_swapper(void)
4557{
4558 struct task_struct *p;
4559 int cpu;
4560
4561 get_online_cpus();
4562 for_each_online_cpu(cpu) {
4563 p = idle_task(cpu);
4564 clear_tsk_trace_trace(p);
4565 }
4566 put_online_cpus();
4567}
4568
4569static void set_ftrace_swapper(void)
4570{
4571 struct task_struct *p;
4572 int cpu;
4573
4574 get_online_cpus();
4575 for_each_online_cpu(cpu) {
4576 p = idle_task(cpu);
4577 set_tsk_trace_trace(p);
4578 }
4579 put_online_cpus();
4580}
4581
4582static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004583{
4584 struct task_struct *p;
4585
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004586 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004587 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004588 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004589 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004590 rcu_read_unlock();
4591
Steven Rostedte32d8952008-12-04 00:26:41 -05004592 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004593}
4594
Steven Rostedte32d8952008-12-04 00:26:41 -05004595static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004596{
4597 struct task_struct *p;
4598
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004599 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004600 do_each_pid_task(pid, PIDTYPE_PID, p) {
4601 set_tsk_trace_trace(p);
4602 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004603 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004604}
4605
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004606static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004607{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004608 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004609 clear_ftrace_swapper();
4610 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004611 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004612}
4613
4614static void set_ftrace_pid_task(struct pid *pid)
4615{
4616 if (pid == ftrace_swapper_pid)
4617 set_ftrace_swapper();
4618 else
4619 set_ftrace_pid(pid);
4620}
4621
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004622static int ftrace_pid_add(int p)
4623{
4624 struct pid *pid;
4625 struct ftrace_pid *fpid;
4626 int ret = -EINVAL;
4627
4628 mutex_lock(&ftrace_lock);
4629
4630 if (!p)
4631 pid = ftrace_swapper_pid;
4632 else
4633 pid = find_get_pid(p);
4634
4635 if (!pid)
4636 goto out;
4637
4638 ret = 0;
4639
4640 list_for_each_entry(fpid, &ftrace_pids, list)
4641 if (fpid->pid == pid)
4642 goto out_put;
4643
4644 ret = -ENOMEM;
4645
4646 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4647 if (!fpid)
4648 goto out_put;
4649
4650 list_add(&fpid->list, &ftrace_pids);
4651 fpid->pid = pid;
4652
4653 set_ftrace_pid_task(pid);
4654
4655 ftrace_update_pid_func();
4656 ftrace_startup_enable(0);
4657
4658 mutex_unlock(&ftrace_lock);
4659 return 0;
4660
4661out_put:
4662 if (pid != ftrace_swapper_pid)
4663 put_pid(pid);
4664
4665out:
4666 mutex_unlock(&ftrace_lock);
4667 return ret;
4668}
4669
4670static void ftrace_pid_reset(void)
4671{
4672 struct ftrace_pid *fpid, *safe;
4673
4674 mutex_lock(&ftrace_lock);
4675 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4676 struct pid *pid = fpid->pid;
4677
4678 clear_ftrace_pid_task(pid);
4679
4680 list_del(&fpid->list);
4681 kfree(fpid);
4682 }
4683
4684 ftrace_update_pid_func();
4685 ftrace_startup_enable(0);
4686
4687 mutex_unlock(&ftrace_lock);
4688}
4689
4690static void *fpid_start(struct seq_file *m, loff_t *pos)
4691{
4692 mutex_lock(&ftrace_lock);
4693
4694 if (list_empty(&ftrace_pids) && (!*pos))
4695 return (void *) 1;
4696
4697 return seq_list_start(&ftrace_pids, *pos);
4698}
4699
4700static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4701{
4702 if (v == (void *)1)
4703 return NULL;
4704
4705 return seq_list_next(v, &ftrace_pids, pos);
4706}
4707
4708static void fpid_stop(struct seq_file *m, void *p)
4709{
4710 mutex_unlock(&ftrace_lock);
4711}
4712
4713static int fpid_show(struct seq_file *m, void *v)
4714{
4715 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4716
4717 if (v == (void *)1) {
4718 seq_printf(m, "no pid\n");
4719 return 0;
4720 }
4721
4722 if (fpid->pid == ftrace_swapper_pid)
4723 seq_printf(m, "swapper tasks\n");
4724 else
4725 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4726
4727 return 0;
4728}
4729
4730static const struct seq_operations ftrace_pid_sops = {
4731 .start = fpid_start,
4732 .next = fpid_next,
4733 .stop = fpid_stop,
4734 .show = fpid_show,
4735};
4736
4737static int
4738ftrace_pid_open(struct inode *inode, struct file *file)
4739{
4740 int ret = 0;
4741
4742 if ((file->f_mode & FMODE_WRITE) &&
4743 (file->f_flags & O_TRUNC))
4744 ftrace_pid_reset();
4745
4746 if (file->f_mode & FMODE_READ)
4747 ret = seq_open(file, &ftrace_pid_sops);
4748
4749 return ret;
4750}
4751
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004752static ssize_t
4753ftrace_pid_write(struct file *filp, const char __user *ubuf,
4754 size_t cnt, loff_t *ppos)
4755{
Ingo Molnar457dc922009-11-23 11:03:28 +01004756 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004757 long val;
4758 int ret;
4759
4760 if (cnt >= sizeof(buf))
4761 return -EINVAL;
4762
4763 if (copy_from_user(&buf, ubuf, cnt))
4764 return -EFAULT;
4765
4766 buf[cnt] = 0;
4767
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004768 /*
4769 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4770 * to clean the filter quietly.
4771 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004772 tmp = strstrip(buf);
4773 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004774 return 1;
4775
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004776 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004777 if (ret < 0)
4778 return ret;
4779
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004780 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004781
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004782 return ret ? ret : cnt;
4783}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004784
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004785static int
4786ftrace_pid_release(struct inode *inode, struct file *file)
4787{
4788 if (file->f_mode & FMODE_READ)
4789 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004790
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004791 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004792}
4793
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004794static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004795 .open = ftrace_pid_open,
4796 .write = ftrace_pid_write,
4797 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004798 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004799 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004800};
4801
4802static __init int ftrace_init_debugfs(void)
4803{
4804 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004805
4806 d_tracer = tracing_init_dentry();
4807 if (!d_tracer)
4808 return 0;
4809
4810 ftrace_init_dyn_debugfs(d_tracer);
4811
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004812 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4813 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004814
4815 ftrace_profile_debugfs(d_tracer);
4816
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004817 return 0;
4818}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004819fs_initcall(ftrace_init_debugfs);
4820
Steven Rostedt3d083392008-05-12 21:20:42 +02004821/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004822 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004823 *
4824 * This function should be used by panic code. It stops ftrace
4825 * but in a not so nice way. If you need to simply kill ftrace
4826 * from a non-atomic section, use ftrace_kill.
4827 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004828void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004829{
4830 ftrace_disabled = 1;
4831 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004832 clear_ftrace_function();
4833}
4834
4835/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004836 * Test if ftrace is dead or not.
4837 */
4838int ftrace_is_dead(void)
4839{
4840 return ftrace_disabled;
4841}
4842
4843/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004844 * register_ftrace_function - register a function for profiling
4845 * @ops - ops structure that holds the function for profiling.
4846 *
4847 * Register a function to be called by all functions in the
4848 * kernel.
4849 *
4850 * Note: @ops->func and all the functions it calls must be labeled
4851 * with "notrace", otherwise it will go into a
4852 * recursive loop.
4853 */
4854int register_ftrace_function(struct ftrace_ops *ops)
4855{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004856 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004857
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004858 ftrace_ops_init(ops);
4859
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004860 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004861
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004862 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004863
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004864 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004865
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004866 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004867}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004868EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004869
4870/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004871 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004872 * @ops - ops structure that holds the function to unregister
4873 *
4874 * Unregister a function that was added to be called by ftrace profiling.
4875 */
4876int unregister_ftrace_function(struct ftrace_ops *ops)
4877{
4878 int ret;
4879
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004880 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004881 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004882 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004883
4884 return ret;
4885}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004886EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004887
Ingo Molnare309b412008-05-12 21:20:51 +02004888int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004889ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004890 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004891 loff_t *ppos)
4892{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004893 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004894
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004895 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004896
Steven Rostedt45a4a232011-04-21 23:16:46 -04004897 if (unlikely(ftrace_disabled))
4898 goto out;
4899
4900 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004901
Li Zefana32c7762009-06-26 16:55:51 +08004902 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004903 goto out;
4904
Li Zefana32c7762009-06-26 16:55:51 +08004905 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004906
4907 if (ftrace_enabled) {
4908
4909 ftrace_startup_sysctl();
4910
4911 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004912 if (ftrace_ops_list != &ftrace_list_end)
4913 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004914
4915 } else {
4916 /* stopping ftrace calls (just send to ftrace_stub) */
4917 ftrace_trace_function = ftrace_stub;
4918
4919 ftrace_shutdown_sysctl();
4920 }
4921
4922 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004923 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004924 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004925}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004926
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004927#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004928
Steven Rostedt597af812009-04-03 15:24:12 -04004929static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004930static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004931
Steven Rostedte49dc192008-12-02 23:50:05 -05004932int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4933{
4934 return 0;
4935}
4936
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004937/* The callbacks that hook a function */
4938trace_func_graph_ret_t ftrace_graph_return =
4939 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004940trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05004941static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004942
4943/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4944static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4945{
4946 int i;
4947 int ret = 0;
4948 unsigned long flags;
4949 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4950 struct task_struct *g, *t;
4951
4952 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4953 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4954 * sizeof(struct ftrace_ret_stack),
4955 GFP_KERNEL);
4956 if (!ret_stack_list[i]) {
4957 start = 0;
4958 end = i;
4959 ret = -ENOMEM;
4960 goto free;
4961 }
4962 }
4963
4964 read_lock_irqsave(&tasklist_lock, flags);
4965 do_each_thread(g, t) {
4966 if (start == end) {
4967 ret = -EAGAIN;
4968 goto unlock;
4969 }
4970
4971 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004972 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004973 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004974 t->curr_ret_stack = -1;
4975 /* Make sure the tasks see the -1 first: */
4976 smp_wmb();
4977 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004978 }
4979 } while_each_thread(g, t);
4980
4981unlock:
4982 read_unlock_irqrestore(&tasklist_lock, flags);
4983free:
4984 for (i = start; i < end; i++)
4985 kfree(ret_stack_list[i]);
4986 return ret;
4987}
4988
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004989static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004990ftrace_graph_probe_sched_switch(void *ignore,
4991 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004992{
4993 unsigned long long timestamp;
4994 int index;
4995
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004996 /*
4997 * Does the user want to count the time a function was asleep.
4998 * If so, do not update the time stamps.
4999 */
5000 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5001 return;
5002
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005003 timestamp = trace_clock_local();
5004
5005 prev->ftrace_timestamp = timestamp;
5006
5007 /* only process tasks that we timestamped */
5008 if (!next->ftrace_timestamp)
5009 return;
5010
5011 /*
5012 * Update all the counters in next to make up for the
5013 * time next was sleeping.
5014 */
5015 timestamp -= next->ftrace_timestamp;
5016
5017 for (index = next->curr_ret_stack; index >= 0; index--)
5018 next->ret_stack[index].calltime += timestamp;
5019}
5020
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005021/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005022static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005023{
5024 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005025 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005026
5027 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5028 sizeof(struct ftrace_ret_stack *),
5029 GFP_KERNEL);
5030
5031 if (!ret_stack_list)
5032 return -ENOMEM;
5033
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005034 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005035 for_each_online_cpu(cpu) {
5036 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005037 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005038 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005039
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005040 do {
5041 ret = alloc_retstack_tasklist(ret_stack_list);
5042 } while (ret == -EAGAIN);
5043
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005044 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005045 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005046 if (ret)
5047 pr_info("ftrace_graph: Couldn't activate tracepoint"
5048 " probe to kernel_sched_switch\n");
5049 }
5050
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005051 kfree(ret_stack_list);
5052 return ret;
5053}
5054
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005055/*
5056 * Hibernation protection.
5057 * The state of the current task is too much unstable during
5058 * suspend/restore to disk. We want to protect against that.
5059 */
5060static int
5061ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5062 void *unused)
5063{
5064 switch (state) {
5065 case PM_HIBERNATION_PREPARE:
5066 pause_graph_tracing();
5067 break;
5068
5069 case PM_POST_HIBERNATION:
5070 unpause_graph_tracing();
5071 break;
5072 }
5073 return NOTIFY_DONE;
5074}
5075
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005076/* Just a place holder for function graph */
5077static struct ftrace_ops fgraph_ops __read_mostly = {
5078 .func = ftrace_stub,
5079 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
5080 FTRACE_OPS_FL_RECURSION_SAFE,
5081};
5082
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005083static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5084{
5085 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5086 return 0;
5087 return __ftrace_graph_entry(trace);
5088}
5089
5090/*
5091 * The function graph tracer should only trace the functions defined
5092 * by set_ftrace_filter and set_ftrace_notrace. If another function
5093 * tracer ops is registered, the graph tracer requires testing the
5094 * function against the global ops, and not just trace any function
5095 * that any ftrace_ops registered.
5096 */
5097static void update_function_graph_func(void)
5098{
5099 if (ftrace_ops_list == &ftrace_list_end ||
5100 (ftrace_ops_list == &global_ops &&
5101 global_ops.next == &ftrace_list_end))
5102 ftrace_graph_entry = __ftrace_graph_entry;
5103 else
5104 ftrace_graph_entry = ftrace_graph_entry_test;
5105}
5106
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005107int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5108 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005109{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005110 int ret = 0;
5111
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005112 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005113
Steven Rostedt05ce5812009-03-24 00:18:31 -04005114 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005115 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005116 ret = -EBUSY;
5117 goto out;
5118 }
5119
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005120 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5121 register_pm_notifier(&ftrace_suspend_notifier);
5122
Steven Rostedt597af812009-04-03 15:24:12 -04005123 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005124 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005125 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005126 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005127 goto out;
5128 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005129
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005130 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005131
5132 /*
5133 * Update the indirect function to the entryfunc, and the
5134 * function that gets called to the entry_test first. Then
5135 * call the update fgraph entry function to determine if
5136 * the entryfunc should be called directly or not.
5137 */
5138 __ftrace_graph_entry = entryfunc;
5139 ftrace_graph_entry = ftrace_graph_entry_test;
5140 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005141
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005142 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005143
5144out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005145 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005146 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005147}
5148
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005149void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005150{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005151 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005152
Steven Rostedt597af812009-04-03 15:24:12 -04005153 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005154 goto out;
5155
Steven Rostedt597af812009-04-03 15:24:12 -04005156 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005157 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005158 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005159 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005160 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005161 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005162 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005163
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005164 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005165 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005166}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005167
Steven Rostedt868baf02011-02-10 21:26:13 -05005168static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5169
5170static void
5171graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5172{
5173 atomic_set(&t->tracing_graph_pause, 0);
5174 atomic_set(&t->trace_overrun, 0);
5175 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005176 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005177 smp_wmb();
5178 t->ret_stack = ret_stack;
5179}
5180
5181/*
5182 * Allocate a return stack for the idle task. May be the first
5183 * time through, or it may be done by CPU hotplug online.
5184 */
5185void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5186{
5187 t->curr_ret_stack = -1;
5188 /*
5189 * The idle task has no parent, it either has its own
5190 * stack or no stack at all.
5191 */
5192 if (t->ret_stack)
5193 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5194
5195 if (ftrace_graph_active) {
5196 struct ftrace_ret_stack *ret_stack;
5197
5198 ret_stack = per_cpu(idle_ret_stack, cpu);
5199 if (!ret_stack) {
5200 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5201 * sizeof(struct ftrace_ret_stack),
5202 GFP_KERNEL);
5203 if (!ret_stack)
5204 return;
5205 per_cpu(idle_ret_stack, cpu) = ret_stack;
5206 }
5207 graph_init_task(t, ret_stack);
5208 }
5209}
5210
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005211/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005212void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005213{
Steven Rostedt84047e32009-06-02 16:51:55 -04005214 /* Make sure we do not use the parent ret_stack */
5215 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005216 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005217
Steven Rostedt597af812009-04-03 15:24:12 -04005218 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005219 struct ftrace_ret_stack *ret_stack;
5220
5221 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005222 * sizeof(struct ftrace_ret_stack),
5223 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005224 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005225 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005226 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005227 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005228}
5229
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005230void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005231{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005232 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5233
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005234 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005235 /* NULL must become visible to IRQs before we free it: */
5236 barrier();
5237
5238 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005239}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005240
5241void ftrace_graph_stop(void)
5242{
5243 ftrace_stop();
5244}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005245#endif