blob: 7181ad15923b86ac0ffbe5e5826ffd09a52224f8 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Jiri Olsae2484912012-02-15 15:51:48 +010065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
68#define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
70#else
71#define INIT_REGEX_LOCK(opsname)
72#endif
73
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040074static struct ftrace_ops ftrace_list_end __read_mostly = {
75 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040076 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077};
78
Steven Rostedt4eebcc82008-05-12 21:20:48 +020079/* ftrace_enabled is a method to turn ftrace on or off */
80int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020081static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020082
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050083/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040084int function_trace_stop __read_mostly;
85
86/* Current function tracing op */
87struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050088/* What to set function_trace_op to */
89static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050090
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040091/* List for set_ftrace_pid's pids. */
92LIST_HEAD(ftrace_pids);
93struct ftrace_pid {
94 struct list_head list;
95 struct pid *pid;
96};
97
Steven Rostedt4eebcc82008-05-12 21:20:48 +020098/*
99 * ftrace_disabled is set when an anomaly is discovered.
100 * ftrace_disabled is much stronger than ftrace_enabled.
101 */
102static int ftrace_disabled __read_mostly;
103
Steven Rostedt52baf112009-02-14 01:15:39 -0500104static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200105
Steven Rostedtb8489142011-05-04 09:27:52 -0400106static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
Jiri Olsae2484912012-02-15 15:51:48 +0100107static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400108static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200109ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500110ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400111static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100112static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200113
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400114#if ARCH_SUPPORTS_FTRACE_OPS
115static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400116 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400117#else
118/* See comment below, where ftrace_ops_list_func is defined */
119static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
120#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
121#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400122
Steven Rostedt0a016402012-11-02 17:03:03 -0400123/*
124 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400125 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400126 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400127 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400128 * concurrent insertions into the ftrace_global_list.
129 *
130 * Silly Alpha and silly pointer-speculation compiler optimizations!
131 */
132#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400133 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400134 do
135
136/*
137 * Optimized for just a single item in the list (as that is the normal case).
138 */
139#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400140 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400141 unlikely((op) != &ftrace_list_end))
142
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900143static inline void ftrace_ops_init(struct ftrace_ops *ops)
144{
145#ifdef CONFIG_DYNAMIC_FTRACE
146 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
147 mutex_init(&ops->regex_lock);
148 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
149 }
150#endif
151}
152
Steven Rostedtea701f12012-07-20 13:08:05 -0400153/**
154 * ftrace_nr_registered_ops - return number of ops registered
155 *
156 * Returns the number of ftrace_ops registered and tracing functions
157 */
158int ftrace_nr_registered_ops(void)
159{
160 struct ftrace_ops *ops;
161 int cnt = 0;
162
163 mutex_lock(&ftrace_lock);
164
165 for (ops = ftrace_ops_list;
166 ops != &ftrace_list_end; ops = ops->next)
167 cnt++;
168
169 mutex_unlock(&ftrace_lock);
170
171 return cnt;
172}
173
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400174static void
175ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400176 struct ftrace_ops *op, struct pt_regs *regs)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200177{
Steven Rostedtc29f1222012-11-02 17:17:59 -0400178 int bit;
179
Steven Rostedtedc15ca2012-11-02 17:47:21 -0400180 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
181 if (bit < 0)
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400182 return;
183
Steven Rostedt0a016402012-11-02 17:03:03 -0400184 do_for_each_ftrace_op(op, ftrace_global_list) {
Steven Rostedta1e2e312011-08-09 12:50:46 -0400185 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -0400186 } while_for_each_ftrace_op(op);
Steven Rostedtedc15ca2012-11-02 17:47:21 -0400187
188 trace_clear_recursion(bit);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200189}
190
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400191static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400192 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500193{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500194 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500195 return;
196
Steven Rostedta1e2e312011-08-09 12:50:46 -0400197 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500198}
199
200static void set_ftrace_pid_function(ftrace_func_t func)
201{
202 /* do not set ftrace_pid_function to itself! */
203 if (func != ftrace_pid_func)
204 ftrace_pid_function = func;
205}
206
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200207/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200208 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200209 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200210 * This NULLs the ftrace function and in essence stops
211 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200212 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200213void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200214{
Steven Rostedt3d083392008-05-12 21:20:42 +0200215 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500216 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200217}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200218
Jiri Olsae2484912012-02-15 15:51:48 +0100219static void control_ops_disable_all(struct ftrace_ops *ops)
220{
221 int cpu;
222
223 for_each_possible_cpu(cpu)
224 *per_cpu_ptr(ops->disabled, cpu) = 1;
225}
226
227static int control_ops_alloc(struct ftrace_ops *ops)
228{
229 int __percpu *disabled;
230
231 disabled = alloc_percpu(int);
232 if (!disabled)
233 return -ENOMEM;
234
235 ops->disabled = disabled;
236 control_ops_disable_all(ops);
237 return 0;
238}
239
240static void control_ops_free(struct ftrace_ops *ops)
241{
242 free_percpu(ops->disabled);
243}
244
Steven Rostedt2b499382011-05-03 22:49:52 -0400245static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400246{
247 ftrace_func_t func;
248
249 /*
250 * If there's only one function registered, then call that
251 * function directly. Otherwise, we need to iterate over the
252 * registered callers.
253 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400254 if (ftrace_global_list == &ftrace_list_end ||
Steven Rostedt63503792012-11-02 16:58:56 -0400255 ftrace_global_list->next == &ftrace_list_end) {
Steven Rostedtb8489142011-05-04 09:27:52 -0400256 func = ftrace_global_list->func;
Steven Rostedt63503792012-11-02 16:58:56 -0400257 /*
258 * As we are calling the function directly.
259 * If it does not have recursion protection,
260 * the function_trace_op needs to be updated
261 * accordingly.
262 */
263 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
264 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
265 else
266 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
267 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -0400268 func = ftrace_global_list_func;
Steven Rostedt63503792012-11-02 16:58:56 -0400269 /* The list has its own recursion protection. */
270 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
271 }
272
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400273
274 /* If we filter on pids, update to use the pid function */
275 if (!list_empty(&ftrace_pids)) {
276 set_ftrace_pid_function(func);
277 func = ftrace_pid_func;
278 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400279
280 global_ops.func = func;
281}
282
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500283static void ftrace_sync(struct work_struct *work)
284{
285 /*
286 * This function is just a stub to implement a hard force
287 * of synchronize_sched(). This requires synchronizing
288 * tasks even in userspace and idle.
289 *
290 * Yes, function tracing is rude.
291 */
292}
293
294static void ftrace_sync_ipi(void *data)
295{
296 /* Probably not needed, but do it anyway */
297 smp_rmb();
298}
299
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500300#ifdef CONFIG_FUNCTION_GRAPH_TRACER
301static void update_function_graph_func(void);
302#else
303static inline void update_function_graph_func(void) { }
304#endif
305
Steven Rostedt2b499382011-05-03 22:49:52 -0400306static void update_ftrace_function(void)
307{
308 ftrace_func_t func;
309
310 update_global_ops();
311
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400312 /*
313 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400314 * recursion safe and not dynamic and the arch supports passing ops,
315 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400316 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400317 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400318 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400319 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400320 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400321 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400322 /* Set the ftrace_ops that the arch callback uses */
323 if (ftrace_ops_list == &global_ops)
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500324 set_function_trace_op = ftrace_global_list;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400325 else
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500326 set_function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400327 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400328 } else {
329 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500330 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400331 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400332 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400333
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500334 /* If there's no change, then do nothing more here */
335 if (ftrace_trace_function == func)
336 return;
337
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500338 update_function_graph_func();
339
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500340 /*
341 * If we are using the list function, it doesn't care
342 * about the function_trace_ops.
343 */
344 if (func == ftrace_ops_list_func) {
345 ftrace_trace_function = func;
346 /*
347 * Don't even bother setting function_trace_ops,
348 * it would be racy to do so anyway.
349 */
350 return;
351 }
352
353#ifndef CONFIG_DYNAMIC_FTRACE
354 /*
355 * For static tracing, we need to be a bit more careful.
356 * The function change takes affect immediately. Thus,
357 * we need to coorditate the setting of the function_trace_ops
358 * with the setting of the ftrace_trace_function.
359 *
360 * Set the function to the list ops, which will call the
361 * function we want, albeit indirectly, but it handles the
362 * ftrace_ops and doesn't depend on function_trace_op.
363 */
364 ftrace_trace_function = ftrace_ops_list_func;
365 /*
366 * Make sure all CPUs see this. Yes this is slow, but static
367 * tracing is slow and nasty to have enabled.
368 */
369 schedule_on_each_cpu(ftrace_sync);
370 /* Now all cpus are using the list ops. */
371 function_trace_op = set_function_trace_op;
372 /* Make sure the function_trace_op is visible on all CPUs */
373 smp_wmb();
374 /* Nasty way to force a rmb on all cpus */
375 smp_call_function(ftrace_sync_ipi, NULL, 1);
376 /* OK, we are all set to update the ftrace_trace_function now! */
377#endif /* !CONFIG_DYNAMIC_FTRACE */
378
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400379 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400380}
381
Steven Rostedt2b499382011-05-03 22:49:52 -0400382static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200383{
Steven Rostedt2b499382011-05-03 22:49:52 -0400384 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200385 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400386 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200387 * CPU might be walking that list. We need to make sure
388 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400389 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200390 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400391 rcu_assign_pointer(*list, ops);
392}
Steven Rostedt3d083392008-05-12 21:20:42 +0200393
Steven Rostedt2b499382011-05-03 22:49:52 -0400394static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
395{
396 struct ftrace_ops **p;
397
398 /*
399 * If we are removing the last function, then simply point
400 * to the ftrace_stub.
401 */
402 if (*list == ops && ops->next == &ftrace_list_end) {
403 *list = &ftrace_list_end;
404 return 0;
405 }
406
407 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
408 if (*p == ops)
409 break;
410
411 if (*p != ops)
412 return -1;
413
414 *p = (*p)->next;
415 return 0;
416}
417
Jiri Olsae2484912012-02-15 15:51:48 +0100418static void add_ftrace_list_ops(struct ftrace_ops **list,
419 struct ftrace_ops *main_ops,
420 struct ftrace_ops *ops)
421{
422 int first = *list == &ftrace_list_end;
423 add_ftrace_ops(list, ops);
424 if (first)
425 add_ftrace_ops(&ftrace_ops_list, main_ops);
426}
427
428static int remove_ftrace_list_ops(struct ftrace_ops **list,
429 struct ftrace_ops *main_ops,
430 struct ftrace_ops *ops)
431{
432 int ret = remove_ftrace_ops(list, ops);
433 if (!ret && *list == &ftrace_list_end)
434 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
435 return ret;
436}
437
Steven Rostedt2b499382011-05-03 22:49:52 -0400438static int __register_ftrace_function(struct ftrace_ops *ops)
439{
Steven Rostedt2b499382011-05-03 22:49:52 -0400440 if (FTRACE_WARN_ON(ops == &global_ops))
441 return -EINVAL;
442
Steven Rostedtb8489142011-05-04 09:27:52 -0400443 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
444 return -EBUSY;
445
Jiri Olsae2484912012-02-15 15:51:48 +0100446 /* We don't support both control and global flags set. */
447 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
448 return -EINVAL;
449
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900450#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400451 /*
452 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
453 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
454 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
455 */
456 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
457 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
458 return -EINVAL;
459
460 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
461 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
462#endif
463
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400464 if (!core_kernel_data((unsigned long)ops))
465 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
466
Steven Rostedtb8489142011-05-04 09:27:52 -0400467 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100468 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400469 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100470 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
471 if (control_ops_alloc(ops))
472 return -ENOMEM;
473 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400474 } else
475 add_ftrace_ops(&ftrace_ops_list, ops);
476
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400477 if (ftrace_enabled)
478 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200479
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200480 return 0;
481}
482
Ingo Molnare309b412008-05-12 21:20:51 +0200483static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200484{
Steven Rostedt2b499382011-05-03 22:49:52 -0400485 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200486
Steven Rostedtb8489142011-05-04 09:27:52 -0400487 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
488 return -EBUSY;
489
Steven Rostedt2b499382011-05-03 22:49:52 -0400490 if (FTRACE_WARN_ON(ops == &global_ops))
491 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200492
Steven Rostedtb8489142011-05-04 09:27:52 -0400493 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100494 ret = remove_ftrace_list_ops(&ftrace_global_list,
495 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400496 if (!ret)
497 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100498 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
499 ret = remove_ftrace_list_ops(&ftrace_control_list,
500 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400501 } else
502 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
503
Steven Rostedt2b499382011-05-03 22:49:52 -0400504 if (ret < 0)
505 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400506
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400507 if (ftrace_enabled)
508 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200509
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500510 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200511}
512
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500513static void ftrace_update_pid_func(void)
514{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400515 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500516 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900517 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500518
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400519 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500520}
521
Steven Rostedt493762f2009-03-23 17:12:36 -0400522#ifdef CONFIG_FUNCTION_PROFILER
523struct ftrace_profile {
524 struct hlist_node node;
525 unsigned long ip;
526 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400527#ifdef CONFIG_FUNCTION_GRAPH_TRACER
528 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400529 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400530#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400531};
532
533struct ftrace_profile_page {
534 struct ftrace_profile_page *next;
535 unsigned long index;
536 struct ftrace_profile records[];
537};
538
Steven Rostedtcafb1682009-03-24 20:50:39 -0400539struct ftrace_profile_stat {
540 atomic_t disabled;
541 struct hlist_head *hash;
542 struct ftrace_profile_page *pages;
543 struct ftrace_profile_page *start;
544 struct tracer_stat stat;
545};
546
Steven Rostedt493762f2009-03-23 17:12:36 -0400547#define PROFILE_RECORDS_SIZE \
548 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
549
550#define PROFILES_PER_PAGE \
551 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
552
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400553static int ftrace_profile_enabled __read_mostly;
554
555/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400556static DEFINE_MUTEX(ftrace_profile_lock);
557
Steven Rostedtcafb1682009-03-24 20:50:39 -0400558static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400559
Namhyung Kim20079eb2013-04-10 08:55:50 +0900560#define FTRACE_PROFILE_HASH_BITS 10
561#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400562
Steven Rostedt493762f2009-03-23 17:12:36 -0400563static void *
564function_stat_next(void *v, int idx)
565{
566 struct ftrace_profile *rec = v;
567 struct ftrace_profile_page *pg;
568
569 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
570
571 again:
Li Zefan0296e422009-06-26 11:15:37 +0800572 if (idx != 0)
573 rec++;
574
Steven Rostedt493762f2009-03-23 17:12:36 -0400575 if ((void *)rec >= (void *)&pg->records[pg->index]) {
576 pg = pg->next;
577 if (!pg)
578 return NULL;
579 rec = &pg->records[0];
580 if (!rec->counter)
581 goto again;
582 }
583
584 return rec;
585}
586
587static void *function_stat_start(struct tracer_stat *trace)
588{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400589 struct ftrace_profile_stat *stat =
590 container_of(trace, struct ftrace_profile_stat, stat);
591
592 if (!stat || !stat->start)
593 return NULL;
594
595 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400596}
597
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400598#ifdef CONFIG_FUNCTION_GRAPH_TRACER
599/* function graph compares on total time */
600static int function_stat_cmp(void *p1, void *p2)
601{
602 struct ftrace_profile *a = p1;
603 struct ftrace_profile *b = p2;
604
605 if (a->time < b->time)
606 return -1;
607 if (a->time > b->time)
608 return 1;
609 else
610 return 0;
611}
612#else
613/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400614static int function_stat_cmp(void *p1, void *p2)
615{
616 struct ftrace_profile *a = p1;
617 struct ftrace_profile *b = p2;
618
619 if (a->counter < b->counter)
620 return -1;
621 if (a->counter > b->counter)
622 return 1;
623 else
624 return 0;
625}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400626#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400627
628static int function_stat_headers(struct seq_file *m)
629{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400630#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400631 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400632 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400633 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400634 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400635#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400636 seq_printf(m, " Function Hit\n"
637 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400638#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400639 return 0;
640}
641
642static int function_stat_show(struct seq_file *m, void *v)
643{
644 struct ftrace_profile *rec = v;
645 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800646 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400647#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400648 static struct trace_seq s;
649 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400650 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400651#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800652 mutex_lock(&ftrace_profile_lock);
653
654 /* we raced with function_profile_reset() */
655 if (unlikely(rec->counter == 0)) {
656 ret = -EBUSY;
657 goto out;
658 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400659
660 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400661 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400662
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400663#ifdef CONFIG_FUNCTION_GRAPH_TRACER
664 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400665 avg = rec->time;
666 do_div(avg, rec->counter);
667
Chase Douglase330b3b2010-04-26 14:02:05 -0400668 /* Sample standard deviation (s^2) */
669 if (rec->counter <= 1)
670 stddev = 0;
671 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200672 /*
673 * Apply Welford's method:
674 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
675 */
676 stddev = rec->counter * rec->time_squared -
677 rec->time * rec->time;
678
Chase Douglase330b3b2010-04-26 14:02:05 -0400679 /*
680 * Divide only 1000 for ns^2 -> us^2 conversion.
681 * trace_print_graph_duration will divide 1000 again.
682 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200683 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400684 }
685
Steven Rostedt34886c82009-03-25 21:00:47 -0400686 trace_seq_init(&s);
687 trace_print_graph_duration(rec->time, &s);
688 trace_seq_puts(&s, " ");
689 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400690 trace_seq_puts(&s, " ");
691 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400692 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400693#endif
694 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800695out:
696 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400697
Li Zefan3aaba202010-08-23 16:50:12 +0800698 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400699}
700
Steven Rostedtcafb1682009-03-24 20:50:39 -0400701static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400702{
703 struct ftrace_profile_page *pg;
704
Steven Rostedtcafb1682009-03-24 20:50:39 -0400705 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400706
707 while (pg) {
708 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
709 pg->index = 0;
710 pg = pg->next;
711 }
712
Steven Rostedtcafb1682009-03-24 20:50:39 -0400713 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400714 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
715}
716
Steven Rostedtcafb1682009-03-24 20:50:39 -0400717int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400718{
719 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400720 int functions;
721 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400722 int i;
723
724 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400725 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400726 return 0;
727
Steven Rostedtcafb1682009-03-24 20:50:39 -0400728 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
729 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400730 return -ENOMEM;
731
Steven Rostedt318e0a72009-03-25 20:06:34 -0400732#ifdef CONFIG_DYNAMIC_FTRACE
733 functions = ftrace_update_tot_cnt;
734#else
735 /*
736 * We do not know the number of functions that exist because
737 * dynamic tracing is what counts them. With past experience
738 * we have around 20K functions. That should be more than enough.
739 * It is highly unlikely we will execute every function in
740 * the kernel.
741 */
742 functions = 20000;
743#endif
744
Steven Rostedtcafb1682009-03-24 20:50:39 -0400745 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400746
Steven Rostedt318e0a72009-03-25 20:06:34 -0400747 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
748
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900749 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400750 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400751 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400752 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400753 pg = pg->next;
754 }
755
756 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400757
758 out_free:
759 pg = stat->start;
760 while (pg) {
761 unsigned long tmp = (unsigned long)pg;
762
763 pg = pg->next;
764 free_page(tmp);
765 }
766
Steven Rostedt318e0a72009-03-25 20:06:34 -0400767 stat->pages = NULL;
768 stat->start = NULL;
769
770 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400771}
772
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400774{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400775 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400776 int size;
777
Steven Rostedtcafb1682009-03-24 20:50:39 -0400778 stat = &per_cpu(ftrace_profile_stats, cpu);
779
780 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400781 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400782 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400783 return 0;
784 }
785
786 /*
787 * We are profiling all functions, but usually only a few thousand
788 * functions are hit. We'll make a hash of 1024 items.
789 */
790 size = FTRACE_PROFILE_HASH_SIZE;
791
Steven Rostedtcafb1682009-03-24 20:50:39 -0400792 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400793
Steven Rostedtcafb1682009-03-24 20:50:39 -0400794 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400795 return -ENOMEM;
796
Steven Rostedt318e0a72009-03-25 20:06:34 -0400797 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400798 if (ftrace_profile_pages_init(stat) < 0) {
799 kfree(stat->hash);
800 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400801 return -ENOMEM;
802 }
803
804 return 0;
805}
806
Steven Rostedtcafb1682009-03-24 20:50:39 -0400807static int ftrace_profile_init(void)
808{
809 int cpu;
810 int ret = 0;
811
812 for_each_online_cpu(cpu) {
813 ret = ftrace_profile_init_cpu(cpu);
814 if (ret)
815 break;
816 }
817
818 return ret;
819}
820
Steven Rostedt493762f2009-03-23 17:12:36 -0400821/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400822static struct ftrace_profile *
823ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400824{
825 struct ftrace_profile *rec;
826 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400827 unsigned long key;
828
Namhyung Kim20079eb2013-04-10 08:55:50 +0900829 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400830 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400831
832 if (hlist_empty(hhd))
833 return NULL;
834
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400835 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400836 if (rec->ip == ip)
837 return rec;
838 }
839
840 return NULL;
841}
842
Steven Rostedtcafb1682009-03-24 20:50:39 -0400843static void ftrace_add_profile(struct ftrace_profile_stat *stat,
844 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400845{
846 unsigned long key;
847
Namhyung Kim20079eb2013-04-10 08:55:50 +0900848 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400849 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400850}
851
Steven Rostedt318e0a72009-03-25 20:06:34 -0400852/*
853 * The memory is already allocated, this simply finds a new record to use.
854 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400855static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400856ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400857{
858 struct ftrace_profile *rec = NULL;
859
Steven Rostedt318e0a72009-03-25 20:06:34 -0400860 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400861 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400862 goto out;
863
Steven Rostedt493762f2009-03-23 17:12:36 -0400864 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400865 * Try to find the function again since an NMI
866 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400867 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400868 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400869 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400870 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400871
Steven Rostedtcafb1682009-03-24 20:50:39 -0400872 if (stat->pages->index == PROFILES_PER_PAGE) {
873 if (!stat->pages->next)
874 goto out;
875 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400876 }
877
Steven Rostedtcafb1682009-03-24 20:50:39 -0400878 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400879 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400880 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400881
Steven Rostedt493762f2009-03-23 17:12:36 -0400882 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400883 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400884
885 return rec;
886}
887
Steven Rostedt493762f2009-03-23 17:12:36 -0400888static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400889function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400890 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400891{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400892 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400893 struct ftrace_profile *rec;
894 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400895
896 if (!ftrace_profile_enabled)
897 return;
898
Steven Rostedt493762f2009-03-23 17:12:36 -0400899 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400900
901 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400902 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400903 goto out;
904
905 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400906 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400907 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400908 if (!rec)
909 goto out;
910 }
911
912 rec->counter++;
913 out:
914 local_irq_restore(flags);
915}
916
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400917#ifdef CONFIG_FUNCTION_GRAPH_TRACER
918static int profile_graph_entry(struct ftrace_graph_ent *trace)
919{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400920 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400921 return 1;
922}
923
924static void profile_graph_return(struct ftrace_graph_ret *trace)
925{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400926 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400927 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400928 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400929 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400930
931 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400932 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400933 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400934 goto out;
935
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400936 /* If the calltime was zero'd ignore it */
937 if (!trace->calltime)
938 goto out;
939
Steven Rostedta2a16d62009-03-24 23:17:58 -0400940 calltime = trace->rettime - trace->calltime;
941
942 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
943 int index;
944
945 index = trace->depth;
946
947 /* Append this call time to the parent time to subtract */
948 if (index)
949 current->ret_stack[index - 1].subtime += calltime;
950
951 if (current->ret_stack[index].subtime < calltime)
952 calltime -= current->ret_stack[index].subtime;
953 else
954 calltime = 0;
955 }
956
Steven Rostedtcafb1682009-03-24 20:50:39 -0400957 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400958 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400959 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400960 rec->time_squared += calltime * calltime;
961 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400962
Steven Rostedtcafb1682009-03-24 20:50:39 -0400963 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400964 local_irq_restore(flags);
965}
966
967static int register_ftrace_profiler(void)
968{
969 return register_ftrace_graph(&profile_graph_return,
970 &profile_graph_entry);
971}
972
973static void unregister_ftrace_profiler(void)
974{
975 unregister_ftrace_graph();
976}
977#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100978static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400979 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900980 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
981 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400982};
983
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400984static int register_ftrace_profiler(void)
985{
986 return register_ftrace_function(&ftrace_profile_ops);
987}
988
989static void unregister_ftrace_profiler(void)
990{
991 unregister_ftrace_function(&ftrace_profile_ops);
992}
993#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
994
Steven Rostedt493762f2009-03-23 17:12:36 -0400995static ssize_t
996ftrace_profile_write(struct file *filp, const char __user *ubuf,
997 size_t cnt, loff_t *ppos)
998{
999 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -04001000 int ret;
1001
Peter Huewe22fe9b52011-06-07 21:58:27 +02001002 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1003 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -04001004 return ret;
1005
1006 val = !!val;
1007
1008 mutex_lock(&ftrace_profile_lock);
1009 if (ftrace_profile_enabled ^ val) {
1010 if (val) {
1011 ret = ftrace_profile_init();
1012 if (ret < 0) {
1013 cnt = ret;
1014 goto out;
1015 }
1016
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001017 ret = register_ftrace_profiler();
1018 if (ret < 0) {
1019 cnt = ret;
1020 goto out;
1021 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001022 ftrace_profile_enabled = 1;
1023 } else {
1024 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -04001025 /*
1026 * unregister_ftrace_profiler calls stop_machine
1027 * so this acts like an synchronize_sched.
1028 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001029 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -04001030 }
1031 }
1032 out:
1033 mutex_unlock(&ftrace_profile_lock);
1034
Jiri Olsacf8517c2009-10-23 19:36:16 -04001035 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001036
1037 return cnt;
1038}
1039
1040static ssize_t
1041ftrace_profile_read(struct file *filp, char __user *ubuf,
1042 size_t cnt, loff_t *ppos)
1043{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001044 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001045 int r;
1046
1047 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1048 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1049}
1050
1051static const struct file_operations ftrace_profile_fops = {
1052 .open = tracing_open_generic,
1053 .read = ftrace_profile_read,
1054 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001055 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001056};
1057
Steven Rostedtcafb1682009-03-24 20:50:39 -04001058/* used to initialize the real stat files */
1059static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001060 .name = "functions",
1061 .stat_start = function_stat_start,
1062 .stat_next = function_stat_next,
1063 .stat_cmp = function_stat_cmp,
1064 .stat_headers = function_stat_headers,
1065 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001066};
1067
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001068static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001069{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001070 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001071 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001072 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001073 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001074 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001075
Steven Rostedtcafb1682009-03-24 20:50:39 -04001076 for_each_possible_cpu(cpu) {
1077 stat = &per_cpu(ftrace_profile_stats, cpu);
1078
1079 /* allocate enough for function name + cpu number */
1080 name = kmalloc(32, GFP_KERNEL);
1081 if (!name) {
1082 /*
1083 * The files created are permanent, if something happens
1084 * we still do not free memory.
1085 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001086 WARN(1,
1087 "Could not allocate stat file for cpu %d\n",
1088 cpu);
1089 return;
1090 }
1091 stat->stat = function_stats;
1092 snprintf(name, 32, "function%d", cpu);
1093 stat->stat.name = name;
1094 ret = register_stat_tracer(&stat->stat);
1095 if (ret) {
1096 WARN(1,
1097 "Could not register function stat for cpu %d\n",
1098 cpu);
1099 kfree(name);
1100 return;
1101 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001102 }
1103
1104 entry = debugfs_create_file("function_profile_enabled", 0644,
1105 d_tracer, NULL, &ftrace_profile_fops);
1106 if (!entry)
1107 pr_warning("Could not create debugfs "
1108 "'function_profile_enabled' entry\n");
1109}
1110
1111#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001112static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001113{
1114}
1115#endif /* CONFIG_FUNCTION_PROFILER */
1116
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001117static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1118
Steven Rostedt3d083392008-05-12 21:20:42 +02001119#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001120
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001121#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001122# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001123#endif
1124
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001125static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1126
Steven Rostedtb6887d72009-02-17 12:32:04 -05001127struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001128 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001129 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001130 unsigned long flags;
1131 unsigned long ip;
1132 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001133 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001134};
1135
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001136struct ftrace_func_entry {
1137 struct hlist_node hlist;
1138 unsigned long ip;
1139};
1140
1141struct ftrace_hash {
1142 unsigned long size_bits;
1143 struct hlist_head *buckets;
1144 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001145 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001146};
1147
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001148/*
1149 * We make these constant because no one should touch them,
1150 * but they are used as the default "empty hash", to avoid allocating
1151 * it all the time. These are in a read only section such that if
1152 * anyone does try to modify it, it will cause an exception.
1153 */
1154static const struct hlist_head empty_buckets[1];
1155static const struct ftrace_hash empty_hash = {
1156 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001157};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001158#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001159
Steven Rostedt2b499382011-05-03 22:49:52 -04001160static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001161 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001162 .notrace_hash = EMPTY_HASH,
1163 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001164 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1165 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001166};
1167
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001168struct ftrace_page {
1169 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001170 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001171 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001172 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001173};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001174
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001175static struct ftrace_page *ftrace_new_pgs;
1176
Steven Rostedta7900872011-12-16 16:23:44 -05001177#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1178#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001179
1180/* estimate from running different kernels */
1181#define NR_TO_INIT 10000
1182
1183static struct ftrace_page *ftrace_pages_start;
1184static struct ftrace_page *ftrace_pages;
1185
Steven Rostedt06a51d92011-12-19 19:07:36 -05001186static bool ftrace_hash_empty(struct ftrace_hash *hash)
1187{
1188 return !hash || !hash->count;
1189}
1190
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001191static struct ftrace_func_entry *
1192ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1193{
1194 unsigned long key;
1195 struct ftrace_func_entry *entry;
1196 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001197
Steven Rostedt06a51d92011-12-19 19:07:36 -05001198 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001199 return NULL;
1200
1201 if (hash->size_bits > 0)
1202 key = hash_long(ip, hash->size_bits);
1203 else
1204 key = 0;
1205
1206 hhd = &hash->buckets[key];
1207
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001208 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001209 if (entry->ip == ip)
1210 return entry;
1211 }
1212 return NULL;
1213}
1214
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001215static void __add_hash_entry(struct ftrace_hash *hash,
1216 struct ftrace_func_entry *entry)
1217{
1218 struct hlist_head *hhd;
1219 unsigned long key;
1220
1221 if (hash->size_bits)
1222 key = hash_long(entry->ip, hash->size_bits);
1223 else
1224 key = 0;
1225
1226 hhd = &hash->buckets[key];
1227 hlist_add_head(&entry->hlist, hhd);
1228 hash->count++;
1229}
1230
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001231static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1232{
1233 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001234
1235 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1236 if (!entry)
1237 return -ENOMEM;
1238
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001239 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001240 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001241
1242 return 0;
1243}
1244
1245static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001246free_hash_entry(struct ftrace_hash *hash,
1247 struct ftrace_func_entry *entry)
1248{
1249 hlist_del(&entry->hlist);
1250 kfree(entry);
1251 hash->count--;
1252}
1253
1254static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001255remove_hash_entry(struct ftrace_hash *hash,
1256 struct ftrace_func_entry *entry)
1257{
1258 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001259 hash->count--;
1260}
1261
1262static void ftrace_hash_clear(struct ftrace_hash *hash)
1263{
1264 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001265 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001266 struct ftrace_func_entry *entry;
1267 int size = 1 << hash->size_bits;
1268 int i;
1269
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001270 if (!hash->count)
1271 return;
1272
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001273 for (i = 0; i < size; i++) {
1274 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001275 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001276 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001277 }
1278 FTRACE_WARN_ON(hash->count);
1279}
1280
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001281static void free_ftrace_hash(struct ftrace_hash *hash)
1282{
1283 if (!hash || hash == EMPTY_HASH)
1284 return;
1285 ftrace_hash_clear(hash);
1286 kfree(hash->buckets);
1287 kfree(hash);
1288}
1289
Steven Rostedt07fd5512011-05-05 18:03:47 -04001290static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1291{
1292 struct ftrace_hash *hash;
1293
1294 hash = container_of(rcu, struct ftrace_hash, rcu);
1295 free_ftrace_hash(hash);
1296}
1297
1298static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1299{
1300 if (!hash || hash == EMPTY_HASH)
1301 return;
1302 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1303}
1304
Jiri Olsa5500fa52012-02-15 15:51:54 +01001305void ftrace_free_filter(struct ftrace_ops *ops)
1306{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001307 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001308 free_ftrace_hash(ops->filter_hash);
1309 free_ftrace_hash(ops->notrace_hash);
1310}
1311
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001312static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1313{
1314 struct ftrace_hash *hash;
1315 int size;
1316
1317 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1318 if (!hash)
1319 return NULL;
1320
1321 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001322 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001323
1324 if (!hash->buckets) {
1325 kfree(hash);
1326 return NULL;
1327 }
1328
1329 hash->size_bits = size_bits;
1330
1331 return hash;
1332}
1333
1334static struct ftrace_hash *
1335alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1336{
1337 struct ftrace_func_entry *entry;
1338 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001339 int size;
1340 int ret;
1341 int i;
1342
1343 new_hash = alloc_ftrace_hash(size_bits);
1344 if (!new_hash)
1345 return NULL;
1346
1347 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001348 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001349 return new_hash;
1350
1351 size = 1 << hash->size_bits;
1352 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001353 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001354 ret = add_hash_entry(new_hash, entry->ip);
1355 if (ret < 0)
1356 goto free_hash;
1357 }
1358 }
1359
1360 FTRACE_WARN_ON(new_hash->count != hash->count);
1361
1362 return new_hash;
1363
1364 free_hash:
1365 free_ftrace_hash(new_hash);
1366 return NULL;
1367}
1368
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001369static void
1370ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1371static void
1372ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1373
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001374static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001375ftrace_hash_move(struct ftrace_ops *ops, int enable,
1376 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001377{
1378 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001379 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001380 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001381 struct ftrace_hash *old_hash;
1382 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001383 int size = src->count;
1384 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001385 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001386 int i;
1387
1388 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001389 * Remove the current set, update the hash and add
1390 * them back.
1391 */
1392 ftrace_hash_rec_disable(ops, enable);
1393
1394 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001395 * If the new source is empty, just free dst and assign it
1396 * the empty_hash.
1397 */
1398 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001399 free_ftrace_hash_rcu(*dst);
1400 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001401 /* still need to update the function records */
1402 ret = 0;
1403 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001404 }
1405
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001406 /*
1407 * Make the hash size about 1/2 the # found
1408 */
1409 for (size /= 2; size; size >>= 1)
1410 bits++;
1411
1412 /* Don't allocate too much */
1413 if (bits > FTRACE_HASH_MAX_BITS)
1414 bits = FTRACE_HASH_MAX_BITS;
1415
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001416 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001417 new_hash = alloc_ftrace_hash(bits);
1418 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001419 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001420
1421 size = 1 << src->size_bits;
1422 for (i = 0; i < size; i++) {
1423 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001424 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001425 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001426 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001427 }
1428 }
1429
Steven Rostedt07fd5512011-05-05 18:03:47 -04001430 old_hash = *dst;
1431 rcu_assign_pointer(*dst, new_hash);
1432 free_ftrace_hash_rcu(old_hash);
1433
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001434 ret = 0;
1435 out:
1436 /*
1437 * Enable regardless of ret:
1438 * On success, we enable the new hash.
1439 * On failure, we re-enable the original hash.
1440 */
1441 ftrace_hash_rec_enable(ops, enable);
1442
1443 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001444}
1445
Steven Rostedt265c8312009-02-13 12:43:56 -05001446/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001447 * Test the hashes for this ops to see if we want to call
1448 * the ops->func or not.
1449 *
1450 * It's a match if the ip is in the ops->filter_hash or
1451 * the filter_hash does not exist or is empty,
1452 * AND
1453 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001454 *
1455 * This needs to be called with preemption disabled as
1456 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001457 */
1458static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001459ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001460{
1461 struct ftrace_hash *filter_hash;
1462 struct ftrace_hash *notrace_hash;
1463 int ret;
1464
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001465#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1466 /*
1467 * There's a small race when adding ops that the ftrace handler
1468 * that wants regs, may be called without them. We can not
1469 * allow that handler to be called if regs is NULL.
1470 */
1471 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1472 return 0;
1473#endif
1474
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001475 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1476 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001477
Steven Rostedt06a51d92011-12-19 19:07:36 -05001478 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001479 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001480 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001481 !ftrace_lookup_ip(notrace_hash, ip)))
1482 ret = 1;
1483 else
1484 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001485
1486 return ret;
1487}
1488
1489/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001490 * This is a double for. Do not use 'break' to break out of the loop,
1491 * you must use a goto.
1492 */
1493#define do_for_each_ftrace_rec(pg, rec) \
1494 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1495 int _____i; \
1496 for (_____i = 0; _____i < pg->index; _____i++) { \
1497 rec = &pg->records[_____i];
1498
1499#define while_for_each_ftrace_rec() \
1500 } \
1501 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301502
Steven Rostedt5855fea2011-12-16 19:27:42 -05001503
1504static int ftrace_cmp_recs(const void *a, const void *b)
1505{
Steven Rostedta650e022012-04-25 13:48:13 -04001506 const struct dyn_ftrace *key = a;
1507 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001508
Steven Rostedta650e022012-04-25 13:48:13 -04001509 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001510 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001511 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1512 return 1;
1513 return 0;
1514}
1515
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001516static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001517{
1518 struct ftrace_page *pg;
1519 struct dyn_ftrace *rec;
1520 struct dyn_ftrace key;
1521
1522 key.ip = start;
1523 key.flags = end; /* overload flags, as it is unsigned long */
1524
1525 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1526 if (end < pg->records[0].ip ||
1527 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1528 continue;
1529 rec = bsearch(&key, pg->records, pg->index,
1530 sizeof(struct dyn_ftrace),
1531 ftrace_cmp_recs);
1532 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001533 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001534 }
1535
Steven Rostedt5855fea2011-12-16 19:27:42 -05001536 return 0;
1537}
1538
Steven Rostedtc88fd862011-08-16 09:53:39 -04001539/**
1540 * ftrace_location - return true if the ip giving is a traced location
1541 * @ip: the instruction pointer to check
1542 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001543 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001544 * That is, the instruction that is either a NOP or call to
1545 * the function tracer. It checks the ftrace internal tables to
1546 * determine if the address belongs or not.
1547 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001548unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001549{
Steven Rostedta650e022012-04-25 13:48:13 -04001550 return ftrace_location_range(ip, ip);
1551}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001552
Steven Rostedta650e022012-04-25 13:48:13 -04001553/**
1554 * ftrace_text_reserved - return true if range contains an ftrace location
1555 * @start: start of range to search
1556 * @end: end of range to search (inclusive). @end points to the last byte to check.
1557 *
1558 * Returns 1 if @start and @end contains a ftrace location.
1559 * That is, the instruction that is either a NOP or call to
1560 * the function tracer. It checks the ftrace internal tables to
1561 * determine if the address belongs or not.
1562 */
1563int ftrace_text_reserved(void *start, void *end)
1564{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001565 unsigned long ret;
1566
1567 ret = ftrace_location_range((unsigned long)start,
1568 (unsigned long)end);
1569
1570 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001571}
1572
Steven Rostedted926f92011-05-03 13:25:24 -04001573static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1574 int filter_hash,
1575 bool inc)
1576{
1577 struct ftrace_hash *hash;
1578 struct ftrace_hash *other_hash;
1579 struct ftrace_page *pg;
1580 struct dyn_ftrace *rec;
1581 int count = 0;
1582 int all = 0;
1583
1584 /* Only update if the ops has been registered */
1585 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1586 return;
1587
1588 /*
1589 * In the filter_hash case:
1590 * If the count is zero, we update all records.
1591 * Otherwise we just update the items in the hash.
1592 *
1593 * In the notrace_hash case:
1594 * We enable the update in the hash.
1595 * As disabling notrace means enabling the tracing,
1596 * and enabling notrace means disabling, the inc variable
1597 * gets inversed.
1598 */
1599 if (filter_hash) {
1600 hash = ops->filter_hash;
1601 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001602 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001603 all = 1;
1604 } else {
1605 inc = !inc;
1606 hash = ops->notrace_hash;
1607 other_hash = ops->filter_hash;
1608 /*
1609 * If the notrace hash has no items,
1610 * then there's nothing to do.
1611 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001612 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001613 return;
1614 }
1615
1616 do_for_each_ftrace_rec(pg, rec) {
1617 int in_other_hash = 0;
1618 int in_hash = 0;
1619 int match = 0;
1620
1621 if (all) {
1622 /*
1623 * Only the filter_hash affects all records.
1624 * Update if the record is not in the notrace hash.
1625 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001626 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001627 match = 1;
1628 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001629 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1630 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001631
1632 /*
1633 *
1634 */
1635 if (filter_hash && in_hash && !in_other_hash)
1636 match = 1;
1637 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001638 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001639 match = 1;
1640 }
1641 if (!match)
1642 continue;
1643
1644 if (inc) {
1645 rec->flags++;
1646 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1647 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001648 /*
1649 * If any ops wants regs saved for this function
1650 * then all ops will get saved regs.
1651 */
1652 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1653 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001654 } else {
1655 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1656 return;
1657 rec->flags--;
1658 }
1659 count++;
1660 /* Shortcut, if we handled all records, we are done. */
1661 if (!all && count == hash->count)
1662 return;
1663 } while_for_each_ftrace_rec();
1664}
1665
1666static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1667 int filter_hash)
1668{
1669 __ftrace_hash_rec_update(ops, filter_hash, 0);
1670}
1671
1672static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1673 int filter_hash)
1674{
1675 __ftrace_hash_rec_update(ops, filter_hash, 1);
1676}
1677
Steven Rostedt05736a42008-09-22 14:55:47 -07001678static void print_ip_ins(const char *fmt, unsigned char *p)
1679{
1680 int i;
1681
1682 printk(KERN_CONT "%s", fmt);
1683
1684 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1685 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1686}
1687
Steven Rostedtc88fd862011-08-16 09:53:39 -04001688/**
1689 * ftrace_bug - report and shutdown function tracer
1690 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1691 * @ip: The address that failed
1692 *
1693 * The arch code that enables or disables the function tracing
1694 * can call ftrace_bug() when it has detected a problem in
1695 * modifying the code. @failed should be one of either:
1696 * EFAULT - if the problem happens on reading the @ip address
1697 * EINVAL - if what is read at @ip is not what was expected
1698 * EPERM - if the problem happens on writting to the @ip address
1699 */
1700void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001701{
1702 switch (failed) {
1703 case -EFAULT:
1704 FTRACE_WARN_ON_ONCE(1);
1705 pr_info("ftrace faulted on modifying ");
1706 print_ip_sym(ip);
1707 break;
1708 case -EINVAL:
1709 FTRACE_WARN_ON_ONCE(1);
1710 pr_info("ftrace failed to modify ");
1711 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001712 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001713 printk(KERN_CONT "\n");
1714 break;
1715 case -EPERM:
1716 FTRACE_WARN_ON_ONCE(1);
1717 pr_info("ftrace faulted on writing ");
1718 print_ip_sym(ip);
1719 break;
1720 default:
1721 FTRACE_WARN_ON_ONCE(1);
1722 pr_info("ftrace faulted on unknown error ");
1723 print_ip_sym(ip);
1724 }
1725}
1726
Steven Rostedtc88fd862011-08-16 09:53:39 -04001727static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001728{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001729 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001730
Steven Rostedt982c3502008-11-15 16:31:41 -05001731 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001732 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001733 *
Steven Rostedted926f92011-05-03 13:25:24 -04001734 * If the record has a ref count, then we need to enable it
1735 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001736 *
Steven Rostedted926f92011-05-03 13:25:24 -04001737 * Otherwise we make sure its disabled.
1738 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001739 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001740 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001741 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001742 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001743 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001744
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001745 /*
1746 * If enabling and the REGS flag does not match the REGS_EN, then
1747 * do not ignore this record. Set flags to fail the compare against
1748 * ENABLED.
1749 */
1750 if (flag &&
1751 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1752 flag |= FTRACE_FL_REGS;
1753
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001754 /* If the state of this record hasn't changed, then do nothing */
1755 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001756 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001757
1758 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001759 /* Save off if rec is being enabled (for return value) */
1760 flag ^= rec->flags & FTRACE_FL_ENABLED;
1761
1762 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001763 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001764 if (flag & FTRACE_FL_REGS) {
1765 if (rec->flags & FTRACE_FL_REGS)
1766 rec->flags |= FTRACE_FL_REGS_EN;
1767 else
1768 rec->flags &= ~FTRACE_FL_REGS_EN;
1769 }
1770 }
1771
1772 /*
1773 * If this record is being updated from a nop, then
1774 * return UPDATE_MAKE_CALL.
1775 * Otherwise, if the EN flag is set, then return
1776 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1777 * from the non-save regs, to a save regs function.
1778 * Otherwise,
1779 * return UPDATE_MODIFY_CALL to tell the caller to convert
1780 * from the save regs, to a non-save regs function.
1781 */
1782 if (flag & FTRACE_FL_ENABLED)
1783 return FTRACE_UPDATE_MAKE_CALL;
1784 else if (rec->flags & FTRACE_FL_REGS_EN)
1785 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1786 else
1787 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001788 }
1789
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001790 if (update) {
1791 /* If there's no more users, clear all flags */
1792 if (!(rec->flags & ~FTRACE_FL_MASK))
1793 rec->flags = 0;
1794 else
1795 /* Just disable the record (keep REGS state) */
1796 rec->flags &= ~FTRACE_FL_ENABLED;
1797 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001798
1799 return FTRACE_UPDATE_MAKE_NOP;
1800}
1801
1802/**
1803 * ftrace_update_record, set a record that now is tracing or not
1804 * @rec: the record to update
1805 * @enable: set to 1 if the record is tracing, zero to force disable
1806 *
1807 * The records that represent all functions that can be traced need
1808 * to be updated when tracing has been enabled.
1809 */
1810int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1811{
1812 return ftrace_check_record(rec, enable, 1);
1813}
1814
1815/**
1816 * ftrace_test_record, check if the record has been enabled or not
1817 * @rec: the record to test
1818 * @enable: set to 1 to check if enabled, 0 if it is disabled
1819 *
1820 * The arch code may need to test if a record is already set to
1821 * tracing to determine how to modify the function code that it
1822 * represents.
1823 */
1824int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1825{
1826 return ftrace_check_record(rec, enable, 0);
1827}
1828
1829static int
1830__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1831{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001832 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001833 unsigned long ftrace_addr;
1834 int ret;
1835
Steven Rostedtc88fd862011-08-16 09:53:39 -04001836 ret = ftrace_update_record(rec, enable);
1837
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001838 if (rec->flags & FTRACE_FL_REGS)
1839 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1840 else
1841 ftrace_addr = (unsigned long)FTRACE_ADDR;
1842
Steven Rostedtc88fd862011-08-16 09:53:39 -04001843 switch (ret) {
1844 case FTRACE_UPDATE_IGNORE:
1845 return 0;
1846
1847 case FTRACE_UPDATE_MAKE_CALL:
1848 return ftrace_make_call(rec, ftrace_addr);
1849
1850 case FTRACE_UPDATE_MAKE_NOP:
1851 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001852
1853 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1854 case FTRACE_UPDATE_MODIFY_CALL:
1855 if (rec->flags & FTRACE_FL_REGS)
1856 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1857 else
1858 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1859
1860 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001861 }
1862
1863 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001864}
1865
Steven Rostedte4f5d542012-04-27 09:13:18 -04001866void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001867{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001868 struct dyn_ftrace *rec;
1869 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001870 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001871
Steven Rostedt45a4a232011-04-21 23:16:46 -04001872 if (unlikely(ftrace_disabled))
1873 return;
1874
Steven Rostedt265c8312009-02-13 12:43:56 -05001875 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001876 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001877 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001878 ftrace_bug(failed, rec->ip);
1879 /* Stop processing */
1880 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001881 }
1882 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001883}
1884
Steven Rostedtc88fd862011-08-16 09:53:39 -04001885struct ftrace_rec_iter {
1886 struct ftrace_page *pg;
1887 int index;
1888};
1889
1890/**
1891 * ftrace_rec_iter_start, start up iterating over traced functions
1892 *
1893 * Returns an iterator handle that is used to iterate over all
1894 * the records that represent address locations where functions
1895 * are traced.
1896 *
1897 * May return NULL if no records are available.
1898 */
1899struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1900{
1901 /*
1902 * We only use a single iterator.
1903 * Protected by the ftrace_lock mutex.
1904 */
1905 static struct ftrace_rec_iter ftrace_rec_iter;
1906 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1907
1908 iter->pg = ftrace_pages_start;
1909 iter->index = 0;
1910
1911 /* Could have empty pages */
1912 while (iter->pg && !iter->pg->index)
1913 iter->pg = iter->pg->next;
1914
1915 if (!iter->pg)
1916 return NULL;
1917
1918 return iter;
1919}
1920
1921/**
1922 * ftrace_rec_iter_next, get the next record to process.
1923 * @iter: The handle to the iterator.
1924 *
1925 * Returns the next iterator after the given iterator @iter.
1926 */
1927struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1928{
1929 iter->index++;
1930
1931 if (iter->index >= iter->pg->index) {
1932 iter->pg = iter->pg->next;
1933 iter->index = 0;
1934
1935 /* Could have empty pages */
1936 while (iter->pg && !iter->pg->index)
1937 iter->pg = iter->pg->next;
1938 }
1939
1940 if (!iter->pg)
1941 return NULL;
1942
1943 return iter;
1944}
1945
1946/**
1947 * ftrace_rec_iter_record, get the record at the iterator location
1948 * @iter: The current iterator location
1949 *
1950 * Returns the record that the current @iter is at.
1951 */
1952struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1953{
1954 return &iter->pg->records[iter->index];
1955}
1956
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301957static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001958ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001959{
1960 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001961 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001962
1963 ip = rec->ip;
1964
Steven Rostedt45a4a232011-04-21 23:16:46 -04001965 if (unlikely(ftrace_disabled))
1966 return 0;
1967
Shaohua Li25aac9d2009-01-09 11:29:40 +08001968 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001969 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001970 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301971 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001972 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301973 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001974}
1975
Steven Rostedt000ab692009-02-17 13:35:06 -05001976/*
1977 * archs can override this function if they must do something
1978 * before the modifying code is performed.
1979 */
1980int __weak ftrace_arch_code_modify_prepare(void)
1981{
1982 return 0;
1983}
1984
1985/*
1986 * archs can override this function if they must do something
1987 * after the modifying code is performed.
1988 */
1989int __weak ftrace_arch_code_modify_post_process(void)
1990{
1991 return 0;
1992}
1993
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001994void ftrace_modify_all_code(int command)
1995{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001996 int update = command & FTRACE_UPDATE_TRACE_FUNC;
1997
1998 /*
1999 * If the ftrace_caller calls a ftrace_ops func directly,
2000 * we need to make sure that it only traces functions it
2001 * expects to trace. When doing the switch of functions,
2002 * we need to update to the ftrace_ops_list_func first
2003 * before the transition between old and new calls are set,
2004 * as the ftrace_ops_list_func will check the ops hashes
2005 * to make sure the ops are having the right functions
2006 * traced.
2007 */
2008 if (update)
2009 ftrace_update_ftrace_func(ftrace_ops_list_func);
2010
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002011 if (command & FTRACE_UPDATE_CALLS)
2012 ftrace_replace_code(1);
2013 else if (command & FTRACE_DISABLE_CALLS)
2014 ftrace_replace_code(0);
2015
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002016 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2017 function_trace_op = set_function_trace_op;
2018 smp_wmb();
2019 /* If irqs are disabled, we are in stop machine */
2020 if (!irqs_disabled())
2021 smp_call_function(ftrace_sync_ipi, NULL, 1);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002022 ftrace_update_ftrace_func(ftrace_trace_function);
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002023 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002024
2025 if (command & FTRACE_START_FUNC_RET)
2026 ftrace_enable_ftrace_graph_caller();
2027 else if (command & FTRACE_STOP_FUNC_RET)
2028 ftrace_disable_ftrace_graph_caller();
2029}
2030
Ingo Molnare309b412008-05-12 21:20:51 +02002031static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002032{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002033 int *command = data;
2034
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002035 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002036
Steven Rostedtc88fd862011-08-16 09:53:39 -04002037 return 0;
2038}
2039
2040/**
2041 * ftrace_run_stop_machine, go back to the stop machine method
2042 * @command: The command to tell ftrace what to do
2043 *
2044 * If an arch needs to fall back to the stop machine method, the
2045 * it can call this function.
2046 */
2047void ftrace_run_stop_machine(int command)
2048{
2049 stop_machine(__ftrace_modify_code, &command, NULL);
2050}
2051
2052/**
2053 * arch_ftrace_update_code, modify the code to trace or not trace
2054 * @command: The command that needs to be done
2055 *
2056 * Archs can override this function if it does not need to
2057 * run stop_machine() to modify code.
2058 */
2059void __weak arch_ftrace_update_code(int command)
2060{
2061 ftrace_run_stop_machine(command);
2062}
2063
2064static void ftrace_run_update_code(int command)
2065{
2066 int ret;
2067
2068 ret = ftrace_arch_code_modify_prepare();
2069 FTRACE_WARN_ON(ret);
2070 if (ret)
2071 return;
2072 /*
2073 * Do not call function tracer while we update the code.
2074 * We are in stop machine.
2075 */
2076 function_trace_stop++;
2077
2078 /*
2079 * By default we use stop_machine() to modify the code.
2080 * But archs can do what ever they want as long as it
2081 * is safe. The stop_machine() is the safest, but also
2082 * produces the most overhead.
2083 */
2084 arch_ftrace_update_code(command);
2085
Steven Rostedt6331c282011-07-13 15:11:02 -04002086 function_trace_stop--;
2087
Steven Rostedt000ab692009-02-17 13:35:06 -05002088 ret = ftrace_arch_code_modify_post_process();
2089 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002090}
2091
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002092static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002093static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002094static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002095
2096static void ftrace_startup_enable(int command)
2097{
2098 if (saved_ftrace_func != ftrace_trace_function) {
2099 saved_ftrace_func = ftrace_trace_function;
2100 command |= FTRACE_UPDATE_TRACE_FUNC;
2101 }
2102
2103 if (!command || !ftrace_enabled)
2104 return;
2105
2106 ftrace_run_update_code(command);
2107}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002108
Steven Rostedta1cd6172011-05-23 15:24:25 -04002109static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002110{
Steven Rostedtb8489142011-05-04 09:27:52 -04002111 bool hash_enable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002112 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002113
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002114 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002115 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002116
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002117 ret = __register_ftrace_function(ops);
2118 if (ret)
2119 return ret;
2120
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002121 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002122 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002123
Steven Rostedtb8489142011-05-04 09:27:52 -04002124 /* ops marked global share the filter hashes */
2125 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2126 ops = &global_ops;
2127 /* Don't update hash if global is already set */
2128 if (global_start_up)
2129 hash_enable = false;
2130 global_start_up++;
2131 }
2132
Steven Rostedted926f92011-05-03 13:25:24 -04002133 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002134 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002135 ftrace_hash_rec_enable(ops, 1);
2136
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002137 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002138
2139 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002140}
2141
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002142static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002143{
Steven Rostedtb8489142011-05-04 09:27:52 -04002144 bool hash_disable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002145 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002146
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002147 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002148 return -ENODEV;
2149
2150 ret = __unregister_ftrace_function(ops);
2151 if (ret)
2152 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002153
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002154 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002155 /*
2156 * Just warn in case of unbalance, no need to kill ftrace, it's not
2157 * critical but the ftrace_call callers may be never nopped again after
2158 * further ftrace uses.
2159 */
2160 WARN_ON_ONCE(ftrace_start_up < 0);
2161
Steven Rostedtb8489142011-05-04 09:27:52 -04002162 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2163 ops = &global_ops;
2164 global_start_up--;
2165 WARN_ON_ONCE(global_start_up < 0);
2166 /* Don't update hash if global still has users */
2167 if (global_start_up) {
2168 WARN_ON_ONCE(!ftrace_start_up);
2169 hash_disable = false;
2170 }
2171 }
2172
2173 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002174 ftrace_hash_rec_disable(ops, 1);
2175
Steven Rostedtb8489142011-05-04 09:27:52 -04002176 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002177 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002178
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002179 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002180
2181 if (saved_ftrace_func != ftrace_trace_function) {
2182 saved_ftrace_func = ftrace_trace_function;
2183 command |= FTRACE_UPDATE_TRACE_FUNC;
2184 }
2185
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002186 if (!command || !ftrace_enabled) {
2187 /*
2188 * If these are control ops, they still need their
2189 * per_cpu field freed. Since, function tracing is
2190 * not currently active, we can just free them
2191 * without synchronizing all CPUs.
2192 */
2193 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2194 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002195 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002196 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002197
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002198 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002199
2200 /*
2201 * Dynamic ops may be freed, we must make sure that all
2202 * callers are done before leaving this function.
2203 * The same goes for freeing the per_cpu data of the control
2204 * ops.
2205 *
2206 * Again, normal synchronize_sched() is not good enough.
2207 * We need to do a hard force of sched synchronization.
2208 * This is because we use preempt_disable() to do RCU, but
2209 * the function tracers can be called where RCU is not watching
2210 * (like before user_exit()). We can not rely on the RCU
2211 * infrastructure to do the synchronization, thus we must do it
2212 * ourselves.
2213 */
2214 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2215 schedule_on_each_cpu(ftrace_sync);
2216
2217 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2218 control_ops_free(ops);
2219 }
2220
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002221 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002222}
2223
Ingo Molnare309b412008-05-12 21:20:51 +02002224static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002225{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002226 if (unlikely(ftrace_disabled))
2227 return;
2228
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002229 /* Force update next time */
2230 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002231 /* ftrace_start_up is true if we want ftrace running */
2232 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002233 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002234}
2235
Ingo Molnare309b412008-05-12 21:20:51 +02002236static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002237{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002238 if (unlikely(ftrace_disabled))
2239 return;
2240
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002241 /* ftrace_start_up is true if ftrace is running */
2242 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002243 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002244}
2245
Steven Rostedt3d083392008-05-12 21:20:42 +02002246static cycle_t ftrace_update_time;
2247static unsigned long ftrace_update_cnt;
2248unsigned long ftrace_update_tot_cnt;
2249
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002250static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002251{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002252 /*
2253 * Filter_hash being empty will default to trace module.
2254 * But notrace hash requires a test of individual module functions.
2255 */
2256 return ftrace_hash_empty(ops->filter_hash) &&
2257 ftrace_hash_empty(ops->notrace_hash);
2258}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002259
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002260/*
2261 * Check if the current ops references the record.
2262 *
2263 * If the ops traces all functions, then it was already accounted for.
2264 * If the ops does not trace the current record function, skip it.
2265 * If the ops ignores the function via notrace filter, skip it.
2266 */
2267static inline bool
2268ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2269{
2270 /* If ops isn't enabled, ignore it */
2271 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2272 return 0;
2273
2274 /* If ops traces all mods, we already accounted for it */
2275 if (ops_traces_mod(ops))
2276 return 0;
2277
2278 /* The function must be in the filter */
2279 if (!ftrace_hash_empty(ops->filter_hash) &&
2280 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2281 return 0;
2282
2283 /* If in notrace hash, we ignore it too */
2284 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2285 return 0;
2286
2287 return 1;
2288}
2289
2290static int referenced_filters(struct dyn_ftrace *rec)
2291{
2292 struct ftrace_ops *ops;
2293 int cnt = 0;
2294
2295 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2296 if (ops_references_rec(ops, rec))
2297 cnt++;
2298 }
2299
2300 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002301}
2302
Steven Rostedt31e88902008-11-14 16:21:19 -08002303static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002304{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002305 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002306 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302307 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002308 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002309 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002310 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002311
2312 /*
2313 * When adding a module, we need to check if tracers are
2314 * currently enabled and if they are set to trace all functions.
2315 * If they are, we need to enable the module functions as well
2316 * as update the reference counts for those function records.
2317 */
2318 if (mod) {
2319 struct ftrace_ops *ops;
2320
2321 for (ops = ftrace_ops_list;
2322 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002323 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2324 if (ops_traces_mod(ops))
2325 ref++;
2326 else
2327 test = true;
2328 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002329 }
2330 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002331
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002332 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002333 ftrace_update_cnt = 0;
2334
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002335 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302336
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002337 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002338 int cnt = ref;
2339
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002340 /* If something went wrong, bail without enabling anything */
2341 if (unlikely(ftrace_disabled))
2342 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002343
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002344 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002345 if (test)
2346 cnt += referenced_filters(p);
2347 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302348
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002349 /*
2350 * Do the initial record conversion from mcount jump
2351 * to the NOP instructions.
2352 */
2353 if (!ftrace_code_disable(mod, p))
2354 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002355
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002356 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002357
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002358 /*
2359 * If the tracing is enabled, go ahead and enable the record.
2360 *
2361 * The reason not to enable the record immediatelly is the
2362 * inherent check of ftrace_make_nop/ftrace_make_call for
2363 * correct previous instructions. Making first the NOP
2364 * conversion puts the module to the correct state, thus
2365 * passing the ftrace_make_call check.
2366 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002367 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002368 int failed = __ftrace_replace_code(p, 1);
2369 if (failed)
2370 ftrace_bug(failed, p->ip);
2371 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002372 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002373 }
2374
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002375 ftrace_new_pgs = NULL;
2376
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002377 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002378 ftrace_update_time = stop - start;
2379 ftrace_update_tot_cnt += ftrace_update_cnt;
2380
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002381 return 0;
2382}
2383
Steven Rostedta7900872011-12-16 16:23:44 -05002384static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002385{
Steven Rostedta7900872011-12-16 16:23:44 -05002386 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002387 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002388
Steven Rostedta7900872011-12-16 16:23:44 -05002389 if (WARN_ON(!count))
2390 return -EINVAL;
2391
2392 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002393
2394 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002395 * We want to fill as much as possible. No more than a page
2396 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002397 */
Steven Rostedta7900872011-12-16 16:23:44 -05002398 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2399 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002400
Steven Rostedta7900872011-12-16 16:23:44 -05002401 again:
2402 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2403
2404 if (!pg->records) {
2405 /* if we can't allocate this size, try something smaller */
2406 if (!order)
2407 return -ENOMEM;
2408 order >>= 1;
2409 goto again;
2410 }
2411
2412 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2413 pg->size = cnt;
2414
2415 if (cnt > count)
2416 cnt = count;
2417
2418 return cnt;
2419}
2420
2421static struct ftrace_page *
2422ftrace_allocate_pages(unsigned long num_to_init)
2423{
2424 struct ftrace_page *start_pg;
2425 struct ftrace_page *pg;
2426 int order;
2427 int cnt;
2428
2429 if (!num_to_init)
2430 return 0;
2431
2432 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2433 if (!pg)
2434 return NULL;
2435
2436 /*
2437 * Try to allocate as much as possible in one continues
2438 * location that fills in all of the space. We want to
2439 * waste as little space as possible.
2440 */
2441 for (;;) {
2442 cnt = ftrace_allocate_records(pg, num_to_init);
2443 if (cnt < 0)
2444 goto free_pages;
2445
2446 num_to_init -= cnt;
2447 if (!num_to_init)
2448 break;
2449
2450 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2451 if (!pg->next)
2452 goto free_pages;
2453
2454 pg = pg->next;
2455 }
2456
2457 return start_pg;
2458
2459 free_pages:
2460 while (start_pg) {
2461 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2462 free_pages((unsigned long)pg->records, order);
2463 start_pg = pg->next;
2464 kfree(pg);
2465 pg = start_pg;
2466 }
2467 pr_info("ftrace: FAILED to allocate memory for functions\n");
2468 return NULL;
2469}
2470
2471static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2472{
2473 int cnt;
2474
2475 if (!num_to_init) {
2476 pr_info("ftrace: No functions to be traced?\n");
2477 return -1;
2478 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002479
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002480 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002481 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002482 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002483
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002484 return 0;
2485}
2486
Steven Rostedt5072c592008-05-12 21:20:43 +02002487#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2488
2489struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002490 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002491 loff_t func_pos;
2492 struct ftrace_page *pg;
2493 struct dyn_ftrace *func;
2494 struct ftrace_func_probe *probe;
2495 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002496 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002497 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002498 int hidx;
2499 int idx;
2500 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002501};
2502
Ingo Molnare309b412008-05-12 21:20:51 +02002503static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002504t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002505{
2506 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002507 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002508 struct hlist_head *hhd;
2509
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002510 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002511 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002512
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002513 if (iter->probe)
2514 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002515 retry:
2516 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2517 return NULL;
2518
2519 hhd = &ftrace_func_hash[iter->hidx];
2520
2521 if (hlist_empty(hhd)) {
2522 iter->hidx++;
2523 hnd = NULL;
2524 goto retry;
2525 }
2526
2527 if (!hnd)
2528 hnd = hhd->first;
2529 else {
2530 hnd = hnd->next;
2531 if (!hnd) {
2532 iter->hidx++;
2533 goto retry;
2534 }
2535 }
2536
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002537 if (WARN_ON_ONCE(!hnd))
2538 return NULL;
2539
2540 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2541
2542 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002543}
2544
2545static void *t_hash_start(struct seq_file *m, loff_t *pos)
2546{
2547 struct ftrace_iterator *iter = m->private;
2548 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002549 loff_t l;
2550
Steven Rostedt69a30832011-12-19 15:21:16 -05002551 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2552 return NULL;
2553
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002554 if (iter->func_pos > *pos)
2555 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002556
Li Zefand82d6242009-06-24 09:54:54 +08002557 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002558 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002559 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002560 if (!p)
2561 break;
2562 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002563 if (!p)
2564 return NULL;
2565
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002566 /* Only set this if we have an item */
2567 iter->flags |= FTRACE_ITER_HASH;
2568
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002569 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002570}
2571
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002572static int
2573t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002574{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002575 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002576
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002577 rec = iter->probe;
2578 if (WARN_ON_ONCE(!rec))
2579 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002580
Steven Rostedt809dcf22009-02-16 23:06:01 -05002581 if (rec->ops->print)
2582 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2583
Steven Rostedtb375a112009-09-17 00:05:58 -04002584 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002585
2586 if (rec->data)
2587 seq_printf(m, ":%p", rec->data);
2588 seq_putc(m, '\n');
2589
2590 return 0;
2591}
2592
2593static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002594t_next(struct seq_file *m, void *v, loff_t *pos)
2595{
2596 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002597 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002598 struct dyn_ftrace *rec = NULL;
2599
Steven Rostedt45a4a232011-04-21 23:16:46 -04002600 if (unlikely(ftrace_disabled))
2601 return NULL;
2602
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002603 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002604 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002605
Steven Rostedt5072c592008-05-12 21:20:43 +02002606 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002607 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002608
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002609 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002610 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002611
Steven Rostedt5072c592008-05-12 21:20:43 +02002612 retry:
2613 if (iter->idx >= iter->pg->index) {
2614 if (iter->pg->next) {
2615 iter->pg = iter->pg->next;
2616 iter->idx = 0;
2617 goto retry;
2618 }
2619 } else {
2620 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002621 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002622 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002623
Steven Rostedt41c52c02008-05-22 11:46:33 -04002624 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002625 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2626
2627 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002628 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002629
Steven Rostedt5072c592008-05-12 21:20:43 +02002630 rec = NULL;
2631 goto retry;
2632 }
2633 }
2634
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002635 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002636 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002637
2638 iter->func = rec;
2639
2640 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002641}
2642
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002643static void reset_iter_read(struct ftrace_iterator *iter)
2644{
2645 iter->pos = 0;
2646 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002647 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002648}
2649
2650static void *t_start(struct seq_file *m, loff_t *pos)
2651{
2652 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002653 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002654 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002655 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002656
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002657 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002658
2659 if (unlikely(ftrace_disabled))
2660 return NULL;
2661
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002662 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002663 * If an lseek was done, then reset and start from beginning.
2664 */
2665 if (*pos < iter->pos)
2666 reset_iter_read(iter);
2667
2668 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002669 * For set_ftrace_filter reading, if we have the filter
2670 * off, we can short cut and just print out that all
2671 * functions are enabled.
2672 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002673 if (iter->flags & FTRACE_ITER_FILTER &&
2674 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002675 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002676 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002677 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002678 /* reset in case of seek/pread */
2679 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002680 return iter;
2681 }
2682
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002683 if (iter->flags & FTRACE_ITER_HASH)
2684 return t_hash_start(m, pos);
2685
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002686 /*
2687 * Unfortunately, we need to restart at ftrace_pages_start
2688 * every time we let go of the ftrace_mutex. This is because
2689 * those pointers can change without the lock.
2690 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002691 iter->pg = ftrace_pages_start;
2692 iter->idx = 0;
2693 for (l = 0; l <= *pos; ) {
2694 p = t_next(m, p, &l);
2695 if (!p)
2696 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002697 }
walimis5821e1b2008-11-15 15:19:06 +08002698
Steven Rostedt69a30832011-12-19 15:21:16 -05002699 if (!p)
2700 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002701
2702 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002703}
2704
2705static void t_stop(struct seq_file *m, void *p)
2706{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002707 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002708}
2709
2710static int t_show(struct seq_file *m, void *v)
2711{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002712 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002713 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002714
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002715 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002716 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002717
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002718 if (iter->flags & FTRACE_ITER_PRINTALL) {
2719 seq_printf(m, "#### all functions enabled ####\n");
2720 return 0;
2721 }
2722
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002723 rec = iter->func;
2724
Steven Rostedt5072c592008-05-12 21:20:43 +02002725 if (!rec)
2726 return 0;
2727
Steven Rostedt647bcd02011-05-03 14:39:21 -04002728 seq_printf(m, "%ps", (void *)rec->ip);
2729 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002730 seq_printf(m, " (%ld)%s",
2731 rec->flags & ~FTRACE_FL_MASK,
2732 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002733 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002734
2735 return 0;
2736}
2737
James Morris88e9d342009-09-22 16:43:43 -07002738static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002739 .start = t_start,
2740 .next = t_next,
2741 .stop = t_stop,
2742 .show = t_show,
2743};
2744
Ingo Molnare309b412008-05-12 21:20:51 +02002745static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002746ftrace_avail_open(struct inode *inode, struct file *file)
2747{
2748 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002749
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002750 if (unlikely(ftrace_disabled))
2751 return -ENODEV;
2752
Jiri Olsa50e18b92012-04-25 10:23:39 +02002753 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2754 if (iter) {
2755 iter->pg = ftrace_pages_start;
2756 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002757 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002758
Jiri Olsa50e18b92012-04-25 10:23:39 +02002759 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002760}
2761
Steven Rostedt647bcd02011-05-03 14:39:21 -04002762static int
2763ftrace_enabled_open(struct inode *inode, struct file *file)
2764{
2765 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002766
2767 if (unlikely(ftrace_disabled))
2768 return -ENODEV;
2769
Jiri Olsa50e18b92012-04-25 10:23:39 +02002770 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2771 if (iter) {
2772 iter->pg = ftrace_pages_start;
2773 iter->flags = FTRACE_ITER_ENABLED;
2774 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002775 }
2776
Jiri Olsa50e18b92012-04-25 10:23:39 +02002777 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002778}
2779
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002780static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002781{
Steven Rostedt52baf112009-02-14 01:15:39 -05002782 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002783 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002784 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002785}
2786
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002787/**
2788 * ftrace_regex_open - initialize function tracer filter files
2789 * @ops: The ftrace_ops that hold the hash filters
2790 * @flag: The type of filter to process
2791 * @inode: The inode, usually passed in to your open routine
2792 * @file: The file, usually passed in to your open routine
2793 *
2794 * ftrace_regex_open() initializes the filter files for the
2795 * @ops. Depending on @flag it may process the filter hash or
2796 * the notrace hash of @ops. With this called from the open
2797 * routine, you can use ftrace_filter_write() for the write
2798 * routine if @flag has FTRACE_ITER_FILTER set, or
2799 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05002800 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002801 * release must call ftrace_regex_release().
2802 */
2803int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002804ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002805 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002806{
2807 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002808 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002809 int ret = 0;
2810
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002811 ftrace_ops_init(ops);
2812
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002813 if (unlikely(ftrace_disabled))
2814 return -ENODEV;
2815
Steven Rostedt5072c592008-05-12 21:20:43 +02002816 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2817 if (!iter)
2818 return -ENOMEM;
2819
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002820 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2821 kfree(iter);
2822 return -ENOMEM;
2823 }
2824
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002825 iter->ops = ops;
2826 iter->flags = flag;
2827
2828 mutex_lock(&ops->regex_lock);
2829
Steven Rostedtf45948e2011-05-02 12:29:25 -04002830 if (flag & FTRACE_ITER_NOTRACE)
2831 hash = ops->notrace_hash;
2832 else
2833 hash = ops->filter_hash;
2834
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002835 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002836 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002837 if (!iter->hash) {
2838 trace_parser_put(&iter->parser);
2839 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002840 ret = -ENOMEM;
2841 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002842 }
2843 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002844
Steven Rostedt5072c592008-05-12 21:20:43 +02002845 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002846 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002847 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002848
2849 if (file->f_mode & FMODE_READ) {
2850 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002851
2852 ret = seq_open(file, &show_ftrace_seq_ops);
2853 if (!ret) {
2854 struct seq_file *m = file->private_data;
2855 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002856 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002857 /* Failed */
2858 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002859 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002860 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002861 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002862 } else
2863 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002864
2865 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002866 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002867
2868 return ret;
2869}
2870
Steven Rostedt41c52c02008-05-22 11:46:33 -04002871static int
2872ftrace_filter_open(struct inode *inode, struct file *file)
2873{
Steven Rostedt69a30832011-12-19 15:21:16 -05002874 return ftrace_regex_open(&global_ops,
2875 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2876 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002877}
2878
2879static int
2880ftrace_notrace_open(struct inode *inode, struct file *file)
2881{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002882 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002883 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002884}
2885
Steven Rostedt64e7c442009-02-13 17:08:48 -05002886static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002887{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002888 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002889 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002890
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002891 switch (type) {
2892 case MATCH_FULL:
2893 if (strcmp(str, regex) == 0)
2894 matched = 1;
2895 break;
2896 case MATCH_FRONT_ONLY:
2897 if (strncmp(str, regex, len) == 0)
2898 matched = 1;
2899 break;
2900 case MATCH_MIDDLE_ONLY:
2901 if (strstr(str, regex))
2902 matched = 1;
2903 break;
2904 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002905 slen = strlen(str);
2906 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002907 matched = 1;
2908 break;
2909 }
2910
2911 return matched;
2912}
2913
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002914static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002915enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002916{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002917 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002918 int ret = 0;
2919
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002920 entry = ftrace_lookup_ip(hash, rec->ip);
2921 if (not) {
2922 /* Do nothing if it doesn't exist */
2923 if (!entry)
2924 return 0;
2925
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002926 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002927 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002928 /* Do nothing if it exists */
2929 if (entry)
2930 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002931
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002932 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002933 }
2934 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002935}
2936
Steven Rostedt64e7c442009-02-13 17:08:48 -05002937static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002938ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2939 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002940{
2941 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002942 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002943
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002944 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2945
2946 if (mod) {
2947 /* module lookup requires matching the module */
2948 if (!modname || strcmp(modname, mod))
2949 return 0;
2950
2951 /* blank search means to match all funcs in the mod */
2952 if (!len)
2953 return 1;
2954 }
2955
Steven Rostedt64e7c442009-02-13 17:08:48 -05002956 return ftrace_match(str, regex, len, type);
2957}
2958
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002959static int
2960match_records(struct ftrace_hash *hash, char *buff,
2961 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002962{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002963 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002964 struct ftrace_page *pg;
2965 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002966 int type = MATCH_FULL;
2967 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002968 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002969 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002970
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002971 if (len) {
2972 type = filter_parse_regex(buff, len, &search, &not);
2973 search_len = strlen(search);
2974 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002975
Steven Rostedt52baf112009-02-14 01:15:39 -05002976 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002977
2978 if (unlikely(ftrace_disabled))
2979 goto out_unlock;
2980
Steven Rostedt265c8312009-02-13 12:43:56 -05002981 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002982 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002983 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002984 if (ret < 0) {
2985 found = ret;
2986 goto out_unlock;
2987 }
Li Zefan311d16d2009-12-08 11:15:11 +08002988 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002989 }
2990 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002991 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002992 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002993
2994 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002995}
2996
Steven Rostedt64e7c442009-02-13 17:08:48 -05002997static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002998ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002999{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003000 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003001}
3002
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003003static int
3004ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003005{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003006 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003007
Steven Rostedt64e7c442009-02-13 17:08:48 -05003008 /* blank or '*' mean the same */
3009 if (strcmp(buff, "*") == 0)
3010 buff[0] = 0;
3011
3012 /* handle the case of 'dont filter this module' */
3013 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3014 buff[0] = 0;
3015 not = 1;
3016 }
3017
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003018 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003019}
3020
Steven Rostedtf6180772009-02-14 00:40:25 -05003021/*
3022 * We register the module command as a template to show others how
3023 * to register the a command as well.
3024 */
3025
3026static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003027ftrace_mod_callback(struct ftrace_hash *hash,
3028 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003029{
3030 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003031 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003032
3033 /*
3034 * cmd == 'mod' because we only registered this func
3035 * for the 'mod' ftrace_func_command.
3036 * But if you register one func with multiple commands,
3037 * you can tell which command was used by the cmd
3038 * parameter.
3039 */
3040
3041 /* we must have a module name */
3042 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003043 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003044
3045 mod = strsep(&param, ":");
3046 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003047 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003048
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003049 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003050 if (!ret)
3051 ret = -EINVAL;
3052 if (ret < 0)
3053 return ret;
3054
3055 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003056}
3057
3058static struct ftrace_func_command ftrace_mod_cmd = {
3059 .name = "mod",
3060 .func = ftrace_mod_callback,
3061};
3062
3063static int __init ftrace_mod_cmd_init(void)
3064{
3065 return register_ftrace_command(&ftrace_mod_cmd);
3066}
Steven Rostedt6f415672012-10-05 12:13:07 -04003067core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003068
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003069static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003070 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003071{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003072 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003073 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003074 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003075
3076 key = hash_long(ip, FTRACE_HASH_BITS);
3077
3078 hhd = &ftrace_func_hash[key];
3079
3080 if (hlist_empty(hhd))
3081 return;
3082
3083 /*
3084 * Disable preemption for these calls to prevent a RCU grace
3085 * period. This syncs the hash iteration and freeing of items
3086 * on the hash. rcu_read_lock is too dangerous here.
3087 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003088 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003089 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003090 if (entry->ip == ip)
3091 entry->ops->func(ip, parent_ip, &entry->data);
3092 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003093 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003094}
3095
Steven Rostedtb6887d72009-02-17 12:32:04 -05003096static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003097{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003098 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003099 .flags = FTRACE_OPS_FL_INITIALIZED,
3100 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003101};
3102
Steven Rostedtb6887d72009-02-17 12:32:04 -05003103static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003104
Steven Rostedtb6887d72009-02-17 12:32:04 -05003105static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003106{
Steven Rostedtb8489142011-05-04 09:27:52 -04003107 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003108 int i;
3109
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003110 if (ftrace_probe_registered) {
3111 /* still need to update the function call sites */
3112 if (ftrace_enabled)
3113 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003114 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003115 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003116
3117 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3118 struct hlist_head *hhd = &ftrace_func_hash[i];
3119 if (hhd->first)
3120 break;
3121 }
3122 /* Nothing registered? */
3123 if (i == FTRACE_FUNC_HASHSIZE)
3124 return;
3125
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003126 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003127
Steven Rostedtb6887d72009-02-17 12:32:04 -05003128 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003129}
3130
Steven Rostedtb6887d72009-02-17 12:32:04 -05003131static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003132{
3133 int i;
3134
Steven Rostedtb6887d72009-02-17 12:32:04 -05003135 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003136 return;
3137
3138 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3139 struct hlist_head *hhd = &ftrace_func_hash[i];
3140 if (hhd->first)
3141 return;
3142 }
3143
3144 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003145 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003146
Steven Rostedtb6887d72009-02-17 12:32:04 -05003147 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003148}
3149
3150
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003151static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003152{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003153 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003154 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003155 kfree(entry);
3156}
3157
Steven Rostedt59df055f2009-02-14 15:29:06 -05003158int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003159register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003160 void *data)
3161{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003162 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003163 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3164 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003165 struct ftrace_page *pg;
3166 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003167 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003168 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003169 int count = 0;
3170 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003171 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003172
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003173 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003174 len = strlen(search);
3175
Steven Rostedtb6887d72009-02-17 12:32:04 -05003176 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003177 if (WARN_ON(not))
3178 return -EINVAL;
3179
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003180 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003181
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003182 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3183 if (!hash) {
3184 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003185 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003186 }
3187
3188 if (unlikely(ftrace_disabled)) {
3189 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003190 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003191 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003192
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003193 mutex_lock(&ftrace_lock);
3194
Steven Rostedt59df055f2009-02-14 15:29:06 -05003195 do_for_each_ftrace_rec(pg, rec) {
3196
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003197 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003198 continue;
3199
3200 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3201 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003202 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003203 if (!count)
3204 count = -ENOMEM;
3205 goto out_unlock;
3206 }
3207
3208 count++;
3209
3210 entry->data = data;
3211
3212 /*
3213 * The caller might want to do something special
3214 * for each function we find. We call the callback
3215 * to give the caller an opportunity to do so.
3216 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003217 if (ops->init) {
3218 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003219 /* caller does not like this func */
3220 kfree(entry);
3221 continue;
3222 }
3223 }
3224
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003225 ret = enter_record(hash, rec, 0);
3226 if (ret < 0) {
3227 kfree(entry);
3228 count = ret;
3229 goto out_unlock;
3230 }
3231
Steven Rostedt59df055f2009-02-14 15:29:06 -05003232 entry->ops = ops;
3233 entry->ip = rec->ip;
3234
3235 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3236 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3237
3238 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003239
3240 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3241 if (ret < 0)
3242 count = ret;
3243
Steven Rostedtb6887d72009-02-17 12:32:04 -05003244 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003245
3246 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003247 mutex_unlock(&ftrace_lock);
3248 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003249 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003250 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003251
3252 return count;
3253}
3254
3255enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003256 PROBE_TEST_FUNC = 1,
3257 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003258};
3259
3260static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003261__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003262 void *data, int flags)
3263{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003264 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003265 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003266 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003267 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003268 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003269 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003270 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003271 char str[KSYM_SYMBOL_LEN];
3272 int type = MATCH_FULL;
3273 int i, len = 0;
3274 char *search;
3275
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003276 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003277 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003278 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003279 int not;
3280
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003281 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003282 len = strlen(search);
3283
Steven Rostedtb6887d72009-02-17 12:32:04 -05003284 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003285 if (WARN_ON(not))
3286 return;
3287 }
3288
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003289 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003290
3291 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3292 if (!hash)
3293 /* Hmm, should report this somehow */
3294 goto out_unlock;
3295
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003296 INIT_LIST_HEAD(&free_list);
3297
Steven Rostedt59df055f2009-02-14 15:29:06 -05003298 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3299 struct hlist_head *hhd = &ftrace_func_hash[i];
3300
Sasha Levinb67bfe02013-02-27 17:06:00 -08003301 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003302
3303 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003304 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003305 continue;
3306
Steven Rostedtb6887d72009-02-17 12:32:04 -05003307 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003308 continue;
3309
3310 /* do this last, since it is the most expensive */
3311 if (glob) {
3312 kallsyms_lookup(entry->ip, NULL, NULL,
3313 NULL, str);
3314 if (!ftrace_match(str, glob, len, type))
3315 continue;
3316 }
3317
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003318 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3319 /* It is possible more than one entry had this ip */
3320 if (rec_entry)
3321 free_hash_entry(hash, rec_entry);
3322
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003323 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003324 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003325 }
3326 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003327 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003328 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003329 /*
3330 * Remove after the disable is called. Otherwise, if the last
3331 * probe is removed, a null hash means *all enabled*.
3332 */
3333 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003334 synchronize_sched();
3335 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3336 list_del(&entry->free_list);
3337 ftrace_free_entry(entry);
3338 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003339 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003340
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003341 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003342 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003343 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003344}
3345
3346void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003347unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003348 void *data)
3349{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003350 __unregister_ftrace_function_probe(glob, ops, data,
3351 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003352}
3353
3354void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003355unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003356{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003357 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003358}
3359
Steven Rostedtb6887d72009-02-17 12:32:04 -05003360void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003361{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003362 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003363}
3364
Steven Rostedtf6180772009-02-14 00:40:25 -05003365static LIST_HEAD(ftrace_commands);
3366static DEFINE_MUTEX(ftrace_cmd_mutex);
3367
Tom Zanussi38de93a2013-10-24 08:34:18 -05003368/*
3369 * Currently we only register ftrace commands from __init, so mark this
3370 * __init too.
3371 */
3372__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003373{
3374 struct ftrace_func_command *p;
3375 int ret = 0;
3376
3377 mutex_lock(&ftrace_cmd_mutex);
3378 list_for_each_entry(p, &ftrace_commands, list) {
3379 if (strcmp(cmd->name, p->name) == 0) {
3380 ret = -EBUSY;
3381 goto out_unlock;
3382 }
3383 }
3384 list_add(&cmd->list, &ftrace_commands);
3385 out_unlock:
3386 mutex_unlock(&ftrace_cmd_mutex);
3387
3388 return ret;
3389}
3390
Tom Zanussi38de93a2013-10-24 08:34:18 -05003391/*
3392 * Currently we only unregister ftrace commands from __init, so mark
3393 * this __init too.
3394 */
3395__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003396{
3397 struct ftrace_func_command *p, *n;
3398 int ret = -ENODEV;
3399
3400 mutex_lock(&ftrace_cmd_mutex);
3401 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3402 if (strcmp(cmd->name, p->name) == 0) {
3403 ret = 0;
3404 list_del_init(&p->list);
3405 goto out_unlock;
3406 }
3407 }
3408 out_unlock:
3409 mutex_unlock(&ftrace_cmd_mutex);
3410
3411 return ret;
3412}
3413
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003414static int ftrace_process_regex(struct ftrace_hash *hash,
3415 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003416{
Steven Rostedtf6180772009-02-14 00:40:25 -05003417 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003418 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003419 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003420
3421 func = strsep(&next, ":");
3422
3423 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003424 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003425 if (!ret)
3426 ret = -EINVAL;
3427 if (ret < 0)
3428 return ret;
3429 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003430 }
3431
Steven Rostedtf6180772009-02-14 00:40:25 -05003432 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003433
3434 command = strsep(&next, ":");
3435
Steven Rostedtf6180772009-02-14 00:40:25 -05003436 mutex_lock(&ftrace_cmd_mutex);
3437 list_for_each_entry(p, &ftrace_commands, list) {
3438 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003439 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003440 goto out_unlock;
3441 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003442 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003443 out_unlock:
3444 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003445
Steven Rostedtf6180772009-02-14 00:40:25 -05003446 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003447}
3448
Ingo Molnare309b412008-05-12 21:20:51 +02003449static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003450ftrace_regex_write(struct file *file, const char __user *ubuf,
3451 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003452{
3453 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003454 struct trace_parser *parser;
3455 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003456
Li Zefan4ba79782009-09-22 13:52:20 +08003457 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003458 return 0;
3459
Steven Rostedt5072c592008-05-12 21:20:43 +02003460 if (file->f_mode & FMODE_READ) {
3461 struct seq_file *m = file->private_data;
3462 iter = m->private;
3463 } else
3464 iter = file->private_data;
3465
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003466 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003467 return -ENODEV;
3468
3469 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003470
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003471 parser = &iter->parser;
3472 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003473
Li Zefan4ba79782009-09-22 13:52:20 +08003474 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003475 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003476 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003477 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003478 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003479 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003480 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003481 }
3482
Steven Rostedt5072c592008-05-12 21:20:43 +02003483 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003484 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003485 return ret;
3486}
3487
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003488ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003489ftrace_filter_write(struct file *file, const char __user *ubuf,
3490 size_t cnt, loff_t *ppos)
3491{
3492 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3493}
3494
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003495ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003496ftrace_notrace_write(struct file *file, const char __user *ubuf,
3497 size_t cnt, loff_t *ppos)
3498{
3499 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3500}
3501
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003502static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003503ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3504{
3505 struct ftrace_func_entry *entry;
3506
3507 if (!ftrace_location(ip))
3508 return -EINVAL;
3509
3510 if (remove) {
3511 entry = ftrace_lookup_ip(hash, ip);
3512 if (!entry)
3513 return -ENOENT;
3514 free_hash_entry(hash, entry);
3515 return 0;
3516 }
3517
3518 return add_hash_entry(hash, ip);
3519}
3520
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003521static void ftrace_ops_update_code(struct ftrace_ops *ops)
3522{
3523 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3524 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3525}
3526
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003527static int
3528ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3529 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003530{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003531 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003532 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003533 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003534
Steven Rostedt936e0742011-05-05 22:54:01 -04003535 /* All global ops uses the global ops filters */
3536 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3537 ops = &global_ops;
3538
Steven Rostedt41c52c02008-05-22 11:46:33 -04003539 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003540 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003541
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003542 mutex_lock(&ops->regex_lock);
3543
Steven Rostedtf45948e2011-05-02 12:29:25 -04003544 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003545 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003546 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003547 orig_hash = &ops->notrace_hash;
3548
3549 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003550 if (!hash) {
3551 ret = -ENOMEM;
3552 goto out_regex_unlock;
3553 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003554
Steven Rostedt41c52c02008-05-22 11:46:33 -04003555 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003556 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003557 if (buf && !ftrace_match_records(hash, buf, len)) {
3558 ret = -EINVAL;
3559 goto out_regex_unlock;
3560 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003561 if (ip) {
3562 ret = ftrace_match_addr(hash, ip, remove);
3563 if (ret < 0)
3564 goto out_regex_unlock;
3565 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003566
3567 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003568 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003569 if (!ret)
3570 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003571
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003572 mutex_unlock(&ftrace_lock);
3573
Jiri Olsaac483c42012-01-02 10:04:14 +01003574 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003575 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003576
3577 free_ftrace_hash(hash);
3578 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003579}
3580
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003581static int
3582ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3583 int reset, int enable)
3584{
3585 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3586}
3587
3588/**
3589 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3590 * @ops - the ops to set the filter with
3591 * @ip - the address to add to or remove from the filter.
3592 * @remove - non zero to remove the ip from the filter
3593 * @reset - non zero to reset all filters before applying this filter.
3594 *
3595 * Filters denote which functions should be enabled when tracing is enabled
3596 * If @ip is NULL, it failes to update filter.
3597 */
3598int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3599 int remove, int reset)
3600{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003601 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003602 return ftrace_set_addr(ops, ip, remove, reset, 1);
3603}
3604EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3605
3606static int
3607ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3608 int reset, int enable)
3609{
3610 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3611}
3612
Steven Rostedt77a2b372008-05-12 21:20:45 +02003613/**
3614 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003615 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003616 * @buf - the string that holds the function filter text.
3617 * @len - the length of the string.
3618 * @reset - non zero to reset all filters before applying this filter.
3619 *
3620 * Filters denote which functions should be enabled when tracing is enabled.
3621 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3622 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003623int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003624 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003625{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003626 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003627 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003628}
Steven Rostedt936e0742011-05-05 22:54:01 -04003629EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003630
Steven Rostedt41c52c02008-05-22 11:46:33 -04003631/**
3632 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003633 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003634 * @buf - the string that holds the function notrace text.
3635 * @len - the length of the string.
3636 * @reset - non zero to reset all filters before applying this filter.
3637 *
3638 * Notrace Filters denote which functions should not be enabled when tracing
3639 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3640 * for tracing.
3641 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003642int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003643 int len, int reset)
3644{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003645 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003646 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003647}
3648EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3649/**
3650 * ftrace_set_filter - set a function to filter on in ftrace
3651 * @ops - the ops to set the filter with
3652 * @buf - the string that holds the function filter text.
3653 * @len - the length of the string.
3654 * @reset - non zero to reset all filters before applying this filter.
3655 *
3656 * Filters denote which functions should be enabled when tracing is enabled.
3657 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3658 */
3659void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3660{
3661 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3662}
3663EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3664
3665/**
3666 * ftrace_set_notrace - set a function to not trace in ftrace
3667 * @ops - the ops to set the notrace filter with
3668 * @buf - the string that holds the function notrace text.
3669 * @len - the length of the string.
3670 * @reset - non zero to reset all filters before applying this filter.
3671 *
3672 * Notrace Filters denote which functions should not be enabled when tracing
3673 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3674 * for tracing.
3675 */
3676void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003677{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003678 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003679}
Steven Rostedt936e0742011-05-05 22:54:01 -04003680EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003681
Steven Rostedt2af15d62009-05-28 13:37:24 -04003682/*
3683 * command line interface to allow users to set filters on boot up.
3684 */
3685#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3686static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3687static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3688
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003689/* Used by function selftest to not test if filter is set */
3690bool ftrace_filter_param __initdata;
3691
Steven Rostedt2af15d62009-05-28 13:37:24 -04003692static int __init set_ftrace_notrace(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_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003696 return 1;
3697}
3698__setup("ftrace_notrace=", set_ftrace_notrace);
3699
3700static int __init set_ftrace_filter(char *str)
3701{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003702 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003703 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003704 return 1;
3705}
3706__setup("ftrace_filter=", set_ftrace_filter);
3707
Stefan Assmann369bc182009-10-12 22:17:21 +02003708#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003709static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003710static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003711
Stefan Assmann369bc182009-10-12 22:17:21 +02003712static int __init set_graph_function(char *str)
3713{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003714 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003715 return 1;
3716}
3717__setup("ftrace_graph_filter=", set_graph_function);
3718
3719static void __init set_ftrace_early_graph(char *buf)
3720{
3721 int ret;
3722 char *func;
3723
3724 while (buf) {
3725 func = strsep(&buf, ",");
3726 /* we allow only one expression at a time */
3727 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003728 FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003729 if (ret)
3730 printk(KERN_DEBUG "ftrace: function %s not "
3731 "traceable\n", func);
3732 }
3733}
3734#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3735
Steven Rostedt2a85a372011-12-19 21:57:44 -05003736void __init
3737ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003738{
3739 char *func;
3740
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003741 ftrace_ops_init(ops);
3742
Steven Rostedt2af15d62009-05-28 13:37:24 -04003743 while (buf) {
3744 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003745 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003746 }
3747}
3748
3749static void __init set_ftrace_early_filters(void)
3750{
3751 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003752 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003753 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003754 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003755#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3756 if (ftrace_graph_buf[0])
3757 set_ftrace_early_graph(ftrace_graph_buf);
3758#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003759}
3760
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003761int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003762{
3763 struct seq_file *m = (struct seq_file *)file->private_data;
3764 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003765 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003766 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003767 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003768 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003769
Steven Rostedt5072c592008-05-12 21:20:43 +02003770 if (file->f_mode & FMODE_READ) {
3771 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003772 seq_release(inode, file);
3773 } else
3774 iter = file->private_data;
3775
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003776 parser = &iter->parser;
3777 if (trace_parser_loaded(parser)) {
3778 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003779 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003780 }
3781
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003782 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003783
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003784 mutex_lock(&iter->ops->regex_lock);
3785
Steven Rostedt058e2972011-04-29 22:35:33 -04003786 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003787 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3788
3789 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003790 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003791 else
3792 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003793
Steven Rostedt058e2972011-04-29 22:35:33 -04003794 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003795 ret = ftrace_hash_move(iter->ops, filter_hash,
3796 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003797 if (!ret)
3798 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003799
Steven Rostedt058e2972011-04-29 22:35:33 -04003800 mutex_unlock(&ftrace_lock);
3801 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003802
3803 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003804 free_ftrace_hash(iter->hash);
3805 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003806
Steven Rostedt5072c592008-05-12 21:20:43 +02003807 return 0;
3808}
3809
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003810static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003811 .open = ftrace_avail_open,
3812 .read = seq_read,
3813 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003814 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003815};
3816
Steven Rostedt647bcd02011-05-03 14:39:21 -04003817static const struct file_operations ftrace_enabled_fops = {
3818 .open = ftrace_enabled_open,
3819 .read = seq_read,
3820 .llseek = seq_lseek,
3821 .release = seq_release_private,
3822};
3823
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003824static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003825 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003826 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003827 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003828 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003829 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003830};
3831
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003832static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003833 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003834 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003835 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003836 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003837 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003838};
3839
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003840#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3841
3842static DEFINE_MUTEX(graph_lock);
3843
3844int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003845int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003846unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003847unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003848
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003849struct ftrace_graph_data {
3850 unsigned long *table;
3851 size_t size;
3852 int *count;
3853 const struct seq_operations *seq_ops;
3854};
3855
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003856static void *
Li Zefan85951842009-06-24 09:54:00 +08003857__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003858{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003859 struct ftrace_graph_data *fgd = m->private;
3860
3861 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003862 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003863 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003864}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003865
Li Zefan85951842009-06-24 09:54:00 +08003866static void *
3867g_next(struct seq_file *m, void *v, loff_t *pos)
3868{
3869 (*pos)++;
3870 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003871}
3872
3873static void *g_start(struct seq_file *m, loff_t *pos)
3874{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003875 struct ftrace_graph_data *fgd = m->private;
3876
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003877 mutex_lock(&graph_lock);
3878
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003879 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003880 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003881 return (void *)1;
3882
Li Zefan85951842009-06-24 09:54:00 +08003883 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003884}
3885
3886static void g_stop(struct seq_file *m, void *p)
3887{
3888 mutex_unlock(&graph_lock);
3889}
3890
3891static int g_show(struct seq_file *m, void *v)
3892{
3893 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003894
3895 if (!ptr)
3896 return 0;
3897
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003898 if (ptr == (unsigned long *)1) {
3899 seq_printf(m, "#### all functions enabled ####\n");
3900 return 0;
3901 }
3902
Steven Rostedtb375a112009-09-17 00:05:58 -04003903 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003904
3905 return 0;
3906}
3907
James Morris88e9d342009-09-22 16:43:43 -07003908static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003909 .start = g_start,
3910 .next = g_next,
3911 .stop = g_stop,
3912 .show = g_show,
3913};
3914
3915static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003916__ftrace_graph_open(struct inode *inode, struct file *file,
3917 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003918{
3919 int ret = 0;
3920
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003921 mutex_lock(&graph_lock);
3922 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003923 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003924 *fgd->count = 0;
3925 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003926 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003927 mutex_unlock(&graph_lock);
3928
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003929 if (file->f_mode & FMODE_READ) {
3930 ret = seq_open(file, fgd->seq_ops);
3931 if (!ret) {
3932 struct seq_file *m = file->private_data;
3933 m->private = fgd;
3934 }
3935 } else
3936 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08003937
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003938 return ret;
3939}
3940
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003941static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003942ftrace_graph_open(struct inode *inode, struct file *file)
3943{
3944 struct ftrace_graph_data *fgd;
3945
3946 if (unlikely(ftrace_disabled))
3947 return -ENODEV;
3948
3949 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3950 if (fgd == NULL)
3951 return -ENOMEM;
3952
3953 fgd->table = ftrace_graph_funcs;
3954 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3955 fgd->count = &ftrace_graph_count;
3956 fgd->seq_ops = &ftrace_graph_seq_ops;
3957
3958 return __ftrace_graph_open(inode, file, fgd);
3959}
3960
3961static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003962ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3963{
3964 struct ftrace_graph_data *fgd;
3965
3966 if (unlikely(ftrace_disabled))
3967 return -ENODEV;
3968
3969 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3970 if (fgd == NULL)
3971 return -ENOMEM;
3972
3973 fgd->table = ftrace_graph_notrace_funcs;
3974 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3975 fgd->count = &ftrace_graph_notrace_count;
3976 fgd->seq_ops = &ftrace_graph_seq_ops;
3977
3978 return __ftrace_graph_open(inode, file, fgd);
3979}
3980
3981static int
Li Zefan87827112009-07-23 11:29:11 +08003982ftrace_graph_release(struct inode *inode, struct file *file)
3983{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003984 if (file->f_mode & FMODE_READ) {
3985 struct seq_file *m = file->private_data;
3986
3987 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08003988 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003989 } else {
3990 kfree(file->private_data);
3991 }
3992
Li Zefan87827112009-07-23 11:29:11 +08003993 return 0;
3994}
3995
3996static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003997ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003998{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003999 struct dyn_ftrace *rec;
4000 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004001 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004002 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004003 int type, not;
4004 char *search;
4005 bool exists;
4006 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004007
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004008 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004009 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004010 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004011 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004012
4013 search_len = strlen(search);
4014
Steven Rostedt52baf112009-02-14 01:15:39 -05004015 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004016
4017 if (unlikely(ftrace_disabled)) {
4018 mutex_unlock(&ftrace_lock);
4019 return -ENODEV;
4020 }
4021
Steven Rostedt265c8312009-02-13 12:43:56 -05004022 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004023
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004024 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004025 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004026 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004027 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004028 if (array[i] == rec->ip) {
4029 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004030 break;
4031 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004032 }
4033
4034 if (!not) {
4035 fail = 0;
4036 if (!exists) {
4037 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004038 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004039 goto out;
4040 }
4041 } else {
4042 if (exists) {
4043 array[i] = array[--(*idx)];
4044 array[*idx] = 0;
4045 fail = 0;
4046 }
4047 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004048 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004049 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004050out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004051 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004052
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004053 if (fail)
4054 return -EINVAL;
4055
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004056 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004057}
4058
4059static ssize_t
4060ftrace_graph_write(struct file *file, const char __user *ubuf,
4061 size_t cnt, loff_t *ppos)
4062{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004063 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004064 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004065 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004066
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004067 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004068 return 0;
4069
Namhyung Kim6a101082013-10-14 17:24:25 +09004070 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4071 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004072
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004073 read = trace_get_user(&parser, ubuf, cnt, ppos);
4074
Li Zefan4ba79782009-09-22 13:52:20 +08004075 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004076 parser.buffer[parser.idx] = 0;
4077
Namhyung Kim6a101082013-10-14 17:24:25 +09004078 mutex_lock(&graph_lock);
4079
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004080 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004081 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4082 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004083
4084 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004085 }
4086
Namhyung Kim6a101082013-10-14 17:24:25 +09004087 if (!ret)
4088 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004089
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004090 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004091
4092 return ret;
4093}
4094
4095static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004096 .open = ftrace_graph_open,
4097 .read = seq_read,
4098 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004099 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004100 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004101};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004102
4103static const struct file_operations ftrace_graph_notrace_fops = {
4104 .open = ftrace_graph_notrace_open,
4105 .read = seq_read,
4106 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004107 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004108 .release = ftrace_graph_release,
4109};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004110#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4111
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004112static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004113{
Steven Rostedt5072c592008-05-12 21:20:43 +02004114
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004115 trace_create_file("available_filter_functions", 0444,
4116 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004117
Steven Rostedt647bcd02011-05-03 14:39:21 -04004118 trace_create_file("enabled_functions", 0444,
4119 d_tracer, NULL, &ftrace_enabled_fops);
4120
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004121 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4122 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004123
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004124 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004125 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004126
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004127#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004128 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004129 NULL,
4130 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004131 trace_create_file("set_graph_notrace", 0444, d_tracer,
4132 NULL,
4133 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004134#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4135
Steven Rostedt5072c592008-05-12 21:20:43 +02004136 return 0;
4137}
4138
Steven Rostedt9fd49322012-04-24 22:32:06 -04004139static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004140{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004141 const unsigned long *ipa = a;
4142 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004143
Steven Rostedt9fd49322012-04-24 22:32:06 -04004144 if (*ipa > *ipb)
4145 return 1;
4146 if (*ipa < *ipb)
4147 return -1;
4148 return 0;
4149}
4150
4151static void ftrace_swap_ips(void *a, void *b, int size)
4152{
4153 unsigned long *ipa = a;
4154 unsigned long *ipb = b;
4155 unsigned long t;
4156
4157 t = *ipa;
4158 *ipa = *ipb;
4159 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004160}
4161
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004162static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004163 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004164 unsigned long *end)
4165{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004166 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004167 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004168 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004169 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004170 unsigned long *p;
4171 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004172 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004173 int ret = -ENOMEM;
4174
4175 count = end - start;
4176
4177 if (!count)
4178 return 0;
4179
Steven Rostedt9fd49322012-04-24 22:32:06 -04004180 sort(start, count, sizeof(*start),
4181 ftrace_cmp_ips, ftrace_swap_ips);
4182
Steven Rostedt706c81f2012-04-24 23:45:26 -04004183 start_pg = ftrace_allocate_pages(count);
4184 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004185 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004186
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004187 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004188
Steven Rostedt32082302011-12-16 14:42:37 -05004189 /*
4190 * Core and each module needs their own pages, as
4191 * modules will free them when they are removed.
4192 * Force a new page to be allocated for modules.
4193 */
Steven Rostedta7900872011-12-16 16:23:44 -05004194 if (!mod) {
4195 WARN_ON(ftrace_pages || ftrace_pages_start);
4196 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004197 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004198 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004199 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004200 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004201
Steven Rostedta7900872011-12-16 16:23:44 -05004202 if (WARN_ON(ftrace_pages->next)) {
4203 /* Hmm, we have free pages? */
4204 while (ftrace_pages->next)
4205 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004206 }
Steven Rostedta7900872011-12-16 16:23:44 -05004207
Steven Rostedt706c81f2012-04-24 23:45:26 -04004208 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004209 }
4210
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004211 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004212 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004213 while (p < end) {
4214 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004215 /*
4216 * Some architecture linkers will pad between
4217 * the different mcount_loc sections of different
4218 * object files to satisfy alignments.
4219 * Skip any NULL pointers.
4220 */
4221 if (!addr)
4222 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004223
4224 if (pg->index == pg->size) {
4225 /* We should have allocated enough */
4226 if (WARN_ON(!pg->next))
4227 break;
4228 pg = pg->next;
4229 }
4230
4231 rec = &pg->records[pg->index++];
4232 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004233 }
4234
Steven Rostedt706c81f2012-04-24 23:45:26 -04004235 /* We should have used all pages */
4236 WARN_ON(pg->next);
4237
4238 /* Assign the last page to ftrace_pages */
4239 ftrace_pages = pg;
4240
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004241 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004242 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004243
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004244 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004245 * We only need to disable interrupts on start up
4246 * because we are modifying code that an interrupt
4247 * may execute, and the modification is not atomic.
4248 * But for modules, nothing runs the code we modify
4249 * until we are finished with it, and there's no
4250 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004251 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004252 if (!mod)
4253 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08004254 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004255 if (!mod)
4256 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004257 ret = 0;
4258 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004259 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004260
Steven Rostedta7900872011-12-16 16:23:44 -05004261 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004262}
4263
Steven Rostedt93eb6772009-04-15 13:24:06 -04004264#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004265
4266#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4267
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004268void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004269{
4270 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004271 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004272 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004273 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004274
Steven Rostedt93eb6772009-04-15 13:24:06 -04004275 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004276
4277 if (ftrace_disabled)
4278 goto out_unlock;
4279
Steven Rostedt32082302011-12-16 14:42:37 -05004280 /*
4281 * Each module has its own ftrace_pages, remove
4282 * them from the list.
4283 */
4284 last_pg = &ftrace_pages_start;
4285 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4286 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004287 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004288 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004289 * As core pages are first, the first
4290 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004291 */
Steven Rostedt32082302011-12-16 14:42:37 -05004292 if (WARN_ON(pg == ftrace_pages_start))
4293 goto out_unlock;
4294
4295 /* Check if we are deleting the last page */
4296 if (pg == ftrace_pages)
4297 ftrace_pages = next_to_ftrace_page(last_pg);
4298
4299 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004300 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4301 free_pages((unsigned long)pg->records, order);
4302 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004303 } else
4304 last_pg = &pg->next;
4305 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004306 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004307 mutex_unlock(&ftrace_lock);
4308}
4309
4310static void ftrace_init_module(struct module *mod,
4311 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004312{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004313 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004314 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004315 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004316}
4317
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004318static int ftrace_module_notify_enter(struct notifier_block *self,
4319 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004320{
4321 struct module *mod = data;
4322
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004323 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004324 ftrace_init_module(mod, mod->ftrace_callsites,
4325 mod->ftrace_callsites +
4326 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004327 return 0;
4328}
4329
4330static int ftrace_module_notify_exit(struct notifier_block *self,
4331 unsigned long val, void *data)
4332{
4333 struct module *mod = data;
4334
4335 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004336 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004337
4338 return 0;
4339}
4340#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004341static int ftrace_module_notify_enter(struct notifier_block *self,
4342 unsigned long val, void *data)
4343{
4344 return 0;
4345}
4346static int ftrace_module_notify_exit(struct notifier_block *self,
4347 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004348{
4349 return 0;
4350}
4351#endif /* CONFIG_MODULES */
4352
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004353struct notifier_block ftrace_module_enter_nb = {
4354 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004355 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004356};
4357
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004358struct notifier_block ftrace_module_exit_nb = {
4359 .notifier_call = ftrace_module_notify_exit,
4360 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4361};
4362
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004363extern unsigned long __start_mcount_loc[];
4364extern unsigned long __stop_mcount_loc[];
4365
4366void __init ftrace_init(void)
4367{
4368 unsigned long count, addr, flags;
4369 int ret;
4370
4371 /* Keep the ftrace pointer to the stub */
4372 addr = (unsigned long)ftrace_stub;
4373
4374 local_irq_save(flags);
4375 ftrace_dyn_arch_init(&addr);
4376 local_irq_restore(flags);
4377
4378 /* ftrace_dyn_arch_init places the return code in addr */
4379 if (addr)
4380 goto failed;
4381
4382 count = __stop_mcount_loc - __start_mcount_loc;
4383
4384 ret = ftrace_dyn_table_alloc(count);
4385 if (ret)
4386 goto failed;
4387
4388 last_ftrace_enabled = ftrace_enabled = 1;
4389
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004390 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004391 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004392 __stop_mcount_loc);
4393
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004394 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004395 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004396 pr_warning("Failed to register trace ftrace module enter notifier\n");
4397
4398 ret = register_module_notifier(&ftrace_module_exit_nb);
4399 if (ret)
4400 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004401
Steven Rostedt2af15d62009-05-28 13:37:24 -04004402 set_ftrace_early_filters();
4403
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004404 return;
4405 failed:
4406 ftrace_disabled = 1;
4407}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004408
Steven Rostedt3d083392008-05-12 21:20:42 +02004409#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004410
Steven Rostedt2b499382011-05-03 22:49:52 -04004411static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004412 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004413 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4414 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004415};
4416
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004417static int __init ftrace_nodyn_init(void)
4418{
4419 ftrace_enabled = 1;
4420 return 0;
4421}
Steven Rostedt6f415672012-10-05 12:13:07 -04004422core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004423
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004424static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4425static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004426/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004427# define ftrace_startup(ops, command) \
4428 ({ \
4429 int ___ret = __register_ftrace_function(ops); \
4430 if (!___ret) \
4431 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4432 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004433 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004434# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4435
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004436# define ftrace_startup_sysctl() do { } while (0)
4437# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004438
4439static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004440ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004441{
4442 return 1;
4443}
4444
Steven Rostedt3d083392008-05-12 21:20:42 +02004445#endif /* CONFIG_DYNAMIC_FTRACE */
4446
Steven Rostedtb8489142011-05-04 09:27:52 -04004447static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004448ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004449 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004450{
Jiri Olsae2484912012-02-15 15:51:48 +01004451 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4452 return;
4453
4454 /*
4455 * Some of the ops may be dynamically allocated,
4456 * they must be freed after a synchronize_sched().
4457 */
4458 preempt_disable_notrace();
4459 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004460
4461 /*
4462 * Control funcs (perf) uses RCU. Only trace if
4463 * RCU is currently active.
4464 */
4465 if (!rcu_is_watching())
4466 goto out;
4467
Steven Rostedt0a016402012-11-02 17:03:03 -04004468 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004469 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4470 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004471 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004472 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004473 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004474 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004475 trace_recursion_clear(TRACE_CONTROL_BIT);
4476 preempt_enable_notrace();
4477}
4478
4479static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004480 .func = ftrace_ops_control_func,
4481 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4482 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004483};
4484
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004485static inline void
4486__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004487 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004488{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004489 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004490 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004491
Steven Rostedtccf36722012-06-05 09:44:25 -04004492 if (function_trace_stop)
4493 return;
4494
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004495 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4496 if (bit < 0)
4497 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004498
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004499 /*
4500 * Some of the ops may be dynamically allocated,
4501 * they must be freed after a synchronize_sched().
4502 */
4503 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004504 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004505 if (ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004506 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004507 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004508 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004509 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004510}
4511
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004512/*
4513 * Some archs only support passing ip and parent_ip. Even though
4514 * the list function ignores the op parameter, we do not want any
4515 * C side effects, where a function is called without the caller
4516 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004517 * Archs are to support both the regs and ftrace_ops at the same time.
4518 * If they support ftrace_ops, it is assumed they support regs.
4519 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004520 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4521 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004522 * An architecture can pass partial regs with ftrace_ops and still
4523 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004524 */
4525#if ARCH_SUPPORTS_FTRACE_OPS
4526static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004527 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004528{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004529 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004530}
4531#else
4532static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4533{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004534 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004535}
4536#endif
4537
Steven Rostedte32d8952008-12-04 00:26:41 -05004538static void clear_ftrace_swapper(void)
4539{
4540 struct task_struct *p;
4541 int cpu;
4542
4543 get_online_cpus();
4544 for_each_online_cpu(cpu) {
4545 p = idle_task(cpu);
4546 clear_tsk_trace_trace(p);
4547 }
4548 put_online_cpus();
4549}
4550
4551static void set_ftrace_swapper(void)
4552{
4553 struct task_struct *p;
4554 int cpu;
4555
4556 get_online_cpus();
4557 for_each_online_cpu(cpu) {
4558 p = idle_task(cpu);
4559 set_tsk_trace_trace(p);
4560 }
4561 put_online_cpus();
4562}
4563
4564static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004565{
4566 struct task_struct *p;
4567
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004568 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004569 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004570 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004571 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004572 rcu_read_unlock();
4573
Steven Rostedte32d8952008-12-04 00:26:41 -05004574 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004575}
4576
Steven Rostedte32d8952008-12-04 00:26:41 -05004577static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004578{
4579 struct task_struct *p;
4580
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004581 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004582 do_each_pid_task(pid, PIDTYPE_PID, p) {
4583 set_tsk_trace_trace(p);
4584 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004585 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004586}
4587
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004588static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004589{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004590 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004591 clear_ftrace_swapper();
4592 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004593 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004594}
4595
4596static void set_ftrace_pid_task(struct pid *pid)
4597{
4598 if (pid == ftrace_swapper_pid)
4599 set_ftrace_swapper();
4600 else
4601 set_ftrace_pid(pid);
4602}
4603
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004604static int ftrace_pid_add(int p)
4605{
4606 struct pid *pid;
4607 struct ftrace_pid *fpid;
4608 int ret = -EINVAL;
4609
4610 mutex_lock(&ftrace_lock);
4611
4612 if (!p)
4613 pid = ftrace_swapper_pid;
4614 else
4615 pid = find_get_pid(p);
4616
4617 if (!pid)
4618 goto out;
4619
4620 ret = 0;
4621
4622 list_for_each_entry(fpid, &ftrace_pids, list)
4623 if (fpid->pid == pid)
4624 goto out_put;
4625
4626 ret = -ENOMEM;
4627
4628 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4629 if (!fpid)
4630 goto out_put;
4631
4632 list_add(&fpid->list, &ftrace_pids);
4633 fpid->pid = pid;
4634
4635 set_ftrace_pid_task(pid);
4636
4637 ftrace_update_pid_func();
4638 ftrace_startup_enable(0);
4639
4640 mutex_unlock(&ftrace_lock);
4641 return 0;
4642
4643out_put:
4644 if (pid != ftrace_swapper_pid)
4645 put_pid(pid);
4646
4647out:
4648 mutex_unlock(&ftrace_lock);
4649 return ret;
4650}
4651
4652static void ftrace_pid_reset(void)
4653{
4654 struct ftrace_pid *fpid, *safe;
4655
4656 mutex_lock(&ftrace_lock);
4657 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4658 struct pid *pid = fpid->pid;
4659
4660 clear_ftrace_pid_task(pid);
4661
4662 list_del(&fpid->list);
4663 kfree(fpid);
4664 }
4665
4666 ftrace_update_pid_func();
4667 ftrace_startup_enable(0);
4668
4669 mutex_unlock(&ftrace_lock);
4670}
4671
4672static void *fpid_start(struct seq_file *m, loff_t *pos)
4673{
4674 mutex_lock(&ftrace_lock);
4675
4676 if (list_empty(&ftrace_pids) && (!*pos))
4677 return (void *) 1;
4678
4679 return seq_list_start(&ftrace_pids, *pos);
4680}
4681
4682static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4683{
4684 if (v == (void *)1)
4685 return NULL;
4686
4687 return seq_list_next(v, &ftrace_pids, pos);
4688}
4689
4690static void fpid_stop(struct seq_file *m, void *p)
4691{
4692 mutex_unlock(&ftrace_lock);
4693}
4694
4695static int fpid_show(struct seq_file *m, void *v)
4696{
4697 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4698
4699 if (v == (void *)1) {
4700 seq_printf(m, "no pid\n");
4701 return 0;
4702 }
4703
4704 if (fpid->pid == ftrace_swapper_pid)
4705 seq_printf(m, "swapper tasks\n");
4706 else
4707 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4708
4709 return 0;
4710}
4711
4712static const struct seq_operations ftrace_pid_sops = {
4713 .start = fpid_start,
4714 .next = fpid_next,
4715 .stop = fpid_stop,
4716 .show = fpid_show,
4717};
4718
4719static int
4720ftrace_pid_open(struct inode *inode, struct file *file)
4721{
4722 int ret = 0;
4723
4724 if ((file->f_mode & FMODE_WRITE) &&
4725 (file->f_flags & O_TRUNC))
4726 ftrace_pid_reset();
4727
4728 if (file->f_mode & FMODE_READ)
4729 ret = seq_open(file, &ftrace_pid_sops);
4730
4731 return ret;
4732}
4733
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004734static ssize_t
4735ftrace_pid_write(struct file *filp, const char __user *ubuf,
4736 size_t cnt, loff_t *ppos)
4737{
Ingo Molnar457dc922009-11-23 11:03:28 +01004738 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004739 long val;
4740 int ret;
4741
4742 if (cnt >= sizeof(buf))
4743 return -EINVAL;
4744
4745 if (copy_from_user(&buf, ubuf, cnt))
4746 return -EFAULT;
4747
4748 buf[cnt] = 0;
4749
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004750 /*
4751 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4752 * to clean the filter quietly.
4753 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004754 tmp = strstrip(buf);
4755 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004756 return 1;
4757
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004758 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004759 if (ret < 0)
4760 return ret;
4761
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004762 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004763
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004764 return ret ? ret : cnt;
4765}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004766
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004767static int
4768ftrace_pid_release(struct inode *inode, struct file *file)
4769{
4770 if (file->f_mode & FMODE_READ)
4771 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004772
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004773 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004774}
4775
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004776static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004777 .open = ftrace_pid_open,
4778 .write = ftrace_pid_write,
4779 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004780 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004781 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004782};
4783
4784static __init int ftrace_init_debugfs(void)
4785{
4786 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004787
4788 d_tracer = tracing_init_dentry();
4789 if (!d_tracer)
4790 return 0;
4791
4792 ftrace_init_dyn_debugfs(d_tracer);
4793
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004794 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4795 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004796
4797 ftrace_profile_debugfs(d_tracer);
4798
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004799 return 0;
4800}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004801fs_initcall(ftrace_init_debugfs);
4802
Steven Rostedt3d083392008-05-12 21:20:42 +02004803/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004804 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004805 *
4806 * This function should be used by panic code. It stops ftrace
4807 * but in a not so nice way. If you need to simply kill ftrace
4808 * from a non-atomic section, use ftrace_kill.
4809 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004810void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004811{
4812 ftrace_disabled = 1;
4813 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004814 clear_ftrace_function();
4815}
4816
4817/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004818 * Test if ftrace is dead or not.
4819 */
4820int ftrace_is_dead(void)
4821{
4822 return ftrace_disabled;
4823}
4824
4825/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004826 * register_ftrace_function - register a function for profiling
4827 * @ops - ops structure that holds the function for profiling.
4828 *
4829 * Register a function to be called by all functions in the
4830 * kernel.
4831 *
4832 * Note: @ops->func and all the functions it calls must be labeled
4833 * with "notrace", otherwise it will go into a
4834 * recursive loop.
4835 */
4836int register_ftrace_function(struct ftrace_ops *ops)
4837{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004838 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004839
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004840 ftrace_ops_init(ops);
4841
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004842 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004843
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004844 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004845
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004846 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004847
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004848 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004849}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004850EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004851
4852/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004853 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004854 * @ops - ops structure that holds the function to unregister
4855 *
4856 * Unregister a function that was added to be called by ftrace profiling.
4857 */
4858int unregister_ftrace_function(struct ftrace_ops *ops)
4859{
4860 int ret;
4861
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004862 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004863 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004864 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004865
4866 return ret;
4867}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004868EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004869
Ingo Molnare309b412008-05-12 21:20:51 +02004870int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004871ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004872 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004873 loff_t *ppos)
4874{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004875 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004876
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004877 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004878
Steven Rostedt45a4a232011-04-21 23:16:46 -04004879 if (unlikely(ftrace_disabled))
4880 goto out;
4881
4882 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004883
Li Zefana32c7762009-06-26 16:55:51 +08004884 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004885 goto out;
4886
Li Zefana32c7762009-06-26 16:55:51 +08004887 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004888
4889 if (ftrace_enabled) {
4890
4891 ftrace_startup_sysctl();
4892
4893 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004894 if (ftrace_ops_list != &ftrace_list_end)
4895 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004896
4897 } else {
4898 /* stopping ftrace calls (just send to ftrace_stub) */
4899 ftrace_trace_function = ftrace_stub;
4900
4901 ftrace_shutdown_sysctl();
4902 }
4903
4904 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004905 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004906 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004907}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004908
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004909#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004910
Steven Rostedt597af812009-04-03 15:24:12 -04004911static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004912static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004913
Steven Rostedte49dc192008-12-02 23:50:05 -05004914int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4915{
4916 return 0;
4917}
4918
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004919/* The callbacks that hook a function */
4920trace_func_graph_ret_t ftrace_graph_return =
4921 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004922trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05004923static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004924
4925/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4926static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4927{
4928 int i;
4929 int ret = 0;
4930 unsigned long flags;
4931 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4932 struct task_struct *g, *t;
4933
4934 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4935 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4936 * sizeof(struct ftrace_ret_stack),
4937 GFP_KERNEL);
4938 if (!ret_stack_list[i]) {
4939 start = 0;
4940 end = i;
4941 ret = -ENOMEM;
4942 goto free;
4943 }
4944 }
4945
4946 read_lock_irqsave(&tasklist_lock, flags);
4947 do_each_thread(g, t) {
4948 if (start == end) {
4949 ret = -EAGAIN;
4950 goto unlock;
4951 }
4952
4953 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004954 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004955 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004956 t->curr_ret_stack = -1;
4957 /* Make sure the tasks see the -1 first: */
4958 smp_wmb();
4959 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004960 }
4961 } while_each_thread(g, t);
4962
4963unlock:
4964 read_unlock_irqrestore(&tasklist_lock, flags);
4965free:
4966 for (i = start; i < end; i++)
4967 kfree(ret_stack_list[i]);
4968 return ret;
4969}
4970
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004971static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004972ftrace_graph_probe_sched_switch(void *ignore,
4973 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004974{
4975 unsigned long long timestamp;
4976 int index;
4977
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004978 /*
4979 * Does the user want to count the time a function was asleep.
4980 * If so, do not update the time stamps.
4981 */
4982 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4983 return;
4984
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004985 timestamp = trace_clock_local();
4986
4987 prev->ftrace_timestamp = timestamp;
4988
4989 /* only process tasks that we timestamped */
4990 if (!next->ftrace_timestamp)
4991 return;
4992
4993 /*
4994 * Update all the counters in next to make up for the
4995 * time next was sleeping.
4996 */
4997 timestamp -= next->ftrace_timestamp;
4998
4999 for (index = next->curr_ret_stack; index >= 0; index--)
5000 next->ret_stack[index].calltime += timestamp;
5001}
5002
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005003/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005004static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005005{
5006 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005007 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005008
5009 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5010 sizeof(struct ftrace_ret_stack *),
5011 GFP_KERNEL);
5012
5013 if (!ret_stack_list)
5014 return -ENOMEM;
5015
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005016 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005017 for_each_online_cpu(cpu) {
5018 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005019 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005020 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005021
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005022 do {
5023 ret = alloc_retstack_tasklist(ret_stack_list);
5024 } while (ret == -EAGAIN);
5025
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005026 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005027 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005028 if (ret)
5029 pr_info("ftrace_graph: Couldn't activate tracepoint"
5030 " probe to kernel_sched_switch\n");
5031 }
5032
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005033 kfree(ret_stack_list);
5034 return ret;
5035}
5036
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005037/*
5038 * Hibernation protection.
5039 * The state of the current task is too much unstable during
5040 * suspend/restore to disk. We want to protect against that.
5041 */
5042static int
5043ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5044 void *unused)
5045{
5046 switch (state) {
5047 case PM_HIBERNATION_PREPARE:
5048 pause_graph_tracing();
5049 break;
5050
5051 case PM_POST_HIBERNATION:
5052 unpause_graph_tracing();
5053 break;
5054 }
5055 return NOTIFY_DONE;
5056}
5057
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005058/* Just a place holder for function graph */
5059static struct ftrace_ops fgraph_ops __read_mostly = {
5060 .func = ftrace_stub,
5061 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
5062 FTRACE_OPS_FL_RECURSION_SAFE,
5063};
5064
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005065static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5066{
5067 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5068 return 0;
5069 return __ftrace_graph_entry(trace);
5070}
5071
5072/*
5073 * The function graph tracer should only trace the functions defined
5074 * by set_ftrace_filter and set_ftrace_notrace. If another function
5075 * tracer ops is registered, the graph tracer requires testing the
5076 * function against the global ops, and not just trace any function
5077 * that any ftrace_ops registered.
5078 */
5079static void update_function_graph_func(void)
5080{
5081 if (ftrace_ops_list == &ftrace_list_end ||
5082 (ftrace_ops_list == &global_ops &&
5083 global_ops.next == &ftrace_list_end))
5084 ftrace_graph_entry = __ftrace_graph_entry;
5085 else
5086 ftrace_graph_entry = ftrace_graph_entry_test;
5087}
5088
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005089int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5090 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005091{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005092 int ret = 0;
5093
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005094 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005095
Steven Rostedt05ce5812009-03-24 00:18:31 -04005096 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005097 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005098 ret = -EBUSY;
5099 goto out;
5100 }
5101
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005102 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5103 register_pm_notifier(&ftrace_suspend_notifier);
5104
Steven Rostedt597af812009-04-03 15:24:12 -04005105 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005106 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005107 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005108 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005109 goto out;
5110 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005111
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005112 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005113
5114 /*
5115 * Update the indirect function to the entryfunc, and the
5116 * function that gets called to the entry_test first. Then
5117 * call the update fgraph entry function to determine if
5118 * the entryfunc should be called directly or not.
5119 */
5120 __ftrace_graph_entry = entryfunc;
5121 ftrace_graph_entry = ftrace_graph_entry_test;
5122 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005123
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005124 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005125
5126out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005127 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005128 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005129}
5130
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005131void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005132{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005133 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005134
Steven Rostedt597af812009-04-03 15:24:12 -04005135 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005136 goto out;
5137
Steven Rostedt597af812009-04-03 15:24:12 -04005138 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005139 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005140 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005141 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005142 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005143 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005144 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005145
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005146 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005147 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005148}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005149
Steven Rostedt868baf02011-02-10 21:26:13 -05005150static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5151
5152static void
5153graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5154{
5155 atomic_set(&t->tracing_graph_pause, 0);
5156 atomic_set(&t->trace_overrun, 0);
5157 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005158 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005159 smp_wmb();
5160 t->ret_stack = ret_stack;
5161}
5162
5163/*
5164 * Allocate a return stack for the idle task. May be the first
5165 * time through, or it may be done by CPU hotplug online.
5166 */
5167void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5168{
5169 t->curr_ret_stack = -1;
5170 /*
5171 * The idle task has no parent, it either has its own
5172 * stack or no stack at all.
5173 */
5174 if (t->ret_stack)
5175 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5176
5177 if (ftrace_graph_active) {
5178 struct ftrace_ret_stack *ret_stack;
5179
5180 ret_stack = per_cpu(idle_ret_stack, cpu);
5181 if (!ret_stack) {
5182 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5183 * sizeof(struct ftrace_ret_stack),
5184 GFP_KERNEL);
5185 if (!ret_stack)
5186 return;
5187 per_cpu(idle_ret_stack, cpu) = ret_stack;
5188 }
5189 graph_init_task(t, ret_stack);
5190 }
5191}
5192
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005193/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005194void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005195{
Steven Rostedt84047e32009-06-02 16:51:55 -04005196 /* Make sure we do not use the parent ret_stack */
5197 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005198 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005199
Steven Rostedt597af812009-04-03 15:24:12 -04005200 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005201 struct ftrace_ret_stack *ret_stack;
5202
5203 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005204 * sizeof(struct ftrace_ret_stack),
5205 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005206 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005207 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005208 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005209 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005210}
5211
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005212void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005213{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005214 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5215
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005216 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005217 /* NULL must become visible to IRQs before we free it: */
5218 barrier();
5219
5220 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005221}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005222
5223void ftrace_graph_stop(void)
5224{
5225 ftrace_stop();
5226}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005227#endif