blob: 1d553d03751f0ec78e2144ab58e8e5e08db8dc80 [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
13 * Copyright (C) 2004 William Lee Irwin III
14 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Jiri Olsae2484912012-02-15 15:51:48 +010065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
Steven Rostedt4eebcc82008-05-12 21:20:48 +020067/* ftrace_enabled is a method to turn ftrace on or off */
68int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020069static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020070
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050071/* Quick disabling of function tracer. */
72int function_trace_stop;
73
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040074/* List for set_ftrace_pid's pids. */
75LIST_HEAD(ftrace_pids);
76struct ftrace_pid {
77 struct list_head list;
78 struct pid *pid;
79};
80
Steven Rostedt4eebcc82008-05-12 21:20:48 +020081/*
82 * ftrace_disabled is set when an anomaly is discovered.
83 * ftrace_disabled is much stronger than ftrace_enabled.
84 */
85static int ftrace_disabled __read_mostly;
86
Steven Rostedt52baf112009-02-14 01:15:39 -050087static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020088
Paul McQuadebd38c0e2011-05-31 20:51:55 +010089static struct ftrace_ops ftrace_list_end __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -040090 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020091};
92
Steven Rostedtb8489142011-05-04 09:27:52 -040093static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
Jiri Olsae2484912012-02-15 15:51:48 +010094static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -040095static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020096ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt6331c282011-07-13 15:11:02 -040097static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050098ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050099ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400100static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100101static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200102
Steven Rostedtb8489142011-05-04 09:27:52 -0400103static void
104ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
105
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800106/*
Steven Rostedtb8489142011-05-04 09:27:52 -0400107 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800108 * can use rcu_dereference_raw() is that elements removed from this list
109 * are simply leaked, so there is no need to interact with a grace-period
110 * mechanism. The rcu_dereference_raw() calls are needed to handle
Steven Rostedtb8489142011-05-04 09:27:52 -0400111 * concurrent insertions into the ftrace_global_list.
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800112 *
113 * Silly Alpha and silly pointer-speculation compiler optimizations!
114 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400115static void ftrace_global_list_func(unsigned long ip,
116 unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200117{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400118 struct ftrace_ops *op;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200119
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400120 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
121 return;
122
123 trace_recursion_set(TRACE_GLOBAL_BIT);
124 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200125 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200126 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800127 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200128 };
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400129 trace_recursion_clear(TRACE_GLOBAL_BIT);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200130}
131
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500132static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
133{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500134 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500135 return;
136
137 ftrace_pid_function(ip, parent_ip);
138}
139
140static void set_ftrace_pid_function(ftrace_func_t func)
141{
142 /* do not set ftrace_pid_function to itself! */
143 if (func != ftrace_pid_func)
144 ftrace_pid_function = func;
145}
146
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200147/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200148 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200149 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200150 * This NULLs the ftrace function and in essence stops
151 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200152 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200153void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200154{
Steven Rostedt3d083392008-05-12 21:20:42 +0200155 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500156 __ftrace_trace_function = ftrace_stub;
Steven Rostedt6331c282011-07-13 15:11:02 -0400157 __ftrace_trace_function_delay = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500158 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200159}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200160
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500161#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
162/*
163 * For those archs that do not test ftrace_trace_stop in their
164 * mcount call site, we need to do it from C.
165 */
166static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
167{
168 if (function_trace_stop)
169 return;
170
171 __ftrace_trace_function(ip, parent_ip);
172}
173#endif
174
Jiri Olsae2484912012-02-15 15:51:48 +0100175static void control_ops_disable_all(struct ftrace_ops *ops)
176{
177 int cpu;
178
179 for_each_possible_cpu(cpu)
180 *per_cpu_ptr(ops->disabled, cpu) = 1;
181}
182
183static int control_ops_alloc(struct ftrace_ops *ops)
184{
185 int __percpu *disabled;
186
187 disabled = alloc_percpu(int);
188 if (!disabled)
189 return -ENOMEM;
190
191 ops->disabled = disabled;
192 control_ops_disable_all(ops);
193 return 0;
194}
195
196static void control_ops_free(struct ftrace_ops *ops)
197{
198 free_percpu(ops->disabled);
199}
200
Steven Rostedt2b499382011-05-03 22:49:52 -0400201static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400202{
203 ftrace_func_t func;
204
205 /*
206 * If there's only one function registered, then call that
207 * function directly. Otherwise, we need to iterate over the
208 * registered callers.
209 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400210 if (ftrace_global_list == &ftrace_list_end ||
211 ftrace_global_list->next == &ftrace_list_end)
212 func = ftrace_global_list->func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400213 else
Steven Rostedtb8489142011-05-04 09:27:52 -0400214 func = ftrace_global_list_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400215
216 /* If we filter on pids, update to use the pid function */
217 if (!list_empty(&ftrace_pids)) {
218 set_ftrace_pid_function(func);
219 func = ftrace_pid_func;
220 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400221
222 global_ops.func = func;
223}
224
Steven Rostedt95bcd162014-02-11 14:49:07 -0500225static void ftrace_sync(struct work_struct *work)
226{
227 /*
228 * This function is just a stub to implement a hard force
229 * of synchronize_sched(). This requires synchronizing
230 * tasks even in userspace and idle.
231 *
232 * Yes, function tracing is rude.
233 */
234}
235
236static void ftrace_sync_ipi(void *data)
237{
238 /* Probably not needed, but do it anyway */
239 smp_rmb();
240}
241
Steven Rostedt2b499382011-05-03 22:49:52 -0400242static void update_ftrace_function(void)
243{
244 ftrace_func_t func;
245
246 update_global_ops();
247
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400248 /*
249 * If we are at the end of the list and this ops is
250 * not dynamic, then have the mcount trampoline call
251 * the function directly
252 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400253 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400254 (ftrace_ops_list->next == &ftrace_list_end &&
255 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
Steven Rostedtb8489142011-05-04 09:27:52 -0400256 func = ftrace_ops_list->func;
257 else
258 func = ftrace_ops_list_func;
Steven Rostedt2b499382011-05-03 22:49:52 -0400259
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400260#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
261 ftrace_trace_function = func;
262#else
Steven Rostedt6331c282011-07-13 15:11:02 -0400263#ifdef CONFIG_DYNAMIC_FTRACE
264 /* do not update till all functions have been modified */
265 __ftrace_trace_function_delay = func;
266#else
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400267 __ftrace_trace_function = func;
Steven Rostedt6331c282011-07-13 15:11:02 -0400268#endif
Rajesh Bhagatdb6544e2012-02-17 13:59:15 +0530269 ftrace_trace_function =
270 (func == ftrace_stub) ? func : ftrace_test_stop_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400271#endif
272}
273
Steven Rostedt2b499382011-05-03 22:49:52 -0400274static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200275{
Steven Rostedt2b499382011-05-03 22:49:52 -0400276 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200277 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400278 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200279 * CPU might be walking that list. We need to make sure
280 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400281 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200282 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400283 rcu_assign_pointer(*list, ops);
284}
Steven Rostedt3d083392008-05-12 21:20:42 +0200285
Steven Rostedt2b499382011-05-03 22:49:52 -0400286static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
287{
288 struct ftrace_ops **p;
289
290 /*
291 * If we are removing the last function, then simply point
292 * to the ftrace_stub.
293 */
294 if (*list == ops && ops->next == &ftrace_list_end) {
295 *list = &ftrace_list_end;
296 return 0;
297 }
298
299 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
300 if (*p == ops)
301 break;
302
303 if (*p != ops)
304 return -1;
305
306 *p = (*p)->next;
307 return 0;
308}
309
Jiri Olsae2484912012-02-15 15:51:48 +0100310static void add_ftrace_list_ops(struct ftrace_ops **list,
311 struct ftrace_ops *main_ops,
312 struct ftrace_ops *ops)
313{
314 int first = *list == &ftrace_list_end;
315 add_ftrace_ops(list, ops);
316 if (first)
317 add_ftrace_ops(&ftrace_ops_list, main_ops);
318}
319
320static int remove_ftrace_list_ops(struct ftrace_ops **list,
321 struct ftrace_ops *main_ops,
322 struct ftrace_ops *ops)
323{
324 int ret = remove_ftrace_ops(list, ops);
325 if (!ret && *list == &ftrace_list_end)
326 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
327 return ret;
328}
329
Steven Rostedt2b499382011-05-03 22:49:52 -0400330static int __register_ftrace_function(struct ftrace_ops *ops)
331{
Steven Rostedt2b499382011-05-03 22:49:52 -0400332 if (FTRACE_WARN_ON(ops == &global_ops))
333 return -EINVAL;
334
Steven Rostedtb8489142011-05-04 09:27:52 -0400335 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
336 return -EBUSY;
337
Jiri Olsae2484912012-02-15 15:51:48 +0100338 /* We don't support both control and global flags set. */
339 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
340 return -EINVAL;
341
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400342 if (!core_kernel_data((unsigned long)ops))
343 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
344
Steven Rostedtb8489142011-05-04 09:27:52 -0400345 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100346 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400347 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100348 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
349 if (control_ops_alloc(ops))
350 return -ENOMEM;
351 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400352 } else
353 add_ftrace_ops(&ftrace_ops_list, ops);
354
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400355 if (ftrace_enabled)
356 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200357
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200358 return 0;
359}
360
Ingo Molnare309b412008-05-12 21:20:51 +0200361static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200362{
Steven Rostedt2b499382011-05-03 22:49:52 -0400363 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200364
Steven Rostedtb8489142011-05-04 09:27:52 -0400365 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
366 return -EBUSY;
367
Steven Rostedt2b499382011-05-03 22:49:52 -0400368 if (FTRACE_WARN_ON(ops == &global_ops))
369 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200370
Steven Rostedtb8489142011-05-04 09:27:52 -0400371 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100372 ret = remove_ftrace_list_ops(&ftrace_global_list,
373 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400374 if (!ret)
375 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100376 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
377 ret = remove_ftrace_list_ops(&ftrace_control_list,
378 &control_ops, ops);
379 if (!ret) {
380 /*
381 * The ftrace_ops is now removed from the list,
382 * so there'll be no new users. We must ensure
383 * all current users are done before we free
384 * the control data.
385 */
386 synchronize_sched();
387 control_ops_free(ops);
388 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400389 } else
390 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
391
Steven Rostedt2b499382011-05-03 22:49:52 -0400392 if (ret < 0)
393 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400394
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400395 if (ftrace_enabled)
396 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200397
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400398 /*
399 * Dynamic ops may be freed, we must make sure that all
400 * callers are done before leaving this function.
401 */
402 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
403 synchronize_sched();
404
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500405 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200406}
407
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500408static void ftrace_update_pid_func(void)
409{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400410 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500411 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900412 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500413
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400414 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500415}
416
Steven Rostedt493762f2009-03-23 17:12:36 -0400417#ifdef CONFIG_FUNCTION_PROFILER
418struct ftrace_profile {
419 struct hlist_node node;
420 unsigned long ip;
421 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400422#ifdef CONFIG_FUNCTION_GRAPH_TRACER
423 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400424 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400425#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400426};
427
428struct ftrace_profile_page {
429 struct ftrace_profile_page *next;
430 unsigned long index;
431 struct ftrace_profile records[];
432};
433
Steven Rostedtcafb1682009-03-24 20:50:39 -0400434struct ftrace_profile_stat {
435 atomic_t disabled;
436 struct hlist_head *hash;
437 struct ftrace_profile_page *pages;
438 struct ftrace_profile_page *start;
439 struct tracer_stat stat;
440};
441
Steven Rostedt493762f2009-03-23 17:12:36 -0400442#define PROFILE_RECORDS_SIZE \
443 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
444
445#define PROFILES_PER_PAGE \
446 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
447
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400448static int ftrace_profile_bits __read_mostly;
449static int ftrace_profile_enabled __read_mostly;
450
451/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400452static DEFINE_MUTEX(ftrace_profile_lock);
453
Steven Rostedtcafb1682009-03-24 20:50:39 -0400454static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400455
456#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
457
Steven Rostedt493762f2009-03-23 17:12:36 -0400458static void *
459function_stat_next(void *v, int idx)
460{
461 struct ftrace_profile *rec = v;
462 struct ftrace_profile_page *pg;
463
464 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
465
466 again:
Li Zefan0296e422009-06-26 11:15:37 +0800467 if (idx != 0)
468 rec++;
469
Steven Rostedt493762f2009-03-23 17:12:36 -0400470 if ((void *)rec >= (void *)&pg->records[pg->index]) {
471 pg = pg->next;
472 if (!pg)
473 return NULL;
474 rec = &pg->records[0];
475 if (!rec->counter)
476 goto again;
477 }
478
479 return rec;
480}
481
482static void *function_stat_start(struct tracer_stat *trace)
483{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400484 struct ftrace_profile_stat *stat =
485 container_of(trace, struct ftrace_profile_stat, stat);
486
487 if (!stat || !stat->start)
488 return NULL;
489
490 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400491}
492
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400493#ifdef CONFIG_FUNCTION_GRAPH_TRACER
494/* function graph compares on total time */
495static int function_stat_cmp(void *p1, void *p2)
496{
497 struct ftrace_profile *a = p1;
498 struct ftrace_profile *b = p2;
499
500 if (a->time < b->time)
501 return -1;
502 if (a->time > b->time)
503 return 1;
504 else
505 return 0;
506}
507#else
508/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400509static int function_stat_cmp(void *p1, void *p2)
510{
511 struct ftrace_profile *a = p1;
512 struct ftrace_profile *b = p2;
513
514 if (a->counter < b->counter)
515 return -1;
516 if (a->counter > b->counter)
517 return 1;
518 else
519 return 0;
520}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400521#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400522
523static int function_stat_headers(struct seq_file *m)
524{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400525#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400526 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400527 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400528 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400529 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400530#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400531 seq_printf(m, " Function Hit\n"
532 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400533#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400534 return 0;
535}
536
537static int function_stat_show(struct seq_file *m, void *v)
538{
539 struct ftrace_profile *rec = v;
540 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800541 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400542#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400543 static struct trace_seq s;
544 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400545 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400546#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800547 mutex_lock(&ftrace_profile_lock);
548
549 /* we raced with function_profile_reset() */
550 if (unlikely(rec->counter == 0)) {
551 ret = -EBUSY;
552 goto out;
553 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400554
555 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400556 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400557
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400558#ifdef CONFIG_FUNCTION_GRAPH_TRACER
559 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400560 avg = rec->time;
561 do_div(avg, rec->counter);
562
Chase Douglase330b3b2010-04-26 14:02:05 -0400563 /* Sample standard deviation (s^2) */
564 if (rec->counter <= 1)
565 stddev = 0;
566 else {
567 stddev = rec->time_squared - rec->counter * avg * avg;
568 /*
569 * Divide only 1000 for ns^2 -> us^2 conversion.
570 * trace_print_graph_duration will divide 1000 again.
571 */
572 do_div(stddev, (rec->counter - 1) * 1000);
573 }
574
Steven Rostedt34886c82009-03-25 21:00:47 -0400575 trace_seq_init(&s);
576 trace_print_graph_duration(rec->time, &s);
577 trace_seq_puts(&s, " ");
578 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400579 trace_seq_puts(&s, " ");
580 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400581 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400582#endif
583 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800584out:
585 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400586
Li Zefan3aaba202010-08-23 16:50:12 +0800587 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400588}
589
Steven Rostedtcafb1682009-03-24 20:50:39 -0400590static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400591{
592 struct ftrace_profile_page *pg;
593
Steven Rostedtcafb1682009-03-24 20:50:39 -0400594 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400595
596 while (pg) {
597 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
598 pg->index = 0;
599 pg = pg->next;
600 }
601
Steven Rostedtcafb1682009-03-24 20:50:39 -0400602 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400603 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
604}
605
Steven Rostedtcafb1682009-03-24 20:50:39 -0400606int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400607{
608 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400609 int functions;
610 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400611 int i;
612
613 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400614 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400615 return 0;
616
Steven Rostedtcafb1682009-03-24 20:50:39 -0400617 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
618 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400619 return -ENOMEM;
620
Steven Rostedt318e0a72009-03-25 20:06:34 -0400621#ifdef CONFIG_DYNAMIC_FTRACE
622 functions = ftrace_update_tot_cnt;
623#else
624 /*
625 * We do not know the number of functions that exist because
626 * dynamic tracing is what counts them. With past experience
627 * we have around 20K functions. That should be more than enough.
628 * It is highly unlikely we will execute every function in
629 * the kernel.
630 */
631 functions = 20000;
632#endif
633
Steven Rostedtcafb1682009-03-24 20:50:39 -0400634 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400635
Steven Rostedt318e0a72009-03-25 20:06:34 -0400636 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
637
Namhyung Kim226c8ea2013-04-01 21:46:24 +0900638 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400639 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400640 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400641 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400642 pg = pg->next;
643 }
644
645 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400646
647 out_free:
648 pg = stat->start;
649 while (pg) {
650 unsigned long tmp = (unsigned long)pg;
651
652 pg = pg->next;
653 free_page(tmp);
654 }
655
Steven Rostedt318e0a72009-03-25 20:06:34 -0400656 stat->pages = NULL;
657 stat->start = NULL;
658
659 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400660}
661
Steven Rostedtcafb1682009-03-24 20:50:39 -0400662static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400663{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400664 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400665 int size;
666
Steven Rostedtcafb1682009-03-24 20:50:39 -0400667 stat = &per_cpu(ftrace_profile_stats, cpu);
668
669 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400670 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400671 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400672 return 0;
673 }
674
675 /*
676 * We are profiling all functions, but usually only a few thousand
677 * functions are hit. We'll make a hash of 1024 items.
678 */
679 size = FTRACE_PROFILE_HASH_SIZE;
680
Steven Rostedtcafb1682009-03-24 20:50:39 -0400681 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400682
Steven Rostedtcafb1682009-03-24 20:50:39 -0400683 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400684 return -ENOMEM;
685
Steven Rostedtcafb1682009-03-24 20:50:39 -0400686 if (!ftrace_profile_bits) {
687 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400688
Steven Rostedtcafb1682009-03-24 20:50:39 -0400689 for (; size; size >>= 1)
690 ftrace_profile_bits++;
691 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400692
Steven Rostedt318e0a72009-03-25 20:06:34 -0400693 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400694 if (ftrace_profile_pages_init(stat) < 0) {
695 kfree(stat->hash);
696 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400697 return -ENOMEM;
698 }
699
700 return 0;
701}
702
Steven Rostedtcafb1682009-03-24 20:50:39 -0400703static int ftrace_profile_init(void)
704{
705 int cpu;
706 int ret = 0;
707
Miao Xie09951c92013-12-16 15:20:01 +0800708 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400709 ret = ftrace_profile_init_cpu(cpu);
710 if (ret)
711 break;
712 }
713
714 return ret;
715}
716
Steven Rostedt493762f2009-03-23 17:12:36 -0400717/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400718static struct ftrace_profile *
719ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400720{
721 struct ftrace_profile *rec;
722 struct hlist_head *hhd;
723 struct hlist_node *n;
724 unsigned long key;
725
726 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400727 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400728
729 if (hlist_empty(hhd))
730 return NULL;
731
732 hlist_for_each_entry_rcu(rec, n, hhd, node) {
733 if (rec->ip == ip)
734 return rec;
735 }
736
737 return NULL;
738}
739
Steven Rostedtcafb1682009-03-24 20:50:39 -0400740static void ftrace_add_profile(struct ftrace_profile_stat *stat,
741 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400742{
743 unsigned long key;
744
745 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400746 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400747}
748
Steven Rostedt318e0a72009-03-25 20:06:34 -0400749/*
750 * The memory is already allocated, this simply finds a new record to use.
751 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400752static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400753ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400754{
755 struct ftrace_profile *rec = NULL;
756
Steven Rostedt318e0a72009-03-25 20:06:34 -0400757 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400758 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400759 goto out;
760
Steven Rostedt493762f2009-03-23 17:12:36 -0400761 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400762 * Try to find the function again since an NMI
763 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400764 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400765 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400766 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400767 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400768
Steven Rostedtcafb1682009-03-24 20:50:39 -0400769 if (stat->pages->index == PROFILES_PER_PAGE) {
770 if (!stat->pages->next)
771 goto out;
772 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400773 }
774
Steven Rostedtcafb1682009-03-24 20:50:39 -0400775 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400776 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400777 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400778
Steven Rostedt493762f2009-03-23 17:12:36 -0400779 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400780 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400781
782 return rec;
783}
784
Steven Rostedt493762f2009-03-23 17:12:36 -0400785static void
786function_profile_call(unsigned long ip, unsigned long parent_ip)
787{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400788 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400789 struct ftrace_profile *rec;
790 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400791
792 if (!ftrace_profile_enabled)
793 return;
794
Steven Rostedt493762f2009-03-23 17:12:36 -0400795 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400796
797 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400798 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400799 goto out;
800
801 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400802 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400803 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400804 if (!rec)
805 goto out;
806 }
807
808 rec->counter++;
809 out:
810 local_irq_restore(flags);
811}
812
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400813#ifdef CONFIG_FUNCTION_GRAPH_TRACER
814static int profile_graph_entry(struct ftrace_graph_ent *trace)
815{
816 function_profile_call(trace->func, 0);
817 return 1;
818}
819
820static void profile_graph_return(struct ftrace_graph_ret *trace)
821{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400822 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400823 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400824 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400825 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400826
827 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400828 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400829 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400830 goto out;
831
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400832 /* If the calltime was zero'd ignore it */
833 if (!trace->calltime)
834 goto out;
835
Steven Rostedta2a16d62009-03-24 23:17:58 -0400836 calltime = trace->rettime - trace->calltime;
837
838 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
839 int index;
840
841 index = trace->depth;
842
843 /* Append this call time to the parent time to subtract */
844 if (index)
845 current->ret_stack[index - 1].subtime += calltime;
846
847 if (current->ret_stack[index].subtime < calltime)
848 calltime -= current->ret_stack[index].subtime;
849 else
850 calltime = 0;
851 }
852
Steven Rostedtcafb1682009-03-24 20:50:39 -0400853 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400854 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400855 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400856 rec->time_squared += calltime * calltime;
857 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400858
Steven Rostedtcafb1682009-03-24 20:50:39 -0400859 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400860 local_irq_restore(flags);
861}
862
863static int register_ftrace_profiler(void)
864{
865 return register_ftrace_graph(&profile_graph_return,
866 &profile_graph_entry);
867}
868
869static void unregister_ftrace_profiler(void)
870{
871 unregister_ftrace_graph();
872}
873#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100874static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400875 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400876};
877
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400878static int register_ftrace_profiler(void)
879{
880 return register_ftrace_function(&ftrace_profile_ops);
881}
882
883static void unregister_ftrace_profiler(void)
884{
885 unregister_ftrace_function(&ftrace_profile_ops);
886}
887#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
888
Steven Rostedt493762f2009-03-23 17:12:36 -0400889static ssize_t
890ftrace_profile_write(struct file *filp, const char __user *ubuf,
891 size_t cnt, loff_t *ppos)
892{
893 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400894 int ret;
895
Peter Huewe22fe9b52011-06-07 21:58:27 +0200896 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
897 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400898 return ret;
899
900 val = !!val;
901
902 mutex_lock(&ftrace_profile_lock);
903 if (ftrace_profile_enabled ^ val) {
904 if (val) {
905 ret = ftrace_profile_init();
906 if (ret < 0) {
907 cnt = ret;
908 goto out;
909 }
910
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400911 ret = register_ftrace_profiler();
912 if (ret < 0) {
913 cnt = ret;
914 goto out;
915 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400916 ftrace_profile_enabled = 1;
917 } else {
918 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400919 /*
920 * unregister_ftrace_profiler calls stop_machine
921 * so this acts like an synchronize_sched.
922 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400923 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400924 }
925 }
926 out:
927 mutex_unlock(&ftrace_profile_lock);
928
Jiri Olsacf8517c2009-10-23 19:36:16 -0400929 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400930
931 return cnt;
932}
933
934static ssize_t
935ftrace_profile_read(struct file *filp, char __user *ubuf,
936 size_t cnt, loff_t *ppos)
937{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400938 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400939 int r;
940
941 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
942 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
943}
944
945static const struct file_operations ftrace_profile_fops = {
946 .open = tracing_open_generic,
947 .read = ftrace_profile_read,
948 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200949 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400950};
951
Steven Rostedtcafb1682009-03-24 20:50:39 -0400952/* used to initialize the real stat files */
953static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400954 .name = "functions",
955 .stat_start = function_stat_start,
956 .stat_next = function_stat_next,
957 .stat_cmp = function_stat_cmp,
958 .stat_headers = function_stat_headers,
959 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400960};
961
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400962static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400963{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400964 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400965 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400966 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400967 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400968 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400969
Steven Rostedtcafb1682009-03-24 20:50:39 -0400970 for_each_possible_cpu(cpu) {
971 stat = &per_cpu(ftrace_profile_stats, cpu);
972
973 /* allocate enough for function name + cpu number */
974 name = kmalloc(32, GFP_KERNEL);
975 if (!name) {
976 /*
977 * The files created are permanent, if something happens
978 * we still do not free memory.
979 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400980 WARN(1,
981 "Could not allocate stat file for cpu %d\n",
982 cpu);
983 return;
984 }
985 stat->stat = function_stats;
986 snprintf(name, 32, "function%d", cpu);
987 stat->stat.name = name;
988 ret = register_stat_tracer(&stat->stat);
989 if (ret) {
990 WARN(1,
991 "Could not register function stat for cpu %d\n",
992 cpu);
993 kfree(name);
994 return;
995 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400996 }
997
998 entry = debugfs_create_file("function_profile_enabled", 0644,
999 d_tracer, NULL, &ftrace_profile_fops);
1000 if (!entry)
1001 pr_warning("Could not create debugfs "
1002 "'function_profile_enabled' entry\n");
1003}
1004
1005#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001006static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001007{
1008}
1009#endif /* CONFIG_FUNCTION_PROFILER */
1010
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001011static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1012
Steven Rostedtbc4d36c2013-06-07 17:02:08 +08001013loff_t
1014ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1015{
1016 loff_t ret;
1017
1018 if (file->f_mode & FMODE_READ)
1019 ret = seq_lseek(file, offset, whence);
1020 else
1021 file->f_pos = ret = 1;
1022
1023 return ret;
1024}
1025
Steven Rostedt3d083392008-05-12 21:20:42 +02001026#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001027
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001028#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001029# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001030#endif
1031
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001032static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1033
Steven Rostedtb6887d72009-02-17 12:32:04 -05001034struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001035 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001036 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001037 unsigned long flags;
1038 unsigned long ip;
1039 void *data;
1040 struct rcu_head rcu;
1041};
1042
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001043struct ftrace_func_entry {
1044 struct hlist_node hlist;
1045 unsigned long ip;
1046};
1047
1048struct ftrace_hash {
1049 unsigned long size_bits;
1050 struct hlist_head *buckets;
1051 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001052 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001053};
1054
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001055/*
1056 * We make these constant because no one should touch them,
1057 * but they are used as the default "empty hash", to avoid allocating
1058 * it all the time. These are in a read only section such that if
1059 * anyone does try to modify it, it will cause an exception.
1060 */
1061static const struct hlist_head empty_buckets[1];
1062static const struct ftrace_hash empty_hash = {
1063 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001064};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001065#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001066
Steven Rostedt2b499382011-05-03 22:49:52 -04001067static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001068 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001069 .notrace_hash = EMPTY_HASH,
1070 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001071};
1072
Steven Rostedt41c52c02008-05-22 11:46:33 -04001073static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001074
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001075struct ftrace_page {
1076 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001077 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001078 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001079 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001080};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001081
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001082static struct ftrace_page *ftrace_new_pgs;
1083
Steven Rostedta7900872011-12-16 16:23:44 -05001084#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1085#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001086
1087/* estimate from running different kernels */
1088#define NR_TO_INIT 10000
1089
1090static struct ftrace_page *ftrace_pages_start;
1091static struct ftrace_page *ftrace_pages;
1092
Steven Rostedt06a51d92011-12-19 19:07:36 -05001093static bool ftrace_hash_empty(struct ftrace_hash *hash)
1094{
1095 return !hash || !hash->count;
1096}
1097
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001098static struct ftrace_func_entry *
1099ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1100{
1101 unsigned long key;
1102 struct ftrace_func_entry *entry;
1103 struct hlist_head *hhd;
1104 struct hlist_node *n;
1105
Steven Rostedt06a51d92011-12-19 19:07:36 -05001106 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001107 return NULL;
1108
1109 if (hash->size_bits > 0)
1110 key = hash_long(ip, hash->size_bits);
1111 else
1112 key = 0;
1113
1114 hhd = &hash->buckets[key];
1115
1116 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1117 if (entry->ip == ip)
1118 return entry;
1119 }
1120 return NULL;
1121}
1122
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001123static void __add_hash_entry(struct ftrace_hash *hash,
1124 struct ftrace_func_entry *entry)
1125{
1126 struct hlist_head *hhd;
1127 unsigned long key;
1128
1129 if (hash->size_bits)
1130 key = hash_long(entry->ip, hash->size_bits);
1131 else
1132 key = 0;
1133
1134 hhd = &hash->buckets[key];
1135 hlist_add_head(&entry->hlist, hhd);
1136 hash->count++;
1137}
1138
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001139static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1140{
1141 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001142
1143 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1144 if (!entry)
1145 return -ENOMEM;
1146
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001147 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001148 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001149
1150 return 0;
1151}
1152
1153static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001154free_hash_entry(struct ftrace_hash *hash,
1155 struct ftrace_func_entry *entry)
1156{
1157 hlist_del(&entry->hlist);
1158 kfree(entry);
1159 hash->count--;
1160}
1161
1162static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001163remove_hash_entry(struct ftrace_hash *hash,
1164 struct ftrace_func_entry *entry)
1165{
1166 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001167 hash->count--;
1168}
1169
1170static void ftrace_hash_clear(struct ftrace_hash *hash)
1171{
1172 struct hlist_head *hhd;
1173 struct hlist_node *tp, *tn;
1174 struct ftrace_func_entry *entry;
1175 int size = 1 << hash->size_bits;
1176 int i;
1177
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001178 if (!hash->count)
1179 return;
1180
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001181 for (i = 0; i < size; i++) {
1182 hhd = &hash->buckets[i];
1183 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001184 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001185 }
1186 FTRACE_WARN_ON(hash->count);
1187}
1188
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001189static void free_ftrace_hash(struct ftrace_hash *hash)
1190{
1191 if (!hash || hash == EMPTY_HASH)
1192 return;
1193 ftrace_hash_clear(hash);
1194 kfree(hash->buckets);
1195 kfree(hash);
1196}
1197
Steven Rostedt07fd5512011-05-05 18:03:47 -04001198static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1199{
1200 struct ftrace_hash *hash;
1201
1202 hash = container_of(rcu, struct ftrace_hash, rcu);
1203 free_ftrace_hash(hash);
1204}
1205
1206static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1207{
1208 if (!hash || hash == EMPTY_HASH)
1209 return;
1210 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1211}
1212
Jiri Olsa5500fa52012-02-15 15:51:54 +01001213void ftrace_free_filter(struct ftrace_ops *ops)
1214{
1215 free_ftrace_hash(ops->filter_hash);
1216 free_ftrace_hash(ops->notrace_hash);
1217}
1218
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001219static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1220{
1221 struct ftrace_hash *hash;
1222 int size;
1223
1224 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1225 if (!hash)
1226 return NULL;
1227
1228 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001229 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001230
1231 if (!hash->buckets) {
1232 kfree(hash);
1233 return NULL;
1234 }
1235
1236 hash->size_bits = size_bits;
1237
1238 return hash;
1239}
1240
1241static struct ftrace_hash *
1242alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1243{
1244 struct ftrace_func_entry *entry;
1245 struct ftrace_hash *new_hash;
1246 struct hlist_node *tp;
1247 int size;
1248 int ret;
1249 int i;
1250
1251 new_hash = alloc_ftrace_hash(size_bits);
1252 if (!new_hash)
1253 return NULL;
1254
1255 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001256 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001257 return new_hash;
1258
1259 size = 1 << hash->size_bits;
1260 for (i = 0; i < size; i++) {
1261 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1262 ret = add_hash_entry(new_hash, entry->ip);
1263 if (ret < 0)
1264 goto free_hash;
1265 }
1266 }
1267
1268 FTRACE_WARN_ON(new_hash->count != hash->count);
1269
1270 return new_hash;
1271
1272 free_hash:
1273 free_ftrace_hash(new_hash);
1274 return NULL;
1275}
1276
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001277static void
1278ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1279static void
1280ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1281
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001282static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001283ftrace_hash_move(struct ftrace_ops *ops, int enable,
1284 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001285{
1286 struct ftrace_func_entry *entry;
1287 struct hlist_node *tp, *tn;
1288 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001289 struct ftrace_hash *old_hash;
1290 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001291 unsigned long key;
1292 int size = src->count;
1293 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001294 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001295 int i;
1296
1297 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001298 * Remove the current set, update the hash and add
1299 * them back.
1300 */
1301 ftrace_hash_rec_disable(ops, enable);
1302
1303 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001304 * If the new source is empty, just free dst and assign it
1305 * the empty_hash.
1306 */
1307 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001308 free_ftrace_hash_rcu(*dst);
1309 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001310 /* still need to update the function records */
1311 ret = 0;
1312 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001313 }
1314
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001315 /*
1316 * Make the hash size about 1/2 the # found
1317 */
1318 for (size /= 2; size; size >>= 1)
1319 bits++;
1320
1321 /* Don't allocate too much */
1322 if (bits > FTRACE_HASH_MAX_BITS)
1323 bits = FTRACE_HASH_MAX_BITS;
1324
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001325 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001326 new_hash = alloc_ftrace_hash(bits);
1327 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001328 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001329
1330 size = 1 << src->size_bits;
1331 for (i = 0; i < size; i++) {
1332 hhd = &src->buckets[i];
1333 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1334 if (bits > 0)
1335 key = hash_long(entry->ip, bits);
1336 else
1337 key = 0;
1338 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001339 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001340 }
1341 }
1342
Steven Rostedt07fd5512011-05-05 18:03:47 -04001343 old_hash = *dst;
1344 rcu_assign_pointer(*dst, new_hash);
1345 free_ftrace_hash_rcu(old_hash);
1346
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001347 ret = 0;
1348 out:
1349 /*
1350 * Enable regardless of ret:
1351 * On success, we enable the new hash.
1352 * On failure, we re-enable the original hash.
1353 */
1354 ftrace_hash_rec_enable(ops, enable);
1355
1356 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001357}
1358
Steven Rostedt265c8312009-02-13 12:43:56 -05001359/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001360 * Test the hashes for this ops to see if we want to call
1361 * the ops->func or not.
1362 *
1363 * It's a match if the ip is in the ops->filter_hash or
1364 * the filter_hash does not exist or is empty,
1365 * AND
1366 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001367 *
1368 * This needs to be called with preemption disabled as
1369 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001370 */
1371static int
1372ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1373{
1374 struct ftrace_hash *filter_hash;
1375 struct ftrace_hash *notrace_hash;
1376 int ret;
1377
Steven Rostedtb8489142011-05-04 09:27:52 -04001378 filter_hash = rcu_dereference_raw(ops->filter_hash);
1379 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1380
Steven Rostedt06a51d92011-12-19 19:07:36 -05001381 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001382 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001383 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001384 !ftrace_lookup_ip(notrace_hash, ip)))
1385 ret = 1;
1386 else
1387 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001388
1389 return ret;
1390}
1391
1392/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001393 * This is a double for. Do not use 'break' to break out of the loop,
1394 * you must use a goto.
1395 */
1396#define do_for_each_ftrace_rec(pg, rec) \
1397 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1398 int _____i; \
1399 for (_____i = 0; _____i < pg->index; _____i++) { \
1400 rec = &pg->records[_____i];
1401
1402#define while_for_each_ftrace_rec() \
1403 } \
1404 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301405
Steven Rostedt5855fea2011-12-16 19:27:42 -05001406
1407static int ftrace_cmp_recs(const void *a, const void *b)
1408{
1409 const struct dyn_ftrace *reca = a;
1410 const struct dyn_ftrace *recb = b;
1411
1412 if (reca->ip > recb->ip)
1413 return 1;
1414 if (reca->ip < recb->ip)
1415 return -1;
1416 return 0;
1417}
1418
Steven Rostedtc88fd862011-08-16 09:53:39 -04001419/**
1420 * ftrace_location - return true if the ip giving is a traced location
1421 * @ip: the instruction pointer to check
1422 *
1423 * Returns 1 if @ip given is a pointer to a ftrace location.
1424 * That is, the instruction that is either a NOP or call to
1425 * the function tracer. It checks the ftrace internal tables to
1426 * determine if the address belongs or not.
1427 */
1428int ftrace_location(unsigned long ip)
1429{
1430 struct ftrace_page *pg;
1431 struct dyn_ftrace *rec;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001432 struct dyn_ftrace key;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001433
Steven Rostedt5855fea2011-12-16 19:27:42 -05001434 key.ip = ip;
1435
1436 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1437 rec = bsearch(&key, pg->records, pg->index,
1438 sizeof(struct dyn_ftrace),
1439 ftrace_cmp_recs);
1440 if (rec)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001441 return 1;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001442 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001443
1444 return 0;
1445}
1446
Steven Rostedted926f92011-05-03 13:25:24 -04001447static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1448 int filter_hash,
1449 bool inc)
1450{
1451 struct ftrace_hash *hash;
1452 struct ftrace_hash *other_hash;
1453 struct ftrace_page *pg;
1454 struct dyn_ftrace *rec;
1455 int count = 0;
1456 int all = 0;
1457
1458 /* Only update if the ops has been registered */
1459 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1460 return;
1461
1462 /*
1463 * In the filter_hash case:
1464 * If the count is zero, we update all records.
1465 * Otherwise we just update the items in the hash.
1466 *
1467 * In the notrace_hash case:
1468 * We enable the update in the hash.
1469 * As disabling notrace means enabling the tracing,
1470 * and enabling notrace means disabling, the inc variable
1471 * gets inversed.
1472 */
1473 if (filter_hash) {
1474 hash = ops->filter_hash;
1475 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001476 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001477 all = 1;
1478 } else {
1479 inc = !inc;
1480 hash = ops->notrace_hash;
1481 other_hash = ops->filter_hash;
1482 /*
1483 * If the notrace hash has no items,
1484 * then there's nothing to do.
1485 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001486 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001487 return;
1488 }
1489
1490 do_for_each_ftrace_rec(pg, rec) {
1491 int in_other_hash = 0;
1492 int in_hash = 0;
1493 int match = 0;
1494
1495 if (all) {
1496 /*
1497 * Only the filter_hash affects all records.
1498 * Update if the record is not in the notrace hash.
1499 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001500 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001501 match = 1;
1502 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001503 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1504 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001505
1506 /*
1507 *
1508 */
1509 if (filter_hash && in_hash && !in_other_hash)
1510 match = 1;
1511 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001512 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001513 match = 1;
1514 }
1515 if (!match)
1516 continue;
1517
1518 if (inc) {
1519 rec->flags++;
1520 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1521 return;
1522 } else {
1523 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1524 return;
1525 rec->flags--;
1526 }
1527 count++;
1528 /* Shortcut, if we handled all records, we are done. */
1529 if (!all && count == hash->count)
1530 return;
1531 } while_for_each_ftrace_rec();
1532}
1533
1534static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1535 int filter_hash)
1536{
1537 __ftrace_hash_rec_update(ops, filter_hash, 0);
1538}
1539
1540static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1541 int filter_hash)
1542{
1543 __ftrace_hash_rec_update(ops, filter_hash, 1);
1544}
1545
Ingo Molnare309b412008-05-12 21:20:51 +02001546static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001547{
Steven Rostedta7900872011-12-16 16:23:44 -05001548 if (ftrace_pages->index == ftrace_pages->size) {
1549 /* We should have allocated enough */
1550 if (WARN_ON(!ftrace_pages->next))
1551 return NULL;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001552 ftrace_pages = ftrace_pages->next;
1553 }
1554
1555 return &ftrace_pages->records[ftrace_pages->index++];
1556}
1557
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001558static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001559ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001560{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001561 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001562
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001563 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001564 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001565
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001566 rec = ftrace_alloc_dyn_node(ip);
1567 if (!rec)
1568 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001569
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001570 rec->ip = ip;
Steven Rostedt3d083392008-05-12 21:20:42 +02001571
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001572 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001573}
1574
Steven Rostedt05736a42008-09-22 14:55:47 -07001575static void print_ip_ins(const char *fmt, unsigned char *p)
1576{
1577 int i;
1578
1579 printk(KERN_CONT "%s", fmt);
1580
1581 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1582 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1583}
1584
Steven Rostedtc88fd862011-08-16 09:53:39 -04001585/**
1586 * ftrace_bug - report and shutdown function tracer
1587 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1588 * @ip: The address that failed
1589 *
1590 * The arch code that enables or disables the function tracing
1591 * can call ftrace_bug() when it has detected a problem in
1592 * modifying the code. @failed should be one of either:
1593 * EFAULT - if the problem happens on reading the @ip address
1594 * EINVAL - if what is read at @ip is not what was expected
1595 * EPERM - if the problem happens on writting to the @ip address
1596 */
1597void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001598{
1599 switch (failed) {
1600 case -EFAULT:
1601 FTRACE_WARN_ON_ONCE(1);
1602 pr_info("ftrace faulted on modifying ");
1603 print_ip_sym(ip);
1604 break;
1605 case -EINVAL:
1606 FTRACE_WARN_ON_ONCE(1);
1607 pr_info("ftrace failed to modify ");
1608 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001609 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001610 printk(KERN_CONT "\n");
1611 break;
1612 case -EPERM:
1613 FTRACE_WARN_ON_ONCE(1);
1614 pr_info("ftrace faulted on writing ");
1615 print_ip_sym(ip);
1616 break;
1617 default:
1618 FTRACE_WARN_ON_ONCE(1);
1619 pr_info("ftrace faulted on unknown error ");
1620 print_ip_sym(ip);
1621 }
1622}
1623
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001624
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001625/* Return 1 if the address range is reserved for ftrace */
1626int ftrace_text_reserved(void *start, void *end)
1627{
1628 struct dyn_ftrace *rec;
1629 struct ftrace_page *pg;
1630
1631 do_for_each_ftrace_rec(pg, rec) {
1632 if (rec->ip <= (unsigned long)end &&
1633 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1634 return 1;
1635 } while_for_each_ftrace_rec();
1636 return 0;
1637}
1638
Steven Rostedtc88fd862011-08-16 09:53:39 -04001639static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001640{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001641 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001642
Steven Rostedt982c3502008-11-15 16:31:41 -05001643 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001644 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001645 *
Steven Rostedted926f92011-05-03 13:25:24 -04001646 * If the record has a ref count, then we need to enable it
1647 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001648 *
Steven Rostedted926f92011-05-03 13:25:24 -04001649 * Otherwise we make sure its disabled.
1650 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001651 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001652 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001653 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001654 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001655 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001656
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001657 /* If the state of this record hasn't changed, then do nothing */
1658 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001659 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001660
1661 if (flag) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001662 if (update)
1663 rec->flags |= FTRACE_FL_ENABLED;
1664 return FTRACE_UPDATE_MAKE_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001665 }
1666
Steven Rostedtc88fd862011-08-16 09:53:39 -04001667 if (update)
1668 rec->flags &= ~FTRACE_FL_ENABLED;
1669
1670 return FTRACE_UPDATE_MAKE_NOP;
1671}
1672
1673/**
1674 * ftrace_update_record, set a record that now is tracing or not
1675 * @rec: the record to update
1676 * @enable: set to 1 if the record is tracing, zero to force disable
1677 *
1678 * The records that represent all functions that can be traced need
1679 * to be updated when tracing has been enabled.
1680 */
1681int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1682{
1683 return ftrace_check_record(rec, enable, 1);
1684}
1685
1686/**
1687 * ftrace_test_record, check if the record has been enabled or not
1688 * @rec: the record to test
1689 * @enable: set to 1 to check if enabled, 0 if it is disabled
1690 *
1691 * The arch code may need to test if a record is already set to
1692 * tracing to determine how to modify the function code that it
1693 * represents.
1694 */
1695int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1696{
1697 return ftrace_check_record(rec, enable, 0);
1698}
1699
1700static int
1701__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1702{
1703 unsigned long ftrace_addr;
1704 int ret;
1705
1706 ftrace_addr = (unsigned long)FTRACE_ADDR;
1707
1708 ret = ftrace_update_record(rec, enable);
1709
1710 switch (ret) {
1711 case FTRACE_UPDATE_IGNORE:
1712 return 0;
1713
1714 case FTRACE_UPDATE_MAKE_CALL:
1715 return ftrace_make_call(rec, ftrace_addr);
1716
1717 case FTRACE_UPDATE_MAKE_NOP:
1718 return ftrace_make_nop(NULL, rec, ftrace_addr);
1719 }
1720
1721 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001722}
1723
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001724static void ftrace_replace_code(int update)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001725{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001726 struct dyn_ftrace *rec;
1727 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001728 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001729
Steven Rostedt45a4a232011-04-21 23:16:46 -04001730 if (unlikely(ftrace_disabled))
1731 return;
1732
Steven Rostedt265c8312009-02-13 12:43:56 -05001733 do_for_each_ftrace_rec(pg, rec) {
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001734 failed = __ftrace_replace_code(rec, update);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001735 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001736 ftrace_bug(failed, rec->ip);
1737 /* Stop processing */
1738 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001739 }
1740 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001741}
1742
Steven Rostedtc88fd862011-08-16 09:53:39 -04001743struct ftrace_rec_iter {
1744 struct ftrace_page *pg;
1745 int index;
1746};
1747
1748/**
1749 * ftrace_rec_iter_start, start up iterating over traced functions
1750 *
1751 * Returns an iterator handle that is used to iterate over all
1752 * the records that represent address locations where functions
1753 * are traced.
1754 *
1755 * May return NULL if no records are available.
1756 */
1757struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1758{
1759 /*
1760 * We only use a single iterator.
1761 * Protected by the ftrace_lock mutex.
1762 */
1763 static struct ftrace_rec_iter ftrace_rec_iter;
1764 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1765
1766 iter->pg = ftrace_pages_start;
1767 iter->index = 0;
1768
1769 /* Could have empty pages */
1770 while (iter->pg && !iter->pg->index)
1771 iter->pg = iter->pg->next;
1772
1773 if (!iter->pg)
1774 return NULL;
1775
1776 return iter;
1777}
1778
1779/**
1780 * ftrace_rec_iter_next, get the next record to process.
1781 * @iter: The handle to the iterator.
1782 *
1783 * Returns the next iterator after the given iterator @iter.
1784 */
1785struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1786{
1787 iter->index++;
1788
1789 if (iter->index >= iter->pg->index) {
1790 iter->pg = iter->pg->next;
1791 iter->index = 0;
1792
1793 /* Could have empty pages */
1794 while (iter->pg && !iter->pg->index)
1795 iter->pg = iter->pg->next;
1796 }
1797
1798 if (!iter->pg)
1799 return NULL;
1800
1801 return iter;
1802}
1803
1804/**
1805 * ftrace_rec_iter_record, get the record at the iterator location
1806 * @iter: The current iterator location
1807 *
1808 * Returns the record that the current @iter is at.
1809 */
1810struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1811{
1812 return &iter->pg->records[iter->index];
1813}
1814
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301815static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001816ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001817{
1818 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001819 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001820
1821 ip = rec->ip;
1822
Steven Rostedt45a4a232011-04-21 23:16:46 -04001823 if (unlikely(ftrace_disabled))
1824 return 0;
1825
Shaohua Li25aac9d2009-01-09 11:29:40 +08001826 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001827 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001828 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301829 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001830 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301831 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001832}
1833
Steven Rostedt000ab692009-02-17 13:35:06 -05001834/*
1835 * archs can override this function if they must do something
1836 * before the modifying code is performed.
1837 */
1838int __weak ftrace_arch_code_modify_prepare(void)
1839{
1840 return 0;
1841}
1842
1843/*
1844 * archs can override this function if they must do something
1845 * after the modifying code is performed.
1846 */
1847int __weak ftrace_arch_code_modify_post_process(void)
1848{
1849 return 0;
1850}
1851
Ingo Molnare309b412008-05-12 21:20:51 +02001852static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001853{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001854 int *command = data;
1855
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001856 if (*command & FTRACE_UPDATE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001857 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001858 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001859 ftrace_replace_code(0);
1860
1861 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1862 ftrace_update_ftrace_func(ftrace_trace_function);
1863
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001864 if (*command & FTRACE_START_FUNC_RET)
1865 ftrace_enable_ftrace_graph_caller();
1866 else if (*command & FTRACE_STOP_FUNC_RET)
1867 ftrace_disable_ftrace_graph_caller();
1868
Steven Rostedtc88fd862011-08-16 09:53:39 -04001869 return 0;
1870}
1871
1872/**
1873 * ftrace_run_stop_machine, go back to the stop machine method
1874 * @command: The command to tell ftrace what to do
1875 *
1876 * If an arch needs to fall back to the stop machine method, the
1877 * it can call this function.
1878 */
1879void ftrace_run_stop_machine(int command)
1880{
1881 stop_machine(__ftrace_modify_code, &command, NULL);
1882}
1883
1884/**
1885 * arch_ftrace_update_code, modify the code to trace or not trace
1886 * @command: The command that needs to be done
1887 *
1888 * Archs can override this function if it does not need to
1889 * run stop_machine() to modify code.
1890 */
1891void __weak arch_ftrace_update_code(int command)
1892{
1893 ftrace_run_stop_machine(command);
1894}
1895
1896static void ftrace_run_update_code(int command)
1897{
1898 int ret;
1899
1900 ret = ftrace_arch_code_modify_prepare();
1901 FTRACE_WARN_ON(ret);
1902 if (ret)
1903 return;
1904 /*
1905 * Do not call function tracer while we update the code.
1906 * We are in stop machine.
1907 */
1908 function_trace_stop++;
1909
1910 /*
1911 * By default we use stop_machine() to modify the code.
1912 * But archs can do what ever they want as long as it
1913 * is safe. The stop_machine() is the safest, but also
1914 * produces the most overhead.
1915 */
1916 arch_ftrace_update_code(command);
1917
Steven Rostedt6331c282011-07-13 15:11:02 -04001918#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1919 /*
1920 * For archs that call ftrace_test_stop_func(), we must
1921 * wait till after we update all the function callers
1922 * before we update the callback. This keeps different
1923 * ops that record different functions from corrupting
1924 * each other.
1925 */
1926 __ftrace_trace_function = __ftrace_trace_function_delay;
1927#endif
1928 function_trace_stop--;
1929
Steven Rostedt000ab692009-02-17 13:35:06 -05001930 ret = ftrace_arch_code_modify_post_process();
1931 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001932}
1933
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001934static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001935static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001936static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001937
1938static void ftrace_startup_enable(int command)
1939{
1940 if (saved_ftrace_func != ftrace_trace_function) {
1941 saved_ftrace_func = ftrace_trace_function;
1942 command |= FTRACE_UPDATE_TRACE_FUNC;
1943 }
1944
1945 if (!command || !ftrace_enabled)
1946 return;
1947
1948 ftrace_run_update_code(command);
1949}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001950
Steven Rostedta1cd6172011-05-23 15:24:25 -04001951static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001952{
Steven Rostedtb8489142011-05-04 09:27:52 -04001953 bool hash_enable = true;
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001954 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04001955
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001956 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001957 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001958
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001959 ret = __register_ftrace_function(ops);
1960 if (ret)
1961 return ret;
1962
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001963 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001964 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001965
Steven Rostedtb8489142011-05-04 09:27:52 -04001966 /* ops marked global share the filter hashes */
1967 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1968 ops = &global_ops;
1969 /* Don't update hash if global is already set */
1970 if (global_start_up)
1971 hash_enable = false;
1972 global_start_up++;
1973 }
1974
Steven Rostedted926f92011-05-03 13:25:24 -04001975 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001976 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001977 ftrace_hash_rec_enable(ops, 1);
1978
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001979 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001980
1981 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001982}
1983
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001984static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001985{
Steven Rostedtb8489142011-05-04 09:27:52 -04001986 bool hash_disable = true;
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001987 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04001988
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001989 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001990 return -ENODEV;
1991
1992 ret = __unregister_ftrace_function(ops);
1993 if (ret)
1994 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001995
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001996 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001997 /*
1998 * Just warn in case of unbalance, no need to kill ftrace, it's not
1999 * critical but the ftrace_call callers may be never nopped again after
2000 * further ftrace uses.
2001 */
2002 WARN_ON_ONCE(ftrace_start_up < 0);
2003
Steven Rostedtb8489142011-05-04 09:27:52 -04002004 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2005 ops = &global_ops;
2006 global_start_up--;
2007 WARN_ON_ONCE(global_start_up < 0);
2008 /* Don't update hash if global still has users */
2009 if (global_start_up) {
2010 WARN_ON_ONCE(!ftrace_start_up);
2011 hash_disable = false;
2012 }
2013 }
2014
2015 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002016 ftrace_hash_rec_disable(ops, 1);
2017
Steven Rostedtb8489142011-05-04 09:27:52 -04002018 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002019 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002020
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002021 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002022
2023 if (saved_ftrace_func != ftrace_trace_function) {
2024 saved_ftrace_func = ftrace_trace_function;
2025 command |= FTRACE_UPDATE_TRACE_FUNC;
2026 }
2027
2028 if (!command || !ftrace_enabled)
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002029 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002030
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002031 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002032 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002033}
2034
Ingo Molnare309b412008-05-12 21:20:51 +02002035static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002036{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002037 if (unlikely(ftrace_disabled))
2038 return;
2039
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002040 /* Force update next time */
2041 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002042 /* ftrace_start_up is true if we want ftrace running */
2043 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002044 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002045}
2046
Ingo Molnare309b412008-05-12 21:20:51 +02002047static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002048{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002049 if (unlikely(ftrace_disabled))
2050 return;
2051
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002052 /* ftrace_start_up is true if ftrace is running */
2053 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002054 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002055}
2056
Steven Rostedt3d083392008-05-12 21:20:42 +02002057static cycle_t ftrace_update_time;
2058static unsigned long ftrace_update_cnt;
2059unsigned long ftrace_update_tot_cnt;
2060
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002061static int ops_traces_mod(struct ftrace_ops *ops)
2062{
2063 struct ftrace_hash *hash;
2064
2065 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002066 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002067}
2068
Steven Rostedt31e88902008-11-14 16:21:19 -08002069static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002070{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002071 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002072 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302073 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002074 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002075 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002076
2077 /*
2078 * When adding a module, we need to check if tracers are
2079 * currently enabled and if they are set to trace all functions.
2080 * If they are, we need to enable the module functions as well
2081 * as update the reference counts for those function records.
2082 */
2083 if (mod) {
2084 struct ftrace_ops *ops;
2085
2086 for (ops = ftrace_ops_list;
2087 ops != &ftrace_list_end; ops = ops->next) {
2088 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2089 ops_traces_mod(ops))
2090 ref++;
2091 }
2092 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002093
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002094 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002095 ftrace_update_cnt = 0;
2096
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002097 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302098
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002099 for (i = 0; i < pg->index; i++) {
2100 /* If something went wrong, bail without enabling anything */
2101 if (unlikely(ftrace_disabled))
2102 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002103
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002104 p = &pg->records[i];
2105 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302106
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002107 /*
2108 * Do the initial record conversion from mcount jump
2109 * to the NOP instructions.
2110 */
2111 if (!ftrace_code_disable(mod, p))
2112 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002113
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002114 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002115
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002116 /*
2117 * If the tracing is enabled, go ahead and enable the record.
2118 *
2119 * The reason not to enable the record immediatelly is the
2120 * inherent check of ftrace_make_nop/ftrace_make_call for
2121 * correct previous instructions. Making first the NOP
2122 * conversion puts the module to the correct state, thus
2123 * passing the ftrace_make_call check.
2124 */
2125 if (ftrace_start_up && ref) {
2126 int failed = __ftrace_replace_code(p, 1);
2127 if (failed)
2128 ftrace_bug(failed, p->ip);
2129 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002130 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002131 }
2132
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002133 ftrace_new_pgs = NULL;
2134
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002135 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002136 ftrace_update_time = stop - start;
2137 ftrace_update_tot_cnt += ftrace_update_cnt;
2138
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002139 return 0;
2140}
2141
Steven Rostedta7900872011-12-16 16:23:44 -05002142static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002143{
Steven Rostedta7900872011-12-16 16:23:44 -05002144 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002145 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002146
Steven Rostedta7900872011-12-16 16:23:44 -05002147 if (WARN_ON(!count))
2148 return -EINVAL;
2149
2150 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002151
2152 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002153 * We want to fill as much as possible. No more than a page
2154 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002155 */
Steven Rostedta7900872011-12-16 16:23:44 -05002156 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2157 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002158
Steven Rostedta7900872011-12-16 16:23:44 -05002159 again:
2160 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2161
2162 if (!pg->records) {
2163 /* if we can't allocate this size, try something smaller */
2164 if (!order)
2165 return -ENOMEM;
2166 order >>= 1;
2167 goto again;
2168 }
2169
2170 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2171 pg->size = cnt;
2172
2173 if (cnt > count)
2174 cnt = count;
2175
2176 return cnt;
2177}
2178
2179static struct ftrace_page *
2180ftrace_allocate_pages(unsigned long num_to_init)
2181{
2182 struct ftrace_page *start_pg;
2183 struct ftrace_page *pg;
2184 int order;
2185 int cnt;
2186
2187 if (!num_to_init)
2188 return 0;
2189
2190 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2191 if (!pg)
2192 return NULL;
2193
2194 /*
2195 * Try to allocate as much as possible in one continues
2196 * location that fills in all of the space. We want to
2197 * waste as little space as possible.
2198 */
2199 for (;;) {
2200 cnt = ftrace_allocate_records(pg, num_to_init);
2201 if (cnt < 0)
2202 goto free_pages;
2203
2204 num_to_init -= cnt;
2205 if (!num_to_init)
2206 break;
2207
2208 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2209 if (!pg->next)
2210 goto free_pages;
2211
2212 pg = pg->next;
2213 }
2214
2215 return start_pg;
2216
2217 free_pages:
2218 while (start_pg) {
2219 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2220 free_pages((unsigned long)pg->records, order);
2221 start_pg = pg->next;
2222 kfree(pg);
2223 pg = start_pg;
2224 }
2225 pr_info("ftrace: FAILED to allocate memory for functions\n");
2226 return NULL;
2227}
2228
2229static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2230{
2231 int cnt;
2232
2233 if (!num_to_init) {
2234 pr_info("ftrace: No functions to be traced?\n");
2235 return -1;
2236 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002237
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002238 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002239 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002240 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002241
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002242 return 0;
2243}
2244
Steven Rostedt5072c592008-05-12 21:20:43 +02002245#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2246
2247struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002248 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002249 loff_t func_pos;
2250 struct ftrace_page *pg;
2251 struct dyn_ftrace *func;
2252 struct ftrace_func_probe *probe;
2253 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002254 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002255 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002256 int hidx;
2257 int idx;
2258 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002259};
2260
Ingo Molnare309b412008-05-12 21:20:51 +02002261static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002262t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002263{
2264 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002265 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002266 struct hlist_head *hhd;
2267
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002268 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002269 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002270
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002271 if (iter->probe)
2272 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002273 retry:
2274 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2275 return NULL;
2276
2277 hhd = &ftrace_func_hash[iter->hidx];
2278
2279 if (hlist_empty(hhd)) {
2280 iter->hidx++;
2281 hnd = NULL;
2282 goto retry;
2283 }
2284
2285 if (!hnd)
2286 hnd = hhd->first;
2287 else {
2288 hnd = hnd->next;
2289 if (!hnd) {
2290 iter->hidx++;
2291 goto retry;
2292 }
2293 }
2294
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002295 if (WARN_ON_ONCE(!hnd))
2296 return NULL;
2297
2298 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2299
2300 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002301}
2302
2303static void *t_hash_start(struct seq_file *m, loff_t *pos)
2304{
2305 struct ftrace_iterator *iter = m->private;
2306 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002307 loff_t l;
2308
Steven Rostedt69a30832011-12-19 15:21:16 -05002309 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2310 return NULL;
2311
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002312 if (iter->func_pos > *pos)
2313 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002314
Li Zefand82d6242009-06-24 09:54:54 +08002315 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002316 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002317 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002318 if (!p)
2319 break;
2320 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002321 if (!p)
2322 return NULL;
2323
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002324 /* Only set this if we have an item */
2325 iter->flags |= FTRACE_ITER_HASH;
2326
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002327 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002328}
2329
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002330static int
2331t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002332{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002333 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002334
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002335 rec = iter->probe;
2336 if (WARN_ON_ONCE(!rec))
2337 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002338
Steven Rostedt809dcf22009-02-16 23:06:01 -05002339 if (rec->ops->print)
2340 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2341
Steven Rostedtb375a112009-09-17 00:05:58 -04002342 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002343
2344 if (rec->data)
2345 seq_printf(m, ":%p", rec->data);
2346 seq_putc(m, '\n');
2347
2348 return 0;
2349}
2350
2351static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002352t_next(struct seq_file *m, void *v, loff_t *pos)
2353{
2354 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002355 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002356 struct dyn_ftrace *rec = NULL;
2357
Steven Rostedt45a4a232011-04-21 23:16:46 -04002358 if (unlikely(ftrace_disabled))
2359 return NULL;
2360
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002361 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002362 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002363
Steven Rostedt5072c592008-05-12 21:20:43 +02002364 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002365 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002366
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002367 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002368 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002369
Steven Rostedt5072c592008-05-12 21:20:43 +02002370 retry:
2371 if (iter->idx >= iter->pg->index) {
2372 if (iter->pg->next) {
2373 iter->pg = iter->pg->next;
2374 iter->idx = 0;
2375 goto retry;
2376 }
2377 } else {
2378 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002379 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002380 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002381
Steven Rostedt41c52c02008-05-22 11:46:33 -04002382 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002383 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2384
2385 ((iter->flags & FTRACE_ITER_ENABLED) &&
2386 !(rec->flags & ~FTRACE_FL_MASK))) {
2387
Steven Rostedt5072c592008-05-12 21:20:43 +02002388 rec = NULL;
2389 goto retry;
2390 }
2391 }
2392
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002393 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002394 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002395
2396 iter->func = rec;
2397
2398 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002399}
2400
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002401static void reset_iter_read(struct ftrace_iterator *iter)
2402{
2403 iter->pos = 0;
2404 iter->func_pos = 0;
Dan Carpenter455d8952012-06-09 19:10:27 +03002405 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002406}
2407
2408static void *t_start(struct seq_file *m, loff_t *pos)
2409{
2410 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002411 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002412 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002413 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002414
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002415 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002416
2417 if (unlikely(ftrace_disabled))
2418 return NULL;
2419
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002420 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002421 * If an lseek was done, then reset and start from beginning.
2422 */
2423 if (*pos < iter->pos)
2424 reset_iter_read(iter);
2425
2426 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002427 * For set_ftrace_filter reading, if we have the filter
2428 * off, we can short cut and just print out that all
2429 * functions are enabled.
2430 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002431 if (iter->flags & FTRACE_ITER_FILTER &&
2432 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002433 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002434 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002435 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002436 /* reset in case of seek/pread */
2437 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002438 return iter;
2439 }
2440
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002441 if (iter->flags & FTRACE_ITER_HASH)
2442 return t_hash_start(m, pos);
2443
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002444 /*
2445 * Unfortunately, we need to restart at ftrace_pages_start
2446 * every time we let go of the ftrace_mutex. This is because
2447 * those pointers can change without the lock.
2448 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002449 iter->pg = ftrace_pages_start;
2450 iter->idx = 0;
2451 for (l = 0; l <= *pos; ) {
2452 p = t_next(m, p, &l);
2453 if (!p)
2454 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002455 }
walimis5821e1b2008-11-15 15:19:06 +08002456
Steven Rostedt69a30832011-12-19 15:21:16 -05002457 if (!p)
2458 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002459
2460 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002461}
2462
2463static void t_stop(struct seq_file *m, void *p)
2464{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002465 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002466}
2467
2468static int t_show(struct seq_file *m, void *v)
2469{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002470 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002471 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002472
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002473 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002474 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002475
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002476 if (iter->flags & FTRACE_ITER_PRINTALL) {
2477 seq_printf(m, "#### all functions enabled ####\n");
2478 return 0;
2479 }
2480
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002481 rec = iter->func;
2482
Steven Rostedt5072c592008-05-12 21:20:43 +02002483 if (!rec)
2484 return 0;
2485
Steven Rostedt647bcd02011-05-03 14:39:21 -04002486 seq_printf(m, "%ps", (void *)rec->ip);
2487 if (iter->flags & FTRACE_ITER_ENABLED)
2488 seq_printf(m, " (%ld)",
2489 rec->flags & ~FTRACE_FL_MASK);
2490 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002491
2492 return 0;
2493}
2494
James Morris88e9d342009-09-22 16:43:43 -07002495static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002496 .start = t_start,
2497 .next = t_next,
2498 .stop = t_stop,
2499 .show = t_show,
2500};
2501
Ingo Molnare309b412008-05-12 21:20:51 +02002502static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002503ftrace_avail_open(struct inode *inode, struct file *file)
2504{
2505 struct ftrace_iterator *iter;
2506 int ret;
2507
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002508 if (unlikely(ftrace_disabled))
2509 return -ENODEV;
2510
Steven Rostedt5072c592008-05-12 21:20:43 +02002511 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2512 if (!iter)
2513 return -ENOMEM;
2514
2515 iter->pg = ftrace_pages_start;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002516 iter->ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002517
2518 ret = seq_open(file, &show_ftrace_seq_ops);
2519 if (!ret) {
2520 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002521
Steven Rostedt5072c592008-05-12 21:20:43 +02002522 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002523 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002524 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002525 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002526
2527 return ret;
2528}
2529
Steven Rostedt647bcd02011-05-03 14:39:21 -04002530static int
2531ftrace_enabled_open(struct inode *inode, struct file *file)
2532{
2533 struct ftrace_iterator *iter;
2534 int ret;
2535
2536 if (unlikely(ftrace_disabled))
2537 return -ENODEV;
2538
2539 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2540 if (!iter)
2541 return -ENOMEM;
2542
2543 iter->pg = ftrace_pages_start;
2544 iter->flags = FTRACE_ITER_ENABLED;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002545 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002546
2547 ret = seq_open(file, &show_ftrace_seq_ops);
2548 if (!ret) {
2549 struct seq_file *m = file->private_data;
2550
2551 m->private = iter;
2552 } else {
2553 kfree(iter);
2554 }
2555
2556 return ret;
2557}
2558
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002559static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002560{
Steven Rostedt52baf112009-02-14 01:15:39 -05002561 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002562 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002563 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002564}
2565
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002566/**
2567 * ftrace_regex_open - initialize function tracer filter files
2568 * @ops: The ftrace_ops that hold the hash filters
2569 * @flag: The type of filter to process
2570 * @inode: The inode, usually passed in to your open routine
2571 * @file: The file, usually passed in to your open routine
2572 *
2573 * ftrace_regex_open() initializes the filter files for the
2574 * @ops. Depending on @flag it may process the filter hash or
2575 * the notrace hash of @ops. With this called from the open
2576 * routine, you can use ftrace_filter_write() for the write
2577 * routine if @flag has FTRACE_ITER_FILTER set, or
2578 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedtbc4d36c2013-06-07 17:02:08 +08002579 * ftrace_filter_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002580 * release must call ftrace_regex_release().
2581 */
2582int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002583ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002584 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002585{
2586 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002587 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002588 int ret = 0;
2589
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002590 if (unlikely(ftrace_disabled))
2591 return -ENODEV;
2592
Steven Rostedt5072c592008-05-12 21:20:43 +02002593 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2594 if (!iter)
2595 return -ENOMEM;
2596
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002597 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2598 kfree(iter);
2599 return -ENOMEM;
2600 }
2601
Steven Rostedtf45948e2011-05-02 12:29:25 -04002602 if (flag & FTRACE_ITER_NOTRACE)
2603 hash = ops->notrace_hash;
2604 else
2605 hash = ops->filter_hash;
2606
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002607 iter->ops = ops;
2608 iter->flags = flag;
2609
2610 if (file->f_mode & FMODE_WRITE) {
2611 mutex_lock(&ftrace_lock);
2612 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2613 mutex_unlock(&ftrace_lock);
2614
2615 if (!iter->hash) {
2616 trace_parser_put(&iter->parser);
2617 kfree(iter);
2618 return -ENOMEM;
2619 }
2620 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002621
Steven Rostedt41c52c02008-05-22 11:46:33 -04002622 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002623
Steven Rostedt5072c592008-05-12 21:20:43 +02002624 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002625 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002626 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002627
2628 if (file->f_mode & FMODE_READ) {
2629 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002630
2631 ret = seq_open(file, &show_ftrace_seq_ops);
2632 if (!ret) {
2633 struct seq_file *m = file->private_data;
2634 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002635 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002636 /* Failed */
2637 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002638 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002639 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002640 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002641 } else
2642 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002643 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002644
2645 return ret;
2646}
2647
Steven Rostedt41c52c02008-05-22 11:46:33 -04002648static int
2649ftrace_filter_open(struct inode *inode, struct file *file)
2650{
Steven Rostedt69a30832011-12-19 15:21:16 -05002651 return ftrace_regex_open(&global_ops,
2652 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2653 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002654}
2655
2656static int
2657ftrace_notrace_open(struct inode *inode, struct file *file)
2658{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002659 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002660 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002661}
2662
Steven Rostedt64e7c442009-02-13 17:08:48 -05002663static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002664{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002665 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002666 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002667
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002668 switch (type) {
2669 case MATCH_FULL:
2670 if (strcmp(str, regex) == 0)
2671 matched = 1;
2672 break;
2673 case MATCH_FRONT_ONLY:
2674 if (strncmp(str, regex, len) == 0)
2675 matched = 1;
2676 break;
2677 case MATCH_MIDDLE_ONLY:
2678 if (strstr(str, regex))
2679 matched = 1;
2680 break;
2681 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002682 slen = strlen(str);
2683 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002684 matched = 1;
2685 break;
2686 }
2687
2688 return matched;
2689}
2690
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002691static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002692enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002693{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002694 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002695 int ret = 0;
2696
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002697 entry = ftrace_lookup_ip(hash, rec->ip);
2698 if (not) {
2699 /* Do nothing if it doesn't exist */
2700 if (!entry)
2701 return 0;
2702
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002703 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002704 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002705 /* Do nothing if it exists */
2706 if (entry)
2707 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002708
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002709 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002710 }
2711 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002712}
2713
Steven Rostedt64e7c442009-02-13 17:08:48 -05002714static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002715ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2716 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002717{
2718 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002719 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002720
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002721 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2722
2723 if (mod) {
2724 /* module lookup requires matching the module */
2725 if (!modname || strcmp(modname, mod))
2726 return 0;
2727
2728 /* blank search means to match all funcs in the mod */
2729 if (!len)
2730 return 1;
2731 }
2732
Steven Rostedt64e7c442009-02-13 17:08:48 -05002733 return ftrace_match(str, regex, len, type);
2734}
2735
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002736static int
2737match_records(struct ftrace_hash *hash, char *buff,
2738 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002739{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002740 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002741 struct ftrace_page *pg;
2742 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002743 int type = MATCH_FULL;
2744 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002745 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002746 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002747
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002748 if (len) {
2749 type = filter_parse_regex(buff, len, &search, &not);
2750 search_len = strlen(search);
2751 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002752
Steven Rostedt52baf112009-02-14 01:15:39 -05002753 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002754
2755 if (unlikely(ftrace_disabled))
2756 goto out_unlock;
2757
Steven Rostedt265c8312009-02-13 12:43:56 -05002758 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002759 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002760 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002761 if (ret < 0) {
2762 found = ret;
2763 goto out_unlock;
2764 }
Li Zefan311d16d2009-12-08 11:15:11 +08002765 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002766 }
2767 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002768 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002769 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002770
2771 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002772}
2773
Steven Rostedt64e7c442009-02-13 17:08:48 -05002774static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002775ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002776{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002777 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002778}
2779
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002780static int
2781ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002782{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002783 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002784
Steven Rostedt64e7c442009-02-13 17:08:48 -05002785 /* blank or '*' mean the same */
2786 if (strcmp(buff, "*") == 0)
2787 buff[0] = 0;
2788
2789 /* handle the case of 'dont filter this module' */
2790 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2791 buff[0] = 0;
2792 not = 1;
2793 }
2794
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002795 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002796}
2797
Steven Rostedtf6180772009-02-14 00:40:25 -05002798/*
2799 * We register the module command as a template to show others how
2800 * to register the a command as well.
2801 */
2802
2803static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002804ftrace_mod_callback(struct ftrace_hash *hash,
2805 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002806{
2807 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002808 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002809
2810 /*
2811 * cmd == 'mod' because we only registered this func
2812 * for the 'mod' ftrace_func_command.
2813 * But if you register one func with multiple commands,
2814 * you can tell which command was used by the cmd
2815 * parameter.
2816 */
2817
2818 /* we must have a module name */
2819 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002820 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002821
2822 mod = strsep(&param, ":");
2823 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002824 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002825
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002826 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002827 if (!ret)
2828 ret = -EINVAL;
2829 if (ret < 0)
2830 return ret;
2831
2832 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002833}
2834
2835static struct ftrace_func_command ftrace_mod_cmd = {
2836 .name = "mod",
2837 .func = ftrace_mod_callback,
2838};
2839
2840static int __init ftrace_mod_cmd_init(void)
2841{
2842 return register_ftrace_command(&ftrace_mod_cmd);
2843}
2844device_initcall(ftrace_mod_cmd_init);
2845
Steven Rostedt59df055f2009-02-14 15:29:06 -05002846static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002847function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002848{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002849 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002850 struct hlist_head *hhd;
2851 struct hlist_node *n;
2852 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002853
2854 key = hash_long(ip, FTRACE_HASH_BITS);
2855
2856 hhd = &ftrace_func_hash[key];
2857
2858 if (hlist_empty(hhd))
2859 return;
2860
2861 /*
2862 * Disable preemption for these calls to prevent a RCU grace
2863 * period. This syncs the hash iteration and freeing of items
2864 * on the hash. rcu_read_lock is too dangerous here.
2865 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002866 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002867 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2868 if (entry->ip == ip)
2869 entry->ops->func(ip, parent_ip, &entry->data);
2870 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002871 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002872}
2873
Steven Rostedtb6887d72009-02-17 12:32:04 -05002874static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002875{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002876 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002877};
2878
Steven Rostedtb6887d72009-02-17 12:32:04 -05002879static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002880
Steven Rostedtb6887d72009-02-17 12:32:04 -05002881static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002882{
Steven Rostedtb8489142011-05-04 09:27:52 -04002883 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002884 int i;
2885
Steven Rostedtb6887d72009-02-17 12:32:04 -05002886 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002887 return;
2888
2889 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2890 struct hlist_head *hhd = &ftrace_func_hash[i];
2891 if (hhd->first)
2892 break;
2893 }
2894 /* Nothing registered? */
2895 if (i == FTRACE_FUNC_HASHSIZE)
2896 return;
2897
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002898 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002899
Steven Rostedtb6887d72009-02-17 12:32:04 -05002900 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002901}
2902
Steven Rostedtb6887d72009-02-17 12:32:04 -05002903static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002904{
2905 int i;
2906
Steven Rostedtb6887d72009-02-17 12:32:04 -05002907 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002908 return;
2909
2910 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2911 struct hlist_head *hhd = &ftrace_func_hash[i];
2912 if (hhd->first)
2913 return;
2914 }
2915
2916 /* no more funcs left */
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002917 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002918
Steven Rostedtb6887d72009-02-17 12:32:04 -05002919 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002920}
2921
2922
2923static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2924{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002925 struct ftrace_func_probe *entry =
2926 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002927
2928 if (entry->ops->free)
2929 entry->ops->free(&entry->data);
2930 kfree(entry);
2931}
2932
2933
2934int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002935register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002936 void *data)
2937{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002938 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002939 struct ftrace_page *pg;
2940 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002941 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002942 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002943 int count = 0;
2944 char *search;
2945
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002946 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002947 len = strlen(search);
2948
Steven Rostedtb6887d72009-02-17 12:32:04 -05002949 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002950 if (WARN_ON(not))
2951 return -EINVAL;
2952
2953 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002954
Steven Rostedt45a4a232011-04-21 23:16:46 -04002955 if (unlikely(ftrace_disabled))
2956 goto out_unlock;
2957
Steven Rostedt59df055f2009-02-14 15:29:06 -05002958 do_for_each_ftrace_rec(pg, rec) {
2959
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002960 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002961 continue;
2962
2963 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2964 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002965 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002966 if (!count)
2967 count = -ENOMEM;
2968 goto out_unlock;
2969 }
2970
2971 count++;
2972
2973 entry->data = data;
2974
2975 /*
2976 * The caller might want to do something special
2977 * for each function we find. We call the callback
2978 * to give the caller an opportunity to do so.
2979 */
2980 if (ops->callback) {
2981 if (ops->callback(rec->ip, &entry->data) < 0) {
2982 /* caller does not like this func */
2983 kfree(entry);
2984 continue;
2985 }
2986 }
2987
2988 entry->ops = ops;
2989 entry->ip = rec->ip;
2990
2991 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2992 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2993
2994 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002995 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002996
2997 out_unlock:
2998 mutex_unlock(&ftrace_lock);
2999
3000 return count;
3001}
3002
3003enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003004 PROBE_TEST_FUNC = 1,
3005 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003006};
3007
3008static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003009__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003010 void *data, int flags)
3011{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003012 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003013 struct hlist_node *n, *tmp;
3014 char str[KSYM_SYMBOL_LEN];
3015 int type = MATCH_FULL;
3016 int i, len = 0;
3017 char *search;
3018
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003019 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003020 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003021 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003022 int not;
3023
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003024 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003025 len = strlen(search);
3026
Steven Rostedtb6887d72009-02-17 12:32:04 -05003027 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003028 if (WARN_ON(not))
3029 return;
3030 }
3031
3032 mutex_lock(&ftrace_lock);
3033 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3034 struct hlist_head *hhd = &ftrace_func_hash[i];
3035
3036 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3037
3038 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003039 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003040 continue;
3041
Steven Rostedtb6887d72009-02-17 12:32:04 -05003042 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003043 continue;
3044
3045 /* do this last, since it is the most expensive */
3046 if (glob) {
3047 kallsyms_lookup(entry->ip, NULL, NULL,
3048 NULL, str);
3049 if (!ftrace_match(str, glob, len, type))
3050 continue;
3051 }
3052
Steven Rostedt (Red Hat)52cecaa2013-03-13 11:15:19 -04003053 hlist_del_rcu(&entry->node);
3054 call_rcu_sched(&entry->rcu, ftrace_free_entry_rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003055 }
3056 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003057 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003058 mutex_unlock(&ftrace_lock);
3059}
3060
3061void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003062unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003063 void *data)
3064{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003065 __unregister_ftrace_function_probe(glob, ops, data,
3066 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003067}
3068
3069void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003070unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003071{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003072 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003073}
3074
Steven Rostedtb6887d72009-02-17 12:32:04 -05003075void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003076{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003077 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003078}
3079
Steven Rostedtf6180772009-02-14 00:40:25 -05003080static LIST_HEAD(ftrace_commands);
3081static DEFINE_MUTEX(ftrace_cmd_mutex);
3082
3083int register_ftrace_command(struct ftrace_func_command *cmd)
3084{
3085 struct ftrace_func_command *p;
3086 int ret = 0;
3087
3088 mutex_lock(&ftrace_cmd_mutex);
3089 list_for_each_entry(p, &ftrace_commands, list) {
3090 if (strcmp(cmd->name, p->name) == 0) {
3091 ret = -EBUSY;
3092 goto out_unlock;
3093 }
3094 }
3095 list_add(&cmd->list, &ftrace_commands);
3096 out_unlock:
3097 mutex_unlock(&ftrace_cmd_mutex);
3098
3099 return ret;
3100}
3101
3102int unregister_ftrace_command(struct ftrace_func_command *cmd)
3103{
3104 struct ftrace_func_command *p, *n;
3105 int ret = -ENODEV;
3106
3107 mutex_lock(&ftrace_cmd_mutex);
3108 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3109 if (strcmp(cmd->name, p->name) == 0) {
3110 ret = 0;
3111 list_del_init(&p->list);
3112 goto out_unlock;
3113 }
3114 }
3115 out_unlock:
3116 mutex_unlock(&ftrace_cmd_mutex);
3117
3118 return ret;
3119}
3120
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003121static int ftrace_process_regex(struct ftrace_hash *hash,
3122 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003123{
Steven Rostedtf6180772009-02-14 00:40:25 -05003124 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003125 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003126 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003127
3128 func = strsep(&next, ":");
3129
3130 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003131 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003132 if (!ret)
3133 ret = -EINVAL;
3134 if (ret < 0)
3135 return ret;
3136 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003137 }
3138
Steven Rostedtf6180772009-02-14 00:40:25 -05003139 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003140
3141 command = strsep(&next, ":");
3142
Steven Rostedtf6180772009-02-14 00:40:25 -05003143 mutex_lock(&ftrace_cmd_mutex);
3144 list_for_each_entry(p, &ftrace_commands, list) {
3145 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003146 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003147 goto out_unlock;
3148 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003149 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003150 out_unlock:
3151 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003152
Steven Rostedtf6180772009-02-14 00:40:25 -05003153 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003154}
3155
Ingo Molnare309b412008-05-12 21:20:51 +02003156static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003157ftrace_regex_write(struct file *file, const char __user *ubuf,
3158 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003159{
3160 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003161 struct trace_parser *parser;
3162 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003163
Li Zefan4ba79782009-09-22 13:52:20 +08003164 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003165 return 0;
3166
Steven Rostedt41c52c02008-05-22 11:46:33 -04003167 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003168
Steven Rostedt45a4a232011-04-21 23:16:46 -04003169 ret = -ENODEV;
3170 if (unlikely(ftrace_disabled))
3171 goto out_unlock;
3172
Steven Rostedt5072c592008-05-12 21:20:43 +02003173 if (file->f_mode & FMODE_READ) {
3174 struct seq_file *m = file->private_data;
3175 iter = m->private;
3176 } else
3177 iter = file->private_data;
3178
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003179 parser = &iter->parser;
3180 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003181
Li Zefan4ba79782009-09-22 13:52:20 +08003182 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003183 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003184 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003185 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003186 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003187 if (ret)
Li Zefaned146b22009-11-03 08:55:38 +08003188 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003189 }
3190
Steven Rostedt5072c592008-05-12 21:20:43 +02003191 ret = read;
Li Zefaned146b22009-11-03 08:55:38 +08003192out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003193 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b22009-11-03 08:55:38 +08003194
Steven Rostedt5072c592008-05-12 21:20:43 +02003195 return ret;
3196}
3197
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003198ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003199ftrace_filter_write(struct file *file, const char __user *ubuf,
3200 size_t cnt, loff_t *ppos)
3201{
3202 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3203}
3204
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003205ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003206ftrace_notrace_write(struct file *file, const char __user *ubuf,
3207 size_t cnt, loff_t *ppos)
3208{
3209 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3210}
3211
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003212static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003213ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3214 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003215{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003216 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003217 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003218 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003219
Steven Rostedt936e0742011-05-05 22:54:01 -04003220 /* All global ops uses the global ops filters */
3221 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3222 ops = &global_ops;
3223
Steven Rostedt41c52c02008-05-22 11:46:33 -04003224 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003225 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003226
Steven Rostedtf45948e2011-05-02 12:29:25 -04003227 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003228 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003229 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003230 orig_hash = &ops->notrace_hash;
3231
3232 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3233 if (!hash)
3234 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003235
Steven Rostedt41c52c02008-05-22 11:46:33 -04003236 mutex_lock(&ftrace_regex_lock);
3237 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003238 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003239 if (buf && !ftrace_match_records(hash, buf, len)) {
3240 ret = -EINVAL;
3241 goto out_regex_unlock;
3242 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003243
3244 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003245 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003246 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3247 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003248 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003249
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003250 mutex_unlock(&ftrace_lock);
3251
Jiri Olsaac483c42012-01-02 10:04:14 +01003252 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003253 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003254
3255 free_ftrace_hash(hash);
3256 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003257}
3258
Steven Rostedt77a2b372008-05-12 21:20:45 +02003259/**
3260 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003261 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003262 * @buf - the string that holds the function filter text.
3263 * @len - the length of the string.
3264 * @reset - non zero to reset all filters before applying this filter.
3265 *
3266 * Filters denote which functions should be enabled when tracing is enabled.
3267 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3268 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003269int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003270 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003271{
Jiri Olsaac483c42012-01-02 10:04:14 +01003272 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003273}
Steven Rostedt936e0742011-05-05 22:54:01 -04003274EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003275
Steven Rostedt41c52c02008-05-22 11:46:33 -04003276/**
3277 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003278 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003279 * @buf - the string that holds the function notrace text.
3280 * @len - the length of the string.
3281 * @reset - non zero to reset all filters before applying this filter.
3282 *
3283 * Notrace Filters denote which functions should not be enabled when tracing
3284 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3285 * for tracing.
3286 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003287int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003288 int len, int reset)
3289{
Jiri Olsaac483c42012-01-02 10:04:14 +01003290 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003291}
3292EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3293/**
3294 * ftrace_set_filter - set a function to filter on in ftrace
3295 * @ops - the ops to set the filter with
3296 * @buf - the string that holds the function filter text.
3297 * @len - the length of the string.
3298 * @reset - non zero to reset all filters before applying this filter.
3299 *
3300 * Filters denote which functions should be enabled when tracing is enabled.
3301 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3302 */
3303void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3304{
3305 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3306}
3307EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3308
3309/**
3310 * ftrace_set_notrace - set a function to not trace in ftrace
3311 * @ops - the ops to set the notrace filter with
3312 * @buf - the string that holds the function notrace text.
3313 * @len - the length of the string.
3314 * @reset - non zero to reset all filters before applying this filter.
3315 *
3316 * Notrace Filters denote which functions should not be enabled when tracing
3317 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3318 * for tracing.
3319 */
3320void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003321{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003322 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003323}
Steven Rostedt936e0742011-05-05 22:54:01 -04003324EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003325
Steven Rostedt2af15d62009-05-28 13:37:24 -04003326/*
3327 * command line interface to allow users to set filters on boot up.
3328 */
3329#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3330static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3331static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3332
3333static int __init set_ftrace_notrace(char *str)
3334{
3335 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3336 return 1;
3337}
3338__setup("ftrace_notrace=", set_ftrace_notrace);
3339
3340static int __init set_ftrace_filter(char *str)
3341{
3342 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3343 return 1;
3344}
3345__setup("ftrace_filter=", set_ftrace_filter);
3346
Stefan Assmann369bc182009-10-12 22:17:21 +02003347#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003348static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003349static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3350
Stefan Assmann369bc182009-10-12 22:17:21 +02003351static int __init set_graph_function(char *str)
3352{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003353 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003354 return 1;
3355}
3356__setup("ftrace_graph_filter=", set_graph_function);
3357
3358static void __init set_ftrace_early_graph(char *buf)
3359{
3360 int ret;
3361 char *func;
3362
3363 while (buf) {
3364 func = strsep(&buf, ",");
3365 /* we allow only one expression at a time */
3366 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3367 func);
3368 if (ret)
3369 printk(KERN_DEBUG "ftrace: function %s not "
3370 "traceable\n", func);
3371 }
3372}
3373#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3374
Steven Rostedt2a85a372011-12-19 21:57:44 -05003375void __init
3376ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003377{
3378 char *func;
3379
3380 while (buf) {
3381 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003382 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003383 }
3384}
3385
3386static void __init set_ftrace_early_filters(void)
3387{
3388 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003389 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003390 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003391 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003392#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3393 if (ftrace_graph_buf[0])
3394 set_ftrace_early_graph(ftrace_graph_buf);
3395#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003396}
3397
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003398int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003399{
3400 struct seq_file *m = (struct seq_file *)file->private_data;
3401 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003402 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003403 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003404 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003405 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003406
Steven Rostedt41c52c02008-05-22 11:46:33 -04003407 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003408 if (file->f_mode & FMODE_READ) {
3409 iter = m->private;
3410
3411 seq_release(inode, file);
3412 } else
3413 iter = file->private_data;
3414
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003415 parser = &iter->parser;
3416 if (trace_parser_loaded(parser)) {
3417 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003418 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003419 }
3420
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003421 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003422
Steven Rostedt058e2972011-04-29 22:35:33 -04003423 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003424 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3425
3426 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003427 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003428 else
3429 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003430
Steven Rostedt058e2972011-04-29 22:35:33 -04003431 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003432 ret = ftrace_hash_move(iter->ops, filter_hash,
3433 orig_hash, iter->hash);
3434 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3435 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003436 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003437
Steven Rostedt058e2972011-04-29 22:35:33 -04003438 mutex_unlock(&ftrace_lock);
3439 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003440 free_ftrace_hash(iter->hash);
3441 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003442
Steven Rostedt41c52c02008-05-22 11:46:33 -04003443 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003444 return 0;
3445}
3446
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003447static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003448 .open = ftrace_avail_open,
3449 .read = seq_read,
3450 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003451 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003452};
3453
Steven Rostedt647bcd02011-05-03 14:39:21 -04003454static const struct file_operations ftrace_enabled_fops = {
3455 .open = ftrace_enabled_open,
3456 .read = seq_read,
3457 .llseek = seq_lseek,
3458 .release = seq_release_private,
3459};
3460
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003461static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003462 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003463 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003464 .write = ftrace_filter_write,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003465 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003466 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003467};
3468
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003469static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003470 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003471 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003472 .write = ftrace_notrace_write,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003473 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003474 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003475};
3476
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003477#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3478
3479static DEFINE_MUTEX(graph_lock);
3480
3481int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003482int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003483unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3484
3485static void *
Li Zefan85951842009-06-24 09:54:00 +08003486__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003487{
Li Zefan85951842009-06-24 09:54:00 +08003488 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003489 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003490 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003491}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003492
Li Zefan85951842009-06-24 09:54:00 +08003493static void *
3494g_next(struct seq_file *m, void *v, loff_t *pos)
3495{
3496 (*pos)++;
3497 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003498}
3499
3500static void *g_start(struct seq_file *m, loff_t *pos)
3501{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003502 mutex_lock(&graph_lock);
3503
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003504 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003505 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003506 return (void *)1;
3507
Li Zefan85951842009-06-24 09:54:00 +08003508 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003509}
3510
3511static void g_stop(struct seq_file *m, void *p)
3512{
3513 mutex_unlock(&graph_lock);
3514}
3515
3516static int g_show(struct seq_file *m, void *v)
3517{
3518 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003519
3520 if (!ptr)
3521 return 0;
3522
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003523 if (ptr == (unsigned long *)1) {
3524 seq_printf(m, "#### all functions enabled ####\n");
3525 return 0;
3526 }
3527
Steven Rostedtb375a112009-09-17 00:05:58 -04003528 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003529
3530 return 0;
3531}
3532
James Morris88e9d342009-09-22 16:43:43 -07003533static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003534 .start = g_start,
3535 .next = g_next,
3536 .stop = g_stop,
3537 .show = g_show,
3538};
3539
3540static int
3541ftrace_graph_open(struct inode *inode, struct file *file)
3542{
3543 int ret = 0;
3544
3545 if (unlikely(ftrace_disabled))
3546 return -ENODEV;
3547
3548 mutex_lock(&graph_lock);
3549 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003550 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003551 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003552 ftrace_graph_count = 0;
3553 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3554 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003555 mutex_unlock(&graph_lock);
3556
Li Zefana4ec5e02009-09-18 14:06:28 +08003557 if (file->f_mode & FMODE_READ)
3558 ret = seq_open(file, &ftrace_graph_seq_ops);
3559
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003560 return ret;
3561}
3562
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003563static int
Li Zefan87827112009-07-23 11:29:11 +08003564ftrace_graph_release(struct inode *inode, struct file *file)
3565{
3566 if (file->f_mode & FMODE_READ)
3567 seq_release(inode, file);
3568 return 0;
3569}
3570
3571static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003572ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003573{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003574 struct dyn_ftrace *rec;
3575 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003576 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003577 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003578 int type, not;
3579 char *search;
3580 bool exists;
3581 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003582
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003583 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003584 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003585 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3586 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003587
3588 search_len = strlen(search);
3589
Steven Rostedt52baf112009-02-14 01:15:39 -05003590 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003591
3592 if (unlikely(ftrace_disabled)) {
3593 mutex_unlock(&ftrace_lock);
3594 return -ENODEV;
3595 }
3596
Steven Rostedt265c8312009-02-13 12:43:56 -05003597 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003598
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003599 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003600 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003601 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003602 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003603 if (array[i] == rec->ip) {
3604 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003605 break;
3606 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003607 }
3608
3609 if (!not) {
3610 fail = 0;
3611 if (!exists) {
3612 array[(*idx)++] = rec->ip;
3613 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3614 goto out;
3615 }
3616 } else {
3617 if (exists) {
3618 array[i] = array[--(*idx)];
3619 array[*idx] = 0;
3620 fail = 0;
3621 }
3622 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003623 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003624 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003625out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003626 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003627
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003628 if (fail)
3629 return -EINVAL;
3630
Namhyung Kim761694a2013-04-11 16:01:38 +09003631 ftrace_graph_filter_enabled = !!(*idx);
3632
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003633 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003634}
3635
3636static ssize_t
3637ftrace_graph_write(struct file *file, const char __user *ubuf,
3638 size_t cnt, loff_t *ppos)
3639{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003640 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003641 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003642
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003643 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003644 return 0;
3645
3646 mutex_lock(&graph_lock);
3647
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003648 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3649 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003650 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003651 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003652
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003653 read = trace_get_user(&parser, ubuf, cnt, ppos);
3654
Li Zefan4ba79782009-09-22 13:52:20 +08003655 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003656 parser.buffer[parser.idx] = 0;
3657
3658 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003659 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003660 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003661 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003662 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003663 }
3664
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003665 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003666
3667out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003668 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003669out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003670 mutex_unlock(&graph_lock);
3671
3672 return ret;
3673}
3674
3675static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003676 .open = ftrace_graph_open,
3677 .read = seq_read,
3678 .write = ftrace_graph_write,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003679 .llseek = ftrace_filter_lseek,
Li Zefan87827112009-07-23 11:29:11 +08003680 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003681};
3682#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3683
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003684static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003685{
Steven Rostedt5072c592008-05-12 21:20:43 +02003686
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003687 trace_create_file("available_filter_functions", 0444,
3688 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003689
Steven Rostedt647bcd02011-05-03 14:39:21 -04003690 trace_create_file("enabled_functions", 0444,
3691 d_tracer, NULL, &ftrace_enabled_fops);
3692
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003693 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3694 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003695
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003696 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003697 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003698
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003699#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003700 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003701 NULL,
3702 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003703#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3704
Steven Rostedt5072c592008-05-12 21:20:43 +02003705 return 0;
3706}
3707
Steven Rostedt68950612011-12-16 17:06:45 -05003708static void ftrace_swap_recs(void *a, void *b, int size)
3709{
3710 struct dyn_ftrace *reca = a;
3711 struct dyn_ftrace *recb = b;
3712 struct dyn_ftrace t;
3713
3714 t = *reca;
3715 *reca = *recb;
3716 *recb = t;
3717}
3718
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003719static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003720 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003721 unsigned long *end)
3722{
Steven Rostedta7900872011-12-16 16:23:44 -05003723 struct ftrace_page *pg;
3724 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003725 unsigned long *p;
3726 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003727 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003728 int ret = -ENOMEM;
3729
3730 count = end - start;
3731
3732 if (!count)
3733 return 0;
3734
3735 pg = ftrace_allocate_pages(count);
3736 if (!pg)
3737 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003738
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003739 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003740
Steven Rostedt32082302011-12-16 14:42:37 -05003741 /*
3742 * Core and each module needs their own pages, as
3743 * modules will free them when they are removed.
3744 * Force a new page to be allocated for modules.
3745 */
Steven Rostedta7900872011-12-16 16:23:44 -05003746 if (!mod) {
3747 WARN_ON(ftrace_pages || ftrace_pages_start);
3748 /* First initialization */
3749 ftrace_pages = ftrace_pages_start = pg;
3750 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003751 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003752 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003753
Steven Rostedta7900872011-12-16 16:23:44 -05003754 if (WARN_ON(ftrace_pages->next)) {
3755 /* Hmm, we have free pages? */
3756 while (ftrace_pages->next)
3757 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003758 }
Steven Rostedta7900872011-12-16 16:23:44 -05003759
3760 ftrace_pages->next = pg;
3761 ftrace_pages = pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003762 }
3763
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003764 p = start;
3765 while (p < end) {
3766 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003767 /*
3768 * Some architecture linkers will pad between
3769 * the different mcount_loc sections of different
3770 * object files to satisfy alignments.
3771 * Skip any NULL pointers.
3772 */
3773 if (!addr)
3774 continue;
Steven Rostedta7900872011-12-16 16:23:44 -05003775 if (!ftrace_record_ip(addr))
3776 break;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003777 }
3778
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003779 /* These new locations need to be initialized */
3780 ftrace_new_pgs = pg;
3781
Steven Rostedt68950612011-12-16 17:06:45 -05003782 /* Make each individual set of pages sorted by ips */
3783 for (; pg; pg = pg->next)
3784 sort(pg->records, pg->index, sizeof(struct dyn_ftrace),
3785 ftrace_cmp_recs, ftrace_swap_recs);
3786
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003787 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003788 * We only need to disable interrupts on start up
3789 * because we are modifying code that an interrupt
3790 * may execute, and the modification is not atomic.
3791 * But for modules, nothing runs the code we modify
3792 * until we are finished with it, and there's no
3793 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003794 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003795 if (!mod)
3796 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003797 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003798 if (!mod)
3799 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003800 ret = 0;
3801 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003802 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003803
Steven Rostedta7900872011-12-16 16:23:44 -05003804 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003805}
3806
Steven Rostedt93eb6772009-04-15 13:24:06 -04003807#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003808
3809#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3810
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003811void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003812{
3813 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003814 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003815 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003816 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003817
Steven Rostedt93eb6772009-04-15 13:24:06 -04003818 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003819
3820 if (ftrace_disabled)
3821 goto out_unlock;
3822
Steven Rostedt32082302011-12-16 14:42:37 -05003823 /*
3824 * Each module has its own ftrace_pages, remove
3825 * them from the list.
3826 */
3827 last_pg = &ftrace_pages_start;
3828 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3829 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003830 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003831 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003832 * As core pages are first, the first
3833 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003834 */
Steven Rostedt32082302011-12-16 14:42:37 -05003835 if (WARN_ON(pg == ftrace_pages_start))
3836 goto out_unlock;
3837
3838 /* Check if we are deleting the last page */
3839 if (pg == ftrace_pages)
3840 ftrace_pages = next_to_ftrace_page(last_pg);
3841
3842 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003843 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3844 free_pages((unsigned long)pg->records, order);
3845 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05003846 } else
3847 last_pg = &pg->next;
3848 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003849 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003850 mutex_unlock(&ftrace_lock);
3851}
3852
3853static void ftrace_init_module(struct module *mod,
3854 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003855{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003856 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003857 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003858 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003859}
3860
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003861static int ftrace_module_notify_enter(struct notifier_block *self,
3862 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003863{
3864 struct module *mod = data;
3865
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003866 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003867 ftrace_init_module(mod, mod->ftrace_callsites,
3868 mod->ftrace_callsites +
3869 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003870 return 0;
3871}
3872
3873static int ftrace_module_notify_exit(struct notifier_block *self,
3874 unsigned long val, void *data)
3875{
3876 struct module *mod = data;
3877
3878 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003879 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003880
3881 return 0;
3882}
3883#else
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003884static int ftrace_module_notify_enter(struct notifier_block *self,
3885 unsigned long val, void *data)
3886{
3887 return 0;
3888}
3889static int ftrace_module_notify_exit(struct notifier_block *self,
3890 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003891{
3892 return 0;
3893}
3894#endif /* CONFIG_MODULES */
3895
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003896struct notifier_block ftrace_module_enter_nb = {
3897 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtf2a01002012-12-14 09:48:15 -05003898 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04003899};
3900
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003901struct notifier_block ftrace_module_exit_nb = {
3902 .notifier_call = ftrace_module_notify_exit,
3903 .priority = INT_MIN, /* Run after anything that can remove kprobes */
3904};
3905
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003906extern unsigned long __start_mcount_loc[];
3907extern unsigned long __stop_mcount_loc[];
3908
3909void __init ftrace_init(void)
3910{
3911 unsigned long count, addr, flags;
3912 int ret;
3913
3914 /* Keep the ftrace pointer to the stub */
3915 addr = (unsigned long)ftrace_stub;
3916
3917 local_irq_save(flags);
3918 ftrace_dyn_arch_init(&addr);
3919 local_irq_restore(flags);
3920
3921 /* ftrace_dyn_arch_init places the return code in addr */
3922 if (addr)
3923 goto failed;
3924
3925 count = __stop_mcount_loc - __start_mcount_loc;
3926
3927 ret = ftrace_dyn_table_alloc(count);
3928 if (ret)
3929 goto failed;
3930
3931 last_ftrace_enabled = ftrace_enabled = 1;
3932
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003933 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003934 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003935 __stop_mcount_loc);
3936
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003937 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003938 if (ret)
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003939 pr_warning("Failed to register trace ftrace module enter notifier\n");
3940
3941 ret = register_module_notifier(&ftrace_module_exit_nb);
3942 if (ret)
3943 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04003944
Steven Rostedt2af15d62009-05-28 13:37:24 -04003945 set_ftrace_early_filters();
3946
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003947 return;
3948 failed:
3949 ftrace_disabled = 1;
3950}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003951
Steven Rostedt3d083392008-05-12 21:20:42 +02003952#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003953
Steven Rostedt2b499382011-05-03 22:49:52 -04003954static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003955 .func = ftrace_stub,
3956};
3957
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003958static int __init ftrace_nodyn_init(void)
3959{
3960 ftrace_enabled = 1;
3961 return 0;
3962}
3963device_initcall(ftrace_nodyn_init);
3964
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003965static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3966static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003967/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05003968# define ftrace_startup(ops, command) \
3969 ({ \
3970 int ___ret = __register_ftrace_function(ops); \
3971 if (!___ret) \
3972 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3973 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003974 })
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05003975# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
3976
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003977# define ftrace_startup_sysctl() do { } while (0)
3978# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003979
3980static inline int
3981ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3982{
3983 return 1;
3984}
3985
Steven Rostedt3d083392008-05-12 21:20:42 +02003986#endif /* CONFIG_DYNAMIC_FTRACE */
3987
Steven Rostedtb8489142011-05-04 09:27:52 -04003988static void
Jiri Olsae2484912012-02-15 15:51:48 +01003989ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip)
3990{
3991 struct ftrace_ops *op;
3992
3993 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
3994 return;
3995
3996 /*
3997 * Some of the ops may be dynamically allocated,
3998 * they must be freed after a synchronize_sched().
3999 */
4000 preempt_disable_notrace();
4001 trace_recursion_set(TRACE_CONTROL_BIT);
4002 op = rcu_dereference_raw(ftrace_control_list);
4003 while (op != &ftrace_list_end) {
4004 if (!ftrace_function_local_disabled(op) &&
4005 ftrace_ops_test(op, ip))
4006 op->func(ip, parent_ip);
4007
4008 op = rcu_dereference_raw(op->next);
4009 };
4010 trace_recursion_clear(TRACE_CONTROL_BIT);
4011 preempt_enable_notrace();
4012}
4013
4014static struct ftrace_ops control_ops = {
4015 .func = ftrace_ops_control_func,
4016};
4017
4018static void
Steven Rostedtb8489142011-05-04 09:27:52 -04004019ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
4020{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004021 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04004022
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004023 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4024 return;
4025
4026 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004027 /*
4028 * Some of the ops may be dynamically allocated,
4029 * they must be freed after a synchronize_sched().
4030 */
4031 preempt_disable_notrace();
4032 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04004033 while (op != &ftrace_list_end) {
4034 if (ftrace_ops_test(op, ip))
4035 op->func(ip, parent_ip);
4036 op = rcu_dereference_raw(op->next);
4037 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004038 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004039 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04004040}
4041
Steven Rostedte32d8952008-12-04 00:26:41 -05004042static void clear_ftrace_swapper(void)
4043{
4044 struct task_struct *p;
4045 int cpu;
4046
4047 get_online_cpus();
4048 for_each_online_cpu(cpu) {
4049 p = idle_task(cpu);
4050 clear_tsk_trace_trace(p);
4051 }
4052 put_online_cpus();
4053}
4054
4055static void set_ftrace_swapper(void)
4056{
4057 struct task_struct *p;
4058 int cpu;
4059
4060 get_online_cpus();
4061 for_each_online_cpu(cpu) {
4062 p = idle_task(cpu);
4063 set_tsk_trace_trace(p);
4064 }
4065 put_online_cpus();
4066}
4067
4068static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004069{
4070 struct task_struct *p;
4071
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004072 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004073 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004074 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004075 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004076 rcu_read_unlock();
4077
Steven Rostedte32d8952008-12-04 00:26:41 -05004078 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004079}
4080
Steven Rostedte32d8952008-12-04 00:26:41 -05004081static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004082{
4083 struct task_struct *p;
4084
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004085 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004086 do_each_pid_task(pid, PIDTYPE_PID, p) {
4087 set_tsk_trace_trace(p);
4088 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004089 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004090}
4091
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004092static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004093{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004094 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004095 clear_ftrace_swapper();
4096 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004097 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004098}
4099
4100static void set_ftrace_pid_task(struct pid *pid)
4101{
4102 if (pid == ftrace_swapper_pid)
4103 set_ftrace_swapper();
4104 else
4105 set_ftrace_pid(pid);
4106}
4107
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004108static int ftrace_pid_add(int p)
4109{
4110 struct pid *pid;
4111 struct ftrace_pid *fpid;
4112 int ret = -EINVAL;
4113
4114 mutex_lock(&ftrace_lock);
4115
4116 if (!p)
4117 pid = ftrace_swapper_pid;
4118 else
4119 pid = find_get_pid(p);
4120
4121 if (!pid)
4122 goto out;
4123
4124 ret = 0;
4125
4126 list_for_each_entry(fpid, &ftrace_pids, list)
4127 if (fpid->pid == pid)
4128 goto out_put;
4129
4130 ret = -ENOMEM;
4131
4132 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4133 if (!fpid)
4134 goto out_put;
4135
4136 list_add(&fpid->list, &ftrace_pids);
4137 fpid->pid = pid;
4138
4139 set_ftrace_pid_task(pid);
4140
4141 ftrace_update_pid_func();
4142 ftrace_startup_enable(0);
4143
4144 mutex_unlock(&ftrace_lock);
4145 return 0;
4146
4147out_put:
4148 if (pid != ftrace_swapper_pid)
4149 put_pid(pid);
4150
4151out:
4152 mutex_unlock(&ftrace_lock);
4153 return ret;
4154}
4155
4156static void ftrace_pid_reset(void)
4157{
4158 struct ftrace_pid *fpid, *safe;
4159
4160 mutex_lock(&ftrace_lock);
4161 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4162 struct pid *pid = fpid->pid;
4163
4164 clear_ftrace_pid_task(pid);
4165
4166 list_del(&fpid->list);
4167 kfree(fpid);
4168 }
4169
4170 ftrace_update_pid_func();
4171 ftrace_startup_enable(0);
4172
4173 mutex_unlock(&ftrace_lock);
4174}
4175
4176static void *fpid_start(struct seq_file *m, loff_t *pos)
4177{
4178 mutex_lock(&ftrace_lock);
4179
4180 if (list_empty(&ftrace_pids) && (!*pos))
4181 return (void *) 1;
4182
4183 return seq_list_start(&ftrace_pids, *pos);
4184}
4185
4186static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4187{
4188 if (v == (void *)1)
4189 return NULL;
4190
4191 return seq_list_next(v, &ftrace_pids, pos);
4192}
4193
4194static void fpid_stop(struct seq_file *m, void *p)
4195{
4196 mutex_unlock(&ftrace_lock);
4197}
4198
4199static int fpid_show(struct seq_file *m, void *v)
4200{
4201 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4202
4203 if (v == (void *)1) {
4204 seq_printf(m, "no pid\n");
4205 return 0;
4206 }
4207
4208 if (fpid->pid == ftrace_swapper_pid)
4209 seq_printf(m, "swapper tasks\n");
4210 else
4211 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4212
4213 return 0;
4214}
4215
4216static const struct seq_operations ftrace_pid_sops = {
4217 .start = fpid_start,
4218 .next = fpid_next,
4219 .stop = fpid_stop,
4220 .show = fpid_show,
4221};
4222
4223static int
4224ftrace_pid_open(struct inode *inode, struct file *file)
4225{
4226 int ret = 0;
4227
4228 if ((file->f_mode & FMODE_WRITE) &&
4229 (file->f_flags & O_TRUNC))
4230 ftrace_pid_reset();
4231
4232 if (file->f_mode & FMODE_READ)
4233 ret = seq_open(file, &ftrace_pid_sops);
4234
4235 return ret;
4236}
4237
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004238static ssize_t
4239ftrace_pid_write(struct file *filp, const char __user *ubuf,
4240 size_t cnt, loff_t *ppos)
4241{
Ingo Molnar457dc922009-11-23 11:03:28 +01004242 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004243 long val;
4244 int ret;
4245
4246 if (cnt >= sizeof(buf))
4247 return -EINVAL;
4248
4249 if (copy_from_user(&buf, ubuf, cnt))
4250 return -EFAULT;
4251
4252 buf[cnt] = 0;
4253
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004254 /*
4255 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4256 * to clean the filter quietly.
4257 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004258 tmp = strstrip(buf);
4259 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004260 return 1;
4261
Ingo Molnar457dc922009-11-23 11:03:28 +01004262 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004263 if (ret < 0)
4264 return ret;
4265
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004266 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004267
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004268 return ret ? ret : cnt;
4269}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004270
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004271static int
4272ftrace_pid_release(struct inode *inode, struct file *file)
4273{
4274 if (file->f_mode & FMODE_READ)
4275 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004276
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004277 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004278}
4279
Steven Rostedt5e2336a02009-03-05 21:44:55 -05004280static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004281 .open = ftrace_pid_open,
4282 .write = ftrace_pid_write,
4283 .read = seq_read,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08004284 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004285 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004286};
4287
4288static __init int ftrace_init_debugfs(void)
4289{
4290 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004291
4292 d_tracer = tracing_init_dentry();
4293 if (!d_tracer)
4294 return 0;
4295
4296 ftrace_init_dyn_debugfs(d_tracer);
4297
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004298 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4299 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004300
4301 ftrace_profile_debugfs(d_tracer);
4302
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004303 return 0;
4304}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004305fs_initcall(ftrace_init_debugfs);
4306
Steven Rostedt3d083392008-05-12 21:20:42 +02004307/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004308 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004309 *
4310 * This function should be used by panic code. It stops ftrace
4311 * but in a not so nice way. If you need to simply kill ftrace
4312 * from a non-atomic section, use ftrace_kill.
4313 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004314void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004315{
4316 ftrace_disabled = 1;
4317 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004318 clear_ftrace_function();
4319}
4320
4321/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004322 * Test if ftrace is dead or not.
4323 */
4324int ftrace_is_dead(void)
4325{
4326 return ftrace_disabled;
4327}
4328
4329/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004330 * register_ftrace_function - register a function for profiling
4331 * @ops - ops structure that holds the function for profiling.
4332 *
4333 * Register a function to be called by all functions in the
4334 * kernel.
4335 *
4336 * Note: @ops->func and all the functions it calls must be labeled
4337 * with "notrace", otherwise it will go into a
4338 * recursive loop.
4339 */
4340int register_ftrace_function(struct ftrace_ops *ops)
4341{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004342 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004343
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004344 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004345
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004346 ret = ftrace_startup(ops, 0);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004347
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004348 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004349 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004350}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004351EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004352
4353/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004354 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004355 * @ops - ops structure that holds the function to unregister
4356 *
4357 * Unregister a function that was added to be called by ftrace profiling.
4358 */
4359int unregister_ftrace_function(struct ftrace_ops *ops)
4360{
4361 int ret;
4362
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004363 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004364 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004365 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004366
4367 return ret;
4368}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004369EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004370
Ingo Molnare309b412008-05-12 21:20:51 +02004371int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004372ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004373 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004374 loff_t *ppos)
4375{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004376 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004377
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004378 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004379
Steven Rostedt45a4a232011-04-21 23:16:46 -04004380 if (unlikely(ftrace_disabled))
4381 goto out;
4382
4383 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004384
Li Zefana32c7762009-06-26 16:55:51 +08004385 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004386 goto out;
4387
Li Zefana32c7762009-06-26 16:55:51 +08004388 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004389
4390 if (ftrace_enabled) {
4391
4392 ftrace_startup_sysctl();
4393
4394 /* we are starting ftrace again */
Jan Kiszkab81d3242013-03-26 17:53:03 +01004395 if (ftrace_ops_list != &ftrace_list_end)
4396 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004397
4398 } else {
4399 /* stopping ftrace calls (just send to ftrace_stub) */
4400 ftrace_trace_function = ftrace_stub;
4401
4402 ftrace_shutdown_sysctl();
4403 }
4404
4405 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004406 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004407 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004408}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004409
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004410#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004411
Steven Rostedt597af812009-04-03 15:24:12 -04004412static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004413static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004414
Steven Rostedte49dc192008-12-02 23:50:05 -05004415int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4416{
4417 return 0;
4418}
4419
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004420/* The callbacks that hook a function */
4421trace_func_graph_ret_t ftrace_graph_return =
4422 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004423trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004424
4425/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4426static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4427{
4428 int i;
4429 int ret = 0;
4430 unsigned long flags;
4431 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4432 struct task_struct *g, *t;
4433
4434 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4435 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4436 * sizeof(struct ftrace_ret_stack),
4437 GFP_KERNEL);
4438 if (!ret_stack_list[i]) {
4439 start = 0;
4440 end = i;
4441 ret = -ENOMEM;
4442 goto free;
4443 }
4444 }
4445
4446 read_lock_irqsave(&tasklist_lock, flags);
4447 do_each_thread(g, t) {
4448 if (start == end) {
4449 ret = -EAGAIN;
4450 goto unlock;
4451 }
4452
4453 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004454 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004455 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004456 t->curr_ret_stack = -1;
4457 /* Make sure the tasks see the -1 first: */
4458 smp_wmb();
4459 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004460 }
4461 } while_each_thread(g, t);
4462
4463unlock:
4464 read_unlock_irqrestore(&tasklist_lock, flags);
4465free:
4466 for (i = start; i < end; i++)
4467 kfree(ret_stack_list[i]);
4468 return ret;
4469}
4470
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004471static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004472ftrace_graph_probe_sched_switch(void *ignore,
4473 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004474{
4475 unsigned long long timestamp;
4476 int index;
4477
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004478 /*
4479 * Does the user want to count the time a function was asleep.
4480 * If so, do not update the time stamps.
4481 */
4482 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4483 return;
4484
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004485 timestamp = trace_clock_local();
4486
4487 prev->ftrace_timestamp = timestamp;
4488
4489 /* only process tasks that we timestamped */
4490 if (!next->ftrace_timestamp)
4491 return;
4492
4493 /*
4494 * Update all the counters in next to make up for the
4495 * time next was sleeping.
4496 */
4497 timestamp -= next->ftrace_timestamp;
4498
4499 for (index = next->curr_ret_stack; index >= 0; index--)
4500 next->ret_stack[index].calltime += timestamp;
4501}
4502
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004503/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004504static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004505{
4506 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004507 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004508
4509 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4510 sizeof(struct ftrace_ret_stack *),
4511 GFP_KERNEL);
4512
4513 if (!ret_stack_list)
4514 return -ENOMEM;
4515
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004516 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004517 for_each_online_cpu(cpu) {
4518 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004519 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004520 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004521
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004522 do {
4523 ret = alloc_retstack_tasklist(ret_stack_list);
4524 } while (ret == -EAGAIN);
4525
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004526 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004527 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004528 if (ret)
4529 pr_info("ftrace_graph: Couldn't activate tracepoint"
4530 " probe to kernel_sched_switch\n");
4531 }
4532
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004533 kfree(ret_stack_list);
4534 return ret;
4535}
4536
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004537/*
4538 * Hibernation protection.
4539 * The state of the current task is too much unstable during
4540 * suspend/restore to disk. We want to protect against that.
4541 */
4542static int
4543ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4544 void *unused)
4545{
4546 switch (state) {
4547 case PM_HIBERNATION_PREPARE:
4548 pause_graph_tracing();
4549 break;
4550
4551 case PM_POST_HIBERNATION:
4552 unpause_graph_tracing();
4553 break;
4554 }
4555 return NOTIFY_DONE;
4556}
4557
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004558/* Just a place holder for function graph */
4559static struct ftrace_ops fgraph_ops __read_mostly = {
4560 .func = ftrace_stub,
4561 .flags = FTRACE_OPS_FL_GLOBAL,
4562};
4563
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004564int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4565 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004566{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004567 int ret = 0;
4568
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004569 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004570
Steven Rostedt05ce5812009-03-24 00:18:31 -04004571 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004572 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004573 ret = -EBUSY;
4574 goto out;
4575 }
4576
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004577 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4578 register_pm_notifier(&ftrace_suspend_notifier);
4579
Steven Rostedt597af812009-04-03 15:24:12 -04004580 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004581 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004582 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004583 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004584 goto out;
4585 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004586
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004587 ftrace_graph_return = retfunc;
4588 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004589
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004590 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004591
4592out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004593 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004594 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004595}
4596
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004597void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004598{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004599 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004600
Steven Rostedt597af812009-04-03 15:24:12 -04004601 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004602 goto out;
4603
Steven Rostedt597af812009-04-03 15:24:12 -04004604 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004605 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004606 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004607 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004608 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004609 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004610
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004611 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004612 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004613}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004614
Steven Rostedt868baf02011-02-10 21:26:13 -05004615static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4616
4617static void
4618graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4619{
4620 atomic_set(&t->tracing_graph_pause, 0);
4621 atomic_set(&t->trace_overrun, 0);
4622 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004623 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004624 smp_wmb();
4625 t->ret_stack = ret_stack;
4626}
4627
4628/*
4629 * Allocate a return stack for the idle task. May be the first
4630 * time through, or it may be done by CPU hotplug online.
4631 */
4632void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4633{
4634 t->curr_ret_stack = -1;
4635 /*
4636 * The idle task has no parent, it either has its own
4637 * stack or no stack at all.
4638 */
4639 if (t->ret_stack)
4640 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4641
4642 if (ftrace_graph_active) {
4643 struct ftrace_ret_stack *ret_stack;
4644
4645 ret_stack = per_cpu(idle_ret_stack, cpu);
4646 if (!ret_stack) {
4647 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4648 * sizeof(struct ftrace_ret_stack),
4649 GFP_KERNEL);
4650 if (!ret_stack)
4651 return;
4652 per_cpu(idle_ret_stack, cpu) = ret_stack;
4653 }
4654 graph_init_task(t, ret_stack);
4655 }
4656}
4657
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004658/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004659void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004660{
Steven Rostedt84047e32009-06-02 16:51:55 -04004661 /* Make sure we do not use the parent ret_stack */
4662 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004663 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004664
Steven Rostedt597af812009-04-03 15:24:12 -04004665 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004666 struct ftrace_ret_stack *ret_stack;
4667
4668 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004669 * sizeof(struct ftrace_ret_stack),
4670 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004671 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004672 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004673 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004674 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004675}
4676
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004677void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004678{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004679 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4680
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004681 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004682 /* NULL must become visible to IRQs before we free it: */
4683 barrier();
4684
4685 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004686}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004687
4688void ftrace_graph_stop(void)
4689{
4690 ftrace_stop();
4691}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004692#endif