blob: 639b6ab1f04c5f0c155b791aa7c2d8d12dbb7233 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt6912896e2008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Jiri Olsae2484912012-02-15 15:51:48 +010065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040067static struct ftrace_ops ftrace_list_end __read_mostly = {
68 .func = ftrace_stub,
Steven Rostedt47409742012-07-20 11:04:44 -040069 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040070};
71
Steven Rostedt4eebcc82008-05-12 21:20:48 +020072/* ftrace_enabled is a method to turn ftrace on or off */
73int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020074static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020075
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050076/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077int function_trace_stop __read_mostly;
78
79/* Current function tracing op */
80struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050081
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040082/* List for set_ftrace_pid's pids. */
83LIST_HEAD(ftrace_pids);
84struct ftrace_pid {
85 struct list_head list;
86 struct pid *pid;
87};
88
Steven Rostedt4eebcc82008-05-12 21:20:48 +020089/*
90 * ftrace_disabled is set when an anomaly is discovered.
91 * ftrace_disabled is much stronger than ftrace_enabled.
92 */
93static int ftrace_disabled __read_mostly;
94
Steven Rostedt52baf112009-02-14 01:15:39 -050095static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020096
Steven Rostedtb8489142011-05-04 09:27:52 -040097static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
Jiri Olsae2484912012-02-15 15:51:48 +010098static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -040099static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200100ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500101ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400102static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100103static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200104
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400105#if ARCH_SUPPORTS_FTRACE_OPS
106static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400107 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400108#else
109/* See comment below, where ftrace_ops_list_func is defined */
110static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
111#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
112#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400113
Steven Rostedt0a016402012-11-02 17:03:03 -0400114/*
115 * Traverse the ftrace_global_list, invoking all entries. The reason that we
116 * can use rcu_dereference_raw() is that elements removed from this list
117 * are simply leaked, so there is no need to interact with a grace-period
118 * mechanism. The rcu_dereference_raw() calls are needed to handle
119 * concurrent insertions into the ftrace_global_list.
120 *
121 * Silly Alpha and silly pointer-speculation compiler optimizations!
122 */
123#define do_for_each_ftrace_op(op, list) \
124 op = rcu_dereference_raw(list); \
125 do
126
127/*
128 * Optimized for just a single item in the list (as that is the normal case).
129 */
130#define while_for_each_ftrace_op(op) \
131 while (likely(op = rcu_dereference_raw((op)->next)) && \
132 unlikely((op) != &ftrace_list_end))
133
Steven Rostedtea701f12012-07-20 13:08:05 -0400134/**
135 * ftrace_nr_registered_ops - return number of ops registered
136 *
137 * Returns the number of ftrace_ops registered and tracing functions
138 */
139int ftrace_nr_registered_ops(void)
140{
141 struct ftrace_ops *ops;
142 int cnt = 0;
143
144 mutex_lock(&ftrace_lock);
145
146 for (ops = ftrace_ops_list;
147 ops != &ftrace_list_end; ops = ops->next)
148 cnt++;
149
150 mutex_unlock(&ftrace_lock);
151
152 return cnt;
153}
154
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400155static void
156ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400157 struct ftrace_ops *op, struct pt_regs *regs)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200158{
Steven Rostedtc29f1222012-11-02 17:17:59 -0400159 int bit;
160
161 if (in_interrupt()) {
162 if (in_nmi())
163 bit = TRACE_GLOBAL_NMI_BIT;
164
165 else if (in_irq())
166 bit = TRACE_GLOBAL_IRQ_BIT;
167 else
168 bit = TRACE_GLOBAL_SIRQ_BIT;
169 } else
170 bit = TRACE_GLOBAL_BIT;
171
172 if (unlikely(trace_recursion_test(bit)))
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400173 return;
174
Steven Rostedtc29f1222012-11-02 17:17:59 -0400175 trace_recursion_set(bit);
Steven Rostedt0a016402012-11-02 17:03:03 -0400176 do_for_each_ftrace_op(op, ftrace_global_list) {
Steven Rostedta1e2e312011-08-09 12:50:46 -0400177 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -0400178 } while_for_each_ftrace_op(op);
Steven Rostedtc29f1222012-11-02 17:17:59 -0400179 trace_recursion_clear(bit);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200180}
181
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400182static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400183 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500184{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500185 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500186 return;
187
Steven Rostedta1e2e312011-08-09 12:50:46 -0400188 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500189}
190
191static void set_ftrace_pid_function(ftrace_func_t func)
192{
193 /* do not set ftrace_pid_function to itself! */
194 if (func != ftrace_pid_func)
195 ftrace_pid_function = func;
196}
197
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200198/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200199 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200200 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200201 * This NULLs the ftrace function and in essence stops
202 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200203 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200204void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200205{
Steven Rostedt3d083392008-05-12 21:20:42 +0200206 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500207 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200208}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200209
Jiri Olsae2484912012-02-15 15:51:48 +0100210static void control_ops_disable_all(struct ftrace_ops *ops)
211{
212 int cpu;
213
214 for_each_possible_cpu(cpu)
215 *per_cpu_ptr(ops->disabled, cpu) = 1;
216}
217
218static int control_ops_alloc(struct ftrace_ops *ops)
219{
220 int __percpu *disabled;
221
222 disabled = alloc_percpu(int);
223 if (!disabled)
224 return -ENOMEM;
225
226 ops->disabled = disabled;
227 control_ops_disable_all(ops);
228 return 0;
229}
230
231static void control_ops_free(struct ftrace_ops *ops)
232{
233 free_percpu(ops->disabled);
234}
235
Steven Rostedt2b499382011-05-03 22:49:52 -0400236static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400237{
238 ftrace_func_t func;
239
240 /*
241 * If there's only one function registered, then call that
242 * function directly. Otherwise, we need to iterate over the
243 * registered callers.
244 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400245 if (ftrace_global_list == &ftrace_list_end ||
Steven Rostedt63503792012-11-02 16:58:56 -0400246 ftrace_global_list->next == &ftrace_list_end) {
Steven Rostedtb8489142011-05-04 09:27:52 -0400247 func = ftrace_global_list->func;
Steven Rostedt63503792012-11-02 16:58:56 -0400248 /*
249 * As we are calling the function directly.
250 * If it does not have recursion protection,
251 * the function_trace_op needs to be updated
252 * accordingly.
253 */
254 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
255 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
256 else
257 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
258 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -0400259 func = ftrace_global_list_func;
Steven Rostedt63503792012-11-02 16:58:56 -0400260 /* The list has its own recursion protection. */
261 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
262 }
263
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400264
265 /* If we filter on pids, update to use the pid function */
266 if (!list_empty(&ftrace_pids)) {
267 set_ftrace_pid_function(func);
268 func = ftrace_pid_func;
269 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400270
271 global_ops.func = func;
272}
273
274static void update_ftrace_function(void)
275{
276 ftrace_func_t func;
277
278 update_global_ops();
279
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400280 /*
281 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400282 * recursion safe and not dynamic and the arch supports passing ops,
283 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400284 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400285 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400286 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400287 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400288 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400289 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400290 /* Set the ftrace_ops that the arch callback uses */
291 if (ftrace_ops_list == &global_ops)
292 function_trace_op = ftrace_global_list;
293 else
294 function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400295 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400296 } else {
297 /* Just use the default ftrace_ops */
298 function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400299 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400300 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400301
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400302 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400303}
304
Steven Rostedt2b499382011-05-03 22:49:52 -0400305static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200306{
Steven Rostedt2b499382011-05-03 22:49:52 -0400307 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200308 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400309 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200310 * CPU might be walking that list. We need to make sure
311 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400312 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200313 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400314 rcu_assign_pointer(*list, ops);
315}
Steven Rostedt3d083392008-05-12 21:20:42 +0200316
Steven Rostedt2b499382011-05-03 22:49:52 -0400317static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
318{
319 struct ftrace_ops **p;
320
321 /*
322 * If we are removing the last function, then simply point
323 * to the ftrace_stub.
324 */
325 if (*list == ops && ops->next == &ftrace_list_end) {
326 *list = &ftrace_list_end;
327 return 0;
328 }
329
330 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
331 if (*p == ops)
332 break;
333
334 if (*p != ops)
335 return -1;
336
337 *p = (*p)->next;
338 return 0;
339}
340
Jiri Olsae2484912012-02-15 15:51:48 +0100341static void add_ftrace_list_ops(struct ftrace_ops **list,
342 struct ftrace_ops *main_ops,
343 struct ftrace_ops *ops)
344{
345 int first = *list == &ftrace_list_end;
346 add_ftrace_ops(list, ops);
347 if (first)
348 add_ftrace_ops(&ftrace_ops_list, main_ops);
349}
350
351static int remove_ftrace_list_ops(struct ftrace_ops **list,
352 struct ftrace_ops *main_ops,
353 struct ftrace_ops *ops)
354{
355 int ret = remove_ftrace_ops(list, ops);
356 if (!ret && *list == &ftrace_list_end)
357 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
358 return ret;
359}
360
Steven Rostedt2b499382011-05-03 22:49:52 -0400361static int __register_ftrace_function(struct ftrace_ops *ops)
362{
Borislav Petkov8d240dd2012-03-29 19:11:40 +0200363 if (unlikely(ftrace_disabled))
Steven Rostedt2b499382011-05-03 22:49:52 -0400364 return -ENODEV;
365
366 if (FTRACE_WARN_ON(ops == &global_ops))
367 return -EINVAL;
368
Steven Rostedtb8489142011-05-04 09:27:52 -0400369 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
370 return -EBUSY;
371
Jiri Olsae2484912012-02-15 15:51:48 +0100372 /* We don't support both control and global flags set. */
373 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
374 return -EINVAL;
375
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900376#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400377 /*
378 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
379 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
380 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
381 */
382 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
383 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
384 return -EINVAL;
385
386 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
387 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
388#endif
389
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400390 if (!core_kernel_data((unsigned long)ops))
391 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
392
Steven Rostedtb8489142011-05-04 09:27:52 -0400393 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100394 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400395 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100396 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
397 if (control_ops_alloc(ops))
398 return -ENOMEM;
399 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400400 } else
401 add_ftrace_ops(&ftrace_ops_list, ops);
402
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400403 if (ftrace_enabled)
404 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200405
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200406 return 0;
407}
408
Ingo Molnare309b412008-05-12 21:20:51 +0200409static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200410{
Steven Rostedt2b499382011-05-03 22:49:52 -0400411 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200412
Steven Rostedt2b499382011-05-03 22:49:52 -0400413 if (ftrace_disabled)
414 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200415
Steven Rostedtb8489142011-05-04 09:27:52 -0400416 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
417 return -EBUSY;
418
Steven Rostedt2b499382011-05-03 22:49:52 -0400419 if (FTRACE_WARN_ON(ops == &global_ops))
420 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200421
Steven Rostedtb8489142011-05-04 09:27:52 -0400422 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100423 ret = remove_ftrace_list_ops(&ftrace_global_list,
424 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400425 if (!ret)
426 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100427 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
428 ret = remove_ftrace_list_ops(&ftrace_control_list,
429 &control_ops, ops);
430 if (!ret) {
431 /*
432 * The ftrace_ops is now removed from the list,
433 * so there'll be no new users. We must ensure
434 * all current users are done before we free
435 * the control data.
436 */
437 synchronize_sched();
438 control_ops_free(ops);
439 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400440 } else
441 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
442
Steven Rostedt2b499382011-05-03 22:49:52 -0400443 if (ret < 0)
444 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400445
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400446 if (ftrace_enabled)
447 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200448
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400449 /*
450 * Dynamic ops may be freed, we must make sure that all
451 * callers are done before leaving this function.
452 */
453 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
454 synchronize_sched();
455
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500456 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200457}
458
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500459static void ftrace_update_pid_func(void)
460{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400461 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500462 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900463 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500464
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400465 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500466}
467
Steven Rostedt493762f2009-03-23 17:12:36 -0400468#ifdef CONFIG_FUNCTION_PROFILER
469struct ftrace_profile {
470 struct hlist_node node;
471 unsigned long ip;
472 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400473#ifdef CONFIG_FUNCTION_GRAPH_TRACER
474 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400475 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400476#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400477};
478
479struct ftrace_profile_page {
480 struct ftrace_profile_page *next;
481 unsigned long index;
482 struct ftrace_profile records[];
483};
484
Steven Rostedtcafb1682009-03-24 20:50:39 -0400485struct ftrace_profile_stat {
486 atomic_t disabled;
487 struct hlist_head *hash;
488 struct ftrace_profile_page *pages;
489 struct ftrace_profile_page *start;
490 struct tracer_stat stat;
491};
492
Steven Rostedt493762f2009-03-23 17:12:36 -0400493#define PROFILE_RECORDS_SIZE \
494 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
495
496#define PROFILES_PER_PAGE \
497 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
498
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400499static int ftrace_profile_bits __read_mostly;
500static int ftrace_profile_enabled __read_mostly;
501
502/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400503static DEFINE_MUTEX(ftrace_profile_lock);
504
Steven Rostedtcafb1682009-03-24 20:50:39 -0400505static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400506
507#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
508
Steven Rostedt493762f2009-03-23 17:12:36 -0400509static void *
510function_stat_next(void *v, int idx)
511{
512 struct ftrace_profile *rec = v;
513 struct ftrace_profile_page *pg;
514
515 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
516
517 again:
Li Zefan0296e422009-06-26 11:15:37 +0800518 if (idx != 0)
519 rec++;
520
Steven Rostedt493762f2009-03-23 17:12:36 -0400521 if ((void *)rec >= (void *)&pg->records[pg->index]) {
522 pg = pg->next;
523 if (!pg)
524 return NULL;
525 rec = &pg->records[0];
526 if (!rec->counter)
527 goto again;
528 }
529
530 return rec;
531}
532
533static void *function_stat_start(struct tracer_stat *trace)
534{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400535 struct ftrace_profile_stat *stat =
536 container_of(trace, struct ftrace_profile_stat, stat);
537
538 if (!stat || !stat->start)
539 return NULL;
540
541 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400542}
543
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400544#ifdef CONFIG_FUNCTION_GRAPH_TRACER
545/* function graph compares on total time */
546static int function_stat_cmp(void *p1, void *p2)
547{
548 struct ftrace_profile *a = p1;
549 struct ftrace_profile *b = p2;
550
551 if (a->time < b->time)
552 return -1;
553 if (a->time > b->time)
554 return 1;
555 else
556 return 0;
557}
558#else
559/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400560static int function_stat_cmp(void *p1, void *p2)
561{
562 struct ftrace_profile *a = p1;
563 struct ftrace_profile *b = p2;
564
565 if (a->counter < b->counter)
566 return -1;
567 if (a->counter > b->counter)
568 return 1;
569 else
570 return 0;
571}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400572#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400573
574static int function_stat_headers(struct seq_file *m)
575{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400576#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400577 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400578 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400579 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400580 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400581#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400582 seq_printf(m, " Function Hit\n"
583 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400584#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400585 return 0;
586}
587
588static int function_stat_show(struct seq_file *m, void *v)
589{
590 struct ftrace_profile *rec = v;
591 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800592 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400593#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400594 static struct trace_seq s;
595 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400596 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400597#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800598 mutex_lock(&ftrace_profile_lock);
599
600 /* we raced with function_profile_reset() */
601 if (unlikely(rec->counter == 0)) {
602 ret = -EBUSY;
603 goto out;
604 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400605
606 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400607 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400608
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400609#ifdef CONFIG_FUNCTION_GRAPH_TRACER
610 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400611 avg = rec->time;
612 do_div(avg, rec->counter);
613
Chase Douglase330b3b2010-04-26 14:02:05 -0400614 /* Sample standard deviation (s^2) */
615 if (rec->counter <= 1)
616 stddev = 0;
617 else {
618 stddev = rec->time_squared - rec->counter * avg * avg;
619 /*
620 * Divide only 1000 for ns^2 -> us^2 conversion.
621 * trace_print_graph_duration will divide 1000 again.
622 */
623 do_div(stddev, (rec->counter - 1) * 1000);
624 }
625
Steven Rostedt34886c82009-03-25 21:00:47 -0400626 trace_seq_init(&s);
627 trace_print_graph_duration(rec->time, &s);
628 trace_seq_puts(&s, " ");
629 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400630 trace_seq_puts(&s, " ");
631 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400632 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400633#endif
634 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800635out:
636 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400637
Li Zefan3aaba202010-08-23 16:50:12 +0800638 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400639}
640
Steven Rostedtcafb1682009-03-24 20:50:39 -0400641static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400642{
643 struct ftrace_profile_page *pg;
644
Steven Rostedtcafb1682009-03-24 20:50:39 -0400645 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400646
647 while (pg) {
648 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
649 pg->index = 0;
650 pg = pg->next;
651 }
652
Steven Rostedtcafb1682009-03-24 20:50:39 -0400653 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400654 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
655}
656
Steven Rostedtcafb1682009-03-24 20:50:39 -0400657int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400658{
659 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400660 int functions;
661 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400662 int i;
663
664 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400665 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400666 return 0;
667
Steven Rostedtcafb1682009-03-24 20:50:39 -0400668 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
669 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400670 return -ENOMEM;
671
Steven Rostedt318e0a72009-03-25 20:06:34 -0400672#ifdef CONFIG_DYNAMIC_FTRACE
673 functions = ftrace_update_tot_cnt;
674#else
675 /*
676 * We do not know the number of functions that exist because
677 * dynamic tracing is what counts them. With past experience
678 * we have around 20K functions. That should be more than enough.
679 * It is highly unlikely we will execute every function in
680 * the kernel.
681 */
682 functions = 20000;
683#endif
684
Steven Rostedtcafb1682009-03-24 20:50:39 -0400685 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400686
Steven Rostedt318e0a72009-03-25 20:06:34 -0400687 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
688
689 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400690 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400691 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400692 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400693 pg = pg->next;
694 }
695
696 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400697
698 out_free:
699 pg = stat->start;
700 while (pg) {
701 unsigned long tmp = (unsigned long)pg;
702
703 pg = pg->next;
704 free_page(tmp);
705 }
706
707 free_page((unsigned long)stat->pages);
708 stat->pages = NULL;
709 stat->start = NULL;
710
711 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400712}
713
Steven Rostedtcafb1682009-03-24 20:50:39 -0400714static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400715{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400717 int size;
718
Steven Rostedtcafb1682009-03-24 20:50:39 -0400719 stat = &per_cpu(ftrace_profile_stats, cpu);
720
721 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400722 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400723 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400724 return 0;
725 }
726
727 /*
728 * We are profiling all functions, but usually only a few thousand
729 * functions are hit. We'll make a hash of 1024 items.
730 */
731 size = FTRACE_PROFILE_HASH_SIZE;
732
Steven Rostedtcafb1682009-03-24 20:50:39 -0400733 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400734
Steven Rostedtcafb1682009-03-24 20:50:39 -0400735 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400736 return -ENOMEM;
737
Steven Rostedtcafb1682009-03-24 20:50:39 -0400738 if (!ftrace_profile_bits) {
739 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400740
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741 for (; size; size >>= 1)
742 ftrace_profile_bits++;
743 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400744
Steven Rostedt318e0a72009-03-25 20:06:34 -0400745 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400746 if (ftrace_profile_pages_init(stat) < 0) {
747 kfree(stat->hash);
748 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 return -ENOMEM;
750 }
751
752 return 0;
753}
754
Steven Rostedtcafb1682009-03-24 20:50:39 -0400755static int ftrace_profile_init(void)
756{
757 int cpu;
758 int ret = 0;
759
760 for_each_online_cpu(cpu) {
761 ret = ftrace_profile_init_cpu(cpu);
762 if (ret)
763 break;
764 }
765
766 return ret;
767}
768
Steven Rostedt493762f2009-03-23 17:12:36 -0400769/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400770static struct ftrace_profile *
771ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400772{
773 struct ftrace_profile *rec;
774 struct hlist_head *hhd;
775 struct hlist_node *n;
776 unsigned long key;
777
778 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400779 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400780
781 if (hlist_empty(hhd))
782 return NULL;
783
784 hlist_for_each_entry_rcu(rec, n, hhd, node) {
785 if (rec->ip == ip)
786 return rec;
787 }
788
789 return NULL;
790}
791
Steven Rostedtcafb1682009-03-24 20:50:39 -0400792static void ftrace_add_profile(struct ftrace_profile_stat *stat,
793 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400794{
795 unsigned long key;
796
797 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400798 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400799}
800
Steven Rostedt318e0a72009-03-25 20:06:34 -0400801/*
802 * The memory is already allocated, this simply finds a new record to use.
803 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400804static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400805ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400806{
807 struct ftrace_profile *rec = NULL;
808
Steven Rostedt318e0a72009-03-25 20:06:34 -0400809 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400810 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400811 goto out;
812
Steven Rostedt493762f2009-03-23 17:12:36 -0400813 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400814 * Try to find the function again since an NMI
815 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400816 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400817 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400818 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400820
Steven Rostedtcafb1682009-03-24 20:50:39 -0400821 if (stat->pages->index == PROFILES_PER_PAGE) {
822 if (!stat->pages->next)
823 goto out;
824 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400825 }
826
Steven Rostedtcafb1682009-03-24 20:50:39 -0400827 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400828 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400829 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400830
Steven Rostedt493762f2009-03-23 17:12:36 -0400831 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400832 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400833
834 return rec;
835}
836
Steven Rostedt493762f2009-03-23 17:12:36 -0400837static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400838function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400839 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400840{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400841 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400842 struct ftrace_profile *rec;
843 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400844
845 if (!ftrace_profile_enabled)
846 return;
847
Steven Rostedt493762f2009-03-23 17:12:36 -0400848 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400849
850 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400851 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400852 goto out;
853
854 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400855 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400856 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400857 if (!rec)
858 goto out;
859 }
860
861 rec->counter++;
862 out:
863 local_irq_restore(flags);
864}
865
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400866#ifdef CONFIG_FUNCTION_GRAPH_TRACER
867static int profile_graph_entry(struct ftrace_graph_ent *trace)
868{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400869 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400870 return 1;
871}
872
873static void profile_graph_return(struct ftrace_graph_ret *trace)
874{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400875 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400876 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400877 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400878 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400879
880 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400881 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400882 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400883 goto out;
884
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400885 /* If the calltime was zero'd ignore it */
886 if (!trace->calltime)
887 goto out;
888
Steven Rostedta2a16d62009-03-24 23:17:58 -0400889 calltime = trace->rettime - trace->calltime;
890
891 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
892 int index;
893
894 index = trace->depth;
895
896 /* Append this call time to the parent time to subtract */
897 if (index)
898 current->ret_stack[index - 1].subtime += calltime;
899
900 if (current->ret_stack[index].subtime < calltime)
901 calltime -= current->ret_stack[index].subtime;
902 else
903 calltime = 0;
904 }
905
Steven Rostedtcafb1682009-03-24 20:50:39 -0400906 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400907 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400908 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400909 rec->time_squared += calltime * calltime;
910 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400911
Steven Rostedtcafb1682009-03-24 20:50:39 -0400912 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400913 local_irq_restore(flags);
914}
915
916static int register_ftrace_profiler(void)
917{
918 return register_ftrace_graph(&profile_graph_return,
919 &profile_graph_entry);
920}
921
922static void unregister_ftrace_profiler(void)
923{
924 unregister_ftrace_graph();
925}
926#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100927static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400928 .func = function_profile_call,
Steven Rostedt47409742012-07-20 11:04:44 -0400929 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt493762f2009-03-23 17:12:36 -0400930};
931
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400932static int register_ftrace_profiler(void)
933{
934 return register_ftrace_function(&ftrace_profile_ops);
935}
936
937static void unregister_ftrace_profiler(void)
938{
939 unregister_ftrace_function(&ftrace_profile_ops);
940}
941#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
942
Steven Rostedt493762f2009-03-23 17:12:36 -0400943static ssize_t
944ftrace_profile_write(struct file *filp, const char __user *ubuf,
945 size_t cnt, loff_t *ppos)
946{
947 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400948 int ret;
949
Peter Huewe22fe9b52011-06-07 21:58:27 +0200950 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
951 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400952 return ret;
953
954 val = !!val;
955
956 mutex_lock(&ftrace_profile_lock);
957 if (ftrace_profile_enabled ^ val) {
958 if (val) {
959 ret = ftrace_profile_init();
960 if (ret < 0) {
961 cnt = ret;
962 goto out;
963 }
964
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400965 ret = register_ftrace_profiler();
966 if (ret < 0) {
967 cnt = ret;
968 goto out;
969 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400970 ftrace_profile_enabled = 1;
971 } else {
972 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400973 /*
974 * unregister_ftrace_profiler calls stop_machine
975 * so this acts like an synchronize_sched.
976 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400977 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400978 }
979 }
980 out:
981 mutex_unlock(&ftrace_profile_lock);
982
Jiri Olsacf8517c2009-10-23 19:36:16 -0400983 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400984
985 return cnt;
986}
987
988static ssize_t
989ftrace_profile_read(struct file *filp, char __user *ubuf,
990 size_t cnt, loff_t *ppos)
991{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400992 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400993 int r;
994
995 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
996 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
997}
998
999static const struct file_operations ftrace_profile_fops = {
1000 .open = tracing_open_generic,
1001 .read = ftrace_profile_read,
1002 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001003 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001004};
1005
Steven Rostedtcafb1682009-03-24 20:50:39 -04001006/* used to initialize the real stat files */
1007static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001008 .name = "functions",
1009 .stat_start = function_stat_start,
1010 .stat_next = function_stat_next,
1011 .stat_cmp = function_stat_cmp,
1012 .stat_headers = function_stat_headers,
1013 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001014};
1015
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001016static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001017{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001018 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001019 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001020 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001021 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001022 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001023
Steven Rostedtcafb1682009-03-24 20:50:39 -04001024 for_each_possible_cpu(cpu) {
1025 stat = &per_cpu(ftrace_profile_stats, cpu);
1026
1027 /* allocate enough for function name + cpu number */
1028 name = kmalloc(32, GFP_KERNEL);
1029 if (!name) {
1030 /*
1031 * The files created are permanent, if something happens
1032 * we still do not free memory.
1033 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001034 WARN(1,
1035 "Could not allocate stat file for cpu %d\n",
1036 cpu);
1037 return;
1038 }
1039 stat->stat = function_stats;
1040 snprintf(name, 32, "function%d", cpu);
1041 stat->stat.name = name;
1042 ret = register_stat_tracer(&stat->stat);
1043 if (ret) {
1044 WARN(1,
1045 "Could not register function stat for cpu %d\n",
1046 cpu);
1047 kfree(name);
1048 return;
1049 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001050 }
1051
1052 entry = debugfs_create_file("function_profile_enabled", 0644,
1053 d_tracer, NULL, &ftrace_profile_fops);
1054 if (!entry)
1055 pr_warning("Could not create debugfs "
1056 "'function_profile_enabled' entry\n");
1057}
1058
1059#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001060static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001061{
1062}
1063#endif /* CONFIG_FUNCTION_PROFILER */
1064
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001065static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1066
Steven Rostedt3d083392008-05-12 21:20:42 +02001067#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001068
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001069#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001070# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001071#endif
1072
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001073static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1074
Steven Rostedtb6887d72009-02-17 12:32:04 -05001075struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001076 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001077 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001078 unsigned long flags;
1079 unsigned long ip;
1080 void *data;
1081 struct rcu_head rcu;
1082};
1083
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001084struct ftrace_func_entry {
1085 struct hlist_node hlist;
1086 unsigned long ip;
1087};
1088
1089struct ftrace_hash {
1090 unsigned long size_bits;
1091 struct hlist_head *buckets;
1092 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001093 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001094};
1095
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001096/*
1097 * We make these constant because no one should touch them,
1098 * but they are used as the default "empty hash", to avoid allocating
1099 * it all the time. These are in a read only section such that if
1100 * anyone does try to modify it, it will cause an exception.
1101 */
1102static const struct hlist_head empty_buckets[1];
1103static const struct ftrace_hash empty_hash = {
1104 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001105};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001106#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001107
Steven Rostedt2b499382011-05-03 22:49:52 -04001108static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001109 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001110 .notrace_hash = EMPTY_HASH,
1111 .filter_hash = EMPTY_HASH,
Steven Rostedt47409742012-07-20 11:04:44 -04001112 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001113};
1114
Steven Rostedt41c52c02008-05-22 11:46:33 -04001115static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001116
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001117struct ftrace_page {
1118 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001119 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001120 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001121 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001122};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001123
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001124static struct ftrace_page *ftrace_new_pgs;
1125
Steven Rostedta7900872011-12-16 16:23:44 -05001126#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1127#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001128
1129/* estimate from running different kernels */
1130#define NR_TO_INIT 10000
1131
1132static struct ftrace_page *ftrace_pages_start;
1133static struct ftrace_page *ftrace_pages;
1134
Steven Rostedt06a51d92011-12-19 19:07:36 -05001135static bool ftrace_hash_empty(struct ftrace_hash *hash)
1136{
1137 return !hash || !hash->count;
1138}
1139
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001140static struct ftrace_func_entry *
1141ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1142{
1143 unsigned long key;
1144 struct ftrace_func_entry *entry;
1145 struct hlist_head *hhd;
1146 struct hlist_node *n;
1147
Steven Rostedt06a51d92011-12-19 19:07:36 -05001148 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001149 return NULL;
1150
1151 if (hash->size_bits > 0)
1152 key = hash_long(ip, hash->size_bits);
1153 else
1154 key = 0;
1155
1156 hhd = &hash->buckets[key];
1157
1158 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1159 if (entry->ip == ip)
1160 return entry;
1161 }
1162 return NULL;
1163}
1164
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001165static void __add_hash_entry(struct ftrace_hash *hash,
1166 struct ftrace_func_entry *entry)
1167{
1168 struct hlist_head *hhd;
1169 unsigned long key;
1170
1171 if (hash->size_bits)
1172 key = hash_long(entry->ip, hash->size_bits);
1173 else
1174 key = 0;
1175
1176 hhd = &hash->buckets[key];
1177 hlist_add_head(&entry->hlist, hhd);
1178 hash->count++;
1179}
1180
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001181static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1182{
1183 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001184
1185 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1186 if (!entry)
1187 return -ENOMEM;
1188
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001189 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001190 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001191
1192 return 0;
1193}
1194
1195static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001196free_hash_entry(struct ftrace_hash *hash,
1197 struct ftrace_func_entry *entry)
1198{
1199 hlist_del(&entry->hlist);
1200 kfree(entry);
1201 hash->count--;
1202}
1203
1204static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001205remove_hash_entry(struct ftrace_hash *hash,
1206 struct ftrace_func_entry *entry)
1207{
1208 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001209 hash->count--;
1210}
1211
1212static void ftrace_hash_clear(struct ftrace_hash *hash)
1213{
1214 struct hlist_head *hhd;
1215 struct hlist_node *tp, *tn;
1216 struct ftrace_func_entry *entry;
1217 int size = 1 << hash->size_bits;
1218 int i;
1219
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001220 if (!hash->count)
1221 return;
1222
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001223 for (i = 0; i < size; i++) {
1224 hhd = &hash->buckets[i];
1225 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001226 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001227 }
1228 FTRACE_WARN_ON(hash->count);
1229}
1230
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001231static void free_ftrace_hash(struct ftrace_hash *hash)
1232{
1233 if (!hash || hash == EMPTY_HASH)
1234 return;
1235 ftrace_hash_clear(hash);
1236 kfree(hash->buckets);
1237 kfree(hash);
1238}
1239
Steven Rostedt07fd5512011-05-05 18:03:47 -04001240static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1241{
1242 struct ftrace_hash *hash;
1243
1244 hash = container_of(rcu, struct ftrace_hash, rcu);
1245 free_ftrace_hash(hash);
1246}
1247
1248static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1249{
1250 if (!hash || hash == EMPTY_HASH)
1251 return;
1252 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1253}
1254
Jiri Olsa5500fa52012-02-15 15:51:54 +01001255void ftrace_free_filter(struct ftrace_ops *ops)
1256{
1257 free_ftrace_hash(ops->filter_hash);
1258 free_ftrace_hash(ops->notrace_hash);
1259}
1260
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001261static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1262{
1263 struct ftrace_hash *hash;
1264 int size;
1265
1266 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1267 if (!hash)
1268 return NULL;
1269
1270 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001271 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001272
1273 if (!hash->buckets) {
1274 kfree(hash);
1275 return NULL;
1276 }
1277
1278 hash->size_bits = size_bits;
1279
1280 return hash;
1281}
1282
1283static struct ftrace_hash *
1284alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1285{
1286 struct ftrace_func_entry *entry;
1287 struct ftrace_hash *new_hash;
1288 struct hlist_node *tp;
1289 int size;
1290 int ret;
1291 int i;
1292
1293 new_hash = alloc_ftrace_hash(size_bits);
1294 if (!new_hash)
1295 return NULL;
1296
1297 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001298 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001299 return new_hash;
1300
1301 size = 1 << hash->size_bits;
1302 for (i = 0; i < size; i++) {
1303 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1304 ret = add_hash_entry(new_hash, entry->ip);
1305 if (ret < 0)
1306 goto free_hash;
1307 }
1308 }
1309
1310 FTRACE_WARN_ON(new_hash->count != hash->count);
1311
1312 return new_hash;
1313
1314 free_hash:
1315 free_ftrace_hash(new_hash);
1316 return NULL;
1317}
1318
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001319static void
1320ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1321static void
1322ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1323
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001324static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001325ftrace_hash_move(struct ftrace_ops *ops, int enable,
1326 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001327{
1328 struct ftrace_func_entry *entry;
1329 struct hlist_node *tp, *tn;
1330 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001331 struct ftrace_hash *old_hash;
1332 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001333 unsigned long key;
1334 int size = src->count;
1335 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001336 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001337 int i;
1338
1339 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001340 * Remove the current set, update the hash and add
1341 * them back.
1342 */
1343 ftrace_hash_rec_disable(ops, enable);
1344
1345 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001346 * If the new source is empty, just free dst and assign it
1347 * the empty_hash.
1348 */
1349 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001350 free_ftrace_hash_rcu(*dst);
1351 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b982011-11-04 20:32:39 -04001352 /* still need to update the function records */
1353 ret = 0;
1354 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001355 }
1356
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001357 /*
1358 * Make the hash size about 1/2 the # found
1359 */
1360 for (size /= 2; size; size >>= 1)
1361 bits++;
1362
1363 /* Don't allocate too much */
1364 if (bits > FTRACE_HASH_MAX_BITS)
1365 bits = FTRACE_HASH_MAX_BITS;
1366
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001367 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001368 new_hash = alloc_ftrace_hash(bits);
1369 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001370 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001371
1372 size = 1 << src->size_bits;
1373 for (i = 0; i < size; i++) {
1374 hhd = &src->buckets[i];
1375 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1376 if (bits > 0)
1377 key = hash_long(entry->ip, bits);
1378 else
1379 key = 0;
1380 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001381 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001382 }
1383 }
1384
Steven Rostedt07fd5512011-05-05 18:03:47 -04001385 old_hash = *dst;
1386 rcu_assign_pointer(*dst, new_hash);
1387 free_ftrace_hash_rcu(old_hash);
1388
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001389 ret = 0;
1390 out:
1391 /*
1392 * Enable regardless of ret:
1393 * On success, we enable the new hash.
1394 * On failure, we re-enable the original hash.
1395 */
1396 ftrace_hash_rec_enable(ops, enable);
1397
1398 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001399}
1400
Steven Rostedt265c8312009-02-13 12:43:56 -05001401/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001402 * Test the hashes for this ops to see if we want to call
1403 * the ops->func or not.
1404 *
1405 * It's a match if the ip is in the ops->filter_hash or
1406 * the filter_hash does not exist or is empty,
1407 * AND
1408 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001409 *
1410 * This needs to be called with preemption disabled as
1411 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001412 */
1413static int
1414ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1415{
1416 struct ftrace_hash *filter_hash;
1417 struct ftrace_hash *notrace_hash;
1418 int ret;
1419
Steven Rostedtb8489142011-05-04 09:27:52 -04001420 filter_hash = rcu_dereference_raw(ops->filter_hash);
1421 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1422
Steven Rostedt06a51d92011-12-19 19:07:36 -05001423 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001424 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001425 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001426 !ftrace_lookup_ip(notrace_hash, ip)))
1427 ret = 1;
1428 else
1429 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001430
1431 return ret;
1432}
1433
1434/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001435 * This is a double for. Do not use 'break' to break out of the loop,
1436 * you must use a goto.
1437 */
1438#define do_for_each_ftrace_rec(pg, rec) \
1439 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1440 int _____i; \
1441 for (_____i = 0; _____i < pg->index; _____i++) { \
1442 rec = &pg->records[_____i];
1443
1444#define while_for_each_ftrace_rec() \
1445 } \
1446 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301447
Steven Rostedt5855fea2011-12-16 19:27:42 -05001448
1449static int ftrace_cmp_recs(const void *a, const void *b)
1450{
Steven Rostedta650e022012-04-25 13:48:13 -04001451 const struct dyn_ftrace *key = a;
1452 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001453
Steven Rostedta650e022012-04-25 13:48:13 -04001454 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001455 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001456 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1457 return 1;
1458 return 0;
1459}
1460
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001461static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001462{
1463 struct ftrace_page *pg;
1464 struct dyn_ftrace *rec;
1465 struct dyn_ftrace key;
1466
1467 key.ip = start;
1468 key.flags = end; /* overload flags, as it is unsigned long */
1469
1470 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1471 if (end < pg->records[0].ip ||
1472 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1473 continue;
1474 rec = bsearch(&key, pg->records, pg->index,
1475 sizeof(struct dyn_ftrace),
1476 ftrace_cmp_recs);
1477 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001478 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001479 }
1480
Steven Rostedt5855fea2011-12-16 19:27:42 -05001481 return 0;
1482}
1483
Steven Rostedtc88fd862011-08-16 09:53:39 -04001484/**
1485 * ftrace_location - return true if the ip giving is a traced location
1486 * @ip: the instruction pointer to check
1487 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001488 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001489 * That is, the instruction that is either a NOP or call to
1490 * the function tracer. It checks the ftrace internal tables to
1491 * determine if the address belongs or not.
1492 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001493unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001494{
Steven Rostedta650e022012-04-25 13:48:13 -04001495 return ftrace_location_range(ip, ip);
1496}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001497
Steven Rostedta650e022012-04-25 13:48:13 -04001498/**
1499 * ftrace_text_reserved - return true if range contains an ftrace location
1500 * @start: start of range to search
1501 * @end: end of range to search (inclusive). @end points to the last byte to check.
1502 *
1503 * Returns 1 if @start and @end contains a ftrace location.
1504 * That is, the instruction that is either a NOP or call to
1505 * the function tracer. It checks the ftrace internal tables to
1506 * determine if the address belongs or not.
1507 */
1508int ftrace_text_reserved(void *start, void *end)
1509{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001510 unsigned long ret;
1511
1512 ret = ftrace_location_range((unsigned long)start,
1513 (unsigned long)end);
1514
1515 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001516}
1517
Steven Rostedted926f92011-05-03 13:25:24 -04001518static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1519 int filter_hash,
1520 bool inc)
1521{
1522 struct ftrace_hash *hash;
1523 struct ftrace_hash *other_hash;
1524 struct ftrace_page *pg;
1525 struct dyn_ftrace *rec;
1526 int count = 0;
1527 int all = 0;
1528
1529 /* Only update if the ops has been registered */
1530 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1531 return;
1532
1533 /*
1534 * In the filter_hash case:
1535 * If the count is zero, we update all records.
1536 * Otherwise we just update the items in the hash.
1537 *
1538 * In the notrace_hash case:
1539 * We enable the update in the hash.
1540 * As disabling notrace means enabling the tracing,
1541 * and enabling notrace means disabling, the inc variable
1542 * gets inversed.
1543 */
1544 if (filter_hash) {
1545 hash = ops->filter_hash;
1546 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001547 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001548 all = 1;
1549 } else {
1550 inc = !inc;
1551 hash = ops->notrace_hash;
1552 other_hash = ops->filter_hash;
1553 /*
1554 * If the notrace hash has no items,
1555 * then there's nothing to do.
1556 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001557 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001558 return;
1559 }
1560
1561 do_for_each_ftrace_rec(pg, rec) {
1562 int in_other_hash = 0;
1563 int in_hash = 0;
1564 int match = 0;
1565
1566 if (all) {
1567 /*
1568 * Only the filter_hash affects all records.
1569 * Update if the record is not in the notrace hash.
1570 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001571 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001572 match = 1;
1573 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001574 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1575 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001576
1577 /*
1578 *
1579 */
1580 if (filter_hash && in_hash && !in_other_hash)
1581 match = 1;
1582 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001583 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001584 match = 1;
1585 }
1586 if (!match)
1587 continue;
1588
1589 if (inc) {
1590 rec->flags++;
1591 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1592 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001593 /*
1594 * If any ops wants regs saved for this function
1595 * then all ops will get saved regs.
1596 */
1597 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1598 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001599 } else {
1600 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1601 return;
1602 rec->flags--;
1603 }
1604 count++;
1605 /* Shortcut, if we handled all records, we are done. */
1606 if (!all && count == hash->count)
1607 return;
1608 } while_for_each_ftrace_rec();
1609}
1610
1611static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1612 int filter_hash)
1613{
1614 __ftrace_hash_rec_update(ops, filter_hash, 0);
1615}
1616
1617static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1618 int filter_hash)
1619{
1620 __ftrace_hash_rec_update(ops, filter_hash, 1);
1621}
1622
Steven Rostedt05736a42008-09-22 14:55:47 -07001623static void print_ip_ins(const char *fmt, unsigned char *p)
1624{
1625 int i;
1626
1627 printk(KERN_CONT "%s", fmt);
1628
1629 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1630 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1631}
1632
Steven Rostedtc88fd862011-08-16 09:53:39 -04001633/**
1634 * ftrace_bug - report and shutdown function tracer
1635 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1636 * @ip: The address that failed
1637 *
1638 * The arch code that enables or disables the function tracing
1639 * can call ftrace_bug() when it has detected a problem in
1640 * modifying the code. @failed should be one of either:
1641 * EFAULT - if the problem happens on reading the @ip address
1642 * EINVAL - if what is read at @ip is not what was expected
1643 * EPERM - if the problem happens on writting to the @ip address
1644 */
1645void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001646{
1647 switch (failed) {
1648 case -EFAULT:
1649 FTRACE_WARN_ON_ONCE(1);
1650 pr_info("ftrace faulted on modifying ");
1651 print_ip_sym(ip);
1652 break;
1653 case -EINVAL:
1654 FTRACE_WARN_ON_ONCE(1);
1655 pr_info("ftrace failed to modify ");
1656 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001657 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001658 printk(KERN_CONT "\n");
1659 break;
1660 case -EPERM:
1661 FTRACE_WARN_ON_ONCE(1);
1662 pr_info("ftrace faulted on writing ");
1663 print_ip_sym(ip);
1664 break;
1665 default:
1666 FTRACE_WARN_ON_ONCE(1);
1667 pr_info("ftrace faulted on unknown error ");
1668 print_ip_sym(ip);
1669 }
1670}
1671
Steven Rostedtc88fd862011-08-16 09:53:39 -04001672static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001673{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001674 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001675
Steven Rostedt982c3502008-11-15 16:31:41 -05001676 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001677 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001678 *
Steven Rostedted926f92011-05-03 13:25:24 -04001679 * If the record has a ref count, then we need to enable it
1680 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001681 *
Steven Rostedted926f92011-05-03 13:25:24 -04001682 * Otherwise we make sure its disabled.
1683 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001684 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001685 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001686 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001687 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001688 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001689
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001690 /*
1691 * If enabling and the REGS flag does not match the REGS_EN, then
1692 * do not ignore this record. Set flags to fail the compare against
1693 * ENABLED.
1694 */
1695 if (flag &&
1696 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1697 flag |= FTRACE_FL_REGS;
1698
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001699 /* If the state of this record hasn't changed, then do nothing */
1700 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001701 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001702
1703 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001704 /* Save off if rec is being enabled (for return value) */
1705 flag ^= rec->flags & FTRACE_FL_ENABLED;
1706
1707 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001708 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001709 if (flag & FTRACE_FL_REGS) {
1710 if (rec->flags & FTRACE_FL_REGS)
1711 rec->flags |= FTRACE_FL_REGS_EN;
1712 else
1713 rec->flags &= ~FTRACE_FL_REGS_EN;
1714 }
1715 }
1716
1717 /*
1718 * If this record is being updated from a nop, then
1719 * return UPDATE_MAKE_CALL.
1720 * Otherwise, if the EN flag is set, then return
1721 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1722 * from the non-save regs, to a save regs function.
1723 * Otherwise,
1724 * return UPDATE_MODIFY_CALL to tell the caller to convert
1725 * from the save regs, to a non-save regs function.
1726 */
1727 if (flag & FTRACE_FL_ENABLED)
1728 return FTRACE_UPDATE_MAKE_CALL;
1729 else if (rec->flags & FTRACE_FL_REGS_EN)
1730 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1731 else
1732 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001733 }
1734
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001735 if (update) {
1736 /* If there's no more users, clear all flags */
1737 if (!(rec->flags & ~FTRACE_FL_MASK))
1738 rec->flags = 0;
1739 else
1740 /* Just disable the record (keep REGS state) */
1741 rec->flags &= ~FTRACE_FL_ENABLED;
1742 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001743
1744 return FTRACE_UPDATE_MAKE_NOP;
1745}
1746
1747/**
1748 * ftrace_update_record, set a record that now is tracing or not
1749 * @rec: the record to update
1750 * @enable: set to 1 if the record is tracing, zero to force disable
1751 *
1752 * The records that represent all functions that can be traced need
1753 * to be updated when tracing has been enabled.
1754 */
1755int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1756{
1757 return ftrace_check_record(rec, enable, 1);
1758}
1759
1760/**
1761 * ftrace_test_record, check if the record has been enabled or not
1762 * @rec: the record to test
1763 * @enable: set to 1 to check if enabled, 0 if it is disabled
1764 *
1765 * The arch code may need to test if a record is already set to
1766 * tracing to determine how to modify the function code that it
1767 * represents.
1768 */
1769int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1770{
1771 return ftrace_check_record(rec, enable, 0);
1772}
1773
1774static int
1775__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1776{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001777 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001778 unsigned long ftrace_addr;
1779 int ret;
1780
Steven Rostedtc88fd862011-08-16 09:53:39 -04001781 ret = ftrace_update_record(rec, enable);
1782
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001783 if (rec->flags & FTRACE_FL_REGS)
1784 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1785 else
1786 ftrace_addr = (unsigned long)FTRACE_ADDR;
1787
Steven Rostedtc88fd862011-08-16 09:53:39 -04001788 switch (ret) {
1789 case FTRACE_UPDATE_IGNORE:
1790 return 0;
1791
1792 case FTRACE_UPDATE_MAKE_CALL:
1793 return ftrace_make_call(rec, ftrace_addr);
1794
1795 case FTRACE_UPDATE_MAKE_NOP:
1796 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001797
1798 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1799 case FTRACE_UPDATE_MODIFY_CALL:
1800 if (rec->flags & FTRACE_FL_REGS)
1801 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1802 else
1803 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1804
1805 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001806 }
1807
1808 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001809}
1810
Steven Rostedte4f5d542012-04-27 09:13:18 -04001811void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001812{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001813 struct dyn_ftrace *rec;
1814 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001815 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001816
Steven Rostedt45a4a232011-04-21 23:16:46 -04001817 if (unlikely(ftrace_disabled))
1818 return;
1819
Steven Rostedt265c8312009-02-13 12:43:56 -05001820 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001821 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001822 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001823 ftrace_bug(failed, rec->ip);
1824 /* Stop processing */
1825 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001826 }
1827 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001828}
1829
Steven Rostedtc88fd862011-08-16 09:53:39 -04001830struct ftrace_rec_iter {
1831 struct ftrace_page *pg;
1832 int index;
1833};
1834
1835/**
1836 * ftrace_rec_iter_start, start up iterating over traced functions
1837 *
1838 * Returns an iterator handle that is used to iterate over all
1839 * the records that represent address locations where functions
1840 * are traced.
1841 *
1842 * May return NULL if no records are available.
1843 */
1844struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1845{
1846 /*
1847 * We only use a single iterator.
1848 * Protected by the ftrace_lock mutex.
1849 */
1850 static struct ftrace_rec_iter ftrace_rec_iter;
1851 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1852
1853 iter->pg = ftrace_pages_start;
1854 iter->index = 0;
1855
1856 /* Could have empty pages */
1857 while (iter->pg && !iter->pg->index)
1858 iter->pg = iter->pg->next;
1859
1860 if (!iter->pg)
1861 return NULL;
1862
1863 return iter;
1864}
1865
1866/**
1867 * ftrace_rec_iter_next, get the next record to process.
1868 * @iter: The handle to the iterator.
1869 *
1870 * Returns the next iterator after the given iterator @iter.
1871 */
1872struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1873{
1874 iter->index++;
1875
1876 if (iter->index >= iter->pg->index) {
1877 iter->pg = iter->pg->next;
1878 iter->index = 0;
1879
1880 /* Could have empty pages */
1881 while (iter->pg && !iter->pg->index)
1882 iter->pg = iter->pg->next;
1883 }
1884
1885 if (!iter->pg)
1886 return NULL;
1887
1888 return iter;
1889}
1890
1891/**
1892 * ftrace_rec_iter_record, get the record at the iterator location
1893 * @iter: The current iterator location
1894 *
1895 * Returns the record that the current @iter is at.
1896 */
1897struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1898{
1899 return &iter->pg->records[iter->index];
1900}
1901
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301902static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001903ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001904{
1905 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001906 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001907
1908 ip = rec->ip;
1909
Steven Rostedt45a4a232011-04-21 23:16:46 -04001910 if (unlikely(ftrace_disabled))
1911 return 0;
1912
Shaohua Li25aac9d2009-01-09 11:29:40 +08001913 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001914 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001915 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301916 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001917 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301918 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001919}
1920
Steven Rostedt000ab692009-02-17 13:35:06 -05001921/*
1922 * archs can override this function if they must do something
1923 * before the modifying code is performed.
1924 */
1925int __weak ftrace_arch_code_modify_prepare(void)
1926{
1927 return 0;
1928}
1929
1930/*
1931 * archs can override this function if they must do something
1932 * after the modifying code is performed.
1933 */
1934int __weak ftrace_arch_code_modify_post_process(void)
1935{
1936 return 0;
1937}
1938
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001939void ftrace_modify_all_code(int command)
1940{
1941 if (command & FTRACE_UPDATE_CALLS)
1942 ftrace_replace_code(1);
1943 else if (command & FTRACE_DISABLE_CALLS)
1944 ftrace_replace_code(0);
1945
1946 if (command & FTRACE_UPDATE_TRACE_FUNC)
1947 ftrace_update_ftrace_func(ftrace_trace_function);
1948
1949 if (command & FTRACE_START_FUNC_RET)
1950 ftrace_enable_ftrace_graph_caller();
1951 else if (command & FTRACE_STOP_FUNC_RET)
1952 ftrace_disable_ftrace_graph_caller();
1953}
1954
Ingo Molnare309b412008-05-12 21:20:51 +02001955static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001956{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001957 int *command = data;
1958
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001959 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001960
Steven Rostedtc88fd862011-08-16 09:53:39 -04001961 return 0;
1962}
1963
1964/**
1965 * ftrace_run_stop_machine, go back to the stop machine method
1966 * @command: The command to tell ftrace what to do
1967 *
1968 * If an arch needs to fall back to the stop machine method, the
1969 * it can call this function.
1970 */
1971void ftrace_run_stop_machine(int command)
1972{
1973 stop_machine(__ftrace_modify_code, &command, NULL);
1974}
1975
1976/**
1977 * arch_ftrace_update_code, modify the code to trace or not trace
1978 * @command: The command that needs to be done
1979 *
1980 * Archs can override this function if it does not need to
1981 * run stop_machine() to modify code.
1982 */
1983void __weak arch_ftrace_update_code(int command)
1984{
1985 ftrace_run_stop_machine(command);
1986}
1987
1988static void ftrace_run_update_code(int command)
1989{
1990 int ret;
1991
1992 ret = ftrace_arch_code_modify_prepare();
1993 FTRACE_WARN_ON(ret);
1994 if (ret)
1995 return;
1996 /*
1997 * Do not call function tracer while we update the code.
1998 * We are in stop machine.
1999 */
2000 function_trace_stop++;
2001
2002 /*
2003 * By default we use stop_machine() to modify the code.
2004 * But archs can do what ever they want as long as it
2005 * is safe. The stop_machine() is the safest, but also
2006 * produces the most overhead.
2007 */
2008 arch_ftrace_update_code(command);
2009
Steven Rostedt6331c282011-07-13 15:11:02 -04002010 function_trace_stop--;
2011
Steven Rostedt000ab692009-02-17 13:35:06 -05002012 ret = ftrace_arch_code_modify_post_process();
2013 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002014}
2015
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002016static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002017static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002018static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002019
2020static void ftrace_startup_enable(int command)
2021{
2022 if (saved_ftrace_func != ftrace_trace_function) {
2023 saved_ftrace_func = ftrace_trace_function;
2024 command |= FTRACE_UPDATE_TRACE_FUNC;
2025 }
2026
2027 if (!command || !ftrace_enabled)
2028 return;
2029
2030 ftrace_run_update_code(command);
2031}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002032
Steven Rostedta1cd6172011-05-23 15:24:25 -04002033static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002034{
Steven Rostedtb8489142011-05-04 09:27:52 -04002035 bool hash_enable = true;
2036
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002037 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002038 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002039
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002040 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002041 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002042
Steven Rostedtb8489142011-05-04 09:27:52 -04002043 /* ops marked global share the filter hashes */
2044 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2045 ops = &global_ops;
2046 /* Don't update hash if global is already set */
2047 if (global_start_up)
2048 hash_enable = false;
2049 global_start_up++;
2050 }
2051
Steven Rostedted926f92011-05-03 13:25:24 -04002052 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002053 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002054 ftrace_hash_rec_enable(ops, 1);
2055
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002056 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002057
2058 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002059}
2060
Steven Rostedtbd69c302011-05-03 21:55:54 -04002061static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002062{
Steven Rostedtb8489142011-05-04 09:27:52 -04002063 bool hash_disable = true;
2064
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002065 if (unlikely(ftrace_disabled))
2066 return;
2067
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002068 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002069 /*
2070 * Just warn in case of unbalance, no need to kill ftrace, it's not
2071 * critical but the ftrace_call callers may be never nopped again after
2072 * further ftrace uses.
2073 */
2074 WARN_ON_ONCE(ftrace_start_up < 0);
2075
Steven Rostedtb8489142011-05-04 09:27:52 -04002076 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2077 ops = &global_ops;
2078 global_start_up--;
2079 WARN_ON_ONCE(global_start_up < 0);
2080 /* Don't update hash if global still has users */
2081 if (global_start_up) {
2082 WARN_ON_ONCE(!ftrace_start_up);
2083 hash_disable = false;
2084 }
2085 }
2086
2087 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002088 ftrace_hash_rec_disable(ops, 1);
2089
Steven Rostedtb8489142011-05-04 09:27:52 -04002090 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002091 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002092
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002093 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002094
2095 if (saved_ftrace_func != ftrace_trace_function) {
2096 saved_ftrace_func = ftrace_trace_function;
2097 command |= FTRACE_UPDATE_TRACE_FUNC;
2098 }
2099
2100 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002101 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002102
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002103 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002104}
2105
Ingo Molnare309b412008-05-12 21:20:51 +02002106static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002107{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002108 if (unlikely(ftrace_disabled))
2109 return;
2110
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002111 /* Force update next time */
2112 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002113 /* ftrace_start_up is true if we want ftrace running */
2114 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002115 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002116}
2117
Ingo Molnare309b412008-05-12 21:20:51 +02002118static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002119{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002120 if (unlikely(ftrace_disabled))
2121 return;
2122
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002123 /* ftrace_start_up is true if ftrace is running */
2124 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002125 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002126}
2127
Steven Rostedt3d083392008-05-12 21:20:42 +02002128static cycle_t ftrace_update_time;
2129static unsigned long ftrace_update_cnt;
2130unsigned long ftrace_update_tot_cnt;
2131
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002132static int ops_traces_mod(struct ftrace_ops *ops)
2133{
2134 struct ftrace_hash *hash;
2135
2136 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002137 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002138}
2139
Steven Rostedt31e88902008-11-14 16:21:19 -08002140static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002141{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002142 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002143 struct dyn_ftrace *p;
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302144 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002145 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002146 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002147
2148 /*
2149 * When adding a module, we need to check if tracers are
2150 * currently enabled and if they are set to trace all functions.
2151 * If they are, we need to enable the module functions as well
2152 * as update the reference counts for those function records.
2153 */
2154 if (mod) {
2155 struct ftrace_ops *ops;
2156
2157 for (ops = ftrace_ops_list;
2158 ops != &ftrace_list_end; ops = ops->next) {
2159 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2160 ops_traces_mod(ops))
2161 ref++;
2162 }
2163 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002164
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002165 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002166 ftrace_update_cnt = 0;
2167
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002168 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302169
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002170 for (i = 0; i < pg->index; i++) {
2171 /* If something went wrong, bail without enabling anything */
2172 if (unlikely(ftrace_disabled))
2173 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002174
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002175 p = &pg->records[i];
2176 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302177
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002178 /*
2179 * Do the initial record conversion from mcount jump
2180 * to the NOP instructions.
2181 */
2182 if (!ftrace_code_disable(mod, p))
2183 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002184
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002185 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002186
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002187 /*
2188 * If the tracing is enabled, go ahead and enable the record.
2189 *
2190 * The reason not to enable the record immediatelly is the
2191 * inherent check of ftrace_make_nop/ftrace_make_call for
2192 * correct previous instructions. Making first the NOP
2193 * conversion puts the module to the correct state, thus
2194 * passing the ftrace_make_call check.
2195 */
2196 if (ftrace_start_up && ref) {
2197 int failed = __ftrace_replace_code(p, 1);
2198 if (failed)
2199 ftrace_bug(failed, p->ip);
2200 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002201 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002202 }
2203
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002204 ftrace_new_pgs = NULL;
2205
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002206 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002207 ftrace_update_time = stop - start;
2208 ftrace_update_tot_cnt += ftrace_update_cnt;
2209
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002210 return 0;
2211}
2212
Steven Rostedta7900872011-12-16 16:23:44 -05002213static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002214{
Steven Rostedta7900872011-12-16 16:23:44 -05002215 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002216 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002217
Steven Rostedta7900872011-12-16 16:23:44 -05002218 if (WARN_ON(!count))
2219 return -EINVAL;
2220
2221 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002222
2223 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002224 * We want to fill as much as possible. No more than a page
2225 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002226 */
Steven Rostedta7900872011-12-16 16:23:44 -05002227 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2228 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002229
Steven Rostedta7900872011-12-16 16:23:44 -05002230 again:
2231 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2232
2233 if (!pg->records) {
2234 /* if we can't allocate this size, try something smaller */
2235 if (!order)
2236 return -ENOMEM;
2237 order >>= 1;
2238 goto again;
2239 }
2240
2241 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2242 pg->size = cnt;
2243
2244 if (cnt > count)
2245 cnt = count;
2246
2247 return cnt;
2248}
2249
2250static struct ftrace_page *
2251ftrace_allocate_pages(unsigned long num_to_init)
2252{
2253 struct ftrace_page *start_pg;
2254 struct ftrace_page *pg;
2255 int order;
2256 int cnt;
2257
2258 if (!num_to_init)
2259 return 0;
2260
2261 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2262 if (!pg)
2263 return NULL;
2264
2265 /*
2266 * Try to allocate as much as possible in one continues
2267 * location that fills in all of the space. We want to
2268 * waste as little space as possible.
2269 */
2270 for (;;) {
2271 cnt = ftrace_allocate_records(pg, num_to_init);
2272 if (cnt < 0)
2273 goto free_pages;
2274
2275 num_to_init -= cnt;
2276 if (!num_to_init)
2277 break;
2278
2279 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2280 if (!pg->next)
2281 goto free_pages;
2282
2283 pg = pg->next;
2284 }
2285
2286 return start_pg;
2287
2288 free_pages:
2289 while (start_pg) {
2290 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2291 free_pages((unsigned long)pg->records, order);
2292 start_pg = pg->next;
2293 kfree(pg);
2294 pg = start_pg;
2295 }
2296 pr_info("ftrace: FAILED to allocate memory for functions\n");
2297 return NULL;
2298}
2299
2300static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2301{
2302 int cnt;
2303
2304 if (!num_to_init) {
2305 pr_info("ftrace: No functions to be traced?\n");
2306 return -1;
2307 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002308
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002309 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002310 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002311 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002312
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002313 return 0;
2314}
2315
Steven Rostedt5072c592008-05-12 21:20:43 +02002316#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2317
2318struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002319 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002320 loff_t func_pos;
2321 struct ftrace_page *pg;
2322 struct dyn_ftrace *func;
2323 struct ftrace_func_probe *probe;
2324 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002325 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002326 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002327 int hidx;
2328 int idx;
2329 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002330};
2331
Ingo Molnare309b412008-05-12 21:20:51 +02002332static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002333t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002334{
2335 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002336 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002337 struct hlist_head *hhd;
2338
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002339 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002340 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002341
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002342 if (iter->probe)
2343 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002344 retry:
2345 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2346 return NULL;
2347
2348 hhd = &ftrace_func_hash[iter->hidx];
2349
2350 if (hlist_empty(hhd)) {
2351 iter->hidx++;
2352 hnd = NULL;
2353 goto retry;
2354 }
2355
2356 if (!hnd)
2357 hnd = hhd->first;
2358 else {
2359 hnd = hnd->next;
2360 if (!hnd) {
2361 iter->hidx++;
2362 goto retry;
2363 }
2364 }
2365
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002366 if (WARN_ON_ONCE(!hnd))
2367 return NULL;
2368
2369 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2370
2371 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002372}
2373
2374static void *t_hash_start(struct seq_file *m, loff_t *pos)
2375{
2376 struct ftrace_iterator *iter = m->private;
2377 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002378 loff_t l;
2379
Steven Rostedt69a30832011-12-19 15:21:16 -05002380 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2381 return NULL;
2382
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002383 if (iter->func_pos > *pos)
2384 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002385
Li Zefand82d6242009-06-24 09:54:54 +08002386 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002387 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002388 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002389 if (!p)
2390 break;
2391 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002392 if (!p)
2393 return NULL;
2394
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002395 /* Only set this if we have an item */
2396 iter->flags |= FTRACE_ITER_HASH;
2397
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002398 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002399}
2400
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002401static int
2402t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002403{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002404 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002405
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002406 rec = iter->probe;
2407 if (WARN_ON_ONCE(!rec))
2408 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002409
Steven Rostedt809dcf22009-02-16 23:06:01 -05002410 if (rec->ops->print)
2411 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2412
Steven Rostedtb375a112009-09-17 00:05:58 -04002413 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002414
2415 if (rec->data)
2416 seq_printf(m, ":%p", rec->data);
2417 seq_putc(m, '\n');
2418
2419 return 0;
2420}
2421
2422static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002423t_next(struct seq_file *m, void *v, loff_t *pos)
2424{
2425 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002426 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002427 struct dyn_ftrace *rec = NULL;
2428
Steven Rostedt45a4a232011-04-21 23:16:46 -04002429 if (unlikely(ftrace_disabled))
2430 return NULL;
2431
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002432 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002433 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002434
Steven Rostedt5072c592008-05-12 21:20:43 +02002435 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002436 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002437
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002438 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002439 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002440
Steven Rostedt5072c592008-05-12 21:20:43 +02002441 retry:
2442 if (iter->idx >= iter->pg->index) {
2443 if (iter->pg->next) {
2444 iter->pg = iter->pg->next;
2445 iter->idx = 0;
2446 goto retry;
2447 }
2448 } else {
2449 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05002450 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002451 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05002452
Steven Rostedt41c52c02008-05-22 11:46:33 -04002453 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002454 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2455
2456 ((iter->flags & FTRACE_ITER_ENABLED) &&
2457 !(rec->flags & ~FTRACE_FL_MASK))) {
2458
Steven Rostedt5072c592008-05-12 21:20:43 +02002459 rec = NULL;
2460 goto retry;
2461 }
2462 }
2463
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002464 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002465 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002466
2467 iter->func = rec;
2468
2469 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002470}
2471
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002472static void reset_iter_read(struct ftrace_iterator *iter)
2473{
2474 iter->pos = 0;
2475 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03002476 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002477}
2478
2479static void *t_start(struct seq_file *m, loff_t *pos)
2480{
2481 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002482 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002483 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002484 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002485
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002486 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002487
2488 if (unlikely(ftrace_disabled))
2489 return NULL;
2490
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002491 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002492 * If an lseek was done, then reset and start from beginning.
2493 */
2494 if (*pos < iter->pos)
2495 reset_iter_read(iter);
2496
2497 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002498 * For set_ftrace_filter reading, if we have the filter
2499 * off, we can short cut and just print out that all
2500 * functions are enabled.
2501 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002502 if (iter->flags & FTRACE_ITER_FILTER &&
2503 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002504 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002505 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002506 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002507 /* reset in case of seek/pread */
2508 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002509 return iter;
2510 }
2511
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002512 if (iter->flags & FTRACE_ITER_HASH)
2513 return t_hash_start(m, pos);
2514
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002515 /*
2516 * Unfortunately, we need to restart at ftrace_pages_start
2517 * every time we let go of the ftrace_mutex. This is because
2518 * those pointers can change without the lock.
2519 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002520 iter->pg = ftrace_pages_start;
2521 iter->idx = 0;
2522 for (l = 0; l <= *pos; ) {
2523 p = t_next(m, p, &l);
2524 if (!p)
2525 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002526 }
walimis5821e1b2008-11-15 15:19:06 +08002527
Steven Rostedt69a30832011-12-19 15:21:16 -05002528 if (!p)
2529 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002530
2531 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002532}
2533
2534static void t_stop(struct seq_file *m, void *p)
2535{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002536 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002537}
2538
2539static int t_show(struct seq_file *m, void *v)
2540{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002541 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002542 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002543
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002544 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002545 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002546
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002547 if (iter->flags & FTRACE_ITER_PRINTALL) {
2548 seq_printf(m, "#### all functions enabled ####\n");
2549 return 0;
2550 }
2551
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002552 rec = iter->func;
2553
Steven Rostedt5072c592008-05-12 21:20:43 +02002554 if (!rec)
2555 return 0;
2556
Steven Rostedt647bcd02011-05-03 14:39:21 -04002557 seq_printf(m, "%ps", (void *)rec->ip);
2558 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002559 seq_printf(m, " (%ld)%s",
2560 rec->flags & ~FTRACE_FL_MASK,
2561 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002562 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002563
2564 return 0;
2565}
2566
James Morris88e9d342009-09-22 16:43:43 -07002567static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002568 .start = t_start,
2569 .next = t_next,
2570 .stop = t_stop,
2571 .show = t_show,
2572};
2573
Ingo Molnare309b412008-05-12 21:20:51 +02002574static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002575ftrace_avail_open(struct inode *inode, struct file *file)
2576{
2577 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002578
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002579 if (unlikely(ftrace_disabled))
2580 return -ENODEV;
2581
Jiri Olsa50e18b92012-04-25 10:23:39 +02002582 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2583 if (iter) {
2584 iter->pg = ftrace_pages_start;
2585 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002586 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002587
Jiri Olsa50e18b92012-04-25 10:23:39 +02002588 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002589}
2590
Steven Rostedt647bcd02011-05-03 14:39:21 -04002591static int
2592ftrace_enabled_open(struct inode *inode, struct file *file)
2593{
2594 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002595
2596 if (unlikely(ftrace_disabled))
2597 return -ENODEV;
2598
Jiri Olsa50e18b92012-04-25 10:23:39 +02002599 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2600 if (iter) {
2601 iter->pg = ftrace_pages_start;
2602 iter->flags = FTRACE_ITER_ENABLED;
2603 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002604 }
2605
Jiri Olsa50e18b92012-04-25 10:23:39 +02002606 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002607}
2608
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002609static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002610{
Steven Rostedt52baf112009-02-14 01:15:39 -05002611 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002612 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002613 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002614}
2615
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002616/**
2617 * ftrace_regex_open - initialize function tracer filter files
2618 * @ops: The ftrace_ops that hold the hash filters
2619 * @flag: The type of filter to process
2620 * @inode: The inode, usually passed in to your open routine
2621 * @file: The file, usually passed in to your open routine
2622 *
2623 * ftrace_regex_open() initializes the filter files for the
2624 * @ops. Depending on @flag it may process the filter hash or
2625 * the notrace hash of @ops. With this called from the open
2626 * routine, you can use ftrace_filter_write() for the write
2627 * routine if @flag has FTRACE_ITER_FILTER set, or
2628 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2629 * ftrace_regex_lseek() should be used as the lseek routine, and
2630 * release must call ftrace_regex_release().
2631 */
2632int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002633ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002634 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002635{
2636 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002637 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002638 int ret = 0;
2639
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002640 if (unlikely(ftrace_disabled))
2641 return -ENODEV;
2642
Steven Rostedt5072c592008-05-12 21:20:43 +02002643 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2644 if (!iter)
2645 return -ENOMEM;
2646
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002647 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2648 kfree(iter);
2649 return -ENOMEM;
2650 }
2651
Steven Rostedtf45948e2011-05-02 12:29:25 -04002652 if (flag & FTRACE_ITER_NOTRACE)
2653 hash = ops->notrace_hash;
2654 else
2655 hash = ops->filter_hash;
2656
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002657 iter->ops = ops;
2658 iter->flags = flag;
2659
2660 if (file->f_mode & FMODE_WRITE) {
2661 mutex_lock(&ftrace_lock);
2662 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2663 mutex_unlock(&ftrace_lock);
2664
2665 if (!iter->hash) {
2666 trace_parser_put(&iter->parser);
2667 kfree(iter);
2668 return -ENOMEM;
2669 }
2670 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002671
Steven Rostedt41c52c02008-05-22 11:46:33 -04002672 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002673
Steven Rostedt5072c592008-05-12 21:20:43 +02002674 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002675 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002676 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002677
2678 if (file->f_mode & FMODE_READ) {
2679 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002680
2681 ret = seq_open(file, &show_ftrace_seq_ops);
2682 if (!ret) {
2683 struct seq_file *m = file->private_data;
2684 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002685 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002686 /* Failed */
2687 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002688 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002689 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002690 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002691 } else
2692 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002693 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002694
2695 return ret;
2696}
2697
Steven Rostedt41c52c02008-05-22 11:46:33 -04002698static int
2699ftrace_filter_open(struct inode *inode, struct file *file)
2700{
Steven Rostedt69a30832011-12-19 15:21:16 -05002701 return ftrace_regex_open(&global_ops,
2702 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2703 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002704}
2705
2706static int
2707ftrace_notrace_open(struct inode *inode, struct file *file)
2708{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002709 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002710 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002711}
2712
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002713loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -08002714ftrace_regex_lseek(struct file *file, loff_t offset, int whence)
Steven Rostedt5072c592008-05-12 21:20:43 +02002715{
2716 loff_t ret;
2717
2718 if (file->f_mode & FMODE_READ)
Andrew Morton965c8e52012-12-17 15:59:39 -08002719 ret = seq_lseek(file, offset, whence);
Steven Rostedt5072c592008-05-12 21:20:43 +02002720 else
2721 file->f_pos = ret = 1;
2722
2723 return ret;
2724}
2725
Steven Rostedt64e7c442009-02-13 17:08:48 -05002726static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002727{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002728 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002729 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002730
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002731 switch (type) {
2732 case MATCH_FULL:
2733 if (strcmp(str, regex) == 0)
2734 matched = 1;
2735 break;
2736 case MATCH_FRONT_ONLY:
2737 if (strncmp(str, regex, len) == 0)
2738 matched = 1;
2739 break;
2740 case MATCH_MIDDLE_ONLY:
2741 if (strstr(str, regex))
2742 matched = 1;
2743 break;
2744 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002745 slen = strlen(str);
2746 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002747 matched = 1;
2748 break;
2749 }
2750
2751 return matched;
2752}
2753
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002754static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002755enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002756{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002757 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002758 int ret = 0;
2759
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002760 entry = ftrace_lookup_ip(hash, rec->ip);
2761 if (not) {
2762 /* Do nothing if it doesn't exist */
2763 if (!entry)
2764 return 0;
2765
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002766 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002767 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002768 /* Do nothing if it exists */
2769 if (entry)
2770 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002771
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002772 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002773 }
2774 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002775}
2776
Steven Rostedt64e7c442009-02-13 17:08:48 -05002777static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002778ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2779 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002780{
2781 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002782 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002783
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002784 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2785
2786 if (mod) {
2787 /* module lookup requires matching the module */
2788 if (!modname || strcmp(modname, mod))
2789 return 0;
2790
2791 /* blank search means to match all funcs in the mod */
2792 if (!len)
2793 return 1;
2794 }
2795
Steven Rostedt64e7c442009-02-13 17:08:48 -05002796 return ftrace_match(str, regex, len, type);
2797}
2798
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002799static int
2800match_records(struct ftrace_hash *hash, char *buff,
2801 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002802{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002803 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002804 struct ftrace_page *pg;
2805 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002806 int type = MATCH_FULL;
2807 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002808 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002809 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002810
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002811 if (len) {
2812 type = filter_parse_regex(buff, len, &search, &not);
2813 search_len = strlen(search);
2814 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002815
Steven Rostedt52baf112009-02-14 01:15:39 -05002816 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002817
2818 if (unlikely(ftrace_disabled))
2819 goto out_unlock;
2820
Steven Rostedt265c8312009-02-13 12:43:56 -05002821 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002822 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002823 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002824 if (ret < 0) {
2825 found = ret;
2826 goto out_unlock;
2827 }
Li Zefan311d16d2009-12-08 11:15:11 +08002828 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002829 }
2830 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002831 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002832 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002833
2834 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002835}
2836
Steven Rostedt64e7c442009-02-13 17:08:48 -05002837static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002838ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002839{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002840 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002841}
2842
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002843static int
2844ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002845{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002846 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002847
Steven Rostedt64e7c442009-02-13 17:08:48 -05002848 /* blank or '*' mean the same */
2849 if (strcmp(buff, "*") == 0)
2850 buff[0] = 0;
2851
2852 /* handle the case of 'dont filter this module' */
2853 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2854 buff[0] = 0;
2855 not = 1;
2856 }
2857
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002858 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002859}
2860
Steven Rostedtf6180772009-02-14 00:40:25 -05002861/*
2862 * We register the module command as a template to show others how
2863 * to register the a command as well.
2864 */
2865
2866static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002867ftrace_mod_callback(struct ftrace_hash *hash,
2868 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002869{
2870 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002871 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002872
2873 /*
2874 * cmd == 'mod' because we only registered this func
2875 * for the 'mod' ftrace_func_command.
2876 * But if you register one func with multiple commands,
2877 * you can tell which command was used by the cmd
2878 * parameter.
2879 */
2880
2881 /* we must have a module name */
2882 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002883 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002884
2885 mod = strsep(&param, ":");
2886 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002887 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002888
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002889 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002890 if (!ret)
2891 ret = -EINVAL;
2892 if (ret < 0)
2893 return ret;
2894
2895 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002896}
2897
2898static struct ftrace_func_command ftrace_mod_cmd = {
2899 .name = "mod",
2900 .func = ftrace_mod_callback,
2901};
2902
2903static int __init ftrace_mod_cmd_init(void)
2904{
2905 return register_ftrace_command(&ftrace_mod_cmd);
2906}
Steven Rostedt6f415672012-10-05 12:13:07 -04002907core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002908
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002909static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002910 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002911{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002912 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002913 struct hlist_head *hhd;
2914 struct hlist_node *n;
2915 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002916
2917 key = hash_long(ip, FTRACE_HASH_BITS);
2918
2919 hhd = &ftrace_func_hash[key];
2920
2921 if (hlist_empty(hhd))
2922 return;
2923
2924 /*
2925 * Disable preemption for these calls to prevent a RCU grace
2926 * period. This syncs the hash iteration and freeing of items
2927 * on the hash. rcu_read_lock is too dangerous here.
2928 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002929 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002930 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2931 if (entry->ip == ip)
2932 entry->ops->func(ip, parent_ip, &entry->data);
2933 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002934 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002935}
2936
Steven Rostedtb6887d72009-02-17 12:32:04 -05002937static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002938{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002939 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002940};
2941
Steven Rostedtb6887d72009-02-17 12:32:04 -05002942static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002943
Steven Rostedtb6887d72009-02-17 12:32:04 -05002944static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002945{
Steven Rostedtb8489142011-05-04 09:27:52 -04002946 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002947 int i;
2948
Steven Rostedtb6887d72009-02-17 12:32:04 -05002949 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002950 return;
2951
2952 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2953 struct hlist_head *hhd = &ftrace_func_hash[i];
2954 if (hhd->first)
2955 break;
2956 }
2957 /* Nothing registered? */
2958 if (i == FTRACE_FUNC_HASHSIZE)
2959 return;
2960
Steven Rostedtb8489142011-05-04 09:27:52 -04002961 ret = __register_ftrace_function(&trace_probe_ops);
2962 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002963 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002964
Steven Rostedtb6887d72009-02-17 12:32:04 -05002965 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002966}
2967
Steven Rostedtb6887d72009-02-17 12:32:04 -05002968static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002969{
Steven Rostedtb8489142011-05-04 09:27:52 -04002970 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002971 int i;
2972
Steven Rostedtb6887d72009-02-17 12:32:04 -05002973 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002974 return;
2975
2976 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2977 struct hlist_head *hhd = &ftrace_func_hash[i];
2978 if (hhd->first)
2979 return;
2980 }
2981
2982 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002983 ret = __unregister_ftrace_function(&trace_probe_ops);
2984 if (!ret)
2985 ftrace_shutdown(&trace_probe_ops, 0);
2986
Steven Rostedtb6887d72009-02-17 12:32:04 -05002987 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002988}
2989
2990
2991static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2992{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002993 struct ftrace_func_probe *entry =
2994 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002995
2996 if (entry->ops->free)
2997 entry->ops->free(&entry->data);
2998 kfree(entry);
2999}
3000
3001
3002int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003003register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003004 void *data)
3005{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003006 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003007 struct ftrace_page *pg;
3008 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003009 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003010 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003011 int count = 0;
3012 char *search;
3013
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003014 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003015 len = strlen(search);
3016
Steven Rostedtb6887d72009-02-17 12:32:04 -05003017 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003018 if (WARN_ON(not))
3019 return -EINVAL;
3020
3021 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003022
Steven Rostedt45a4a232011-04-21 23:16:46 -04003023 if (unlikely(ftrace_disabled))
3024 goto out_unlock;
3025
Steven Rostedt59df055f2009-02-14 15:29:06 -05003026 do_for_each_ftrace_rec(pg, rec) {
3027
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003028 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003029 continue;
3030
3031 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3032 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003033 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003034 if (!count)
3035 count = -ENOMEM;
3036 goto out_unlock;
3037 }
3038
3039 count++;
3040
3041 entry->data = data;
3042
3043 /*
3044 * The caller might want to do something special
3045 * for each function we find. We call the callback
3046 * to give the caller an opportunity to do so.
3047 */
3048 if (ops->callback) {
3049 if (ops->callback(rec->ip, &entry->data) < 0) {
3050 /* caller does not like this func */
3051 kfree(entry);
3052 continue;
3053 }
3054 }
3055
3056 entry->ops = ops;
3057 entry->ip = rec->ip;
3058
3059 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3060 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3061
3062 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05003063 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003064
3065 out_unlock:
3066 mutex_unlock(&ftrace_lock);
3067
3068 return count;
3069}
3070
3071enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003072 PROBE_TEST_FUNC = 1,
3073 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003074};
3075
3076static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003077__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003078 void *data, int flags)
3079{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003080 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003081 struct hlist_node *n, *tmp;
3082 char str[KSYM_SYMBOL_LEN];
3083 int type = MATCH_FULL;
3084 int i, len = 0;
3085 char *search;
3086
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003087 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003088 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003089 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003090 int not;
3091
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003092 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003093 len = strlen(search);
3094
Steven Rostedtb6887d72009-02-17 12:32:04 -05003095 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003096 if (WARN_ON(not))
3097 return;
3098 }
3099
3100 mutex_lock(&ftrace_lock);
3101 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3102 struct hlist_head *hhd = &ftrace_func_hash[i];
3103
3104 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3105
3106 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003107 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003108 continue;
3109
Steven Rostedtb6887d72009-02-17 12:32:04 -05003110 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003111 continue;
3112
3113 /* do this last, since it is the most expensive */
3114 if (glob) {
3115 kallsyms_lookup(entry->ip, NULL, NULL,
3116 NULL, str);
3117 if (!ftrace_match(str, glob, len, type))
3118 continue;
3119 }
3120
3121 hlist_del(&entry->node);
3122 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3123 }
3124 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003125 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003126 mutex_unlock(&ftrace_lock);
3127}
3128
3129void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003130unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003131 void *data)
3132{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003133 __unregister_ftrace_function_probe(glob, ops, data,
3134 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003135}
3136
3137void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003138unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003139{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003140 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003141}
3142
Steven Rostedtb6887d72009-02-17 12:32:04 -05003143void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003144{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003145 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003146}
3147
Steven Rostedtf6180772009-02-14 00:40:25 -05003148static LIST_HEAD(ftrace_commands);
3149static DEFINE_MUTEX(ftrace_cmd_mutex);
3150
3151int register_ftrace_command(struct ftrace_func_command *cmd)
3152{
3153 struct ftrace_func_command *p;
3154 int ret = 0;
3155
3156 mutex_lock(&ftrace_cmd_mutex);
3157 list_for_each_entry(p, &ftrace_commands, list) {
3158 if (strcmp(cmd->name, p->name) == 0) {
3159 ret = -EBUSY;
3160 goto out_unlock;
3161 }
3162 }
3163 list_add(&cmd->list, &ftrace_commands);
3164 out_unlock:
3165 mutex_unlock(&ftrace_cmd_mutex);
3166
3167 return ret;
3168}
3169
3170int unregister_ftrace_command(struct ftrace_func_command *cmd)
3171{
3172 struct ftrace_func_command *p, *n;
3173 int ret = -ENODEV;
3174
3175 mutex_lock(&ftrace_cmd_mutex);
3176 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3177 if (strcmp(cmd->name, p->name) == 0) {
3178 ret = 0;
3179 list_del_init(&p->list);
3180 goto out_unlock;
3181 }
3182 }
3183 out_unlock:
3184 mutex_unlock(&ftrace_cmd_mutex);
3185
3186 return ret;
3187}
3188
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003189static int ftrace_process_regex(struct ftrace_hash *hash,
3190 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003191{
Steven Rostedtf6180772009-02-14 00:40:25 -05003192 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003193 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003194 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003195
3196 func = strsep(&next, ":");
3197
3198 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003199 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003200 if (!ret)
3201 ret = -EINVAL;
3202 if (ret < 0)
3203 return ret;
3204 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003205 }
3206
Steven Rostedtf6180772009-02-14 00:40:25 -05003207 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003208
3209 command = strsep(&next, ":");
3210
Steven Rostedtf6180772009-02-14 00:40:25 -05003211 mutex_lock(&ftrace_cmd_mutex);
3212 list_for_each_entry(p, &ftrace_commands, list) {
3213 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003214 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003215 goto out_unlock;
3216 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003217 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003218 out_unlock:
3219 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003220
Steven Rostedtf6180772009-02-14 00:40:25 -05003221 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003222}
3223
Ingo Molnare309b412008-05-12 21:20:51 +02003224static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003225ftrace_regex_write(struct file *file, const char __user *ubuf,
3226 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003227{
3228 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003229 struct trace_parser *parser;
3230 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003231
Li Zefan4ba79782009-09-22 13:52:20 +08003232 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003233 return 0;
3234
Steven Rostedt41c52c02008-05-22 11:46:33 -04003235 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003236
Steven Rostedt45a4a232011-04-21 23:16:46 -04003237 ret = -ENODEV;
3238 if (unlikely(ftrace_disabled))
3239 goto out_unlock;
3240
Steven Rostedt5072c592008-05-12 21:20:43 +02003241 if (file->f_mode & FMODE_READ) {
3242 struct seq_file *m = file->private_data;
3243 iter = m->private;
3244 } else
3245 iter = file->private_data;
3246
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003247 parser = &iter->parser;
3248 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003249
Li Zefan4ba79782009-09-22 13:52:20 +08003250 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003251 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003252 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003253 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003254 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003255 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003256 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003257 }
3258
Steven Rostedt5072c592008-05-12 21:20:43 +02003259 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003260out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003261 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003262
Steven Rostedt5072c592008-05-12 21:20:43 +02003263 return ret;
3264}
3265
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003266ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003267ftrace_filter_write(struct file *file, const char __user *ubuf,
3268 size_t cnt, loff_t *ppos)
3269{
3270 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3271}
3272
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003273ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003274ftrace_notrace_write(struct file *file, const char __user *ubuf,
3275 size_t cnt, loff_t *ppos)
3276{
3277 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3278}
3279
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003280static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003281ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3282{
3283 struct ftrace_func_entry *entry;
3284
3285 if (!ftrace_location(ip))
3286 return -EINVAL;
3287
3288 if (remove) {
3289 entry = ftrace_lookup_ip(hash, ip);
3290 if (!entry)
3291 return -ENOENT;
3292 free_hash_entry(hash, entry);
3293 return 0;
3294 }
3295
3296 return add_hash_entry(hash, ip);
3297}
3298
3299static int
3300ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3301 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003302{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003303 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003304 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003305 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003306
Steven Rostedt936e0742011-05-05 22:54:01 -04003307 /* All global ops uses the global ops filters */
3308 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3309 ops = &global_ops;
3310
Steven Rostedt41c52c02008-05-22 11:46:33 -04003311 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003312 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003313
Steven Rostedtf45948e2011-05-02 12:29:25 -04003314 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003315 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003316 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003317 orig_hash = &ops->notrace_hash;
3318
3319 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3320 if (!hash)
3321 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003322
Steven Rostedt41c52c02008-05-22 11:46:33 -04003323 mutex_lock(&ftrace_regex_lock);
3324 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003325 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003326 if (buf && !ftrace_match_records(hash, buf, len)) {
3327 ret = -EINVAL;
3328 goto out_regex_unlock;
3329 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003330 if (ip) {
3331 ret = ftrace_match_addr(hash, ip, remove);
3332 if (ret < 0)
3333 goto out_regex_unlock;
3334 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003335
3336 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003337 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003338 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3339 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003340 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003341
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003342 mutex_unlock(&ftrace_lock);
3343
Jiri Olsaac483c42012-01-02 10:04:14 +01003344 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003345 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003346
3347 free_ftrace_hash(hash);
3348 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003349}
3350
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003351static int
3352ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3353 int reset, int enable)
3354{
3355 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3356}
3357
3358/**
3359 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3360 * @ops - the ops to set the filter with
3361 * @ip - the address to add to or remove from the filter.
3362 * @remove - non zero to remove the ip from the filter
3363 * @reset - non zero to reset all filters before applying this filter.
3364 *
3365 * Filters denote which functions should be enabled when tracing is enabled
3366 * If @ip is NULL, it failes to update filter.
3367 */
3368int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3369 int remove, int reset)
3370{
3371 return ftrace_set_addr(ops, ip, remove, reset, 1);
3372}
3373EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3374
3375static int
3376ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3377 int reset, int enable)
3378{
3379 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3380}
3381
Steven Rostedt77a2b372008-05-12 21:20:45 +02003382/**
3383 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003384 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003385 * @buf - the string that holds the function filter text.
3386 * @len - the length of the string.
3387 * @reset - non zero to reset all filters before applying this filter.
3388 *
3389 * Filters denote which functions should be enabled when tracing is enabled.
3390 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3391 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003392int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003393 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003394{
Jiri Olsaac483c42012-01-02 10:04:14 +01003395 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003396}
Steven Rostedt936e0742011-05-05 22:54:01 -04003397EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003398
Steven Rostedt41c52c02008-05-22 11:46:33 -04003399/**
3400 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003401 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003402 * @buf - the string that holds the function notrace text.
3403 * @len - the length of the string.
3404 * @reset - non zero to reset all filters before applying this filter.
3405 *
3406 * Notrace Filters denote which functions should not be enabled when tracing
3407 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3408 * for tracing.
3409 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003410int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003411 int len, int reset)
3412{
Jiri Olsaac483c42012-01-02 10:04:14 +01003413 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003414}
3415EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3416/**
3417 * ftrace_set_filter - set a function to filter on in ftrace
3418 * @ops - the ops to set the filter with
3419 * @buf - the string that holds the function filter text.
3420 * @len - the length of the string.
3421 * @reset - non zero to reset all filters before applying this filter.
3422 *
3423 * Filters denote which functions should be enabled when tracing is enabled.
3424 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3425 */
3426void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3427{
3428 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3429}
3430EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3431
3432/**
3433 * ftrace_set_notrace - set a function to not trace in ftrace
3434 * @ops - the ops to set the notrace filter with
3435 * @buf - the string that holds the function notrace text.
3436 * @len - the length of the string.
3437 * @reset - non zero to reset all filters before applying this filter.
3438 *
3439 * Notrace Filters denote which functions should not be enabled when tracing
3440 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3441 * for tracing.
3442 */
3443void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003444{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003445 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003446}
Steven Rostedt936e0742011-05-05 22:54:01 -04003447EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003448
Steven Rostedt2af15d62009-05-28 13:37:24 -04003449/*
3450 * command line interface to allow users to set filters on boot up.
3451 */
3452#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3453static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3454static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3455
3456static int __init set_ftrace_notrace(char *str)
3457{
3458 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3459 return 1;
3460}
3461__setup("ftrace_notrace=", set_ftrace_notrace);
3462
3463static int __init set_ftrace_filter(char *str)
3464{
3465 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3466 return 1;
3467}
3468__setup("ftrace_filter=", set_ftrace_filter);
3469
Stefan Assmann369bc182009-10-12 22:17:21 +02003470#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003471static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003472static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3473
Stefan Assmann369bc182009-10-12 22:17:21 +02003474static int __init set_graph_function(char *str)
3475{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003476 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003477 return 1;
3478}
3479__setup("ftrace_graph_filter=", set_graph_function);
3480
3481static void __init set_ftrace_early_graph(char *buf)
3482{
3483 int ret;
3484 char *func;
3485
3486 while (buf) {
3487 func = strsep(&buf, ",");
3488 /* we allow only one expression at a time */
3489 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3490 func);
3491 if (ret)
3492 printk(KERN_DEBUG "ftrace: function %s not "
3493 "traceable\n", func);
3494 }
3495}
3496#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3497
Steven Rostedt2a85a372011-12-19 21:57:44 -05003498void __init
3499ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003500{
3501 char *func;
3502
3503 while (buf) {
3504 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003505 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003506 }
3507}
3508
3509static void __init set_ftrace_early_filters(void)
3510{
3511 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003512 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003513 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003514 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003515#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3516 if (ftrace_graph_buf[0])
3517 set_ftrace_early_graph(ftrace_graph_buf);
3518#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003519}
3520
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003521int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003522{
3523 struct seq_file *m = (struct seq_file *)file->private_data;
3524 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003525 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003526 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003527 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003528 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003529
Steven Rostedt41c52c02008-05-22 11:46:33 -04003530 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003531 if (file->f_mode & FMODE_READ) {
3532 iter = m->private;
3533
3534 seq_release(inode, file);
3535 } else
3536 iter = file->private_data;
3537
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003538 parser = &iter->parser;
3539 if (trace_parser_loaded(parser)) {
3540 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003541 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003542 }
3543
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003544 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003545
Steven Rostedt058e2972011-04-29 22:35:33 -04003546 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003547 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3548
3549 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003550 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003551 else
3552 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003553
Steven Rostedt058e2972011-04-29 22:35:33 -04003554 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003555 ret = ftrace_hash_move(iter->ops, filter_hash,
3556 orig_hash, iter->hash);
3557 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3558 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003559 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003560
Steven Rostedt058e2972011-04-29 22:35:33 -04003561 mutex_unlock(&ftrace_lock);
3562 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003563 free_ftrace_hash(iter->hash);
3564 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003565
Steven Rostedt41c52c02008-05-22 11:46:33 -04003566 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003567 return 0;
3568}
3569
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003570static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003571 .open = ftrace_avail_open,
3572 .read = seq_read,
3573 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003574 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003575};
3576
Steven Rostedt647bcd02011-05-03 14:39:21 -04003577static const struct file_operations ftrace_enabled_fops = {
3578 .open = ftrace_enabled_open,
3579 .read = seq_read,
3580 .llseek = seq_lseek,
3581 .release = seq_release_private,
3582};
3583
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003584static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003585 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003586 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003587 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003588 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003589 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003590};
3591
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003592static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003593 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003594 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003595 .write = ftrace_notrace_write,
3596 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003597 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003598};
3599
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003600#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3601
3602static DEFINE_MUTEX(graph_lock);
3603
3604int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003605int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003606unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3607
3608static void *
Li Zefan85951842009-06-24 09:54:00 +08003609__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003610{
Li Zefan85951842009-06-24 09:54:00 +08003611 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003612 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003613 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003614}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003615
Li Zefan85951842009-06-24 09:54:00 +08003616static void *
3617g_next(struct seq_file *m, void *v, loff_t *pos)
3618{
3619 (*pos)++;
3620 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003621}
3622
3623static void *g_start(struct seq_file *m, loff_t *pos)
3624{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003625 mutex_lock(&graph_lock);
3626
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003627 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003628 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003629 return (void *)1;
3630
Li Zefan85951842009-06-24 09:54:00 +08003631 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003632}
3633
3634static void g_stop(struct seq_file *m, void *p)
3635{
3636 mutex_unlock(&graph_lock);
3637}
3638
3639static int g_show(struct seq_file *m, void *v)
3640{
3641 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003642
3643 if (!ptr)
3644 return 0;
3645
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003646 if (ptr == (unsigned long *)1) {
3647 seq_printf(m, "#### all functions enabled ####\n");
3648 return 0;
3649 }
3650
Steven Rostedtb375a112009-09-17 00:05:58 -04003651 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003652
3653 return 0;
3654}
3655
James Morris88e9d342009-09-22 16:43:43 -07003656static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003657 .start = g_start,
3658 .next = g_next,
3659 .stop = g_stop,
3660 .show = g_show,
3661};
3662
3663static int
3664ftrace_graph_open(struct inode *inode, struct file *file)
3665{
3666 int ret = 0;
3667
3668 if (unlikely(ftrace_disabled))
3669 return -ENODEV;
3670
3671 mutex_lock(&graph_lock);
3672 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003673 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003674 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003675 ftrace_graph_count = 0;
3676 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3677 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003678 mutex_unlock(&graph_lock);
3679
Li Zefana4ec5e02009-09-18 14:06:28 +08003680 if (file->f_mode & FMODE_READ)
3681 ret = seq_open(file, &ftrace_graph_seq_ops);
3682
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003683 return ret;
3684}
3685
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003686static int
Li Zefan87827112009-07-23 11:29:11 +08003687ftrace_graph_release(struct inode *inode, struct file *file)
3688{
3689 if (file->f_mode & FMODE_READ)
3690 seq_release(inode, file);
3691 return 0;
3692}
3693
3694static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003695ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003696{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003697 struct dyn_ftrace *rec;
3698 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003699 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003700 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003701 int type, not;
3702 char *search;
3703 bool exists;
3704 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003705
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003706 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003707 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003708 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3709 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003710
3711 search_len = strlen(search);
3712
Steven Rostedt52baf112009-02-14 01:15:39 -05003713 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003714
3715 if (unlikely(ftrace_disabled)) {
3716 mutex_unlock(&ftrace_lock);
3717 return -ENODEV;
3718 }
3719
Steven Rostedt265c8312009-02-13 12:43:56 -05003720 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003721
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003722 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003723 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003724 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003725 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003726 if (array[i] == rec->ip) {
3727 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003728 break;
3729 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003730 }
3731
3732 if (!not) {
3733 fail = 0;
3734 if (!exists) {
3735 array[(*idx)++] = rec->ip;
3736 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3737 goto out;
3738 }
3739 } else {
3740 if (exists) {
3741 array[i] = array[--(*idx)];
3742 array[*idx] = 0;
3743 fail = 0;
3744 }
3745 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003746 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003747 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003748out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003749 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003750
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003751 if (fail)
3752 return -EINVAL;
3753
3754 ftrace_graph_filter_enabled = 1;
3755 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003756}
3757
3758static ssize_t
3759ftrace_graph_write(struct file *file, const char __user *ubuf,
3760 size_t cnt, loff_t *ppos)
3761{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003762 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003763 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003764
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003765 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003766 return 0;
3767
3768 mutex_lock(&graph_lock);
3769
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003770 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3771 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003772 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003773 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003774
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003775 read = trace_get_user(&parser, ubuf, cnt, ppos);
3776
Li Zefan4ba79782009-09-22 13:52:20 +08003777 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003778 parser.buffer[parser.idx] = 0;
3779
3780 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003781 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003782 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003783 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003784 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003785 }
3786
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003787 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003788
3789out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003790 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003791out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003792 mutex_unlock(&graph_lock);
3793
3794 return ret;
3795}
3796
3797static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003798 .open = ftrace_graph_open,
3799 .read = seq_read,
3800 .write = ftrace_graph_write,
3801 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003802 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003803};
3804#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3805
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003806static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003807{
Steven Rostedt5072c592008-05-12 21:20:43 +02003808
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003809 trace_create_file("available_filter_functions", 0444,
3810 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003811
Steven Rostedt647bcd02011-05-03 14:39:21 -04003812 trace_create_file("enabled_functions", 0444,
3813 d_tracer, NULL, &ftrace_enabled_fops);
3814
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003815 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3816 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003817
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003818 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003819 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003820
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003821#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003822 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003823 NULL,
3824 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003825#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3826
Steven Rostedt5072c592008-05-12 21:20:43 +02003827 return 0;
3828}
3829
Steven Rostedt9fd49322012-04-24 22:32:06 -04003830static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003831{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003832 const unsigned long *ipa = a;
3833 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003834
Steven Rostedt9fd49322012-04-24 22:32:06 -04003835 if (*ipa > *ipb)
3836 return 1;
3837 if (*ipa < *ipb)
3838 return -1;
3839 return 0;
3840}
3841
3842static void ftrace_swap_ips(void *a, void *b, int size)
3843{
3844 unsigned long *ipa = a;
3845 unsigned long *ipb = b;
3846 unsigned long t;
3847
3848 t = *ipa;
3849 *ipa = *ipb;
3850 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003851}
3852
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003853static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003854 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003855 unsigned long *end)
3856{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003857 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003858 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003859 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003860 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003861 unsigned long *p;
3862 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003863 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003864 int ret = -ENOMEM;
3865
3866 count = end - start;
3867
3868 if (!count)
3869 return 0;
3870
Steven Rostedt9fd49322012-04-24 22:32:06 -04003871 sort(start, count, sizeof(*start),
3872 ftrace_cmp_ips, ftrace_swap_ips);
3873
Steven Rostedt706c81f2012-04-24 23:45:26 -04003874 start_pg = ftrace_allocate_pages(count);
3875 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05003876 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003877
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003878 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003879
Steven Rostedt320823092011-12-16 14:42:37 -05003880 /*
3881 * Core and each module needs their own pages, as
3882 * modules will free them when they are removed.
3883 * Force a new page to be allocated for modules.
3884 */
Steven Rostedta7900872011-12-16 16:23:44 -05003885 if (!mod) {
3886 WARN_ON(ftrace_pages || ftrace_pages_start);
3887 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003888 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003889 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05003890 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003891 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05003892
Steven Rostedta7900872011-12-16 16:23:44 -05003893 if (WARN_ON(ftrace_pages->next)) {
3894 /* Hmm, we have free pages? */
3895 while (ftrace_pages->next)
3896 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05003897 }
Steven Rostedta7900872011-12-16 16:23:44 -05003898
Steven Rostedt706c81f2012-04-24 23:45:26 -04003899 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05003900 }
3901
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003902 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003903 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003904 while (p < end) {
3905 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003906 /*
3907 * Some architecture linkers will pad between
3908 * the different mcount_loc sections of different
3909 * object files to satisfy alignments.
3910 * Skip any NULL pointers.
3911 */
3912 if (!addr)
3913 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003914
3915 if (pg->index == pg->size) {
3916 /* We should have allocated enough */
3917 if (WARN_ON(!pg->next))
3918 break;
3919 pg = pg->next;
3920 }
3921
3922 rec = &pg->records[pg->index++];
3923 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003924 }
3925
Steven Rostedt706c81f2012-04-24 23:45:26 -04003926 /* We should have used all pages */
3927 WARN_ON(pg->next);
3928
3929 /* Assign the last page to ftrace_pages */
3930 ftrace_pages = pg;
3931
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003932 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003933 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003934
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003935 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003936 * We only need to disable interrupts on start up
3937 * because we are modifying code that an interrupt
3938 * may execute, and the modification is not atomic.
3939 * But for modules, nothing runs the code we modify
3940 * until we are finished with it, and there's no
3941 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003942 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003943 if (!mod)
3944 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003945 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003946 if (!mod)
3947 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003948 ret = 0;
3949 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003950 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003951
Steven Rostedta7900872011-12-16 16:23:44 -05003952 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003953}
3954
Steven Rostedt93eb6772009-04-15 13:24:06 -04003955#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05003956
3957#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3958
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003959void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003960{
3961 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05003962 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003963 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003964 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003965
Steven Rostedt93eb6772009-04-15 13:24:06 -04003966 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003967
3968 if (ftrace_disabled)
3969 goto out_unlock;
3970
Steven Rostedt320823092011-12-16 14:42:37 -05003971 /*
3972 * Each module has its own ftrace_pages, remove
3973 * them from the list.
3974 */
3975 last_pg = &ftrace_pages_start;
3976 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3977 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003978 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003979 /*
Steven Rostedt320823092011-12-16 14:42:37 -05003980 * As core pages are first, the first
3981 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003982 */
Steven Rostedt320823092011-12-16 14:42:37 -05003983 if (WARN_ON(pg == ftrace_pages_start))
3984 goto out_unlock;
3985
3986 /* Check if we are deleting the last page */
3987 if (pg == ftrace_pages)
3988 ftrace_pages = next_to_ftrace_page(last_pg);
3989
3990 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003991 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3992 free_pages((unsigned long)pg->records, order);
3993 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05003994 } else
3995 last_pg = &pg->next;
3996 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003997 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003998 mutex_unlock(&ftrace_lock);
3999}
4000
4001static void ftrace_init_module(struct module *mod,
4002 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004003{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004004 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004005 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004006 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004007}
4008
Steven Rostedt93eb6772009-04-15 13:24:06 -04004009static int ftrace_module_notify(struct notifier_block *self,
4010 unsigned long val, void *data)
4011{
4012 struct module *mod = data;
4013
4014 switch (val) {
4015 case MODULE_STATE_COMING:
4016 ftrace_init_module(mod, mod->ftrace_callsites,
4017 mod->ftrace_callsites +
4018 mod->num_ftrace_callsites);
4019 break;
4020 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004021 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004022 break;
4023 }
4024
4025 return 0;
4026}
4027#else
4028static int ftrace_module_notify(struct notifier_block *self,
4029 unsigned long val, void *data)
4030{
4031 return 0;
4032}
4033#endif /* CONFIG_MODULES */
4034
4035struct notifier_block ftrace_module_nb = {
4036 .notifier_call = ftrace_module_notify,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004037 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004038};
4039
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004040extern unsigned long __start_mcount_loc[];
4041extern unsigned long __stop_mcount_loc[];
4042
4043void __init ftrace_init(void)
4044{
4045 unsigned long count, addr, flags;
4046 int ret;
4047
4048 /* Keep the ftrace pointer to the stub */
4049 addr = (unsigned long)ftrace_stub;
4050
4051 local_irq_save(flags);
4052 ftrace_dyn_arch_init(&addr);
4053 local_irq_restore(flags);
4054
4055 /* ftrace_dyn_arch_init places the return code in addr */
4056 if (addr)
4057 goto failed;
4058
4059 count = __stop_mcount_loc - __start_mcount_loc;
4060
4061 ret = ftrace_dyn_table_alloc(count);
4062 if (ret)
4063 goto failed;
4064
4065 last_ftrace_enabled = ftrace_enabled = 1;
4066
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004067 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004068 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004069 __stop_mcount_loc);
4070
Steven Rostedt93eb6772009-04-15 13:24:06 -04004071 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004072 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004073 pr_warning("Failed to register trace ftrace module notifier\n");
4074
Steven Rostedt2af15d62009-05-28 13:37:24 -04004075 set_ftrace_early_filters();
4076
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004077 return;
4078 failed:
4079 ftrace_disabled = 1;
4080}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004081
Steven Rostedt3d083392008-05-12 21:20:42 +02004082#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004083
Steven Rostedt2b499382011-05-03 22:49:52 -04004084static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004085 .func = ftrace_stub,
Steven Rostedt47409742012-07-20 11:04:44 -04004086 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004087};
4088
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004089static int __init ftrace_nodyn_init(void)
4090{
4091 ftrace_enabled = 1;
4092 return 0;
4093}
Steven Rostedt6f415672012-10-05 12:13:07 -04004094core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004095
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004096static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4097static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004098/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004099# define ftrace_startup(ops, command) \
4100 ({ \
4101 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4102 0; \
4103 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04004104# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004105# define ftrace_startup_sysctl() do { } while (0)
4106# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004107
4108static inline int
4109ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4110{
4111 return 1;
4112}
4113
Steven Rostedt3d083392008-05-12 21:20:42 +02004114#endif /* CONFIG_DYNAMIC_FTRACE */
4115
Steven Rostedtb8489142011-05-04 09:27:52 -04004116static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004117ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004118 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004119{
Jiri Olsae2484912012-02-15 15:51:48 +01004120 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4121 return;
4122
4123 /*
4124 * Some of the ops may be dynamically allocated,
4125 * they must be freed after a synchronize_sched().
4126 */
4127 preempt_disable_notrace();
4128 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt0a016402012-11-02 17:03:03 -04004129 do_for_each_ftrace_op(op, ftrace_control_list) {
Jiri Olsae2484912012-02-15 15:51:48 +01004130 if (!ftrace_function_local_disabled(op) &&
4131 ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004132 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004133 } while_for_each_ftrace_op(op);
Jiri Olsae2484912012-02-15 15:51:48 +01004134 trace_recursion_clear(TRACE_CONTROL_BIT);
4135 preempt_enable_notrace();
4136}
4137
4138static struct ftrace_ops control_ops = {
4139 .func = ftrace_ops_control_func,
Steven Rostedt47409742012-07-20 11:04:44 -04004140 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Jiri Olsae2484912012-02-15 15:51:48 +01004141};
4142
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004143static inline void
4144__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004145 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004146{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004147 struct ftrace_ops *op;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004148 unsigned int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004149
Steven Rostedtccf36722012-06-05 09:44:25 -04004150 if (function_trace_stop)
4151 return;
4152
Steven Rostedtc29f1222012-11-02 17:17:59 -04004153 if (in_interrupt()) {
4154 if (in_nmi())
4155 bit = TRACE_INTERNAL_NMI_BIT;
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004156
Steven Rostedtc29f1222012-11-02 17:17:59 -04004157 else if (in_irq())
4158 bit = TRACE_INTERNAL_IRQ_BIT;
4159 else
4160 bit = TRACE_INTERNAL_SIRQ_BIT;
4161 } else
4162 bit = TRACE_INTERNAL_BIT;
4163
4164 if (unlikely(trace_recursion_test(bit)))
4165 return;
4166
4167 trace_recursion_set(bit);
4168
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004169 /*
4170 * Some of the ops may be dynamically allocated,
4171 * they must be freed after a synchronize_sched().
4172 */
4173 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004174 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedtb8489142011-05-04 09:27:52 -04004175 if (ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004176 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004177 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004178 preempt_enable_notrace();
Steven Rostedtc29f1222012-11-02 17:17:59 -04004179 trace_recursion_clear(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004180}
4181
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004182/*
4183 * Some archs only support passing ip and parent_ip. Even though
4184 * the list function ignores the op parameter, we do not want any
4185 * C side effects, where a function is called without the caller
4186 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004187 * Archs are to support both the regs and ftrace_ops at the same time.
4188 * If they support ftrace_ops, it is assumed they support regs.
4189 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004190 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4191 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004192 * An architecture can pass partial regs with ftrace_ops and still
4193 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004194 */
4195#if ARCH_SUPPORTS_FTRACE_OPS
4196static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004197 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004198{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004199 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004200}
4201#else
4202static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4203{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004204 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004205}
4206#endif
4207
Steven Rostedte32d8952008-12-04 00:26:41 -05004208static void clear_ftrace_swapper(void)
4209{
4210 struct task_struct *p;
4211 int cpu;
4212
4213 get_online_cpus();
4214 for_each_online_cpu(cpu) {
4215 p = idle_task(cpu);
4216 clear_tsk_trace_trace(p);
4217 }
4218 put_online_cpus();
4219}
4220
4221static void set_ftrace_swapper(void)
4222{
4223 struct task_struct *p;
4224 int cpu;
4225
4226 get_online_cpus();
4227 for_each_online_cpu(cpu) {
4228 p = idle_task(cpu);
4229 set_tsk_trace_trace(p);
4230 }
4231 put_online_cpus();
4232}
4233
4234static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004235{
4236 struct task_struct *p;
4237
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004238 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004239 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004240 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004241 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004242 rcu_read_unlock();
4243
Steven Rostedte32d8952008-12-04 00:26:41 -05004244 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004245}
4246
Steven Rostedte32d8952008-12-04 00:26:41 -05004247static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004248{
4249 struct task_struct *p;
4250
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004251 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004252 do_each_pid_task(pid, PIDTYPE_PID, p) {
4253 set_tsk_trace_trace(p);
4254 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004255 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004256}
4257
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004258static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004259{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004260 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004261 clear_ftrace_swapper();
4262 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004263 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004264}
4265
4266static void set_ftrace_pid_task(struct pid *pid)
4267{
4268 if (pid == ftrace_swapper_pid)
4269 set_ftrace_swapper();
4270 else
4271 set_ftrace_pid(pid);
4272}
4273
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004274static int ftrace_pid_add(int p)
4275{
4276 struct pid *pid;
4277 struct ftrace_pid *fpid;
4278 int ret = -EINVAL;
4279
4280 mutex_lock(&ftrace_lock);
4281
4282 if (!p)
4283 pid = ftrace_swapper_pid;
4284 else
4285 pid = find_get_pid(p);
4286
4287 if (!pid)
4288 goto out;
4289
4290 ret = 0;
4291
4292 list_for_each_entry(fpid, &ftrace_pids, list)
4293 if (fpid->pid == pid)
4294 goto out_put;
4295
4296 ret = -ENOMEM;
4297
4298 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4299 if (!fpid)
4300 goto out_put;
4301
4302 list_add(&fpid->list, &ftrace_pids);
4303 fpid->pid = pid;
4304
4305 set_ftrace_pid_task(pid);
4306
4307 ftrace_update_pid_func();
4308 ftrace_startup_enable(0);
4309
4310 mutex_unlock(&ftrace_lock);
4311 return 0;
4312
4313out_put:
4314 if (pid != ftrace_swapper_pid)
4315 put_pid(pid);
4316
4317out:
4318 mutex_unlock(&ftrace_lock);
4319 return ret;
4320}
4321
4322static void ftrace_pid_reset(void)
4323{
4324 struct ftrace_pid *fpid, *safe;
4325
4326 mutex_lock(&ftrace_lock);
4327 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4328 struct pid *pid = fpid->pid;
4329
4330 clear_ftrace_pid_task(pid);
4331
4332 list_del(&fpid->list);
4333 kfree(fpid);
4334 }
4335
4336 ftrace_update_pid_func();
4337 ftrace_startup_enable(0);
4338
4339 mutex_unlock(&ftrace_lock);
4340}
4341
4342static void *fpid_start(struct seq_file *m, loff_t *pos)
4343{
4344 mutex_lock(&ftrace_lock);
4345
4346 if (list_empty(&ftrace_pids) && (!*pos))
4347 return (void *) 1;
4348
4349 return seq_list_start(&ftrace_pids, *pos);
4350}
4351
4352static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4353{
4354 if (v == (void *)1)
4355 return NULL;
4356
4357 return seq_list_next(v, &ftrace_pids, pos);
4358}
4359
4360static void fpid_stop(struct seq_file *m, void *p)
4361{
4362 mutex_unlock(&ftrace_lock);
4363}
4364
4365static int fpid_show(struct seq_file *m, void *v)
4366{
4367 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4368
4369 if (v == (void *)1) {
4370 seq_printf(m, "no pid\n");
4371 return 0;
4372 }
4373
4374 if (fpid->pid == ftrace_swapper_pid)
4375 seq_printf(m, "swapper tasks\n");
4376 else
4377 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4378
4379 return 0;
4380}
4381
4382static const struct seq_operations ftrace_pid_sops = {
4383 .start = fpid_start,
4384 .next = fpid_next,
4385 .stop = fpid_stop,
4386 .show = fpid_show,
4387};
4388
4389static int
4390ftrace_pid_open(struct inode *inode, struct file *file)
4391{
4392 int ret = 0;
4393
4394 if ((file->f_mode & FMODE_WRITE) &&
4395 (file->f_flags & O_TRUNC))
4396 ftrace_pid_reset();
4397
4398 if (file->f_mode & FMODE_READ)
4399 ret = seq_open(file, &ftrace_pid_sops);
4400
4401 return ret;
4402}
4403
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004404static ssize_t
4405ftrace_pid_write(struct file *filp, const char __user *ubuf,
4406 size_t cnt, loff_t *ppos)
4407{
Ingo Molnar457dc922009-11-23 11:03:28 +01004408 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004409 long val;
4410 int ret;
4411
4412 if (cnt >= sizeof(buf))
4413 return -EINVAL;
4414
4415 if (copy_from_user(&buf, ubuf, cnt))
4416 return -EFAULT;
4417
4418 buf[cnt] = 0;
4419
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004420 /*
4421 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4422 * to clean the filter quietly.
4423 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004424 tmp = strstrip(buf);
4425 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004426 return 1;
4427
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004428 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004429 if (ret < 0)
4430 return ret;
4431
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004432 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004433
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004434 return ret ? ret : cnt;
4435}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004436
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004437static int
4438ftrace_pid_release(struct inode *inode, struct file *file)
4439{
4440 if (file->f_mode & FMODE_READ)
4441 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004442
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004443 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004444}
4445
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004446static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004447 .open = ftrace_pid_open,
4448 .write = ftrace_pid_write,
4449 .read = seq_read,
4450 .llseek = seq_lseek,
4451 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004452};
4453
4454static __init int ftrace_init_debugfs(void)
4455{
4456 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004457
4458 d_tracer = tracing_init_dentry();
4459 if (!d_tracer)
4460 return 0;
4461
4462 ftrace_init_dyn_debugfs(d_tracer);
4463
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004464 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4465 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004466
4467 ftrace_profile_debugfs(d_tracer);
4468
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004469 return 0;
4470}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004471fs_initcall(ftrace_init_debugfs);
4472
Steven Rostedt3d083392008-05-12 21:20:42 +02004473/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004474 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004475 *
4476 * This function should be used by panic code. It stops ftrace
4477 * but in a not so nice way. If you need to simply kill ftrace
4478 * from a non-atomic section, use ftrace_kill.
4479 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004480void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004481{
4482 ftrace_disabled = 1;
4483 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004484 clear_ftrace_function();
4485}
4486
4487/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004488 * Test if ftrace is dead or not.
4489 */
4490int ftrace_is_dead(void)
4491{
4492 return ftrace_disabled;
4493}
4494
4495/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004496 * register_ftrace_function - register a function for profiling
4497 * @ops - ops structure that holds the function for profiling.
4498 *
4499 * Register a function to be called by all functions in the
4500 * kernel.
4501 *
4502 * Note: @ops->func and all the functions it calls must be labeled
4503 * with "notrace", otherwise it will go into a
4504 * recursive loop.
4505 */
4506int register_ftrace_function(struct ftrace_ops *ops)
4507{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004508 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004509
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004510 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004511
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004512 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004513 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004514 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004515
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004516 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004517
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004518 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004519}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004520EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004521
4522/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004523 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004524 * @ops - ops structure that holds the function to unregister
4525 *
4526 * Unregister a function that was added to be called by ftrace profiling.
4527 */
4528int unregister_ftrace_function(struct ftrace_ops *ops)
4529{
4530 int ret;
4531
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004532 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004533 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004534 if (!ret)
4535 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004536 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004537
4538 return ret;
4539}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004540EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004541
Ingo Molnare309b412008-05-12 21:20:51 +02004542int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004543ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004544 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004545 loff_t *ppos)
4546{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004547 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004548
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004549 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004550
Steven Rostedt45a4a232011-04-21 23:16:46 -04004551 if (unlikely(ftrace_disabled))
4552 goto out;
4553
4554 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004555
Li Zefana32c7762009-06-26 16:55:51 +08004556 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004557 goto out;
4558
Li Zefana32c7762009-06-26 16:55:51 +08004559 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004560
4561 if (ftrace_enabled) {
4562
4563 ftrace_startup_sysctl();
4564
4565 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004566 if (ftrace_ops_list != &ftrace_list_end) {
4567 if (ftrace_ops_list->next == &ftrace_list_end)
4568 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004569 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004570 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004571 }
4572
4573 } else {
4574 /* stopping ftrace calls (just send to ftrace_stub) */
4575 ftrace_trace_function = ftrace_stub;
4576
4577 ftrace_shutdown_sysctl();
4578 }
4579
4580 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004581 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004582 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004583}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004584
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004585#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004586
Steven Rostedt597af812009-04-03 15:24:12 -04004587static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004588static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004589
Steven Rostedte49dc192008-12-02 23:50:05 -05004590int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4591{
4592 return 0;
4593}
4594
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004595/* The callbacks that hook a function */
4596trace_func_graph_ret_t ftrace_graph_return =
4597 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004598trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004599
4600/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4601static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4602{
4603 int i;
4604 int ret = 0;
4605 unsigned long flags;
4606 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4607 struct task_struct *g, *t;
4608
4609 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4610 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4611 * sizeof(struct ftrace_ret_stack),
4612 GFP_KERNEL);
4613 if (!ret_stack_list[i]) {
4614 start = 0;
4615 end = i;
4616 ret = -ENOMEM;
4617 goto free;
4618 }
4619 }
4620
4621 read_lock_irqsave(&tasklist_lock, flags);
4622 do_each_thread(g, t) {
4623 if (start == end) {
4624 ret = -EAGAIN;
4625 goto unlock;
4626 }
4627
4628 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004629 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004630 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004631 t->curr_ret_stack = -1;
4632 /* Make sure the tasks see the -1 first: */
4633 smp_wmb();
4634 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004635 }
4636 } while_each_thread(g, t);
4637
4638unlock:
4639 read_unlock_irqrestore(&tasklist_lock, flags);
4640free:
4641 for (i = start; i < end; i++)
4642 kfree(ret_stack_list[i]);
4643 return ret;
4644}
4645
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004646static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004647ftrace_graph_probe_sched_switch(void *ignore,
4648 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004649{
4650 unsigned long long timestamp;
4651 int index;
4652
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004653 /*
4654 * Does the user want to count the time a function was asleep.
4655 * If so, do not update the time stamps.
4656 */
4657 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4658 return;
4659
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004660 timestamp = trace_clock_local();
4661
4662 prev->ftrace_timestamp = timestamp;
4663
4664 /* only process tasks that we timestamped */
4665 if (!next->ftrace_timestamp)
4666 return;
4667
4668 /*
4669 * Update all the counters in next to make up for the
4670 * time next was sleeping.
4671 */
4672 timestamp -= next->ftrace_timestamp;
4673
4674 for (index = next->curr_ret_stack; index >= 0; index--)
4675 next->ret_stack[index].calltime += timestamp;
4676}
4677
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004678/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004679static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004680{
4681 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004682 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004683
4684 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4685 sizeof(struct ftrace_ret_stack *),
4686 GFP_KERNEL);
4687
4688 if (!ret_stack_list)
4689 return -ENOMEM;
4690
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004691 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004692 for_each_online_cpu(cpu) {
4693 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004694 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004695 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004696
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004697 do {
4698 ret = alloc_retstack_tasklist(ret_stack_list);
4699 } while (ret == -EAGAIN);
4700
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004701 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004702 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004703 if (ret)
4704 pr_info("ftrace_graph: Couldn't activate tracepoint"
4705 " probe to kernel_sched_switch\n");
4706 }
4707
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004708 kfree(ret_stack_list);
4709 return ret;
4710}
4711
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004712/*
4713 * Hibernation protection.
4714 * The state of the current task is too much unstable during
4715 * suspend/restore to disk. We want to protect against that.
4716 */
4717static int
4718ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4719 void *unused)
4720{
4721 switch (state) {
4722 case PM_HIBERNATION_PREPARE:
4723 pause_graph_tracing();
4724 break;
4725
4726 case PM_POST_HIBERNATION:
4727 unpause_graph_tracing();
4728 break;
4729 }
4730 return NOTIFY_DONE;
4731}
4732
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004733int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4734 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004735{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004736 int ret = 0;
4737
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004738 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004739
Steven Rostedt05ce5812009-03-24 00:18:31 -04004740 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004741 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004742 ret = -EBUSY;
4743 goto out;
4744 }
4745
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004746 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4747 register_pm_notifier(&ftrace_suspend_notifier);
4748
Steven Rostedt597af812009-04-03 15:24:12 -04004749 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004750 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004751 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004752 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004753 goto out;
4754 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004755
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004756 ftrace_graph_return = retfunc;
4757 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004758
Steven Rostedta1cd6172011-05-23 15:24:25 -04004759 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004760
4761out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004762 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004763 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004764}
4765
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004766void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004767{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004768 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004769
Steven Rostedt597af812009-04-03 15:24:12 -04004770 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004771 goto out;
4772
Steven Rostedt597af812009-04-03 15:24:12 -04004773 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004774 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004775 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004776 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004777 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004778 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004779
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004780 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004781 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004782}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004783
Steven Rostedt868baf02011-02-10 21:26:13 -05004784static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4785
4786static void
4787graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4788{
4789 atomic_set(&t->tracing_graph_pause, 0);
4790 atomic_set(&t->trace_overrun, 0);
4791 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004792 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004793 smp_wmb();
4794 t->ret_stack = ret_stack;
4795}
4796
4797/*
4798 * Allocate a return stack for the idle task. May be the first
4799 * time through, or it may be done by CPU hotplug online.
4800 */
4801void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4802{
4803 t->curr_ret_stack = -1;
4804 /*
4805 * The idle task has no parent, it either has its own
4806 * stack or no stack at all.
4807 */
4808 if (t->ret_stack)
4809 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4810
4811 if (ftrace_graph_active) {
4812 struct ftrace_ret_stack *ret_stack;
4813
4814 ret_stack = per_cpu(idle_ret_stack, cpu);
4815 if (!ret_stack) {
4816 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4817 * sizeof(struct ftrace_ret_stack),
4818 GFP_KERNEL);
4819 if (!ret_stack)
4820 return;
4821 per_cpu(idle_ret_stack, cpu) = ret_stack;
4822 }
4823 graph_init_task(t, ret_stack);
4824 }
4825}
4826
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004827/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004828void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004829{
Steven Rostedt84047e32009-06-02 16:51:55 -04004830 /* Make sure we do not use the parent ret_stack */
4831 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004832 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004833
Steven Rostedt597af812009-04-03 15:24:12 -04004834 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004835 struct ftrace_ret_stack *ret_stack;
4836
4837 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004838 * sizeof(struct ftrace_ret_stack),
4839 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004840 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004841 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004842 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004843 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004844}
4845
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004846void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004847{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004848 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4849
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004850 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004851 /* NULL must become visible to IRQs before we free it: */
4852 barrier();
4853
4854 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004855}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004856
4857void ftrace_graph_stop(void)
4858{
4859 ftrace_stop();
4860}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004861#endif