blob: 7f21b06648e92f4ca2bda411938deb7eb36163ff [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);
501 if (!ret) {
502 /*
503 * The ftrace_ops is now removed from the list,
504 * so there'll be no new users. We must ensure
505 * all current users are done before we free
506 * the control data.
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400507 * Note synchronize_sched() is not enough, as we
508 * use preempt_disable() to do RCU, but the function
509 * tracer can be called where RCU is not active
510 * (before user_exit()).
Jiri Olsae2484912012-02-15 15:51:48 +0100511 */
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400512 schedule_on_each_cpu(ftrace_sync);
Jiri Olsae2484912012-02-15 15:51:48 +0100513 control_ops_free(ops);
514 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400515 } else
516 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
517
Steven Rostedt2b499382011-05-03 22:49:52 -0400518 if (ret < 0)
519 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400520
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400521 if (ftrace_enabled)
522 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200523
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400524 /*
525 * Dynamic ops may be freed, we must make sure that all
526 * callers are done before leaving this function.
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400527 *
528 * Again, normal synchronize_sched() is not good enough.
529 * We need to do a hard force of sched synchronization.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400530 */
531 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400532 schedule_on_each_cpu(ftrace_sync);
533
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400534
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500535 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200536}
537
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500538static void ftrace_update_pid_func(void)
539{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400540 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500541 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900542 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500543
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400544 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500545}
546
Steven Rostedt493762f2009-03-23 17:12:36 -0400547#ifdef CONFIG_FUNCTION_PROFILER
548struct ftrace_profile {
549 struct hlist_node node;
550 unsigned long ip;
551 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400552#ifdef CONFIG_FUNCTION_GRAPH_TRACER
553 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400554 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400555#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400556};
557
558struct ftrace_profile_page {
559 struct ftrace_profile_page *next;
560 unsigned long index;
561 struct ftrace_profile records[];
562};
563
Steven Rostedtcafb1682009-03-24 20:50:39 -0400564struct ftrace_profile_stat {
565 atomic_t disabled;
566 struct hlist_head *hash;
567 struct ftrace_profile_page *pages;
568 struct ftrace_profile_page *start;
569 struct tracer_stat stat;
570};
571
Steven Rostedt493762f2009-03-23 17:12:36 -0400572#define PROFILE_RECORDS_SIZE \
573 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
574
575#define PROFILES_PER_PAGE \
576 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
577
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400578static int ftrace_profile_enabled __read_mostly;
579
580/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400581static DEFINE_MUTEX(ftrace_profile_lock);
582
Steven Rostedtcafb1682009-03-24 20:50:39 -0400583static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400584
Namhyung Kim20079eb2013-04-10 08:55:50 +0900585#define FTRACE_PROFILE_HASH_BITS 10
586#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400587
Steven Rostedt493762f2009-03-23 17:12:36 -0400588static void *
589function_stat_next(void *v, int idx)
590{
591 struct ftrace_profile *rec = v;
592 struct ftrace_profile_page *pg;
593
594 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
595
596 again:
Li Zefan0296e422009-06-26 11:15:37 +0800597 if (idx != 0)
598 rec++;
599
Steven Rostedt493762f2009-03-23 17:12:36 -0400600 if ((void *)rec >= (void *)&pg->records[pg->index]) {
601 pg = pg->next;
602 if (!pg)
603 return NULL;
604 rec = &pg->records[0];
605 if (!rec->counter)
606 goto again;
607 }
608
609 return rec;
610}
611
612static void *function_stat_start(struct tracer_stat *trace)
613{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400614 struct ftrace_profile_stat *stat =
615 container_of(trace, struct ftrace_profile_stat, stat);
616
617 if (!stat || !stat->start)
618 return NULL;
619
620 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400621}
622
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400623#ifdef CONFIG_FUNCTION_GRAPH_TRACER
624/* function graph compares on total time */
625static int function_stat_cmp(void *p1, void *p2)
626{
627 struct ftrace_profile *a = p1;
628 struct ftrace_profile *b = p2;
629
630 if (a->time < b->time)
631 return -1;
632 if (a->time > b->time)
633 return 1;
634 else
635 return 0;
636}
637#else
638/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400639static int function_stat_cmp(void *p1, void *p2)
640{
641 struct ftrace_profile *a = p1;
642 struct ftrace_profile *b = p2;
643
644 if (a->counter < b->counter)
645 return -1;
646 if (a->counter > b->counter)
647 return 1;
648 else
649 return 0;
650}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400651#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400652
653static int function_stat_headers(struct seq_file *m)
654{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400655#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400656 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400657 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400658 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400659 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400660#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400661 seq_printf(m, " Function Hit\n"
662 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400663#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400664 return 0;
665}
666
667static int function_stat_show(struct seq_file *m, void *v)
668{
669 struct ftrace_profile *rec = v;
670 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800671 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400672#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400673 static struct trace_seq s;
674 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400675 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400676#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800677 mutex_lock(&ftrace_profile_lock);
678
679 /* we raced with function_profile_reset() */
680 if (unlikely(rec->counter == 0)) {
681 ret = -EBUSY;
682 goto out;
683 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400684
685 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400686 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400687
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400688#ifdef CONFIG_FUNCTION_GRAPH_TRACER
689 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400690 avg = rec->time;
691 do_div(avg, rec->counter);
692
Chase Douglase330b3b2010-04-26 14:02:05 -0400693 /* Sample standard deviation (s^2) */
694 if (rec->counter <= 1)
695 stddev = 0;
696 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200697 /*
698 * Apply Welford's method:
699 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
700 */
701 stddev = rec->counter * rec->time_squared -
702 rec->time * rec->time;
703
Chase Douglase330b3b2010-04-26 14:02:05 -0400704 /*
705 * Divide only 1000 for ns^2 -> us^2 conversion.
706 * trace_print_graph_duration will divide 1000 again.
707 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200708 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400709 }
710
Steven Rostedt34886c82009-03-25 21:00:47 -0400711 trace_seq_init(&s);
712 trace_print_graph_duration(rec->time, &s);
713 trace_seq_puts(&s, " ");
714 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400715 trace_seq_puts(&s, " ");
716 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400717 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400718#endif
719 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800720out:
721 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400722
Li Zefan3aaba202010-08-23 16:50:12 +0800723 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400724}
725
Steven Rostedtcafb1682009-03-24 20:50:39 -0400726static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400727{
728 struct ftrace_profile_page *pg;
729
Steven Rostedtcafb1682009-03-24 20:50:39 -0400730 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400731
732 while (pg) {
733 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
734 pg->index = 0;
735 pg = pg->next;
736 }
737
Steven Rostedtcafb1682009-03-24 20:50:39 -0400738 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400739 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
740}
741
Steven Rostedtcafb1682009-03-24 20:50:39 -0400742int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400743{
744 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400745 int functions;
746 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400747 int i;
748
749 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400751 return 0;
752
Steven Rostedtcafb1682009-03-24 20:50:39 -0400753 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
754 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400755 return -ENOMEM;
756
Steven Rostedt318e0a72009-03-25 20:06:34 -0400757#ifdef CONFIG_DYNAMIC_FTRACE
758 functions = ftrace_update_tot_cnt;
759#else
760 /*
761 * We do not know the number of functions that exist because
762 * dynamic tracing is what counts them. With past experience
763 * we have around 20K functions. That should be more than enough.
764 * It is highly unlikely we will execute every function in
765 * the kernel.
766 */
767 functions = 20000;
768#endif
769
Steven Rostedtcafb1682009-03-24 20:50:39 -0400770 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400771
Steven Rostedt318e0a72009-03-25 20:06:34 -0400772 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
773
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900774 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400775 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400776 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400777 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400778 pg = pg->next;
779 }
780
781 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400782
783 out_free:
784 pg = stat->start;
785 while (pg) {
786 unsigned long tmp = (unsigned long)pg;
787
788 pg = pg->next;
789 free_page(tmp);
790 }
791
Steven Rostedt318e0a72009-03-25 20:06:34 -0400792 stat->pages = NULL;
793 stat->start = NULL;
794
795 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400796}
797
Steven Rostedtcafb1682009-03-24 20:50:39 -0400798static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400799{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400800 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400801 int size;
802
Steven Rostedtcafb1682009-03-24 20:50:39 -0400803 stat = &per_cpu(ftrace_profile_stats, cpu);
804
805 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400806 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400807 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400808 return 0;
809 }
810
811 /*
812 * We are profiling all functions, but usually only a few thousand
813 * functions are hit. We'll make a hash of 1024 items.
814 */
815 size = FTRACE_PROFILE_HASH_SIZE;
816
Steven Rostedtcafb1682009-03-24 20:50:39 -0400817 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400818
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400820 return -ENOMEM;
821
Steven Rostedt318e0a72009-03-25 20:06:34 -0400822 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400823 if (ftrace_profile_pages_init(stat) < 0) {
824 kfree(stat->hash);
825 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400826 return -ENOMEM;
827 }
828
829 return 0;
830}
831
Steven Rostedtcafb1682009-03-24 20:50:39 -0400832static int ftrace_profile_init(void)
833{
834 int cpu;
835 int ret = 0;
836
837 for_each_online_cpu(cpu) {
838 ret = ftrace_profile_init_cpu(cpu);
839 if (ret)
840 break;
841 }
842
843 return ret;
844}
845
Steven Rostedt493762f2009-03-23 17:12:36 -0400846/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400847static struct ftrace_profile *
848ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400849{
850 struct ftrace_profile *rec;
851 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400852 unsigned long key;
853
Namhyung Kim20079eb2013-04-10 08:55:50 +0900854 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400855 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400856
857 if (hlist_empty(hhd))
858 return NULL;
859
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400860 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400861 if (rec->ip == ip)
862 return rec;
863 }
864
865 return NULL;
866}
867
Steven Rostedtcafb1682009-03-24 20:50:39 -0400868static void ftrace_add_profile(struct ftrace_profile_stat *stat,
869 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400870{
871 unsigned long key;
872
Namhyung Kim20079eb2013-04-10 08:55:50 +0900873 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400874 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400875}
876
Steven Rostedt318e0a72009-03-25 20:06:34 -0400877/*
878 * The memory is already allocated, this simply finds a new record to use.
879 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400880static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400881ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400882{
883 struct ftrace_profile *rec = NULL;
884
Steven Rostedt318e0a72009-03-25 20:06:34 -0400885 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400886 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400887 goto out;
888
Steven Rostedt493762f2009-03-23 17:12:36 -0400889 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400890 * Try to find the function again since an NMI
891 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400892 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400893 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400894 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400895 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400896
Steven Rostedtcafb1682009-03-24 20:50:39 -0400897 if (stat->pages->index == PROFILES_PER_PAGE) {
898 if (!stat->pages->next)
899 goto out;
900 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400901 }
902
Steven Rostedtcafb1682009-03-24 20:50:39 -0400903 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400904 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400905 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400906
Steven Rostedt493762f2009-03-23 17:12:36 -0400907 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400908 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400909
910 return rec;
911}
912
Steven Rostedt493762f2009-03-23 17:12:36 -0400913static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400914function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400915 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400916{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400917 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400918 struct ftrace_profile *rec;
919 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400920
921 if (!ftrace_profile_enabled)
922 return;
923
Steven Rostedt493762f2009-03-23 17:12:36 -0400924 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400925
926 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400927 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400928 goto out;
929
930 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400931 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400932 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400933 if (!rec)
934 goto out;
935 }
936
937 rec->counter++;
938 out:
939 local_irq_restore(flags);
940}
941
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400942#ifdef CONFIG_FUNCTION_GRAPH_TRACER
943static int profile_graph_entry(struct ftrace_graph_ent *trace)
944{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400945 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400946 return 1;
947}
948
949static void profile_graph_return(struct ftrace_graph_ret *trace)
950{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400951 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400952 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400953 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400954 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400955
956 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400957 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400958 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400959 goto out;
960
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400961 /* If the calltime was zero'd ignore it */
962 if (!trace->calltime)
963 goto out;
964
Steven Rostedta2a16d62009-03-24 23:17:58 -0400965 calltime = trace->rettime - trace->calltime;
966
967 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
968 int index;
969
970 index = trace->depth;
971
972 /* Append this call time to the parent time to subtract */
973 if (index)
974 current->ret_stack[index - 1].subtime += calltime;
975
976 if (current->ret_stack[index].subtime < calltime)
977 calltime -= current->ret_stack[index].subtime;
978 else
979 calltime = 0;
980 }
981
Steven Rostedtcafb1682009-03-24 20:50:39 -0400982 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400983 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400984 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400985 rec->time_squared += calltime * calltime;
986 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400987
Steven Rostedtcafb1682009-03-24 20:50:39 -0400988 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400989 local_irq_restore(flags);
990}
991
992static int register_ftrace_profiler(void)
993{
994 return register_ftrace_graph(&profile_graph_return,
995 &profile_graph_entry);
996}
997
998static void unregister_ftrace_profiler(void)
999{
1000 unregister_ftrace_graph();
1001}
1002#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +01001003static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001004 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001005 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1006 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -04001007};
1008
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001009static int register_ftrace_profiler(void)
1010{
1011 return register_ftrace_function(&ftrace_profile_ops);
1012}
1013
1014static void unregister_ftrace_profiler(void)
1015{
1016 unregister_ftrace_function(&ftrace_profile_ops);
1017}
1018#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1019
Steven Rostedt493762f2009-03-23 17:12:36 -04001020static ssize_t
1021ftrace_profile_write(struct file *filp, const char __user *ubuf,
1022 size_t cnt, loff_t *ppos)
1023{
1024 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -04001025 int ret;
1026
Peter Huewe22fe9b52011-06-07 21:58:27 +02001027 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1028 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -04001029 return ret;
1030
1031 val = !!val;
1032
1033 mutex_lock(&ftrace_profile_lock);
1034 if (ftrace_profile_enabled ^ val) {
1035 if (val) {
1036 ret = ftrace_profile_init();
1037 if (ret < 0) {
1038 cnt = ret;
1039 goto out;
1040 }
1041
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001042 ret = register_ftrace_profiler();
1043 if (ret < 0) {
1044 cnt = ret;
1045 goto out;
1046 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001047 ftrace_profile_enabled = 1;
1048 } else {
1049 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -04001050 /*
1051 * unregister_ftrace_profiler calls stop_machine
1052 * so this acts like an synchronize_sched.
1053 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001054 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -04001055 }
1056 }
1057 out:
1058 mutex_unlock(&ftrace_profile_lock);
1059
Jiri Olsacf8517c2009-10-23 19:36:16 -04001060 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001061
1062 return cnt;
1063}
1064
1065static ssize_t
1066ftrace_profile_read(struct file *filp, char __user *ubuf,
1067 size_t cnt, loff_t *ppos)
1068{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001069 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001070 int r;
1071
1072 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1073 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1074}
1075
1076static const struct file_operations ftrace_profile_fops = {
1077 .open = tracing_open_generic,
1078 .read = ftrace_profile_read,
1079 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001080 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001081};
1082
Steven Rostedtcafb1682009-03-24 20:50:39 -04001083/* used to initialize the real stat files */
1084static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001085 .name = "functions",
1086 .stat_start = function_stat_start,
1087 .stat_next = function_stat_next,
1088 .stat_cmp = function_stat_cmp,
1089 .stat_headers = function_stat_headers,
1090 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001091};
1092
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001093static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001094{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001095 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001096 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001097 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001098 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001099 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001100
Steven Rostedtcafb1682009-03-24 20:50:39 -04001101 for_each_possible_cpu(cpu) {
1102 stat = &per_cpu(ftrace_profile_stats, cpu);
1103
1104 /* allocate enough for function name + cpu number */
1105 name = kmalloc(32, GFP_KERNEL);
1106 if (!name) {
1107 /*
1108 * The files created are permanent, if something happens
1109 * we still do not free memory.
1110 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001111 WARN(1,
1112 "Could not allocate stat file for cpu %d\n",
1113 cpu);
1114 return;
1115 }
1116 stat->stat = function_stats;
1117 snprintf(name, 32, "function%d", cpu);
1118 stat->stat.name = name;
1119 ret = register_stat_tracer(&stat->stat);
1120 if (ret) {
1121 WARN(1,
1122 "Could not register function stat for cpu %d\n",
1123 cpu);
1124 kfree(name);
1125 return;
1126 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001127 }
1128
1129 entry = debugfs_create_file("function_profile_enabled", 0644,
1130 d_tracer, NULL, &ftrace_profile_fops);
1131 if (!entry)
1132 pr_warning("Could not create debugfs "
1133 "'function_profile_enabled' entry\n");
1134}
1135
1136#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001137static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001138{
1139}
1140#endif /* CONFIG_FUNCTION_PROFILER */
1141
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001142static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1143
Steven Rostedt3d083392008-05-12 21:20:42 +02001144#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001145
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001146#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001147# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001148#endif
1149
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001150static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1151
Steven Rostedtb6887d72009-02-17 12:32:04 -05001152struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001153 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001154 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001155 unsigned long flags;
1156 unsigned long ip;
1157 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001158 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001159};
1160
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001161struct ftrace_func_entry {
1162 struct hlist_node hlist;
1163 unsigned long ip;
1164};
1165
1166struct ftrace_hash {
1167 unsigned long size_bits;
1168 struct hlist_head *buckets;
1169 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001170 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001171};
1172
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001173/*
1174 * We make these constant because no one should touch them,
1175 * but they are used as the default "empty hash", to avoid allocating
1176 * it all the time. These are in a read only section such that if
1177 * anyone does try to modify it, it will cause an exception.
1178 */
1179static const struct hlist_head empty_buckets[1];
1180static const struct ftrace_hash empty_hash = {
1181 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001182};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001183#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001184
Steven Rostedt2b499382011-05-03 22:49:52 -04001185static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001186 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001187 .notrace_hash = EMPTY_HASH,
1188 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001189 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1190 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001191};
1192
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001193struct ftrace_page {
1194 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001195 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001196 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001197 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001198};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001199
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001200static struct ftrace_page *ftrace_new_pgs;
1201
Steven Rostedta7900872011-12-16 16:23:44 -05001202#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1203#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001204
1205/* estimate from running different kernels */
1206#define NR_TO_INIT 10000
1207
1208static struct ftrace_page *ftrace_pages_start;
1209static struct ftrace_page *ftrace_pages;
1210
Steven Rostedt06a51d92011-12-19 19:07:36 -05001211static bool ftrace_hash_empty(struct ftrace_hash *hash)
1212{
1213 return !hash || !hash->count;
1214}
1215
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001216static struct ftrace_func_entry *
1217ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1218{
1219 unsigned long key;
1220 struct ftrace_func_entry *entry;
1221 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001222
Steven Rostedt06a51d92011-12-19 19:07:36 -05001223 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001224 return NULL;
1225
1226 if (hash->size_bits > 0)
1227 key = hash_long(ip, hash->size_bits);
1228 else
1229 key = 0;
1230
1231 hhd = &hash->buckets[key];
1232
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001233 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001234 if (entry->ip == ip)
1235 return entry;
1236 }
1237 return NULL;
1238}
1239
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001240static void __add_hash_entry(struct ftrace_hash *hash,
1241 struct ftrace_func_entry *entry)
1242{
1243 struct hlist_head *hhd;
1244 unsigned long key;
1245
1246 if (hash->size_bits)
1247 key = hash_long(entry->ip, hash->size_bits);
1248 else
1249 key = 0;
1250
1251 hhd = &hash->buckets[key];
1252 hlist_add_head(&entry->hlist, hhd);
1253 hash->count++;
1254}
1255
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001256static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1257{
1258 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001259
1260 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1261 if (!entry)
1262 return -ENOMEM;
1263
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001264 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001265 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001266
1267 return 0;
1268}
1269
1270static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001271free_hash_entry(struct ftrace_hash *hash,
1272 struct ftrace_func_entry *entry)
1273{
1274 hlist_del(&entry->hlist);
1275 kfree(entry);
1276 hash->count--;
1277}
1278
1279static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001280remove_hash_entry(struct ftrace_hash *hash,
1281 struct ftrace_func_entry *entry)
1282{
1283 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001284 hash->count--;
1285}
1286
1287static void ftrace_hash_clear(struct ftrace_hash *hash)
1288{
1289 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001290 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001291 struct ftrace_func_entry *entry;
1292 int size = 1 << hash->size_bits;
1293 int i;
1294
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001295 if (!hash->count)
1296 return;
1297
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001298 for (i = 0; i < size; i++) {
1299 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001300 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001301 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001302 }
1303 FTRACE_WARN_ON(hash->count);
1304}
1305
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001306static void free_ftrace_hash(struct ftrace_hash *hash)
1307{
1308 if (!hash || hash == EMPTY_HASH)
1309 return;
1310 ftrace_hash_clear(hash);
1311 kfree(hash->buckets);
1312 kfree(hash);
1313}
1314
Steven Rostedt07fd5512011-05-05 18:03:47 -04001315static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1316{
1317 struct ftrace_hash *hash;
1318
1319 hash = container_of(rcu, struct ftrace_hash, rcu);
1320 free_ftrace_hash(hash);
1321}
1322
1323static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1324{
1325 if (!hash || hash == EMPTY_HASH)
1326 return;
1327 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1328}
1329
Jiri Olsa5500fa52012-02-15 15:51:54 +01001330void ftrace_free_filter(struct ftrace_ops *ops)
1331{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001332 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001333 free_ftrace_hash(ops->filter_hash);
1334 free_ftrace_hash(ops->notrace_hash);
1335}
1336
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001337static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1338{
1339 struct ftrace_hash *hash;
1340 int size;
1341
1342 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1343 if (!hash)
1344 return NULL;
1345
1346 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001347 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001348
1349 if (!hash->buckets) {
1350 kfree(hash);
1351 return NULL;
1352 }
1353
1354 hash->size_bits = size_bits;
1355
1356 return hash;
1357}
1358
1359static struct ftrace_hash *
1360alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1361{
1362 struct ftrace_func_entry *entry;
1363 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001364 int size;
1365 int ret;
1366 int i;
1367
1368 new_hash = alloc_ftrace_hash(size_bits);
1369 if (!new_hash)
1370 return NULL;
1371
1372 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001373 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001374 return new_hash;
1375
1376 size = 1 << hash->size_bits;
1377 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001378 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001379 ret = add_hash_entry(new_hash, entry->ip);
1380 if (ret < 0)
1381 goto free_hash;
1382 }
1383 }
1384
1385 FTRACE_WARN_ON(new_hash->count != hash->count);
1386
1387 return new_hash;
1388
1389 free_hash:
1390 free_ftrace_hash(new_hash);
1391 return NULL;
1392}
1393
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001394static void
1395ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1396static void
1397ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1398
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001399static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001400ftrace_hash_move(struct ftrace_ops *ops, int enable,
1401 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001402{
1403 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001404 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001405 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001406 struct ftrace_hash *old_hash;
1407 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001408 int size = src->count;
1409 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001410 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001411 int i;
1412
1413 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001414 * Remove the current set, update the hash and add
1415 * them back.
1416 */
1417 ftrace_hash_rec_disable(ops, enable);
1418
1419 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001420 * If the new source is empty, just free dst and assign it
1421 * the empty_hash.
1422 */
1423 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001424 free_ftrace_hash_rcu(*dst);
1425 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001426 /* still need to update the function records */
1427 ret = 0;
1428 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001429 }
1430
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001431 /*
1432 * Make the hash size about 1/2 the # found
1433 */
1434 for (size /= 2; size; size >>= 1)
1435 bits++;
1436
1437 /* Don't allocate too much */
1438 if (bits > FTRACE_HASH_MAX_BITS)
1439 bits = FTRACE_HASH_MAX_BITS;
1440
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001441 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001442 new_hash = alloc_ftrace_hash(bits);
1443 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001444 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001445
1446 size = 1 << src->size_bits;
1447 for (i = 0; i < size; i++) {
1448 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001449 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001450 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001451 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001452 }
1453 }
1454
Steven Rostedt07fd5512011-05-05 18:03:47 -04001455 old_hash = *dst;
1456 rcu_assign_pointer(*dst, new_hash);
1457 free_ftrace_hash_rcu(old_hash);
1458
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001459 ret = 0;
1460 out:
1461 /*
1462 * Enable regardless of ret:
1463 * On success, we enable the new hash.
1464 * On failure, we re-enable the original hash.
1465 */
1466 ftrace_hash_rec_enable(ops, enable);
1467
1468 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001469}
1470
Steven Rostedt265c8312009-02-13 12:43:56 -05001471/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001472 * Test the hashes for this ops to see if we want to call
1473 * the ops->func or not.
1474 *
1475 * It's a match if the ip is in the ops->filter_hash or
1476 * the filter_hash does not exist or is empty,
1477 * AND
1478 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001479 *
1480 * This needs to be called with preemption disabled as
1481 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001482 */
1483static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001484ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001485{
1486 struct ftrace_hash *filter_hash;
1487 struct ftrace_hash *notrace_hash;
1488 int ret;
1489
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001490#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1491 /*
1492 * There's a small race when adding ops that the ftrace handler
1493 * that wants regs, may be called without them. We can not
1494 * allow that handler to be called if regs is NULL.
1495 */
1496 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1497 return 0;
1498#endif
1499
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001500 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1501 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001502
Steven Rostedt06a51d92011-12-19 19:07:36 -05001503 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001504 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001505 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001506 !ftrace_lookup_ip(notrace_hash, ip)))
1507 ret = 1;
1508 else
1509 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001510
1511 return ret;
1512}
1513
1514/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001515 * This is a double for. Do not use 'break' to break out of the loop,
1516 * you must use a goto.
1517 */
1518#define do_for_each_ftrace_rec(pg, rec) \
1519 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1520 int _____i; \
1521 for (_____i = 0; _____i < pg->index; _____i++) { \
1522 rec = &pg->records[_____i];
1523
1524#define while_for_each_ftrace_rec() \
1525 } \
1526 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301527
Steven Rostedt5855fea2011-12-16 19:27:42 -05001528
1529static int ftrace_cmp_recs(const void *a, const void *b)
1530{
Steven Rostedta650e022012-04-25 13:48:13 -04001531 const struct dyn_ftrace *key = a;
1532 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001533
Steven Rostedta650e022012-04-25 13:48:13 -04001534 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001535 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001536 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1537 return 1;
1538 return 0;
1539}
1540
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001541static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001542{
1543 struct ftrace_page *pg;
1544 struct dyn_ftrace *rec;
1545 struct dyn_ftrace key;
1546
1547 key.ip = start;
1548 key.flags = end; /* overload flags, as it is unsigned long */
1549
1550 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1551 if (end < pg->records[0].ip ||
1552 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1553 continue;
1554 rec = bsearch(&key, pg->records, pg->index,
1555 sizeof(struct dyn_ftrace),
1556 ftrace_cmp_recs);
1557 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001558 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001559 }
1560
Steven Rostedt5855fea2011-12-16 19:27:42 -05001561 return 0;
1562}
1563
Steven Rostedtc88fd862011-08-16 09:53:39 -04001564/**
1565 * ftrace_location - return true if the ip giving is a traced location
1566 * @ip: the instruction pointer to check
1567 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001568 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001569 * That is, the instruction that is either a NOP or call to
1570 * the function tracer. It checks the ftrace internal tables to
1571 * determine if the address belongs or not.
1572 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001573unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001574{
Steven Rostedta650e022012-04-25 13:48:13 -04001575 return ftrace_location_range(ip, ip);
1576}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001577
Steven Rostedta650e022012-04-25 13:48:13 -04001578/**
1579 * ftrace_text_reserved - return true if range contains an ftrace location
1580 * @start: start of range to search
1581 * @end: end of range to search (inclusive). @end points to the last byte to check.
1582 *
1583 * Returns 1 if @start and @end contains a ftrace location.
1584 * That is, the instruction that is either a NOP or call to
1585 * the function tracer. It checks the ftrace internal tables to
1586 * determine if the address belongs or not.
1587 */
1588int ftrace_text_reserved(void *start, void *end)
1589{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001590 unsigned long ret;
1591
1592 ret = ftrace_location_range((unsigned long)start,
1593 (unsigned long)end);
1594
1595 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001596}
1597
Steven Rostedted926f92011-05-03 13:25:24 -04001598static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1599 int filter_hash,
1600 bool inc)
1601{
1602 struct ftrace_hash *hash;
1603 struct ftrace_hash *other_hash;
1604 struct ftrace_page *pg;
1605 struct dyn_ftrace *rec;
1606 int count = 0;
1607 int all = 0;
1608
1609 /* Only update if the ops has been registered */
1610 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1611 return;
1612
1613 /*
1614 * In the filter_hash case:
1615 * If the count is zero, we update all records.
1616 * Otherwise we just update the items in the hash.
1617 *
1618 * In the notrace_hash case:
1619 * We enable the update in the hash.
1620 * As disabling notrace means enabling the tracing,
1621 * and enabling notrace means disabling, the inc variable
1622 * gets inversed.
1623 */
1624 if (filter_hash) {
1625 hash = ops->filter_hash;
1626 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001627 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001628 all = 1;
1629 } else {
1630 inc = !inc;
1631 hash = ops->notrace_hash;
1632 other_hash = ops->filter_hash;
1633 /*
1634 * If the notrace hash has no items,
1635 * then there's nothing to do.
1636 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001637 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001638 return;
1639 }
1640
1641 do_for_each_ftrace_rec(pg, rec) {
1642 int in_other_hash = 0;
1643 int in_hash = 0;
1644 int match = 0;
1645
1646 if (all) {
1647 /*
1648 * Only the filter_hash affects all records.
1649 * Update if the record is not in the notrace hash.
1650 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001651 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001652 match = 1;
1653 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001654 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1655 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001656
1657 /*
1658 *
1659 */
1660 if (filter_hash && in_hash && !in_other_hash)
1661 match = 1;
1662 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001663 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001664 match = 1;
1665 }
1666 if (!match)
1667 continue;
1668
1669 if (inc) {
1670 rec->flags++;
1671 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1672 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001673 /*
1674 * If any ops wants regs saved for this function
1675 * then all ops will get saved regs.
1676 */
1677 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1678 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001679 } else {
1680 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1681 return;
1682 rec->flags--;
1683 }
1684 count++;
1685 /* Shortcut, if we handled all records, we are done. */
1686 if (!all && count == hash->count)
1687 return;
1688 } while_for_each_ftrace_rec();
1689}
1690
1691static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1692 int filter_hash)
1693{
1694 __ftrace_hash_rec_update(ops, filter_hash, 0);
1695}
1696
1697static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1698 int filter_hash)
1699{
1700 __ftrace_hash_rec_update(ops, filter_hash, 1);
1701}
1702
Steven Rostedt05736a42008-09-22 14:55:47 -07001703static void print_ip_ins(const char *fmt, unsigned char *p)
1704{
1705 int i;
1706
1707 printk(KERN_CONT "%s", fmt);
1708
1709 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1710 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1711}
1712
Steven Rostedtc88fd862011-08-16 09:53:39 -04001713/**
1714 * ftrace_bug - report and shutdown function tracer
1715 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1716 * @ip: The address that failed
1717 *
1718 * The arch code that enables or disables the function tracing
1719 * can call ftrace_bug() when it has detected a problem in
1720 * modifying the code. @failed should be one of either:
1721 * EFAULT - if the problem happens on reading the @ip address
1722 * EINVAL - if what is read at @ip is not what was expected
1723 * EPERM - if the problem happens on writting to the @ip address
1724 */
1725void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001726{
1727 switch (failed) {
1728 case -EFAULT:
1729 FTRACE_WARN_ON_ONCE(1);
1730 pr_info("ftrace faulted on modifying ");
1731 print_ip_sym(ip);
1732 break;
1733 case -EINVAL:
1734 FTRACE_WARN_ON_ONCE(1);
1735 pr_info("ftrace failed to modify ");
1736 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001737 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001738 printk(KERN_CONT "\n");
1739 break;
1740 case -EPERM:
1741 FTRACE_WARN_ON_ONCE(1);
1742 pr_info("ftrace faulted on writing ");
1743 print_ip_sym(ip);
1744 break;
1745 default:
1746 FTRACE_WARN_ON_ONCE(1);
1747 pr_info("ftrace faulted on unknown error ");
1748 print_ip_sym(ip);
1749 }
1750}
1751
Steven Rostedtc88fd862011-08-16 09:53:39 -04001752static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001753{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001754 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001755
Steven Rostedt982c3502008-11-15 16:31:41 -05001756 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001757 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001758 *
Steven Rostedted926f92011-05-03 13:25:24 -04001759 * If the record has a ref count, then we need to enable it
1760 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001761 *
Steven Rostedted926f92011-05-03 13:25:24 -04001762 * Otherwise we make sure its disabled.
1763 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001764 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001765 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001766 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001767 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001768 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001769
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001770 /*
1771 * If enabling and the REGS flag does not match the REGS_EN, then
1772 * do not ignore this record. Set flags to fail the compare against
1773 * ENABLED.
1774 */
1775 if (flag &&
1776 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1777 flag |= FTRACE_FL_REGS;
1778
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001779 /* If the state of this record hasn't changed, then do nothing */
1780 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001781 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001782
1783 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001784 /* Save off if rec is being enabled (for return value) */
1785 flag ^= rec->flags & FTRACE_FL_ENABLED;
1786
1787 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001788 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001789 if (flag & FTRACE_FL_REGS) {
1790 if (rec->flags & FTRACE_FL_REGS)
1791 rec->flags |= FTRACE_FL_REGS_EN;
1792 else
1793 rec->flags &= ~FTRACE_FL_REGS_EN;
1794 }
1795 }
1796
1797 /*
1798 * If this record is being updated from a nop, then
1799 * return UPDATE_MAKE_CALL.
1800 * Otherwise, if the EN flag is set, then return
1801 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1802 * from the non-save regs, to a save regs function.
1803 * Otherwise,
1804 * return UPDATE_MODIFY_CALL to tell the caller to convert
1805 * from the save regs, to a non-save regs function.
1806 */
1807 if (flag & FTRACE_FL_ENABLED)
1808 return FTRACE_UPDATE_MAKE_CALL;
1809 else if (rec->flags & FTRACE_FL_REGS_EN)
1810 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1811 else
1812 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001813 }
1814
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001815 if (update) {
1816 /* If there's no more users, clear all flags */
1817 if (!(rec->flags & ~FTRACE_FL_MASK))
1818 rec->flags = 0;
1819 else
1820 /* Just disable the record (keep REGS state) */
1821 rec->flags &= ~FTRACE_FL_ENABLED;
1822 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001823
1824 return FTRACE_UPDATE_MAKE_NOP;
1825}
1826
1827/**
1828 * ftrace_update_record, set a record that now is tracing or not
1829 * @rec: the record to update
1830 * @enable: set to 1 if the record is tracing, zero to force disable
1831 *
1832 * The records that represent all functions that can be traced need
1833 * to be updated when tracing has been enabled.
1834 */
1835int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1836{
1837 return ftrace_check_record(rec, enable, 1);
1838}
1839
1840/**
1841 * ftrace_test_record, check if the record has been enabled or not
1842 * @rec: the record to test
1843 * @enable: set to 1 to check if enabled, 0 if it is disabled
1844 *
1845 * The arch code may need to test if a record is already set to
1846 * tracing to determine how to modify the function code that it
1847 * represents.
1848 */
1849int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1850{
1851 return ftrace_check_record(rec, enable, 0);
1852}
1853
1854static int
1855__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1856{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001857 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001858 unsigned long ftrace_addr;
1859 int ret;
1860
Steven Rostedtc88fd862011-08-16 09:53:39 -04001861 ret = ftrace_update_record(rec, enable);
1862
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001863 if (rec->flags & FTRACE_FL_REGS)
1864 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1865 else
1866 ftrace_addr = (unsigned long)FTRACE_ADDR;
1867
Steven Rostedtc88fd862011-08-16 09:53:39 -04001868 switch (ret) {
1869 case FTRACE_UPDATE_IGNORE:
1870 return 0;
1871
1872 case FTRACE_UPDATE_MAKE_CALL:
1873 return ftrace_make_call(rec, ftrace_addr);
1874
1875 case FTRACE_UPDATE_MAKE_NOP:
1876 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001877
1878 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1879 case FTRACE_UPDATE_MODIFY_CALL:
1880 if (rec->flags & FTRACE_FL_REGS)
1881 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1882 else
1883 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1884
1885 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001886 }
1887
1888 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001889}
1890
Steven Rostedte4f5d542012-04-27 09:13:18 -04001891void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001892{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001893 struct dyn_ftrace *rec;
1894 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001895 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001896
Steven Rostedt45a4a232011-04-21 23:16:46 -04001897 if (unlikely(ftrace_disabled))
1898 return;
1899
Steven Rostedt265c8312009-02-13 12:43:56 -05001900 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001901 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001902 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001903 ftrace_bug(failed, rec->ip);
1904 /* Stop processing */
1905 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001906 }
1907 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001908}
1909
Steven Rostedtc88fd862011-08-16 09:53:39 -04001910struct ftrace_rec_iter {
1911 struct ftrace_page *pg;
1912 int index;
1913};
1914
1915/**
1916 * ftrace_rec_iter_start, start up iterating over traced functions
1917 *
1918 * Returns an iterator handle that is used to iterate over all
1919 * the records that represent address locations where functions
1920 * are traced.
1921 *
1922 * May return NULL if no records are available.
1923 */
1924struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1925{
1926 /*
1927 * We only use a single iterator.
1928 * Protected by the ftrace_lock mutex.
1929 */
1930 static struct ftrace_rec_iter ftrace_rec_iter;
1931 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1932
1933 iter->pg = ftrace_pages_start;
1934 iter->index = 0;
1935
1936 /* Could have empty pages */
1937 while (iter->pg && !iter->pg->index)
1938 iter->pg = iter->pg->next;
1939
1940 if (!iter->pg)
1941 return NULL;
1942
1943 return iter;
1944}
1945
1946/**
1947 * ftrace_rec_iter_next, get the next record to process.
1948 * @iter: The handle to the iterator.
1949 *
1950 * Returns the next iterator after the given iterator @iter.
1951 */
1952struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1953{
1954 iter->index++;
1955
1956 if (iter->index >= iter->pg->index) {
1957 iter->pg = iter->pg->next;
1958 iter->index = 0;
1959
1960 /* Could have empty pages */
1961 while (iter->pg && !iter->pg->index)
1962 iter->pg = iter->pg->next;
1963 }
1964
1965 if (!iter->pg)
1966 return NULL;
1967
1968 return iter;
1969}
1970
1971/**
1972 * ftrace_rec_iter_record, get the record at the iterator location
1973 * @iter: The current iterator location
1974 *
1975 * Returns the record that the current @iter is at.
1976 */
1977struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1978{
1979 return &iter->pg->records[iter->index];
1980}
1981
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301982static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001983ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001984{
1985 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001986 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001987
1988 ip = rec->ip;
1989
Steven Rostedt45a4a232011-04-21 23:16:46 -04001990 if (unlikely(ftrace_disabled))
1991 return 0;
1992
Shaohua Li25aac9d2009-01-09 11:29:40 +08001993 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001994 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001995 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301996 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001997 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301998 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001999}
2000
Steven Rostedt000ab692009-02-17 13:35:06 -05002001/*
2002 * archs can override this function if they must do something
2003 * before the modifying code is performed.
2004 */
2005int __weak ftrace_arch_code_modify_prepare(void)
2006{
2007 return 0;
2008}
2009
2010/*
2011 * archs can override this function if they must do something
2012 * after the modifying code is performed.
2013 */
2014int __weak ftrace_arch_code_modify_post_process(void)
2015{
2016 return 0;
2017}
2018
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002019void ftrace_modify_all_code(int command)
2020{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002021 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2022
2023 /*
2024 * If the ftrace_caller calls a ftrace_ops func directly,
2025 * we need to make sure that it only traces functions it
2026 * expects to trace. When doing the switch of functions,
2027 * we need to update to the ftrace_ops_list_func first
2028 * before the transition between old and new calls are set,
2029 * as the ftrace_ops_list_func will check the ops hashes
2030 * to make sure the ops are having the right functions
2031 * traced.
2032 */
2033 if (update)
2034 ftrace_update_ftrace_func(ftrace_ops_list_func);
2035
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002036 if (command & FTRACE_UPDATE_CALLS)
2037 ftrace_replace_code(1);
2038 else if (command & FTRACE_DISABLE_CALLS)
2039 ftrace_replace_code(0);
2040
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002041 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2042 function_trace_op = set_function_trace_op;
2043 smp_wmb();
2044 /* If irqs are disabled, we are in stop machine */
2045 if (!irqs_disabled())
2046 smp_call_function(ftrace_sync_ipi, NULL, 1);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002047 ftrace_update_ftrace_func(ftrace_trace_function);
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002048 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002049
2050 if (command & FTRACE_START_FUNC_RET)
2051 ftrace_enable_ftrace_graph_caller();
2052 else if (command & FTRACE_STOP_FUNC_RET)
2053 ftrace_disable_ftrace_graph_caller();
2054}
2055
Ingo Molnare309b412008-05-12 21:20:51 +02002056static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002057{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002058 int *command = data;
2059
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002060 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002061
Steven Rostedtc88fd862011-08-16 09:53:39 -04002062 return 0;
2063}
2064
2065/**
2066 * ftrace_run_stop_machine, go back to the stop machine method
2067 * @command: The command to tell ftrace what to do
2068 *
2069 * If an arch needs to fall back to the stop machine method, the
2070 * it can call this function.
2071 */
2072void ftrace_run_stop_machine(int command)
2073{
2074 stop_machine(__ftrace_modify_code, &command, NULL);
2075}
2076
2077/**
2078 * arch_ftrace_update_code, modify the code to trace or not trace
2079 * @command: The command that needs to be done
2080 *
2081 * Archs can override this function if it does not need to
2082 * run stop_machine() to modify code.
2083 */
2084void __weak arch_ftrace_update_code(int command)
2085{
2086 ftrace_run_stop_machine(command);
2087}
2088
2089static void ftrace_run_update_code(int command)
2090{
2091 int ret;
2092
2093 ret = ftrace_arch_code_modify_prepare();
2094 FTRACE_WARN_ON(ret);
2095 if (ret)
2096 return;
2097 /*
2098 * Do not call function tracer while we update the code.
2099 * We are in stop machine.
2100 */
2101 function_trace_stop++;
2102
2103 /*
2104 * By default we use stop_machine() to modify the code.
2105 * But archs can do what ever they want as long as it
2106 * is safe. The stop_machine() is the safest, but also
2107 * produces the most overhead.
2108 */
2109 arch_ftrace_update_code(command);
2110
Steven Rostedt6331c282011-07-13 15:11:02 -04002111 function_trace_stop--;
2112
Steven Rostedt000ab692009-02-17 13:35:06 -05002113 ret = ftrace_arch_code_modify_post_process();
2114 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002115}
2116
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002117static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002118static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002119static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002120
2121static void ftrace_startup_enable(int command)
2122{
2123 if (saved_ftrace_func != ftrace_trace_function) {
2124 saved_ftrace_func = ftrace_trace_function;
2125 command |= FTRACE_UPDATE_TRACE_FUNC;
2126 }
2127
2128 if (!command || !ftrace_enabled)
2129 return;
2130
2131 ftrace_run_update_code(command);
2132}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002133
Steven Rostedta1cd6172011-05-23 15:24:25 -04002134static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002135{
Steven Rostedtb8489142011-05-04 09:27:52 -04002136 bool hash_enable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002137 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002138
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002139 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002140 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002141
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002142 ret = __register_ftrace_function(ops);
2143 if (ret)
2144 return ret;
2145
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002146 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002147 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002148
Steven Rostedtb8489142011-05-04 09:27:52 -04002149 /* ops marked global share the filter hashes */
2150 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2151 ops = &global_ops;
2152 /* Don't update hash if global is already set */
2153 if (global_start_up)
2154 hash_enable = false;
2155 global_start_up++;
2156 }
2157
Steven Rostedted926f92011-05-03 13:25:24 -04002158 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002159 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002160 ftrace_hash_rec_enable(ops, 1);
2161
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002162 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002163
2164 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002165}
2166
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002167static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002168{
Steven Rostedtb8489142011-05-04 09:27:52 -04002169 bool hash_disable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002170 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002171
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002172 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002173 return -ENODEV;
2174
2175 ret = __unregister_ftrace_function(ops);
2176 if (ret)
2177 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002178
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002179 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002180 /*
2181 * Just warn in case of unbalance, no need to kill ftrace, it's not
2182 * critical but the ftrace_call callers may be never nopped again after
2183 * further ftrace uses.
2184 */
2185 WARN_ON_ONCE(ftrace_start_up < 0);
2186
Steven Rostedtb8489142011-05-04 09:27:52 -04002187 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2188 ops = &global_ops;
2189 global_start_up--;
2190 WARN_ON_ONCE(global_start_up < 0);
2191 /* Don't update hash if global still has users */
2192 if (global_start_up) {
2193 WARN_ON_ONCE(!ftrace_start_up);
2194 hash_disable = false;
2195 }
2196 }
2197
2198 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002199 ftrace_hash_rec_disable(ops, 1);
2200
Steven Rostedtb8489142011-05-04 09:27:52 -04002201 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002202 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002203
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002204 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002205
2206 if (saved_ftrace_func != ftrace_trace_function) {
2207 saved_ftrace_func = ftrace_trace_function;
2208 command |= FTRACE_UPDATE_TRACE_FUNC;
2209 }
2210
2211 if (!command || !ftrace_enabled)
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002212 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002213
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002214 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002215 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002216}
2217
Ingo Molnare309b412008-05-12 21:20:51 +02002218static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002219{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002220 if (unlikely(ftrace_disabled))
2221 return;
2222
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002223 /* Force update next time */
2224 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002225 /* ftrace_start_up is true if we want ftrace running */
2226 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002227 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002228}
2229
Ingo Molnare309b412008-05-12 21:20:51 +02002230static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002231{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002232 if (unlikely(ftrace_disabled))
2233 return;
2234
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002235 /* ftrace_start_up is true if ftrace is running */
2236 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002237 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002238}
2239
Steven Rostedt3d083392008-05-12 21:20:42 +02002240static cycle_t ftrace_update_time;
2241static unsigned long ftrace_update_cnt;
2242unsigned long ftrace_update_tot_cnt;
2243
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002244static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002245{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002246 /*
2247 * Filter_hash being empty will default to trace module.
2248 * But notrace hash requires a test of individual module functions.
2249 */
2250 return ftrace_hash_empty(ops->filter_hash) &&
2251 ftrace_hash_empty(ops->notrace_hash);
2252}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002253
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002254/*
2255 * Check if the current ops references the record.
2256 *
2257 * If the ops traces all functions, then it was already accounted for.
2258 * If the ops does not trace the current record function, skip it.
2259 * If the ops ignores the function via notrace filter, skip it.
2260 */
2261static inline bool
2262ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2263{
2264 /* If ops isn't enabled, ignore it */
2265 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2266 return 0;
2267
2268 /* If ops traces all mods, we already accounted for it */
2269 if (ops_traces_mod(ops))
2270 return 0;
2271
2272 /* The function must be in the filter */
2273 if (!ftrace_hash_empty(ops->filter_hash) &&
2274 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2275 return 0;
2276
2277 /* If in notrace hash, we ignore it too */
2278 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2279 return 0;
2280
2281 return 1;
2282}
2283
2284static int referenced_filters(struct dyn_ftrace *rec)
2285{
2286 struct ftrace_ops *ops;
2287 int cnt = 0;
2288
2289 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2290 if (ops_references_rec(ops, rec))
2291 cnt++;
2292 }
2293
2294 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002295}
2296
Steven Rostedt31e88902008-11-14 16:21:19 -08002297static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002298{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002299 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002300 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302301 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002302 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002303 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002304 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002305
2306 /*
2307 * When adding a module, we need to check if tracers are
2308 * currently enabled and if they are set to trace all functions.
2309 * If they are, we need to enable the module functions as well
2310 * as update the reference counts for those function records.
2311 */
2312 if (mod) {
2313 struct ftrace_ops *ops;
2314
2315 for (ops = ftrace_ops_list;
2316 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002317 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2318 if (ops_traces_mod(ops))
2319 ref++;
2320 else
2321 test = true;
2322 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002323 }
2324 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002325
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002326 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002327 ftrace_update_cnt = 0;
2328
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002329 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302330
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002331 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002332 int cnt = ref;
2333
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002334 /* If something went wrong, bail without enabling anything */
2335 if (unlikely(ftrace_disabled))
2336 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002337
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002338 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002339 if (test)
2340 cnt += referenced_filters(p);
2341 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302342
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002343 /*
2344 * Do the initial record conversion from mcount jump
2345 * to the NOP instructions.
2346 */
2347 if (!ftrace_code_disable(mod, p))
2348 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002349
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002350 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002351
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002352 /*
2353 * If the tracing is enabled, go ahead and enable the record.
2354 *
2355 * The reason not to enable the record immediatelly is the
2356 * inherent check of ftrace_make_nop/ftrace_make_call for
2357 * correct previous instructions. Making first the NOP
2358 * conversion puts the module to the correct state, thus
2359 * passing the ftrace_make_call check.
2360 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002361 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002362 int failed = __ftrace_replace_code(p, 1);
2363 if (failed)
2364 ftrace_bug(failed, p->ip);
2365 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002366 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002367 }
2368
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002369 ftrace_new_pgs = NULL;
2370
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002371 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002372 ftrace_update_time = stop - start;
2373 ftrace_update_tot_cnt += ftrace_update_cnt;
2374
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002375 return 0;
2376}
2377
Steven Rostedta7900872011-12-16 16:23:44 -05002378static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002379{
Steven Rostedta7900872011-12-16 16:23:44 -05002380 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002381 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002382
Steven Rostedta7900872011-12-16 16:23:44 -05002383 if (WARN_ON(!count))
2384 return -EINVAL;
2385
2386 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002387
2388 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002389 * We want to fill as much as possible. No more than a page
2390 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002391 */
Steven Rostedta7900872011-12-16 16:23:44 -05002392 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2393 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002394
Steven Rostedta7900872011-12-16 16:23:44 -05002395 again:
2396 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2397
2398 if (!pg->records) {
2399 /* if we can't allocate this size, try something smaller */
2400 if (!order)
2401 return -ENOMEM;
2402 order >>= 1;
2403 goto again;
2404 }
2405
2406 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2407 pg->size = cnt;
2408
2409 if (cnt > count)
2410 cnt = count;
2411
2412 return cnt;
2413}
2414
2415static struct ftrace_page *
2416ftrace_allocate_pages(unsigned long num_to_init)
2417{
2418 struct ftrace_page *start_pg;
2419 struct ftrace_page *pg;
2420 int order;
2421 int cnt;
2422
2423 if (!num_to_init)
2424 return 0;
2425
2426 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2427 if (!pg)
2428 return NULL;
2429
2430 /*
2431 * Try to allocate as much as possible in one continues
2432 * location that fills in all of the space. We want to
2433 * waste as little space as possible.
2434 */
2435 for (;;) {
2436 cnt = ftrace_allocate_records(pg, num_to_init);
2437 if (cnt < 0)
2438 goto free_pages;
2439
2440 num_to_init -= cnt;
2441 if (!num_to_init)
2442 break;
2443
2444 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2445 if (!pg->next)
2446 goto free_pages;
2447
2448 pg = pg->next;
2449 }
2450
2451 return start_pg;
2452
2453 free_pages:
2454 while (start_pg) {
2455 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2456 free_pages((unsigned long)pg->records, order);
2457 start_pg = pg->next;
2458 kfree(pg);
2459 pg = start_pg;
2460 }
2461 pr_info("ftrace: FAILED to allocate memory for functions\n");
2462 return NULL;
2463}
2464
2465static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2466{
2467 int cnt;
2468
2469 if (!num_to_init) {
2470 pr_info("ftrace: No functions to be traced?\n");
2471 return -1;
2472 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002473
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002474 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002475 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002476 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002477
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002478 return 0;
2479}
2480
Steven Rostedt5072c592008-05-12 21:20:43 +02002481#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2482
2483struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002484 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002485 loff_t func_pos;
2486 struct ftrace_page *pg;
2487 struct dyn_ftrace *func;
2488 struct ftrace_func_probe *probe;
2489 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002490 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002491 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002492 int hidx;
2493 int idx;
2494 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002495};
2496
Ingo Molnare309b412008-05-12 21:20:51 +02002497static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002498t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002499{
2500 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002501 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002502 struct hlist_head *hhd;
2503
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002504 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002505 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002506
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002507 if (iter->probe)
2508 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002509 retry:
2510 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2511 return NULL;
2512
2513 hhd = &ftrace_func_hash[iter->hidx];
2514
2515 if (hlist_empty(hhd)) {
2516 iter->hidx++;
2517 hnd = NULL;
2518 goto retry;
2519 }
2520
2521 if (!hnd)
2522 hnd = hhd->first;
2523 else {
2524 hnd = hnd->next;
2525 if (!hnd) {
2526 iter->hidx++;
2527 goto retry;
2528 }
2529 }
2530
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002531 if (WARN_ON_ONCE(!hnd))
2532 return NULL;
2533
2534 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2535
2536 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002537}
2538
2539static void *t_hash_start(struct seq_file *m, loff_t *pos)
2540{
2541 struct ftrace_iterator *iter = m->private;
2542 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002543 loff_t l;
2544
Steven Rostedt69a30832011-12-19 15:21:16 -05002545 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2546 return NULL;
2547
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002548 if (iter->func_pos > *pos)
2549 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002550
Li Zefand82d6242009-06-24 09:54:54 +08002551 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002552 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002553 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002554 if (!p)
2555 break;
2556 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002557 if (!p)
2558 return NULL;
2559
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002560 /* Only set this if we have an item */
2561 iter->flags |= FTRACE_ITER_HASH;
2562
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002563 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002564}
2565
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002566static int
2567t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002568{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002569 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002570
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002571 rec = iter->probe;
2572 if (WARN_ON_ONCE(!rec))
2573 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002574
Steven Rostedt809dcf22009-02-16 23:06:01 -05002575 if (rec->ops->print)
2576 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2577
Steven Rostedtb375a112009-09-17 00:05:58 -04002578 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002579
2580 if (rec->data)
2581 seq_printf(m, ":%p", rec->data);
2582 seq_putc(m, '\n');
2583
2584 return 0;
2585}
2586
2587static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002588t_next(struct seq_file *m, void *v, loff_t *pos)
2589{
2590 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002591 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002592 struct dyn_ftrace *rec = NULL;
2593
Steven Rostedt45a4a232011-04-21 23:16:46 -04002594 if (unlikely(ftrace_disabled))
2595 return NULL;
2596
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002597 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002598 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002599
Steven Rostedt5072c592008-05-12 21:20:43 +02002600 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002601 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002602
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002603 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002604 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002605
Steven Rostedt5072c592008-05-12 21:20:43 +02002606 retry:
2607 if (iter->idx >= iter->pg->index) {
2608 if (iter->pg->next) {
2609 iter->pg = iter->pg->next;
2610 iter->idx = 0;
2611 goto retry;
2612 }
2613 } else {
2614 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002615 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002616 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002617
Steven Rostedt41c52c02008-05-22 11:46:33 -04002618 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002619 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2620
2621 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002622 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002623
Steven Rostedt5072c592008-05-12 21:20:43 +02002624 rec = NULL;
2625 goto retry;
2626 }
2627 }
2628
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002629 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002630 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002631
2632 iter->func = rec;
2633
2634 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002635}
2636
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002637static void reset_iter_read(struct ftrace_iterator *iter)
2638{
2639 iter->pos = 0;
2640 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002641 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002642}
2643
2644static void *t_start(struct seq_file *m, loff_t *pos)
2645{
2646 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002647 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002648 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002649 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002650
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002651 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002652
2653 if (unlikely(ftrace_disabled))
2654 return NULL;
2655
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002656 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002657 * If an lseek was done, then reset and start from beginning.
2658 */
2659 if (*pos < iter->pos)
2660 reset_iter_read(iter);
2661
2662 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002663 * For set_ftrace_filter reading, if we have the filter
2664 * off, we can short cut and just print out that all
2665 * functions are enabled.
2666 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002667 if (iter->flags & FTRACE_ITER_FILTER &&
2668 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002669 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002670 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002671 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002672 /* reset in case of seek/pread */
2673 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002674 return iter;
2675 }
2676
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002677 if (iter->flags & FTRACE_ITER_HASH)
2678 return t_hash_start(m, pos);
2679
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002680 /*
2681 * Unfortunately, we need to restart at ftrace_pages_start
2682 * every time we let go of the ftrace_mutex. This is because
2683 * those pointers can change without the lock.
2684 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002685 iter->pg = ftrace_pages_start;
2686 iter->idx = 0;
2687 for (l = 0; l <= *pos; ) {
2688 p = t_next(m, p, &l);
2689 if (!p)
2690 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002691 }
walimis5821e1b2008-11-15 15:19:06 +08002692
Steven Rostedt69a30832011-12-19 15:21:16 -05002693 if (!p)
2694 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002695
2696 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002697}
2698
2699static void t_stop(struct seq_file *m, void *p)
2700{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002701 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002702}
2703
2704static int t_show(struct seq_file *m, void *v)
2705{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002706 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002707 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002708
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002709 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002710 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002711
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002712 if (iter->flags & FTRACE_ITER_PRINTALL) {
2713 seq_printf(m, "#### all functions enabled ####\n");
2714 return 0;
2715 }
2716
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002717 rec = iter->func;
2718
Steven Rostedt5072c592008-05-12 21:20:43 +02002719 if (!rec)
2720 return 0;
2721
Steven Rostedt647bcd02011-05-03 14:39:21 -04002722 seq_printf(m, "%ps", (void *)rec->ip);
2723 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002724 seq_printf(m, " (%ld)%s",
2725 rec->flags & ~FTRACE_FL_MASK,
2726 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002727 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002728
2729 return 0;
2730}
2731
James Morris88e9d342009-09-22 16:43:43 -07002732static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002733 .start = t_start,
2734 .next = t_next,
2735 .stop = t_stop,
2736 .show = t_show,
2737};
2738
Ingo Molnare309b412008-05-12 21:20:51 +02002739static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002740ftrace_avail_open(struct inode *inode, struct file *file)
2741{
2742 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002743
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002744 if (unlikely(ftrace_disabled))
2745 return -ENODEV;
2746
Jiri Olsa50e18b92012-04-25 10:23:39 +02002747 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2748 if (iter) {
2749 iter->pg = ftrace_pages_start;
2750 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002751 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002752
Jiri Olsa50e18b92012-04-25 10:23:39 +02002753 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002754}
2755
Steven Rostedt647bcd02011-05-03 14:39:21 -04002756static int
2757ftrace_enabled_open(struct inode *inode, struct file *file)
2758{
2759 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002760
2761 if (unlikely(ftrace_disabled))
2762 return -ENODEV;
2763
Jiri Olsa50e18b92012-04-25 10:23:39 +02002764 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2765 if (iter) {
2766 iter->pg = ftrace_pages_start;
2767 iter->flags = FTRACE_ITER_ENABLED;
2768 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002769 }
2770
Jiri Olsa50e18b92012-04-25 10:23:39 +02002771 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002772}
2773
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002774static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002775{
Steven Rostedt52baf112009-02-14 01:15:39 -05002776 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002777 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002778 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002779}
2780
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002781/**
2782 * ftrace_regex_open - initialize function tracer filter files
2783 * @ops: The ftrace_ops that hold the hash filters
2784 * @flag: The type of filter to process
2785 * @inode: The inode, usually passed in to your open routine
2786 * @file: The file, usually passed in to your open routine
2787 *
2788 * ftrace_regex_open() initializes the filter files for the
2789 * @ops. Depending on @flag it may process the filter hash or
2790 * the notrace hash of @ops. With this called from the open
2791 * routine, you can use ftrace_filter_write() for the write
2792 * routine if @flag has FTRACE_ITER_FILTER set, or
2793 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05002794 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002795 * release must call ftrace_regex_release().
2796 */
2797int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002798ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002799 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002800{
2801 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002802 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002803 int ret = 0;
2804
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002805 ftrace_ops_init(ops);
2806
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002807 if (unlikely(ftrace_disabled))
2808 return -ENODEV;
2809
Steven Rostedt5072c592008-05-12 21:20:43 +02002810 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2811 if (!iter)
2812 return -ENOMEM;
2813
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002814 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2815 kfree(iter);
2816 return -ENOMEM;
2817 }
2818
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002819 iter->ops = ops;
2820 iter->flags = flag;
2821
2822 mutex_lock(&ops->regex_lock);
2823
Steven Rostedtf45948e2011-05-02 12:29:25 -04002824 if (flag & FTRACE_ITER_NOTRACE)
2825 hash = ops->notrace_hash;
2826 else
2827 hash = ops->filter_hash;
2828
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002829 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002830 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002831 if (!iter->hash) {
2832 trace_parser_put(&iter->parser);
2833 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002834 ret = -ENOMEM;
2835 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002836 }
2837 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002838
Steven Rostedt5072c592008-05-12 21:20:43 +02002839 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002840 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002841 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002842
2843 if (file->f_mode & FMODE_READ) {
2844 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002845
2846 ret = seq_open(file, &show_ftrace_seq_ops);
2847 if (!ret) {
2848 struct seq_file *m = file->private_data;
2849 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002850 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002851 /* Failed */
2852 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002853 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002854 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002855 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002856 } else
2857 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002858
2859 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002860 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002861
2862 return ret;
2863}
2864
Steven Rostedt41c52c02008-05-22 11:46:33 -04002865static int
2866ftrace_filter_open(struct inode *inode, struct file *file)
2867{
Steven Rostedt69a30832011-12-19 15:21:16 -05002868 return ftrace_regex_open(&global_ops,
2869 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2870 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002871}
2872
2873static int
2874ftrace_notrace_open(struct inode *inode, struct file *file)
2875{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002876 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002877 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002878}
2879
Steven Rostedt64e7c442009-02-13 17:08:48 -05002880static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002881{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002882 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002883 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002884
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002885 switch (type) {
2886 case MATCH_FULL:
2887 if (strcmp(str, regex) == 0)
2888 matched = 1;
2889 break;
2890 case MATCH_FRONT_ONLY:
2891 if (strncmp(str, regex, len) == 0)
2892 matched = 1;
2893 break;
2894 case MATCH_MIDDLE_ONLY:
2895 if (strstr(str, regex))
2896 matched = 1;
2897 break;
2898 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002899 slen = strlen(str);
2900 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002901 matched = 1;
2902 break;
2903 }
2904
2905 return matched;
2906}
2907
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002908static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002909enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002910{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002911 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002912 int ret = 0;
2913
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002914 entry = ftrace_lookup_ip(hash, rec->ip);
2915 if (not) {
2916 /* Do nothing if it doesn't exist */
2917 if (!entry)
2918 return 0;
2919
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002920 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002921 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002922 /* Do nothing if it exists */
2923 if (entry)
2924 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002925
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002926 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002927 }
2928 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002929}
2930
Steven Rostedt64e7c442009-02-13 17:08:48 -05002931static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002932ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2933 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002934{
2935 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002936 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002937
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002938 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2939
2940 if (mod) {
2941 /* module lookup requires matching the module */
2942 if (!modname || strcmp(modname, mod))
2943 return 0;
2944
2945 /* blank search means to match all funcs in the mod */
2946 if (!len)
2947 return 1;
2948 }
2949
Steven Rostedt64e7c442009-02-13 17:08:48 -05002950 return ftrace_match(str, regex, len, type);
2951}
2952
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002953static int
2954match_records(struct ftrace_hash *hash, char *buff,
2955 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002956{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002957 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002958 struct ftrace_page *pg;
2959 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002960 int type = MATCH_FULL;
2961 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002962 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002963 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002964
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002965 if (len) {
2966 type = filter_parse_regex(buff, len, &search, &not);
2967 search_len = strlen(search);
2968 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002969
Steven Rostedt52baf112009-02-14 01:15:39 -05002970 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002971
2972 if (unlikely(ftrace_disabled))
2973 goto out_unlock;
2974
Steven Rostedt265c8312009-02-13 12:43:56 -05002975 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002976 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002977 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002978 if (ret < 0) {
2979 found = ret;
2980 goto out_unlock;
2981 }
Li Zefan311d16d2009-12-08 11:15:11 +08002982 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002983 }
2984 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002985 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002986 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002987
2988 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002989}
2990
Steven Rostedt64e7c442009-02-13 17:08:48 -05002991static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002992ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002993{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002994 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002995}
2996
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002997static int
2998ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002999{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003000 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003001
Steven Rostedt64e7c442009-02-13 17:08:48 -05003002 /* blank or '*' mean the same */
3003 if (strcmp(buff, "*") == 0)
3004 buff[0] = 0;
3005
3006 /* handle the case of 'dont filter this module' */
3007 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3008 buff[0] = 0;
3009 not = 1;
3010 }
3011
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003012 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003013}
3014
Steven Rostedtf6180772009-02-14 00:40:25 -05003015/*
3016 * We register the module command as a template to show others how
3017 * to register the a command as well.
3018 */
3019
3020static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003021ftrace_mod_callback(struct ftrace_hash *hash,
3022 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003023{
3024 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003025 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003026
3027 /*
3028 * cmd == 'mod' because we only registered this func
3029 * for the 'mod' ftrace_func_command.
3030 * But if you register one func with multiple commands,
3031 * you can tell which command was used by the cmd
3032 * parameter.
3033 */
3034
3035 /* we must have a module name */
3036 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003037 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003038
3039 mod = strsep(&param, ":");
3040 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003041 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003042
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003043 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003044 if (!ret)
3045 ret = -EINVAL;
3046 if (ret < 0)
3047 return ret;
3048
3049 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003050}
3051
3052static struct ftrace_func_command ftrace_mod_cmd = {
3053 .name = "mod",
3054 .func = ftrace_mod_callback,
3055};
3056
3057static int __init ftrace_mod_cmd_init(void)
3058{
3059 return register_ftrace_command(&ftrace_mod_cmd);
3060}
Steven Rostedt6f415672012-10-05 12:13:07 -04003061core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003062
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003063static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003064 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003065{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003066 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003067 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003068 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003069
3070 key = hash_long(ip, FTRACE_HASH_BITS);
3071
3072 hhd = &ftrace_func_hash[key];
3073
3074 if (hlist_empty(hhd))
3075 return;
3076
3077 /*
3078 * Disable preemption for these calls to prevent a RCU grace
3079 * period. This syncs the hash iteration and freeing of items
3080 * on the hash. rcu_read_lock is too dangerous here.
3081 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003082 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003083 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003084 if (entry->ip == ip)
3085 entry->ops->func(ip, parent_ip, &entry->data);
3086 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003087 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003088}
3089
Steven Rostedtb6887d72009-02-17 12:32:04 -05003090static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003091{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003092 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003093 .flags = FTRACE_OPS_FL_INITIALIZED,
3094 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003095};
3096
Steven Rostedtb6887d72009-02-17 12:32:04 -05003097static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003098
Steven Rostedtb6887d72009-02-17 12:32:04 -05003099static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003100{
Steven Rostedtb8489142011-05-04 09:27:52 -04003101 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003102 int i;
3103
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003104 if (ftrace_probe_registered) {
3105 /* still need to update the function call sites */
3106 if (ftrace_enabled)
3107 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003108 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003109 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003110
3111 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3112 struct hlist_head *hhd = &ftrace_func_hash[i];
3113 if (hhd->first)
3114 break;
3115 }
3116 /* Nothing registered? */
3117 if (i == FTRACE_FUNC_HASHSIZE)
3118 return;
3119
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003120 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003121
Steven Rostedtb6887d72009-02-17 12:32:04 -05003122 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003123}
3124
Steven Rostedtb6887d72009-02-17 12:32:04 -05003125static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003126{
3127 int i;
3128
Steven Rostedtb6887d72009-02-17 12:32:04 -05003129 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003130 return;
3131
3132 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3133 struct hlist_head *hhd = &ftrace_func_hash[i];
3134 if (hhd->first)
3135 return;
3136 }
3137
3138 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003139 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003140
Steven Rostedtb6887d72009-02-17 12:32:04 -05003141 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003142}
3143
3144
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003145static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003146{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003147 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003148 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003149 kfree(entry);
3150}
3151
Steven Rostedt59df055f2009-02-14 15:29:06 -05003152int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003153register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003154 void *data)
3155{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003156 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003157 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3158 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003159 struct ftrace_page *pg;
3160 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003161 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003162 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003163 int count = 0;
3164 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003165 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003166
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003167 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003168 len = strlen(search);
3169
Steven Rostedtb6887d72009-02-17 12:32:04 -05003170 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003171 if (WARN_ON(not))
3172 return -EINVAL;
3173
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003174 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003175
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003176 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3177 if (!hash) {
3178 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003179 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003180 }
3181
3182 if (unlikely(ftrace_disabled)) {
3183 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003184 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003185 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003186
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003187 mutex_lock(&ftrace_lock);
3188
Steven Rostedt59df055f2009-02-14 15:29:06 -05003189 do_for_each_ftrace_rec(pg, rec) {
3190
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003191 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003192 continue;
3193
3194 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3195 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003196 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003197 if (!count)
3198 count = -ENOMEM;
3199 goto out_unlock;
3200 }
3201
3202 count++;
3203
3204 entry->data = data;
3205
3206 /*
3207 * The caller might want to do something special
3208 * for each function we find. We call the callback
3209 * to give the caller an opportunity to do so.
3210 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003211 if (ops->init) {
3212 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003213 /* caller does not like this func */
3214 kfree(entry);
3215 continue;
3216 }
3217 }
3218
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003219 ret = enter_record(hash, rec, 0);
3220 if (ret < 0) {
3221 kfree(entry);
3222 count = ret;
3223 goto out_unlock;
3224 }
3225
Steven Rostedt59df055f2009-02-14 15:29:06 -05003226 entry->ops = ops;
3227 entry->ip = rec->ip;
3228
3229 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3230 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3231
3232 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003233
3234 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3235 if (ret < 0)
3236 count = ret;
3237
Steven Rostedtb6887d72009-02-17 12:32:04 -05003238 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003239
3240 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003241 mutex_unlock(&ftrace_lock);
3242 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003243 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003244 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003245
3246 return count;
3247}
3248
3249enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003250 PROBE_TEST_FUNC = 1,
3251 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003252};
3253
3254static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003255__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003256 void *data, int flags)
3257{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003258 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003259 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003260 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003261 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003262 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003263 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003264 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003265 char str[KSYM_SYMBOL_LEN];
3266 int type = MATCH_FULL;
3267 int i, len = 0;
3268 char *search;
3269
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003270 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003271 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003272 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003273 int not;
3274
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003275 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003276 len = strlen(search);
3277
Steven Rostedtb6887d72009-02-17 12:32:04 -05003278 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003279 if (WARN_ON(not))
3280 return;
3281 }
3282
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003283 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003284
3285 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3286 if (!hash)
3287 /* Hmm, should report this somehow */
3288 goto out_unlock;
3289
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003290 INIT_LIST_HEAD(&free_list);
3291
Steven Rostedt59df055f2009-02-14 15:29:06 -05003292 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3293 struct hlist_head *hhd = &ftrace_func_hash[i];
3294
Sasha Levinb67bfe02013-02-27 17:06:00 -08003295 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003296
3297 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003298 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003299 continue;
3300
Steven Rostedtb6887d72009-02-17 12:32:04 -05003301 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003302 continue;
3303
3304 /* do this last, since it is the most expensive */
3305 if (glob) {
3306 kallsyms_lookup(entry->ip, NULL, NULL,
3307 NULL, str);
3308 if (!ftrace_match(str, glob, len, type))
3309 continue;
3310 }
3311
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003312 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3313 /* It is possible more than one entry had this ip */
3314 if (rec_entry)
3315 free_hash_entry(hash, rec_entry);
3316
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003317 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003318 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003319 }
3320 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003321 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003322 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003323 /*
3324 * Remove after the disable is called. Otherwise, if the last
3325 * probe is removed, a null hash means *all enabled*.
3326 */
3327 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003328 synchronize_sched();
3329 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3330 list_del(&entry->free_list);
3331 ftrace_free_entry(entry);
3332 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003333 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003334
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003335 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003336 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003337 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003338}
3339
3340void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003341unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003342 void *data)
3343{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003344 __unregister_ftrace_function_probe(glob, ops, data,
3345 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003346}
3347
3348void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003349unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003350{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003351 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003352}
3353
Steven Rostedtb6887d72009-02-17 12:32:04 -05003354void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003355{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003356 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003357}
3358
Steven Rostedtf6180772009-02-14 00:40:25 -05003359static LIST_HEAD(ftrace_commands);
3360static DEFINE_MUTEX(ftrace_cmd_mutex);
3361
Tom Zanussi38de93a2013-10-24 08:34:18 -05003362/*
3363 * Currently we only register ftrace commands from __init, so mark this
3364 * __init too.
3365 */
3366__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003367{
3368 struct ftrace_func_command *p;
3369 int ret = 0;
3370
3371 mutex_lock(&ftrace_cmd_mutex);
3372 list_for_each_entry(p, &ftrace_commands, list) {
3373 if (strcmp(cmd->name, p->name) == 0) {
3374 ret = -EBUSY;
3375 goto out_unlock;
3376 }
3377 }
3378 list_add(&cmd->list, &ftrace_commands);
3379 out_unlock:
3380 mutex_unlock(&ftrace_cmd_mutex);
3381
3382 return ret;
3383}
3384
Tom Zanussi38de93a2013-10-24 08:34:18 -05003385/*
3386 * Currently we only unregister ftrace commands from __init, so mark
3387 * this __init too.
3388 */
3389__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003390{
3391 struct ftrace_func_command *p, *n;
3392 int ret = -ENODEV;
3393
3394 mutex_lock(&ftrace_cmd_mutex);
3395 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3396 if (strcmp(cmd->name, p->name) == 0) {
3397 ret = 0;
3398 list_del_init(&p->list);
3399 goto out_unlock;
3400 }
3401 }
3402 out_unlock:
3403 mutex_unlock(&ftrace_cmd_mutex);
3404
3405 return ret;
3406}
3407
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003408static int ftrace_process_regex(struct ftrace_hash *hash,
3409 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003410{
Steven Rostedtf6180772009-02-14 00:40:25 -05003411 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003412 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003413 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003414
3415 func = strsep(&next, ":");
3416
3417 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003418 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003419 if (!ret)
3420 ret = -EINVAL;
3421 if (ret < 0)
3422 return ret;
3423 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003424 }
3425
Steven Rostedtf6180772009-02-14 00:40:25 -05003426 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003427
3428 command = strsep(&next, ":");
3429
Steven Rostedtf6180772009-02-14 00:40:25 -05003430 mutex_lock(&ftrace_cmd_mutex);
3431 list_for_each_entry(p, &ftrace_commands, list) {
3432 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003433 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003434 goto out_unlock;
3435 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003436 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003437 out_unlock:
3438 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003439
Steven Rostedtf6180772009-02-14 00:40:25 -05003440 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003441}
3442
Ingo Molnare309b412008-05-12 21:20:51 +02003443static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003444ftrace_regex_write(struct file *file, const char __user *ubuf,
3445 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003446{
3447 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003448 struct trace_parser *parser;
3449 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003450
Li Zefan4ba79782009-09-22 13:52:20 +08003451 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003452 return 0;
3453
Steven Rostedt5072c592008-05-12 21:20:43 +02003454 if (file->f_mode & FMODE_READ) {
3455 struct seq_file *m = file->private_data;
3456 iter = m->private;
3457 } else
3458 iter = file->private_data;
3459
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003460 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003461 return -ENODEV;
3462
3463 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003464
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003465 parser = &iter->parser;
3466 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003467
Li Zefan4ba79782009-09-22 13:52:20 +08003468 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003469 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003470 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003471 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003472 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003473 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003474 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003475 }
3476
Steven Rostedt5072c592008-05-12 21:20:43 +02003477 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003478 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003479 return ret;
3480}
3481
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003482ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003483ftrace_filter_write(struct file *file, const char __user *ubuf,
3484 size_t cnt, loff_t *ppos)
3485{
3486 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3487}
3488
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003489ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003490ftrace_notrace_write(struct file *file, const char __user *ubuf,
3491 size_t cnt, loff_t *ppos)
3492{
3493 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3494}
3495
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003496static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003497ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3498{
3499 struct ftrace_func_entry *entry;
3500
3501 if (!ftrace_location(ip))
3502 return -EINVAL;
3503
3504 if (remove) {
3505 entry = ftrace_lookup_ip(hash, ip);
3506 if (!entry)
3507 return -ENOENT;
3508 free_hash_entry(hash, entry);
3509 return 0;
3510 }
3511
3512 return add_hash_entry(hash, ip);
3513}
3514
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003515static void ftrace_ops_update_code(struct ftrace_ops *ops)
3516{
3517 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3518 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3519}
3520
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003521static int
3522ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3523 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003524{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003525 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003526 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003527 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003528
Steven Rostedt936e0742011-05-05 22:54:01 -04003529 /* All global ops uses the global ops filters */
3530 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3531 ops = &global_ops;
3532
Steven Rostedt41c52c02008-05-22 11:46:33 -04003533 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003534 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003535
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003536 mutex_lock(&ops->regex_lock);
3537
Steven Rostedtf45948e2011-05-02 12:29:25 -04003538 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003539 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003540 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003541 orig_hash = &ops->notrace_hash;
3542
3543 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003544 if (!hash) {
3545 ret = -ENOMEM;
3546 goto out_regex_unlock;
3547 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003548
Steven Rostedt41c52c02008-05-22 11:46:33 -04003549 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003550 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003551 if (buf && !ftrace_match_records(hash, buf, len)) {
3552 ret = -EINVAL;
3553 goto out_regex_unlock;
3554 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003555 if (ip) {
3556 ret = ftrace_match_addr(hash, ip, remove);
3557 if (ret < 0)
3558 goto out_regex_unlock;
3559 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003560
3561 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003562 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003563 if (!ret)
3564 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003565
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003566 mutex_unlock(&ftrace_lock);
3567
Jiri Olsaac483c42012-01-02 10:04:14 +01003568 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003569 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003570
3571 free_ftrace_hash(hash);
3572 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003573}
3574
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003575static int
3576ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3577 int reset, int enable)
3578{
3579 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3580}
3581
3582/**
3583 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3584 * @ops - the ops to set the filter with
3585 * @ip - the address to add to or remove from the filter.
3586 * @remove - non zero to remove the ip from the filter
3587 * @reset - non zero to reset all filters before applying this filter.
3588 *
3589 * Filters denote which functions should be enabled when tracing is enabled
3590 * If @ip is NULL, it failes to update filter.
3591 */
3592int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3593 int remove, int reset)
3594{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003595 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003596 return ftrace_set_addr(ops, ip, remove, reset, 1);
3597}
3598EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3599
3600static int
3601ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3602 int reset, int enable)
3603{
3604 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3605}
3606
Steven Rostedt77a2b372008-05-12 21:20:45 +02003607/**
3608 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003609 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003610 * @buf - the string that holds the function filter text.
3611 * @len - the length of the string.
3612 * @reset - non zero to reset all filters before applying this filter.
3613 *
3614 * Filters denote which functions should be enabled when tracing is enabled.
3615 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3616 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003617int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003618 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003619{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003620 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003621 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003622}
Steven Rostedt936e0742011-05-05 22:54:01 -04003623EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003624
Steven Rostedt41c52c02008-05-22 11:46:33 -04003625/**
3626 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003627 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003628 * @buf - the string that holds the function notrace text.
3629 * @len - the length of the string.
3630 * @reset - non zero to reset all filters before applying this filter.
3631 *
3632 * Notrace Filters denote which functions should not be enabled when tracing
3633 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3634 * for tracing.
3635 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003636int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003637 int len, int reset)
3638{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003639 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003640 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003641}
3642EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3643/**
3644 * ftrace_set_filter - set a function to filter on in ftrace
3645 * @ops - the ops to set the filter with
3646 * @buf - the string that holds the function filter text.
3647 * @len - the length of the string.
3648 * @reset - non zero to reset all filters before applying this filter.
3649 *
3650 * Filters denote which functions should be enabled when tracing is enabled.
3651 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3652 */
3653void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3654{
3655 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3656}
3657EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3658
3659/**
3660 * ftrace_set_notrace - set a function to not trace in ftrace
3661 * @ops - the ops to set the notrace filter with
3662 * @buf - the string that holds the function notrace text.
3663 * @len - the length of the string.
3664 * @reset - non zero to reset all filters before applying this filter.
3665 *
3666 * Notrace Filters denote which functions should not be enabled when tracing
3667 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3668 * for tracing.
3669 */
3670void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003671{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003672 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003673}
Steven Rostedt936e0742011-05-05 22:54:01 -04003674EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003675
Steven Rostedt2af15d62009-05-28 13:37:24 -04003676/*
3677 * command line interface to allow users to set filters on boot up.
3678 */
3679#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3680static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3681static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3682
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003683/* Used by function selftest to not test if filter is set */
3684bool ftrace_filter_param __initdata;
3685
Steven Rostedt2af15d62009-05-28 13:37:24 -04003686static int __init set_ftrace_notrace(char *str)
3687{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003688 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003689 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003690 return 1;
3691}
3692__setup("ftrace_notrace=", set_ftrace_notrace);
3693
3694static int __init set_ftrace_filter(char *str)
3695{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003696 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003697 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003698 return 1;
3699}
3700__setup("ftrace_filter=", set_ftrace_filter);
3701
Stefan Assmann369bc182009-10-12 22:17:21 +02003702#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003703static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003704static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003705
Stefan Assmann369bc182009-10-12 22:17:21 +02003706static int __init set_graph_function(char *str)
3707{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003708 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003709 return 1;
3710}
3711__setup("ftrace_graph_filter=", set_graph_function);
3712
3713static void __init set_ftrace_early_graph(char *buf)
3714{
3715 int ret;
3716 char *func;
3717
3718 while (buf) {
3719 func = strsep(&buf, ",");
3720 /* we allow only one expression at a time */
3721 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003722 FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003723 if (ret)
3724 printk(KERN_DEBUG "ftrace: function %s not "
3725 "traceable\n", func);
3726 }
3727}
3728#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3729
Steven Rostedt2a85a372011-12-19 21:57:44 -05003730void __init
3731ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003732{
3733 char *func;
3734
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003735 ftrace_ops_init(ops);
3736
Steven Rostedt2af15d62009-05-28 13:37:24 -04003737 while (buf) {
3738 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003739 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003740 }
3741}
3742
3743static void __init set_ftrace_early_filters(void)
3744{
3745 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003746 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003747 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003748 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003749#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3750 if (ftrace_graph_buf[0])
3751 set_ftrace_early_graph(ftrace_graph_buf);
3752#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003753}
3754
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003755int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003756{
3757 struct seq_file *m = (struct seq_file *)file->private_data;
3758 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003759 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003760 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003761 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003762 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003763
Steven Rostedt5072c592008-05-12 21:20:43 +02003764 if (file->f_mode & FMODE_READ) {
3765 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003766 seq_release(inode, file);
3767 } else
3768 iter = file->private_data;
3769
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003770 parser = &iter->parser;
3771 if (trace_parser_loaded(parser)) {
3772 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003773 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003774 }
3775
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003776 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003777
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003778 mutex_lock(&iter->ops->regex_lock);
3779
Steven Rostedt058e2972011-04-29 22:35:33 -04003780 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003781 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3782
3783 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003784 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003785 else
3786 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003787
Steven Rostedt058e2972011-04-29 22:35:33 -04003788 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003789 ret = ftrace_hash_move(iter->ops, filter_hash,
3790 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003791 if (!ret)
3792 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003793
Steven Rostedt058e2972011-04-29 22:35:33 -04003794 mutex_unlock(&ftrace_lock);
3795 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003796
3797 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003798 free_ftrace_hash(iter->hash);
3799 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003800
Steven Rostedt5072c592008-05-12 21:20:43 +02003801 return 0;
3802}
3803
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003804static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003805 .open = ftrace_avail_open,
3806 .read = seq_read,
3807 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003808 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003809};
3810
Steven Rostedt647bcd02011-05-03 14:39:21 -04003811static const struct file_operations ftrace_enabled_fops = {
3812 .open = ftrace_enabled_open,
3813 .read = seq_read,
3814 .llseek = seq_lseek,
3815 .release = seq_release_private,
3816};
3817
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003818static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003819 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003820 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003821 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003822 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003823 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003824};
3825
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003826static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003827 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003828 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003829 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003830 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003831 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003832};
3833
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003834#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3835
3836static DEFINE_MUTEX(graph_lock);
3837
3838int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003839int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003840unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003841unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003842
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003843struct ftrace_graph_data {
3844 unsigned long *table;
3845 size_t size;
3846 int *count;
3847 const struct seq_operations *seq_ops;
3848};
3849
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003850static void *
Li Zefan85951842009-06-24 09:54:00 +08003851__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003852{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003853 struct ftrace_graph_data *fgd = m->private;
3854
3855 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003856 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003857 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003858}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003859
Li Zefan85951842009-06-24 09:54:00 +08003860static void *
3861g_next(struct seq_file *m, void *v, loff_t *pos)
3862{
3863 (*pos)++;
3864 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003865}
3866
3867static void *g_start(struct seq_file *m, loff_t *pos)
3868{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003869 struct ftrace_graph_data *fgd = m->private;
3870
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003871 mutex_lock(&graph_lock);
3872
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003873 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003874 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003875 return (void *)1;
3876
Li Zefan85951842009-06-24 09:54:00 +08003877 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003878}
3879
3880static void g_stop(struct seq_file *m, void *p)
3881{
3882 mutex_unlock(&graph_lock);
3883}
3884
3885static int g_show(struct seq_file *m, void *v)
3886{
3887 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003888
3889 if (!ptr)
3890 return 0;
3891
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003892 if (ptr == (unsigned long *)1) {
3893 seq_printf(m, "#### all functions enabled ####\n");
3894 return 0;
3895 }
3896
Steven Rostedtb375a112009-09-17 00:05:58 -04003897 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003898
3899 return 0;
3900}
3901
James Morris88e9d342009-09-22 16:43:43 -07003902static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003903 .start = g_start,
3904 .next = g_next,
3905 .stop = g_stop,
3906 .show = g_show,
3907};
3908
3909static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003910__ftrace_graph_open(struct inode *inode, struct file *file,
3911 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003912{
3913 int ret = 0;
3914
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003915 mutex_lock(&graph_lock);
3916 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003917 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003918 *fgd->count = 0;
3919 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003920 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003921 mutex_unlock(&graph_lock);
3922
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003923 if (file->f_mode & FMODE_READ) {
3924 ret = seq_open(file, fgd->seq_ops);
3925 if (!ret) {
3926 struct seq_file *m = file->private_data;
3927 m->private = fgd;
3928 }
3929 } else
3930 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08003931
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003932 return ret;
3933}
3934
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003935static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003936ftrace_graph_open(struct inode *inode, struct file *file)
3937{
3938 struct ftrace_graph_data *fgd;
3939
3940 if (unlikely(ftrace_disabled))
3941 return -ENODEV;
3942
3943 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3944 if (fgd == NULL)
3945 return -ENOMEM;
3946
3947 fgd->table = ftrace_graph_funcs;
3948 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3949 fgd->count = &ftrace_graph_count;
3950 fgd->seq_ops = &ftrace_graph_seq_ops;
3951
3952 return __ftrace_graph_open(inode, file, fgd);
3953}
3954
3955static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003956ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3957{
3958 struct ftrace_graph_data *fgd;
3959
3960 if (unlikely(ftrace_disabled))
3961 return -ENODEV;
3962
3963 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3964 if (fgd == NULL)
3965 return -ENOMEM;
3966
3967 fgd->table = ftrace_graph_notrace_funcs;
3968 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3969 fgd->count = &ftrace_graph_notrace_count;
3970 fgd->seq_ops = &ftrace_graph_seq_ops;
3971
3972 return __ftrace_graph_open(inode, file, fgd);
3973}
3974
3975static int
Li Zefan87827112009-07-23 11:29:11 +08003976ftrace_graph_release(struct inode *inode, struct file *file)
3977{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003978 if (file->f_mode & FMODE_READ) {
3979 struct seq_file *m = file->private_data;
3980
3981 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08003982 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003983 } else {
3984 kfree(file->private_data);
3985 }
3986
Li Zefan87827112009-07-23 11:29:11 +08003987 return 0;
3988}
3989
3990static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003991ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003992{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003993 struct dyn_ftrace *rec;
3994 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003995 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003996 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003997 int type, not;
3998 char *search;
3999 bool exists;
4000 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004001
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004002 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004003 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004004 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004005 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004006
4007 search_len = strlen(search);
4008
Steven Rostedt52baf112009-02-14 01:15:39 -05004009 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004010
4011 if (unlikely(ftrace_disabled)) {
4012 mutex_unlock(&ftrace_lock);
4013 return -ENODEV;
4014 }
4015
Steven Rostedt265c8312009-02-13 12:43:56 -05004016 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004017
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004018 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004019 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004020 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004021 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004022 if (array[i] == rec->ip) {
4023 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004024 break;
4025 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004026 }
4027
4028 if (!not) {
4029 fail = 0;
4030 if (!exists) {
4031 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004032 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004033 goto out;
4034 }
4035 } else {
4036 if (exists) {
4037 array[i] = array[--(*idx)];
4038 array[*idx] = 0;
4039 fail = 0;
4040 }
4041 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004042 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004043 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004044out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004045 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004046
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004047 if (fail)
4048 return -EINVAL;
4049
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004050 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004051}
4052
4053static ssize_t
4054ftrace_graph_write(struct file *file, const char __user *ubuf,
4055 size_t cnt, loff_t *ppos)
4056{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004057 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004058 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004059 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004060
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004061 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004062 return 0;
4063
Namhyung Kim6a101082013-10-14 17:24:25 +09004064 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4065 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004066
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004067 read = trace_get_user(&parser, ubuf, cnt, ppos);
4068
Li Zefan4ba79782009-09-22 13:52:20 +08004069 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004070 parser.buffer[parser.idx] = 0;
4071
Namhyung Kim6a101082013-10-14 17:24:25 +09004072 mutex_lock(&graph_lock);
4073
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004074 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004075 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4076 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004077
4078 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004079 }
4080
Namhyung Kim6a101082013-10-14 17:24:25 +09004081 if (!ret)
4082 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004083
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004084 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004085
4086 return ret;
4087}
4088
4089static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004090 .open = ftrace_graph_open,
4091 .read = seq_read,
4092 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004093 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004094 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004095};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004096
4097static const struct file_operations ftrace_graph_notrace_fops = {
4098 .open = ftrace_graph_notrace_open,
4099 .read = seq_read,
4100 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004101 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004102 .release = ftrace_graph_release,
4103};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004104#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4105
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004106static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004107{
Steven Rostedt5072c592008-05-12 21:20:43 +02004108
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004109 trace_create_file("available_filter_functions", 0444,
4110 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004111
Steven Rostedt647bcd02011-05-03 14:39:21 -04004112 trace_create_file("enabled_functions", 0444,
4113 d_tracer, NULL, &ftrace_enabled_fops);
4114
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004115 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4116 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004117
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004118 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004119 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004120
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004121#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004122 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004123 NULL,
4124 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004125 trace_create_file("set_graph_notrace", 0444, d_tracer,
4126 NULL,
4127 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004128#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4129
Steven Rostedt5072c592008-05-12 21:20:43 +02004130 return 0;
4131}
4132
Steven Rostedt9fd49322012-04-24 22:32:06 -04004133static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004134{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004135 const unsigned long *ipa = a;
4136 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004137
Steven Rostedt9fd49322012-04-24 22:32:06 -04004138 if (*ipa > *ipb)
4139 return 1;
4140 if (*ipa < *ipb)
4141 return -1;
4142 return 0;
4143}
4144
4145static void ftrace_swap_ips(void *a, void *b, int size)
4146{
4147 unsigned long *ipa = a;
4148 unsigned long *ipb = b;
4149 unsigned long t;
4150
4151 t = *ipa;
4152 *ipa = *ipb;
4153 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004154}
4155
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004156static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004157 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004158 unsigned long *end)
4159{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004160 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004161 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004162 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004163 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004164 unsigned long *p;
4165 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004166 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004167 int ret = -ENOMEM;
4168
4169 count = end - start;
4170
4171 if (!count)
4172 return 0;
4173
Steven Rostedt9fd49322012-04-24 22:32:06 -04004174 sort(start, count, sizeof(*start),
4175 ftrace_cmp_ips, ftrace_swap_ips);
4176
Steven Rostedt706c81f2012-04-24 23:45:26 -04004177 start_pg = ftrace_allocate_pages(count);
4178 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004179 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004180
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004181 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004182
Steven Rostedt32082302011-12-16 14:42:37 -05004183 /*
4184 * Core and each module needs their own pages, as
4185 * modules will free them when they are removed.
4186 * Force a new page to be allocated for modules.
4187 */
Steven Rostedta7900872011-12-16 16:23:44 -05004188 if (!mod) {
4189 WARN_ON(ftrace_pages || ftrace_pages_start);
4190 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004191 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004192 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004193 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004194 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004195
Steven Rostedta7900872011-12-16 16:23:44 -05004196 if (WARN_ON(ftrace_pages->next)) {
4197 /* Hmm, we have free pages? */
4198 while (ftrace_pages->next)
4199 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004200 }
Steven Rostedta7900872011-12-16 16:23:44 -05004201
Steven Rostedt706c81f2012-04-24 23:45:26 -04004202 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004203 }
4204
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004205 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004206 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004207 while (p < end) {
4208 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004209 /*
4210 * Some architecture linkers will pad between
4211 * the different mcount_loc sections of different
4212 * object files to satisfy alignments.
4213 * Skip any NULL pointers.
4214 */
4215 if (!addr)
4216 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004217
4218 if (pg->index == pg->size) {
4219 /* We should have allocated enough */
4220 if (WARN_ON(!pg->next))
4221 break;
4222 pg = pg->next;
4223 }
4224
4225 rec = &pg->records[pg->index++];
4226 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004227 }
4228
Steven Rostedt706c81f2012-04-24 23:45:26 -04004229 /* We should have used all pages */
4230 WARN_ON(pg->next);
4231
4232 /* Assign the last page to ftrace_pages */
4233 ftrace_pages = pg;
4234
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004235 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004236 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004237
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004238 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004239 * We only need to disable interrupts on start up
4240 * because we are modifying code that an interrupt
4241 * may execute, and the modification is not atomic.
4242 * But for modules, nothing runs the code we modify
4243 * until we are finished with it, and there's no
4244 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004245 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004246 if (!mod)
4247 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08004248 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004249 if (!mod)
4250 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004251 ret = 0;
4252 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004253 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004254
Steven Rostedta7900872011-12-16 16:23:44 -05004255 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004256}
4257
Steven Rostedt93eb6772009-04-15 13:24:06 -04004258#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004259
4260#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4261
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004262void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004263{
4264 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004265 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004266 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004267 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004268
Steven Rostedt93eb6772009-04-15 13:24:06 -04004269 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004270
4271 if (ftrace_disabled)
4272 goto out_unlock;
4273
Steven Rostedt32082302011-12-16 14:42:37 -05004274 /*
4275 * Each module has its own ftrace_pages, remove
4276 * them from the list.
4277 */
4278 last_pg = &ftrace_pages_start;
4279 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4280 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004281 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004282 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004283 * As core pages are first, the first
4284 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004285 */
Steven Rostedt32082302011-12-16 14:42:37 -05004286 if (WARN_ON(pg == ftrace_pages_start))
4287 goto out_unlock;
4288
4289 /* Check if we are deleting the last page */
4290 if (pg == ftrace_pages)
4291 ftrace_pages = next_to_ftrace_page(last_pg);
4292
4293 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004294 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4295 free_pages((unsigned long)pg->records, order);
4296 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004297 } else
4298 last_pg = &pg->next;
4299 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004300 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004301 mutex_unlock(&ftrace_lock);
4302}
4303
4304static void ftrace_init_module(struct module *mod,
4305 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004306{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004307 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004308 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004309 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004310}
4311
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004312static int ftrace_module_notify_enter(struct notifier_block *self,
4313 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004314{
4315 struct module *mod = data;
4316
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004317 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004318 ftrace_init_module(mod, mod->ftrace_callsites,
4319 mod->ftrace_callsites +
4320 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004321 return 0;
4322}
4323
4324static int ftrace_module_notify_exit(struct notifier_block *self,
4325 unsigned long val, void *data)
4326{
4327 struct module *mod = data;
4328
4329 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004330 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004331
4332 return 0;
4333}
4334#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004335static int ftrace_module_notify_enter(struct notifier_block *self,
4336 unsigned long val, void *data)
4337{
4338 return 0;
4339}
4340static int ftrace_module_notify_exit(struct notifier_block *self,
4341 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004342{
4343 return 0;
4344}
4345#endif /* CONFIG_MODULES */
4346
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004347struct notifier_block ftrace_module_enter_nb = {
4348 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004349 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004350};
4351
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004352struct notifier_block ftrace_module_exit_nb = {
4353 .notifier_call = ftrace_module_notify_exit,
4354 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4355};
4356
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004357extern unsigned long __start_mcount_loc[];
4358extern unsigned long __stop_mcount_loc[];
4359
4360void __init ftrace_init(void)
4361{
4362 unsigned long count, addr, flags;
4363 int ret;
4364
4365 /* Keep the ftrace pointer to the stub */
4366 addr = (unsigned long)ftrace_stub;
4367
4368 local_irq_save(flags);
4369 ftrace_dyn_arch_init(&addr);
4370 local_irq_restore(flags);
4371
4372 /* ftrace_dyn_arch_init places the return code in addr */
4373 if (addr)
4374 goto failed;
4375
4376 count = __stop_mcount_loc - __start_mcount_loc;
4377
4378 ret = ftrace_dyn_table_alloc(count);
4379 if (ret)
4380 goto failed;
4381
4382 last_ftrace_enabled = ftrace_enabled = 1;
4383
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004384 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004385 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004386 __stop_mcount_loc);
4387
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004388 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004389 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004390 pr_warning("Failed to register trace ftrace module enter notifier\n");
4391
4392 ret = register_module_notifier(&ftrace_module_exit_nb);
4393 if (ret)
4394 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004395
Steven Rostedt2af15d62009-05-28 13:37:24 -04004396 set_ftrace_early_filters();
4397
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004398 return;
4399 failed:
4400 ftrace_disabled = 1;
4401}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004402
Steven Rostedt3d083392008-05-12 21:20:42 +02004403#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004404
Steven Rostedt2b499382011-05-03 22:49:52 -04004405static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004406 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004407 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4408 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004409};
4410
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004411static int __init ftrace_nodyn_init(void)
4412{
4413 ftrace_enabled = 1;
4414 return 0;
4415}
Steven Rostedt6f415672012-10-05 12:13:07 -04004416core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004417
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004418static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4419static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004420/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004421# define ftrace_startup(ops, command) \
4422 ({ \
4423 int ___ret = __register_ftrace_function(ops); \
4424 if (!___ret) \
4425 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4426 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004427 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004428# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4429
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004430# define ftrace_startup_sysctl() do { } while (0)
4431# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004432
4433static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004434ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004435{
4436 return 1;
4437}
4438
Steven Rostedt3d083392008-05-12 21:20:42 +02004439#endif /* CONFIG_DYNAMIC_FTRACE */
4440
Steven Rostedtb8489142011-05-04 09:27:52 -04004441static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004442ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004443 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004444{
Jiri Olsae2484912012-02-15 15:51:48 +01004445 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4446 return;
4447
4448 /*
4449 * Some of the ops may be dynamically allocated,
4450 * they must be freed after a synchronize_sched().
4451 */
4452 preempt_disable_notrace();
4453 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004454
4455 /*
4456 * Control funcs (perf) uses RCU. Only trace if
4457 * RCU is currently active.
4458 */
4459 if (!rcu_is_watching())
4460 goto out;
4461
Steven Rostedt0a016402012-11-02 17:03:03 -04004462 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004463 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4464 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004465 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004466 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004467 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004468 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004469 trace_recursion_clear(TRACE_CONTROL_BIT);
4470 preempt_enable_notrace();
4471}
4472
4473static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004474 .func = ftrace_ops_control_func,
4475 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4476 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004477};
4478
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004479static inline void
4480__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004481 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004482{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004483 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004484 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004485
Steven Rostedtccf36722012-06-05 09:44:25 -04004486 if (function_trace_stop)
4487 return;
4488
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004489 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4490 if (bit < 0)
4491 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004492
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004493 /*
4494 * Some of the ops may be dynamically allocated,
4495 * they must be freed after a synchronize_sched().
4496 */
4497 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004498 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004499 if (ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004500 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004501 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004502 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004503 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004504}
4505
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004506/*
4507 * Some archs only support passing ip and parent_ip. Even though
4508 * the list function ignores the op parameter, we do not want any
4509 * C side effects, where a function is called without the caller
4510 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004511 * Archs are to support both the regs and ftrace_ops at the same time.
4512 * If they support ftrace_ops, it is assumed they support regs.
4513 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004514 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4515 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004516 * An architecture can pass partial regs with ftrace_ops and still
4517 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004518 */
4519#if ARCH_SUPPORTS_FTRACE_OPS
4520static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004521 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004522{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004523 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004524}
4525#else
4526static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4527{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004528 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004529}
4530#endif
4531
Steven Rostedte32d8952008-12-04 00:26:41 -05004532static void clear_ftrace_swapper(void)
4533{
4534 struct task_struct *p;
4535 int cpu;
4536
4537 get_online_cpus();
4538 for_each_online_cpu(cpu) {
4539 p = idle_task(cpu);
4540 clear_tsk_trace_trace(p);
4541 }
4542 put_online_cpus();
4543}
4544
4545static void set_ftrace_swapper(void)
4546{
4547 struct task_struct *p;
4548 int cpu;
4549
4550 get_online_cpus();
4551 for_each_online_cpu(cpu) {
4552 p = idle_task(cpu);
4553 set_tsk_trace_trace(p);
4554 }
4555 put_online_cpus();
4556}
4557
4558static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004559{
4560 struct task_struct *p;
4561
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004562 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004563 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004564 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004565 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004566 rcu_read_unlock();
4567
Steven Rostedte32d8952008-12-04 00:26:41 -05004568 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004569}
4570
Steven Rostedte32d8952008-12-04 00:26:41 -05004571static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004572{
4573 struct task_struct *p;
4574
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004575 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004576 do_each_pid_task(pid, PIDTYPE_PID, p) {
4577 set_tsk_trace_trace(p);
4578 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004579 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004580}
4581
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004582static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004583{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004584 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004585 clear_ftrace_swapper();
4586 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004587 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004588}
4589
4590static void set_ftrace_pid_task(struct pid *pid)
4591{
4592 if (pid == ftrace_swapper_pid)
4593 set_ftrace_swapper();
4594 else
4595 set_ftrace_pid(pid);
4596}
4597
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004598static int ftrace_pid_add(int p)
4599{
4600 struct pid *pid;
4601 struct ftrace_pid *fpid;
4602 int ret = -EINVAL;
4603
4604 mutex_lock(&ftrace_lock);
4605
4606 if (!p)
4607 pid = ftrace_swapper_pid;
4608 else
4609 pid = find_get_pid(p);
4610
4611 if (!pid)
4612 goto out;
4613
4614 ret = 0;
4615
4616 list_for_each_entry(fpid, &ftrace_pids, list)
4617 if (fpid->pid == pid)
4618 goto out_put;
4619
4620 ret = -ENOMEM;
4621
4622 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4623 if (!fpid)
4624 goto out_put;
4625
4626 list_add(&fpid->list, &ftrace_pids);
4627 fpid->pid = pid;
4628
4629 set_ftrace_pid_task(pid);
4630
4631 ftrace_update_pid_func();
4632 ftrace_startup_enable(0);
4633
4634 mutex_unlock(&ftrace_lock);
4635 return 0;
4636
4637out_put:
4638 if (pid != ftrace_swapper_pid)
4639 put_pid(pid);
4640
4641out:
4642 mutex_unlock(&ftrace_lock);
4643 return ret;
4644}
4645
4646static void ftrace_pid_reset(void)
4647{
4648 struct ftrace_pid *fpid, *safe;
4649
4650 mutex_lock(&ftrace_lock);
4651 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4652 struct pid *pid = fpid->pid;
4653
4654 clear_ftrace_pid_task(pid);
4655
4656 list_del(&fpid->list);
4657 kfree(fpid);
4658 }
4659
4660 ftrace_update_pid_func();
4661 ftrace_startup_enable(0);
4662
4663 mutex_unlock(&ftrace_lock);
4664}
4665
4666static void *fpid_start(struct seq_file *m, loff_t *pos)
4667{
4668 mutex_lock(&ftrace_lock);
4669
4670 if (list_empty(&ftrace_pids) && (!*pos))
4671 return (void *) 1;
4672
4673 return seq_list_start(&ftrace_pids, *pos);
4674}
4675
4676static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4677{
4678 if (v == (void *)1)
4679 return NULL;
4680
4681 return seq_list_next(v, &ftrace_pids, pos);
4682}
4683
4684static void fpid_stop(struct seq_file *m, void *p)
4685{
4686 mutex_unlock(&ftrace_lock);
4687}
4688
4689static int fpid_show(struct seq_file *m, void *v)
4690{
4691 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4692
4693 if (v == (void *)1) {
4694 seq_printf(m, "no pid\n");
4695 return 0;
4696 }
4697
4698 if (fpid->pid == ftrace_swapper_pid)
4699 seq_printf(m, "swapper tasks\n");
4700 else
4701 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4702
4703 return 0;
4704}
4705
4706static const struct seq_operations ftrace_pid_sops = {
4707 .start = fpid_start,
4708 .next = fpid_next,
4709 .stop = fpid_stop,
4710 .show = fpid_show,
4711};
4712
4713static int
4714ftrace_pid_open(struct inode *inode, struct file *file)
4715{
4716 int ret = 0;
4717
4718 if ((file->f_mode & FMODE_WRITE) &&
4719 (file->f_flags & O_TRUNC))
4720 ftrace_pid_reset();
4721
4722 if (file->f_mode & FMODE_READ)
4723 ret = seq_open(file, &ftrace_pid_sops);
4724
4725 return ret;
4726}
4727
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004728static ssize_t
4729ftrace_pid_write(struct file *filp, const char __user *ubuf,
4730 size_t cnt, loff_t *ppos)
4731{
Ingo Molnar457dc922009-11-23 11:03:28 +01004732 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004733 long val;
4734 int ret;
4735
4736 if (cnt >= sizeof(buf))
4737 return -EINVAL;
4738
4739 if (copy_from_user(&buf, ubuf, cnt))
4740 return -EFAULT;
4741
4742 buf[cnt] = 0;
4743
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004744 /*
4745 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4746 * to clean the filter quietly.
4747 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004748 tmp = strstrip(buf);
4749 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004750 return 1;
4751
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004752 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004753 if (ret < 0)
4754 return ret;
4755
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004756 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004757
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004758 return ret ? ret : cnt;
4759}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004760
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004761static int
4762ftrace_pid_release(struct inode *inode, struct file *file)
4763{
4764 if (file->f_mode & FMODE_READ)
4765 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004766
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004767 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004768}
4769
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004770static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004771 .open = ftrace_pid_open,
4772 .write = ftrace_pid_write,
4773 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004774 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004775 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004776};
4777
4778static __init int ftrace_init_debugfs(void)
4779{
4780 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004781
4782 d_tracer = tracing_init_dentry();
4783 if (!d_tracer)
4784 return 0;
4785
4786 ftrace_init_dyn_debugfs(d_tracer);
4787
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004788 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4789 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004790
4791 ftrace_profile_debugfs(d_tracer);
4792
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004793 return 0;
4794}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004795fs_initcall(ftrace_init_debugfs);
4796
Steven Rostedt3d083392008-05-12 21:20:42 +02004797/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004798 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004799 *
4800 * This function should be used by panic code. It stops ftrace
4801 * but in a not so nice way. If you need to simply kill ftrace
4802 * from a non-atomic section, use ftrace_kill.
4803 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004804void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004805{
4806 ftrace_disabled = 1;
4807 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004808 clear_ftrace_function();
4809}
4810
4811/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004812 * Test if ftrace is dead or not.
4813 */
4814int ftrace_is_dead(void)
4815{
4816 return ftrace_disabled;
4817}
4818
4819/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004820 * register_ftrace_function - register a function for profiling
4821 * @ops - ops structure that holds the function for profiling.
4822 *
4823 * Register a function to be called by all functions in the
4824 * kernel.
4825 *
4826 * Note: @ops->func and all the functions it calls must be labeled
4827 * with "notrace", otherwise it will go into a
4828 * recursive loop.
4829 */
4830int register_ftrace_function(struct ftrace_ops *ops)
4831{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004832 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004833
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004834 ftrace_ops_init(ops);
4835
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004836 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004837
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004838 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004839
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004840 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004841
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004842 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004843}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004844EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004845
4846/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004847 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004848 * @ops - ops structure that holds the function to unregister
4849 *
4850 * Unregister a function that was added to be called by ftrace profiling.
4851 */
4852int unregister_ftrace_function(struct ftrace_ops *ops)
4853{
4854 int ret;
4855
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004856 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004857 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004858 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004859
4860 return ret;
4861}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004862EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004863
Ingo Molnare309b412008-05-12 21:20:51 +02004864int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004865ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004866 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004867 loff_t *ppos)
4868{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004869 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004870
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004871 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004872
Steven Rostedt45a4a232011-04-21 23:16:46 -04004873 if (unlikely(ftrace_disabled))
4874 goto out;
4875
4876 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004877
Li Zefana32c7762009-06-26 16:55:51 +08004878 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004879 goto out;
4880
Li Zefana32c7762009-06-26 16:55:51 +08004881 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004882
4883 if (ftrace_enabled) {
4884
4885 ftrace_startup_sysctl();
4886
4887 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004888 if (ftrace_ops_list != &ftrace_list_end)
4889 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004890
4891 } else {
4892 /* stopping ftrace calls (just send to ftrace_stub) */
4893 ftrace_trace_function = ftrace_stub;
4894
4895 ftrace_shutdown_sysctl();
4896 }
4897
4898 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004899 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004900 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004901}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004902
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004903#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004904
Steven Rostedt597af812009-04-03 15:24:12 -04004905static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004906static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004907
Steven Rostedte49dc192008-12-02 23:50:05 -05004908int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4909{
4910 return 0;
4911}
4912
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004913/* The callbacks that hook a function */
4914trace_func_graph_ret_t ftrace_graph_return =
4915 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004916trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05004917static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004918
4919/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4920static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4921{
4922 int i;
4923 int ret = 0;
4924 unsigned long flags;
4925 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4926 struct task_struct *g, *t;
4927
4928 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4929 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4930 * sizeof(struct ftrace_ret_stack),
4931 GFP_KERNEL);
4932 if (!ret_stack_list[i]) {
4933 start = 0;
4934 end = i;
4935 ret = -ENOMEM;
4936 goto free;
4937 }
4938 }
4939
4940 read_lock_irqsave(&tasklist_lock, flags);
4941 do_each_thread(g, t) {
4942 if (start == end) {
4943 ret = -EAGAIN;
4944 goto unlock;
4945 }
4946
4947 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004948 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004949 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004950 t->curr_ret_stack = -1;
4951 /* Make sure the tasks see the -1 first: */
4952 smp_wmb();
4953 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004954 }
4955 } while_each_thread(g, t);
4956
4957unlock:
4958 read_unlock_irqrestore(&tasklist_lock, flags);
4959free:
4960 for (i = start; i < end; i++)
4961 kfree(ret_stack_list[i]);
4962 return ret;
4963}
4964
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004965static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004966ftrace_graph_probe_sched_switch(void *ignore,
4967 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004968{
4969 unsigned long long timestamp;
4970 int index;
4971
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004972 /*
4973 * Does the user want to count the time a function was asleep.
4974 * If so, do not update the time stamps.
4975 */
4976 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4977 return;
4978
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004979 timestamp = trace_clock_local();
4980
4981 prev->ftrace_timestamp = timestamp;
4982
4983 /* only process tasks that we timestamped */
4984 if (!next->ftrace_timestamp)
4985 return;
4986
4987 /*
4988 * Update all the counters in next to make up for the
4989 * time next was sleeping.
4990 */
4991 timestamp -= next->ftrace_timestamp;
4992
4993 for (index = next->curr_ret_stack; index >= 0; index--)
4994 next->ret_stack[index].calltime += timestamp;
4995}
4996
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004997/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004998static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004999{
5000 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005001 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005002
5003 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5004 sizeof(struct ftrace_ret_stack *),
5005 GFP_KERNEL);
5006
5007 if (!ret_stack_list)
5008 return -ENOMEM;
5009
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005010 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005011 for_each_online_cpu(cpu) {
5012 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005013 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005014 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005015
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005016 do {
5017 ret = alloc_retstack_tasklist(ret_stack_list);
5018 } while (ret == -EAGAIN);
5019
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005020 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005021 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005022 if (ret)
5023 pr_info("ftrace_graph: Couldn't activate tracepoint"
5024 " probe to kernel_sched_switch\n");
5025 }
5026
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005027 kfree(ret_stack_list);
5028 return ret;
5029}
5030
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005031/*
5032 * Hibernation protection.
5033 * The state of the current task is too much unstable during
5034 * suspend/restore to disk. We want to protect against that.
5035 */
5036static int
5037ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5038 void *unused)
5039{
5040 switch (state) {
5041 case PM_HIBERNATION_PREPARE:
5042 pause_graph_tracing();
5043 break;
5044
5045 case PM_POST_HIBERNATION:
5046 unpause_graph_tracing();
5047 break;
5048 }
5049 return NOTIFY_DONE;
5050}
5051
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005052/* Just a place holder for function graph */
5053static struct ftrace_ops fgraph_ops __read_mostly = {
5054 .func = ftrace_stub,
5055 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
5056 FTRACE_OPS_FL_RECURSION_SAFE,
5057};
5058
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005059static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5060{
5061 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5062 return 0;
5063 return __ftrace_graph_entry(trace);
5064}
5065
5066/*
5067 * The function graph tracer should only trace the functions defined
5068 * by set_ftrace_filter and set_ftrace_notrace. If another function
5069 * tracer ops is registered, the graph tracer requires testing the
5070 * function against the global ops, and not just trace any function
5071 * that any ftrace_ops registered.
5072 */
5073static void update_function_graph_func(void)
5074{
5075 if (ftrace_ops_list == &ftrace_list_end ||
5076 (ftrace_ops_list == &global_ops &&
5077 global_ops.next == &ftrace_list_end))
5078 ftrace_graph_entry = __ftrace_graph_entry;
5079 else
5080 ftrace_graph_entry = ftrace_graph_entry_test;
5081}
5082
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005083int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5084 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005085{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005086 int ret = 0;
5087
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005088 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005089
Steven Rostedt05ce5812009-03-24 00:18:31 -04005090 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005091 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005092 ret = -EBUSY;
5093 goto out;
5094 }
5095
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005096 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5097 register_pm_notifier(&ftrace_suspend_notifier);
5098
Steven Rostedt597af812009-04-03 15:24:12 -04005099 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005100 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005101 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005102 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005103 goto out;
5104 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005105
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005106 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005107
5108 /*
5109 * Update the indirect function to the entryfunc, and the
5110 * function that gets called to the entry_test first. Then
5111 * call the update fgraph entry function to determine if
5112 * the entryfunc should be called directly or not.
5113 */
5114 __ftrace_graph_entry = entryfunc;
5115 ftrace_graph_entry = ftrace_graph_entry_test;
5116 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005117
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005118 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005119
5120out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005121 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005122 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005123}
5124
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005125void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005126{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005127 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005128
Steven Rostedt597af812009-04-03 15:24:12 -04005129 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005130 goto out;
5131
Steven Rostedt597af812009-04-03 15:24:12 -04005132 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005133 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005134 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005135 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005136 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005137 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005138 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005139
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005140 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005141 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005142}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005143
Steven Rostedt868baf02011-02-10 21:26:13 -05005144static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5145
5146static void
5147graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5148{
5149 atomic_set(&t->tracing_graph_pause, 0);
5150 atomic_set(&t->trace_overrun, 0);
5151 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005152 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005153 smp_wmb();
5154 t->ret_stack = ret_stack;
5155}
5156
5157/*
5158 * Allocate a return stack for the idle task. May be the first
5159 * time through, or it may be done by CPU hotplug online.
5160 */
5161void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5162{
5163 t->curr_ret_stack = -1;
5164 /*
5165 * The idle task has no parent, it either has its own
5166 * stack or no stack at all.
5167 */
5168 if (t->ret_stack)
5169 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5170
5171 if (ftrace_graph_active) {
5172 struct ftrace_ret_stack *ret_stack;
5173
5174 ret_stack = per_cpu(idle_ret_stack, cpu);
5175 if (!ret_stack) {
5176 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5177 * sizeof(struct ftrace_ret_stack),
5178 GFP_KERNEL);
5179 if (!ret_stack)
5180 return;
5181 per_cpu(idle_ret_stack, cpu) = ret_stack;
5182 }
5183 graph_init_task(t, ret_stack);
5184 }
5185}
5186
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005187/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005188void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005189{
Steven Rostedt84047e32009-06-02 16:51:55 -04005190 /* Make sure we do not use the parent ret_stack */
5191 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005192 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005193
Steven Rostedt597af812009-04-03 15:24:12 -04005194 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005195 struct ftrace_ret_stack *ret_stack;
5196
5197 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005198 * sizeof(struct ftrace_ret_stack),
5199 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005200 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005201 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005202 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005203 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005204}
5205
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005206void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005207{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005208 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5209
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005210 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005211 /* NULL must become visible to IRQs before we free it: */
5212 barrier();
5213
5214 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005215}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005216
5217void ftrace_graph_stop(void)
5218{
5219 ftrace_stop();
5220}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005221#endif