blob: 171b43b94a3ed87fcc83fba1f563567000bb4909 [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);
Steven Rostedtb8489142011-05-04 09:27:52 -0400379 } else
380 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
381
Steven Rostedt2b499382011-05-03 22:49:52 -0400382 if (ret < 0)
383 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400384
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400385 if (ftrace_enabled)
386 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200387
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500388 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200389}
390
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500391static void ftrace_update_pid_func(void)
392{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400393 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500394 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900395 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500396
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400397 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500398}
399
Steven Rostedt493762f2009-03-23 17:12:36 -0400400#ifdef CONFIG_FUNCTION_PROFILER
401struct ftrace_profile {
402 struct hlist_node node;
403 unsigned long ip;
404 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400405#ifdef CONFIG_FUNCTION_GRAPH_TRACER
406 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400407 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400408#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400409};
410
411struct ftrace_profile_page {
412 struct ftrace_profile_page *next;
413 unsigned long index;
414 struct ftrace_profile records[];
415};
416
Steven Rostedtcafb1682009-03-24 20:50:39 -0400417struct ftrace_profile_stat {
418 atomic_t disabled;
419 struct hlist_head *hash;
420 struct ftrace_profile_page *pages;
421 struct ftrace_profile_page *start;
422 struct tracer_stat stat;
423};
424
Steven Rostedt493762f2009-03-23 17:12:36 -0400425#define PROFILE_RECORDS_SIZE \
426 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
427
428#define PROFILES_PER_PAGE \
429 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
430
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400431static int ftrace_profile_bits __read_mostly;
432static int ftrace_profile_enabled __read_mostly;
433
434/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400435static DEFINE_MUTEX(ftrace_profile_lock);
436
Steven Rostedtcafb1682009-03-24 20:50:39 -0400437static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400438
439#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
440
Steven Rostedt493762f2009-03-23 17:12:36 -0400441static void *
442function_stat_next(void *v, int idx)
443{
444 struct ftrace_profile *rec = v;
445 struct ftrace_profile_page *pg;
446
447 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
448
449 again:
Li Zefan0296e422009-06-26 11:15:37 +0800450 if (idx != 0)
451 rec++;
452
Steven Rostedt493762f2009-03-23 17:12:36 -0400453 if ((void *)rec >= (void *)&pg->records[pg->index]) {
454 pg = pg->next;
455 if (!pg)
456 return NULL;
457 rec = &pg->records[0];
458 if (!rec->counter)
459 goto again;
460 }
461
462 return rec;
463}
464
465static void *function_stat_start(struct tracer_stat *trace)
466{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400467 struct ftrace_profile_stat *stat =
468 container_of(trace, struct ftrace_profile_stat, stat);
469
470 if (!stat || !stat->start)
471 return NULL;
472
473 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400474}
475
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400476#ifdef CONFIG_FUNCTION_GRAPH_TRACER
477/* function graph compares on total time */
478static int function_stat_cmp(void *p1, void *p2)
479{
480 struct ftrace_profile *a = p1;
481 struct ftrace_profile *b = p2;
482
483 if (a->time < b->time)
484 return -1;
485 if (a->time > b->time)
486 return 1;
487 else
488 return 0;
489}
490#else
491/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400492static int function_stat_cmp(void *p1, void *p2)
493{
494 struct ftrace_profile *a = p1;
495 struct ftrace_profile *b = p2;
496
497 if (a->counter < b->counter)
498 return -1;
499 if (a->counter > b->counter)
500 return 1;
501 else
502 return 0;
503}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400504#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400505
506static int function_stat_headers(struct seq_file *m)
507{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400508#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400509 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400510 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400511 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400512 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400513#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400514 seq_printf(m, " Function Hit\n"
515 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400516#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400517 return 0;
518}
519
520static int function_stat_show(struct seq_file *m, void *v)
521{
522 struct ftrace_profile *rec = v;
523 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800524 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400525#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400526 static struct trace_seq s;
527 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400528 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400529#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800530 mutex_lock(&ftrace_profile_lock);
531
532 /* we raced with function_profile_reset() */
533 if (unlikely(rec->counter == 0)) {
534 ret = -EBUSY;
535 goto out;
536 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400537
538 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400539 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400540
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400541#ifdef CONFIG_FUNCTION_GRAPH_TRACER
542 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400543 avg = rec->time;
544 do_div(avg, rec->counter);
545
Chase Douglase330b3b2010-04-26 14:02:05 -0400546 /* Sample standard deviation (s^2) */
547 if (rec->counter <= 1)
548 stddev = 0;
549 else {
550 stddev = rec->time_squared - rec->counter * avg * avg;
551 /*
552 * Divide only 1000 for ns^2 -> us^2 conversion.
553 * trace_print_graph_duration will divide 1000 again.
554 */
555 do_div(stddev, (rec->counter - 1) * 1000);
556 }
557
Steven Rostedt34886c82009-03-25 21:00:47 -0400558 trace_seq_init(&s);
559 trace_print_graph_duration(rec->time, &s);
560 trace_seq_puts(&s, " ");
561 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400562 trace_seq_puts(&s, " ");
563 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400564 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400565#endif
566 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800567out:
568 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400569
Li Zefan3aaba202010-08-23 16:50:12 +0800570 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400571}
572
Steven Rostedtcafb1682009-03-24 20:50:39 -0400573static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400574{
575 struct ftrace_profile_page *pg;
576
Steven Rostedtcafb1682009-03-24 20:50:39 -0400577 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400578
579 while (pg) {
580 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
581 pg->index = 0;
582 pg = pg->next;
583 }
584
Steven Rostedtcafb1682009-03-24 20:50:39 -0400585 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400586 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
587}
588
Steven Rostedtcafb1682009-03-24 20:50:39 -0400589int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400590{
591 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400592 int functions;
593 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400594 int i;
595
596 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400597 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400598 return 0;
599
Steven Rostedtcafb1682009-03-24 20:50:39 -0400600 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
601 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400602 return -ENOMEM;
603
Steven Rostedt318e0a72009-03-25 20:06:34 -0400604#ifdef CONFIG_DYNAMIC_FTRACE
605 functions = ftrace_update_tot_cnt;
606#else
607 /*
608 * We do not know the number of functions that exist because
609 * dynamic tracing is what counts them. With past experience
610 * we have around 20K functions. That should be more than enough.
611 * It is highly unlikely we will execute every function in
612 * the kernel.
613 */
614 functions = 20000;
615#endif
616
Steven Rostedtcafb1682009-03-24 20:50:39 -0400617 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400618
Steven Rostedt318e0a72009-03-25 20:06:34 -0400619 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
620
Namhyung Kim226c8ea2013-04-01 21:46:24 +0900621 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400622 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400623 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400624 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400625 pg = pg->next;
626 }
627
628 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400629
630 out_free:
631 pg = stat->start;
632 while (pg) {
633 unsigned long tmp = (unsigned long)pg;
634
635 pg = pg->next;
636 free_page(tmp);
637 }
638
Steven Rostedt318e0a72009-03-25 20:06:34 -0400639 stat->pages = NULL;
640 stat->start = NULL;
641
642 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400643}
644
Steven Rostedtcafb1682009-03-24 20:50:39 -0400645static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400646{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400647 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400648 int size;
649
Steven Rostedtcafb1682009-03-24 20:50:39 -0400650 stat = &per_cpu(ftrace_profile_stats, cpu);
651
652 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400653 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400654 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400655 return 0;
656 }
657
658 /*
659 * We are profiling all functions, but usually only a few thousand
660 * functions are hit. We'll make a hash of 1024 items.
661 */
662 size = FTRACE_PROFILE_HASH_SIZE;
663
Steven Rostedtcafb1682009-03-24 20:50:39 -0400664 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400665
Steven Rostedtcafb1682009-03-24 20:50:39 -0400666 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400667 return -ENOMEM;
668
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669 if (!ftrace_profile_bits) {
670 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400671
Steven Rostedtcafb1682009-03-24 20:50:39 -0400672 for (; size; size >>= 1)
673 ftrace_profile_bits++;
674 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400675
Steven Rostedt318e0a72009-03-25 20:06:34 -0400676 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400677 if (ftrace_profile_pages_init(stat) < 0) {
678 kfree(stat->hash);
679 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400680 return -ENOMEM;
681 }
682
683 return 0;
684}
685
Steven Rostedtcafb1682009-03-24 20:50:39 -0400686static int ftrace_profile_init(void)
687{
688 int cpu;
689 int ret = 0;
690
Miao Xie09951c92013-12-16 15:20:01 +0800691 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400692 ret = ftrace_profile_init_cpu(cpu);
693 if (ret)
694 break;
695 }
696
697 return ret;
698}
699
Steven Rostedt493762f2009-03-23 17:12:36 -0400700/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400701static struct ftrace_profile *
702ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400703{
704 struct ftrace_profile *rec;
705 struct hlist_head *hhd;
706 struct hlist_node *n;
707 unsigned long key;
708
709 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400710 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400711
712 if (hlist_empty(hhd))
713 return NULL;
714
715 hlist_for_each_entry_rcu(rec, n, hhd, node) {
716 if (rec->ip == ip)
717 return rec;
718 }
719
720 return NULL;
721}
722
Steven Rostedtcafb1682009-03-24 20:50:39 -0400723static void ftrace_add_profile(struct ftrace_profile_stat *stat,
724 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400725{
726 unsigned long key;
727
728 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400729 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400730}
731
Steven Rostedt318e0a72009-03-25 20:06:34 -0400732/*
733 * The memory is already allocated, this simply finds a new record to use.
734 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400735static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400736ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400737{
738 struct ftrace_profile *rec = NULL;
739
Steven Rostedt318e0a72009-03-25 20:06:34 -0400740 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400742 goto out;
743
Steven Rostedt493762f2009-03-23 17:12:36 -0400744 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400745 * Try to find the function again since an NMI
746 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400747 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400751
Steven Rostedtcafb1682009-03-24 20:50:39 -0400752 if (stat->pages->index == PROFILES_PER_PAGE) {
753 if (!stat->pages->next)
754 goto out;
755 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400756 }
757
Steven Rostedtcafb1682009-03-24 20:50:39 -0400758 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400759 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400760 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400761
Steven Rostedt493762f2009-03-23 17:12:36 -0400762 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400763 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400764
765 return rec;
766}
767
Steven Rostedt493762f2009-03-23 17:12:36 -0400768static void
769function_profile_call(unsigned long ip, unsigned long parent_ip)
770{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400771 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400772 struct ftrace_profile *rec;
773 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400774
775 if (!ftrace_profile_enabled)
776 return;
777
Steven Rostedt493762f2009-03-23 17:12:36 -0400778 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400779
780 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400781 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400782 goto out;
783
784 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400785 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400786 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400787 if (!rec)
788 goto out;
789 }
790
791 rec->counter++;
792 out:
793 local_irq_restore(flags);
794}
795
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400796#ifdef CONFIG_FUNCTION_GRAPH_TRACER
797static int profile_graph_entry(struct ftrace_graph_ent *trace)
798{
799 function_profile_call(trace->func, 0);
800 return 1;
801}
802
803static void profile_graph_return(struct ftrace_graph_ret *trace)
804{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400805 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400806 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400807 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400808 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400809
810 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400811 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400812 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400813 goto out;
814
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400815 /* If the calltime was zero'd ignore it */
816 if (!trace->calltime)
817 goto out;
818
Steven Rostedta2a16d62009-03-24 23:17:58 -0400819 calltime = trace->rettime - trace->calltime;
820
821 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
822 int index;
823
824 index = trace->depth;
825
826 /* Append this call time to the parent time to subtract */
827 if (index)
828 current->ret_stack[index - 1].subtime += calltime;
829
830 if (current->ret_stack[index].subtime < calltime)
831 calltime -= current->ret_stack[index].subtime;
832 else
833 calltime = 0;
834 }
835
Steven Rostedtcafb1682009-03-24 20:50:39 -0400836 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400837 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400838 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400839 rec->time_squared += calltime * calltime;
840 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400841
Steven Rostedtcafb1682009-03-24 20:50:39 -0400842 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400843 local_irq_restore(flags);
844}
845
846static int register_ftrace_profiler(void)
847{
848 return register_ftrace_graph(&profile_graph_return,
849 &profile_graph_entry);
850}
851
852static void unregister_ftrace_profiler(void)
853{
854 unregister_ftrace_graph();
855}
856#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100857static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400858 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400859};
860
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400861static int register_ftrace_profiler(void)
862{
863 return register_ftrace_function(&ftrace_profile_ops);
864}
865
866static void unregister_ftrace_profiler(void)
867{
868 unregister_ftrace_function(&ftrace_profile_ops);
869}
870#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
871
Steven Rostedt493762f2009-03-23 17:12:36 -0400872static ssize_t
873ftrace_profile_write(struct file *filp, const char __user *ubuf,
874 size_t cnt, loff_t *ppos)
875{
876 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400877 int ret;
878
Peter Huewe22fe9b52011-06-07 21:58:27 +0200879 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
880 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400881 return ret;
882
883 val = !!val;
884
885 mutex_lock(&ftrace_profile_lock);
886 if (ftrace_profile_enabled ^ val) {
887 if (val) {
888 ret = ftrace_profile_init();
889 if (ret < 0) {
890 cnt = ret;
891 goto out;
892 }
893
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400894 ret = register_ftrace_profiler();
895 if (ret < 0) {
896 cnt = ret;
897 goto out;
898 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400899 ftrace_profile_enabled = 1;
900 } else {
901 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400902 /*
903 * unregister_ftrace_profiler calls stop_machine
904 * so this acts like an synchronize_sched.
905 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400906 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400907 }
908 }
909 out:
910 mutex_unlock(&ftrace_profile_lock);
911
Jiri Olsacf8517c2009-10-23 19:36:16 -0400912 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400913
914 return cnt;
915}
916
917static ssize_t
918ftrace_profile_read(struct file *filp, char __user *ubuf,
919 size_t cnt, loff_t *ppos)
920{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400921 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400922 int r;
923
924 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
925 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
926}
927
928static const struct file_operations ftrace_profile_fops = {
929 .open = tracing_open_generic,
930 .read = ftrace_profile_read,
931 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200932 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400933};
934
Steven Rostedtcafb1682009-03-24 20:50:39 -0400935/* used to initialize the real stat files */
936static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400937 .name = "functions",
938 .stat_start = function_stat_start,
939 .stat_next = function_stat_next,
940 .stat_cmp = function_stat_cmp,
941 .stat_headers = function_stat_headers,
942 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400943};
944
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400945static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400946{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400947 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400948 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400949 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400950 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400951 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400952
Steven Rostedtcafb1682009-03-24 20:50:39 -0400953 for_each_possible_cpu(cpu) {
954 stat = &per_cpu(ftrace_profile_stats, cpu);
955
956 /* allocate enough for function name + cpu number */
957 name = kmalloc(32, GFP_KERNEL);
958 if (!name) {
959 /*
960 * The files created are permanent, if something happens
961 * we still do not free memory.
962 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400963 WARN(1,
964 "Could not allocate stat file for cpu %d\n",
965 cpu);
966 return;
967 }
968 stat->stat = function_stats;
969 snprintf(name, 32, "function%d", cpu);
970 stat->stat.name = name;
971 ret = register_stat_tracer(&stat->stat);
972 if (ret) {
973 WARN(1,
974 "Could not register function stat for cpu %d\n",
975 cpu);
976 kfree(name);
977 return;
978 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400979 }
980
981 entry = debugfs_create_file("function_profile_enabled", 0644,
982 d_tracer, NULL, &ftrace_profile_fops);
983 if (!entry)
984 pr_warning("Could not create debugfs "
985 "'function_profile_enabled' entry\n");
986}
987
988#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400989static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400990{
991}
992#endif /* CONFIG_FUNCTION_PROFILER */
993
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100994static struct pid * const ftrace_swapper_pid = &init_struct_pid;
995
Steven Rostedtbc4d36c2013-06-07 17:02:08 +0800996loff_t
997ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
998{
999 loff_t ret;
1000
1001 if (file->f_mode & FMODE_READ)
1002 ret = seq_lseek(file, offset, whence);
1003 else
1004 file->f_pos = ret = 1;
1005
1006 return ret;
1007}
1008
Steven Rostedt3d083392008-05-12 21:20:42 +02001009#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001010
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001011#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001012# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001013#endif
1014
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001015static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1016
Steven Rostedtb6887d72009-02-17 12:32:04 -05001017struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001018 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001019 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001020 unsigned long flags;
1021 unsigned long ip;
1022 void *data;
1023 struct rcu_head rcu;
1024};
1025
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001026struct ftrace_func_entry {
1027 struct hlist_node hlist;
1028 unsigned long ip;
1029};
1030
1031struct ftrace_hash {
1032 unsigned long size_bits;
1033 struct hlist_head *buckets;
1034 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001035 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001036};
1037
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001038/*
1039 * We make these constant because no one should touch them,
1040 * but they are used as the default "empty hash", to avoid allocating
1041 * it all the time. These are in a read only section such that if
1042 * anyone does try to modify it, it will cause an exception.
1043 */
1044static const struct hlist_head empty_buckets[1];
1045static const struct ftrace_hash empty_hash = {
1046 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001047};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001048#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001049
Steven Rostedt2b499382011-05-03 22:49:52 -04001050static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001051 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001052 .notrace_hash = EMPTY_HASH,
1053 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001054};
1055
Steven Rostedt41c52c02008-05-22 11:46:33 -04001056static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001057
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001058struct ftrace_page {
1059 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001060 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001061 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001062 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001063};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001064
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001065static struct ftrace_page *ftrace_new_pgs;
1066
Steven Rostedta7900872011-12-16 16:23:44 -05001067#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1068#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001069
1070/* estimate from running different kernels */
1071#define NR_TO_INIT 10000
1072
1073static struct ftrace_page *ftrace_pages_start;
1074static struct ftrace_page *ftrace_pages;
1075
Steven Rostedt06a51d92011-12-19 19:07:36 -05001076static bool ftrace_hash_empty(struct ftrace_hash *hash)
1077{
1078 return !hash || !hash->count;
1079}
1080
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001081static struct ftrace_func_entry *
1082ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1083{
1084 unsigned long key;
1085 struct ftrace_func_entry *entry;
1086 struct hlist_head *hhd;
1087 struct hlist_node *n;
1088
Steven Rostedt06a51d92011-12-19 19:07:36 -05001089 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001090 return NULL;
1091
1092 if (hash->size_bits > 0)
1093 key = hash_long(ip, hash->size_bits);
1094 else
1095 key = 0;
1096
1097 hhd = &hash->buckets[key];
1098
1099 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1100 if (entry->ip == ip)
1101 return entry;
1102 }
1103 return NULL;
1104}
1105
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001106static void __add_hash_entry(struct ftrace_hash *hash,
1107 struct ftrace_func_entry *entry)
1108{
1109 struct hlist_head *hhd;
1110 unsigned long key;
1111
1112 if (hash->size_bits)
1113 key = hash_long(entry->ip, hash->size_bits);
1114 else
1115 key = 0;
1116
1117 hhd = &hash->buckets[key];
1118 hlist_add_head(&entry->hlist, hhd);
1119 hash->count++;
1120}
1121
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001122static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1123{
1124 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001125
1126 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1127 if (!entry)
1128 return -ENOMEM;
1129
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001130 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001131 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001132
1133 return 0;
1134}
1135
1136static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001137free_hash_entry(struct ftrace_hash *hash,
1138 struct ftrace_func_entry *entry)
1139{
1140 hlist_del(&entry->hlist);
1141 kfree(entry);
1142 hash->count--;
1143}
1144
1145static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001146remove_hash_entry(struct ftrace_hash *hash,
1147 struct ftrace_func_entry *entry)
1148{
1149 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001150 hash->count--;
1151}
1152
1153static void ftrace_hash_clear(struct ftrace_hash *hash)
1154{
1155 struct hlist_head *hhd;
1156 struct hlist_node *tp, *tn;
1157 struct ftrace_func_entry *entry;
1158 int size = 1 << hash->size_bits;
1159 int i;
1160
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001161 if (!hash->count)
1162 return;
1163
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001164 for (i = 0; i < size; i++) {
1165 hhd = &hash->buckets[i];
1166 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001167 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001168 }
1169 FTRACE_WARN_ON(hash->count);
1170}
1171
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001172static void free_ftrace_hash(struct ftrace_hash *hash)
1173{
1174 if (!hash || hash == EMPTY_HASH)
1175 return;
1176 ftrace_hash_clear(hash);
1177 kfree(hash->buckets);
1178 kfree(hash);
1179}
1180
Steven Rostedt07fd5512011-05-05 18:03:47 -04001181static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1182{
1183 struct ftrace_hash *hash;
1184
1185 hash = container_of(rcu, struct ftrace_hash, rcu);
1186 free_ftrace_hash(hash);
1187}
1188
1189static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1190{
1191 if (!hash || hash == EMPTY_HASH)
1192 return;
1193 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1194}
1195
Jiri Olsa5500fa52012-02-15 15:51:54 +01001196void ftrace_free_filter(struct ftrace_ops *ops)
1197{
1198 free_ftrace_hash(ops->filter_hash);
1199 free_ftrace_hash(ops->notrace_hash);
1200}
1201
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001202static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1203{
1204 struct ftrace_hash *hash;
1205 int size;
1206
1207 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1208 if (!hash)
1209 return NULL;
1210
1211 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001212 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001213
1214 if (!hash->buckets) {
1215 kfree(hash);
1216 return NULL;
1217 }
1218
1219 hash->size_bits = size_bits;
1220
1221 return hash;
1222}
1223
1224static struct ftrace_hash *
1225alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1226{
1227 struct ftrace_func_entry *entry;
1228 struct ftrace_hash *new_hash;
1229 struct hlist_node *tp;
1230 int size;
1231 int ret;
1232 int i;
1233
1234 new_hash = alloc_ftrace_hash(size_bits);
1235 if (!new_hash)
1236 return NULL;
1237
1238 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001239 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001240 return new_hash;
1241
1242 size = 1 << hash->size_bits;
1243 for (i = 0; i < size; i++) {
1244 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1245 ret = add_hash_entry(new_hash, entry->ip);
1246 if (ret < 0)
1247 goto free_hash;
1248 }
1249 }
1250
1251 FTRACE_WARN_ON(new_hash->count != hash->count);
1252
1253 return new_hash;
1254
1255 free_hash:
1256 free_ftrace_hash(new_hash);
1257 return NULL;
1258}
1259
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001260static void
1261ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1262static void
1263ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1264
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001265static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001266ftrace_hash_move(struct ftrace_ops *ops, int enable,
1267 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001268{
1269 struct ftrace_func_entry *entry;
1270 struct hlist_node *tp, *tn;
1271 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001272 struct ftrace_hash *old_hash;
1273 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001274 unsigned long key;
1275 int size = src->count;
1276 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001277 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001278 int i;
1279
1280 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001281 * Remove the current set, update the hash and add
1282 * them back.
1283 */
1284 ftrace_hash_rec_disable(ops, enable);
1285
1286 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001287 * If the new source is empty, just free dst and assign it
1288 * the empty_hash.
1289 */
1290 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001291 free_ftrace_hash_rcu(*dst);
1292 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001293 /* still need to update the function records */
1294 ret = 0;
1295 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001296 }
1297
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001298 /*
1299 * Make the hash size about 1/2 the # found
1300 */
1301 for (size /= 2; size; size >>= 1)
1302 bits++;
1303
1304 /* Don't allocate too much */
1305 if (bits > FTRACE_HASH_MAX_BITS)
1306 bits = FTRACE_HASH_MAX_BITS;
1307
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001308 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001309 new_hash = alloc_ftrace_hash(bits);
1310 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001311 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001312
1313 size = 1 << src->size_bits;
1314 for (i = 0; i < size; i++) {
1315 hhd = &src->buckets[i];
1316 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1317 if (bits > 0)
1318 key = hash_long(entry->ip, bits);
1319 else
1320 key = 0;
1321 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001322 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001323 }
1324 }
1325
Steven Rostedt07fd5512011-05-05 18:03:47 -04001326 old_hash = *dst;
1327 rcu_assign_pointer(*dst, new_hash);
1328 free_ftrace_hash_rcu(old_hash);
1329
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001330 ret = 0;
1331 out:
1332 /*
1333 * Enable regardless of ret:
1334 * On success, we enable the new hash.
1335 * On failure, we re-enable the original hash.
1336 */
1337 ftrace_hash_rec_enable(ops, enable);
1338
1339 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001340}
1341
Steven Rostedt265c8312009-02-13 12:43:56 -05001342/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001343 * Test the hashes for this ops to see if we want to call
1344 * the ops->func or not.
1345 *
1346 * It's a match if the ip is in the ops->filter_hash or
1347 * the filter_hash does not exist or is empty,
1348 * AND
1349 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001350 *
1351 * This needs to be called with preemption disabled as
1352 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001353 */
1354static int
1355ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1356{
1357 struct ftrace_hash *filter_hash;
1358 struct ftrace_hash *notrace_hash;
1359 int ret;
1360
Steven Rostedtb8489142011-05-04 09:27:52 -04001361 filter_hash = rcu_dereference_raw(ops->filter_hash);
1362 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1363
Steven Rostedt06a51d92011-12-19 19:07:36 -05001364 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001365 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001366 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001367 !ftrace_lookup_ip(notrace_hash, ip)))
1368 ret = 1;
1369 else
1370 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001371
1372 return ret;
1373}
1374
1375/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001376 * This is a double for. Do not use 'break' to break out of the loop,
1377 * you must use a goto.
1378 */
1379#define do_for_each_ftrace_rec(pg, rec) \
1380 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1381 int _____i; \
1382 for (_____i = 0; _____i < pg->index; _____i++) { \
1383 rec = &pg->records[_____i];
1384
1385#define while_for_each_ftrace_rec() \
1386 } \
1387 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301388
Steven Rostedt5855fea2011-12-16 19:27:42 -05001389
1390static int ftrace_cmp_recs(const void *a, const void *b)
1391{
1392 const struct dyn_ftrace *reca = a;
1393 const struct dyn_ftrace *recb = b;
1394
1395 if (reca->ip > recb->ip)
1396 return 1;
1397 if (reca->ip < recb->ip)
1398 return -1;
1399 return 0;
1400}
1401
Steven Rostedtc88fd862011-08-16 09:53:39 -04001402/**
1403 * ftrace_location - return true if the ip giving is a traced location
1404 * @ip: the instruction pointer to check
1405 *
1406 * Returns 1 if @ip given is a pointer to a ftrace location.
1407 * That is, the instruction that is either a NOP or call to
1408 * the function tracer. It checks the ftrace internal tables to
1409 * determine if the address belongs or not.
1410 */
1411int ftrace_location(unsigned long ip)
1412{
1413 struct ftrace_page *pg;
1414 struct dyn_ftrace *rec;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001415 struct dyn_ftrace key;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001416
Steven Rostedt5855fea2011-12-16 19:27:42 -05001417 key.ip = ip;
1418
1419 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1420 rec = bsearch(&key, pg->records, pg->index,
1421 sizeof(struct dyn_ftrace),
1422 ftrace_cmp_recs);
1423 if (rec)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001424 return 1;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001425 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001426
1427 return 0;
1428}
1429
Steven Rostedted926f92011-05-03 13:25:24 -04001430static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1431 int filter_hash,
1432 bool inc)
1433{
1434 struct ftrace_hash *hash;
1435 struct ftrace_hash *other_hash;
1436 struct ftrace_page *pg;
1437 struct dyn_ftrace *rec;
1438 int count = 0;
1439 int all = 0;
1440
1441 /* Only update if the ops has been registered */
1442 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1443 return;
1444
1445 /*
1446 * In the filter_hash case:
1447 * If the count is zero, we update all records.
1448 * Otherwise we just update the items in the hash.
1449 *
1450 * In the notrace_hash case:
1451 * We enable the update in the hash.
1452 * As disabling notrace means enabling the tracing,
1453 * and enabling notrace means disabling, the inc variable
1454 * gets inversed.
1455 */
1456 if (filter_hash) {
1457 hash = ops->filter_hash;
1458 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001459 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001460 all = 1;
1461 } else {
1462 inc = !inc;
1463 hash = ops->notrace_hash;
1464 other_hash = ops->filter_hash;
1465 /*
1466 * If the notrace hash has no items,
1467 * then there's nothing to do.
1468 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001469 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001470 return;
1471 }
1472
1473 do_for_each_ftrace_rec(pg, rec) {
1474 int in_other_hash = 0;
1475 int in_hash = 0;
1476 int match = 0;
1477
1478 if (all) {
1479 /*
1480 * Only the filter_hash affects all records.
1481 * Update if the record is not in the notrace hash.
1482 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001483 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001484 match = 1;
1485 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001486 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1487 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001488
1489 /*
1490 *
1491 */
1492 if (filter_hash && in_hash && !in_other_hash)
1493 match = 1;
1494 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001495 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001496 match = 1;
1497 }
1498 if (!match)
1499 continue;
1500
1501 if (inc) {
1502 rec->flags++;
1503 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1504 return;
1505 } else {
1506 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1507 return;
1508 rec->flags--;
1509 }
1510 count++;
1511 /* Shortcut, if we handled all records, we are done. */
1512 if (!all && count == hash->count)
1513 return;
1514 } while_for_each_ftrace_rec();
1515}
1516
1517static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1518 int filter_hash)
1519{
1520 __ftrace_hash_rec_update(ops, filter_hash, 0);
1521}
1522
1523static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1524 int filter_hash)
1525{
1526 __ftrace_hash_rec_update(ops, filter_hash, 1);
1527}
1528
Ingo Molnare309b412008-05-12 21:20:51 +02001529static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001530{
Steven Rostedta7900872011-12-16 16:23:44 -05001531 if (ftrace_pages->index == ftrace_pages->size) {
1532 /* We should have allocated enough */
1533 if (WARN_ON(!ftrace_pages->next))
1534 return NULL;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001535 ftrace_pages = ftrace_pages->next;
1536 }
1537
1538 return &ftrace_pages->records[ftrace_pages->index++];
1539}
1540
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001541static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001542ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001543{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001544 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001545
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001546 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001547 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001548
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001549 rec = ftrace_alloc_dyn_node(ip);
1550 if (!rec)
1551 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001552
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001553 rec->ip = ip;
Steven Rostedt3d083392008-05-12 21:20:42 +02001554
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001555 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001556}
1557
Steven Rostedt05736a42008-09-22 14:55:47 -07001558static void print_ip_ins(const char *fmt, unsigned char *p)
1559{
1560 int i;
1561
1562 printk(KERN_CONT "%s", fmt);
1563
1564 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1565 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1566}
1567
Steven Rostedtc88fd862011-08-16 09:53:39 -04001568/**
1569 * ftrace_bug - report and shutdown function tracer
1570 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1571 * @ip: The address that failed
1572 *
1573 * The arch code that enables or disables the function tracing
1574 * can call ftrace_bug() when it has detected a problem in
1575 * modifying the code. @failed should be one of either:
1576 * EFAULT - if the problem happens on reading the @ip address
1577 * EINVAL - if what is read at @ip is not what was expected
1578 * EPERM - if the problem happens on writting to the @ip address
1579 */
1580void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001581{
1582 switch (failed) {
1583 case -EFAULT:
1584 FTRACE_WARN_ON_ONCE(1);
1585 pr_info("ftrace faulted on modifying ");
1586 print_ip_sym(ip);
1587 break;
1588 case -EINVAL:
1589 FTRACE_WARN_ON_ONCE(1);
1590 pr_info("ftrace failed to modify ");
1591 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001592 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001593 printk(KERN_CONT "\n");
1594 break;
1595 case -EPERM:
1596 FTRACE_WARN_ON_ONCE(1);
1597 pr_info("ftrace faulted on writing ");
1598 print_ip_sym(ip);
1599 break;
1600 default:
1601 FTRACE_WARN_ON_ONCE(1);
1602 pr_info("ftrace faulted on unknown error ");
1603 print_ip_sym(ip);
1604 }
1605}
1606
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001607
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001608/* Return 1 if the address range is reserved for ftrace */
1609int ftrace_text_reserved(void *start, void *end)
1610{
1611 struct dyn_ftrace *rec;
1612 struct ftrace_page *pg;
1613
1614 do_for_each_ftrace_rec(pg, rec) {
1615 if (rec->ip <= (unsigned long)end &&
1616 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1617 return 1;
1618 } while_for_each_ftrace_rec();
1619 return 0;
1620}
1621
Steven Rostedtc88fd862011-08-16 09:53:39 -04001622static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001623{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001624 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001625
Steven Rostedt982c3502008-11-15 16:31:41 -05001626 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001627 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001628 *
Steven Rostedted926f92011-05-03 13:25:24 -04001629 * If the record has a ref count, then we need to enable it
1630 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001631 *
Steven Rostedted926f92011-05-03 13:25:24 -04001632 * Otherwise we make sure its disabled.
1633 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001634 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001635 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001636 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001637 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001638 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001639
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001640 /* If the state of this record hasn't changed, then do nothing */
1641 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001642 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001643
1644 if (flag) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001645 if (update)
1646 rec->flags |= FTRACE_FL_ENABLED;
1647 return FTRACE_UPDATE_MAKE_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001648 }
1649
Steven Rostedtc88fd862011-08-16 09:53:39 -04001650 if (update)
1651 rec->flags &= ~FTRACE_FL_ENABLED;
1652
1653 return FTRACE_UPDATE_MAKE_NOP;
1654}
1655
1656/**
1657 * ftrace_update_record, set a record that now is tracing or not
1658 * @rec: the record to update
1659 * @enable: set to 1 if the record is tracing, zero to force disable
1660 *
1661 * The records that represent all functions that can be traced need
1662 * to be updated when tracing has been enabled.
1663 */
1664int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1665{
1666 return ftrace_check_record(rec, enable, 1);
1667}
1668
1669/**
1670 * ftrace_test_record, check if the record has been enabled or not
1671 * @rec: the record to test
1672 * @enable: set to 1 to check if enabled, 0 if it is disabled
1673 *
1674 * The arch code may need to test if a record is already set to
1675 * tracing to determine how to modify the function code that it
1676 * represents.
1677 */
1678int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1679{
1680 return ftrace_check_record(rec, enable, 0);
1681}
1682
1683static int
1684__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1685{
1686 unsigned long ftrace_addr;
1687 int ret;
1688
1689 ftrace_addr = (unsigned long)FTRACE_ADDR;
1690
1691 ret = ftrace_update_record(rec, enable);
1692
1693 switch (ret) {
1694 case FTRACE_UPDATE_IGNORE:
1695 return 0;
1696
1697 case FTRACE_UPDATE_MAKE_CALL:
1698 return ftrace_make_call(rec, ftrace_addr);
1699
1700 case FTRACE_UPDATE_MAKE_NOP:
1701 return ftrace_make_nop(NULL, rec, ftrace_addr);
1702 }
1703
1704 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001705}
1706
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001707static void ftrace_replace_code(int update)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001708{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001709 struct dyn_ftrace *rec;
1710 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001711 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001712
Steven Rostedt45a4a232011-04-21 23:16:46 -04001713 if (unlikely(ftrace_disabled))
1714 return;
1715
Steven Rostedt265c8312009-02-13 12:43:56 -05001716 do_for_each_ftrace_rec(pg, rec) {
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001717 failed = __ftrace_replace_code(rec, update);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001718 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001719 ftrace_bug(failed, rec->ip);
1720 /* Stop processing */
1721 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001722 }
1723 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001724}
1725
Steven Rostedtc88fd862011-08-16 09:53:39 -04001726struct ftrace_rec_iter {
1727 struct ftrace_page *pg;
1728 int index;
1729};
1730
1731/**
1732 * ftrace_rec_iter_start, start up iterating over traced functions
1733 *
1734 * Returns an iterator handle that is used to iterate over all
1735 * the records that represent address locations where functions
1736 * are traced.
1737 *
1738 * May return NULL if no records are available.
1739 */
1740struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1741{
1742 /*
1743 * We only use a single iterator.
1744 * Protected by the ftrace_lock mutex.
1745 */
1746 static struct ftrace_rec_iter ftrace_rec_iter;
1747 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1748
1749 iter->pg = ftrace_pages_start;
1750 iter->index = 0;
1751
1752 /* Could have empty pages */
1753 while (iter->pg && !iter->pg->index)
1754 iter->pg = iter->pg->next;
1755
1756 if (!iter->pg)
1757 return NULL;
1758
1759 return iter;
1760}
1761
1762/**
1763 * ftrace_rec_iter_next, get the next record to process.
1764 * @iter: The handle to the iterator.
1765 *
1766 * Returns the next iterator after the given iterator @iter.
1767 */
1768struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1769{
1770 iter->index++;
1771
1772 if (iter->index >= iter->pg->index) {
1773 iter->pg = iter->pg->next;
1774 iter->index = 0;
1775
1776 /* Could have empty pages */
1777 while (iter->pg && !iter->pg->index)
1778 iter->pg = iter->pg->next;
1779 }
1780
1781 if (!iter->pg)
1782 return NULL;
1783
1784 return iter;
1785}
1786
1787/**
1788 * ftrace_rec_iter_record, get the record at the iterator location
1789 * @iter: The current iterator location
1790 *
1791 * Returns the record that the current @iter is at.
1792 */
1793struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1794{
1795 return &iter->pg->records[iter->index];
1796}
1797
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301798static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001799ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001800{
1801 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001802 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001803
1804 ip = rec->ip;
1805
Steven Rostedt45a4a232011-04-21 23:16:46 -04001806 if (unlikely(ftrace_disabled))
1807 return 0;
1808
Shaohua Li25aac9d2009-01-09 11:29:40 +08001809 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001810 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001811 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301812 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001813 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301814 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001815}
1816
Steven Rostedt000ab692009-02-17 13:35:06 -05001817/*
1818 * archs can override this function if they must do something
1819 * before the modifying code is performed.
1820 */
1821int __weak ftrace_arch_code_modify_prepare(void)
1822{
1823 return 0;
1824}
1825
1826/*
1827 * archs can override this function if they must do something
1828 * after the modifying code is performed.
1829 */
1830int __weak ftrace_arch_code_modify_post_process(void)
1831{
1832 return 0;
1833}
1834
Ingo Molnare309b412008-05-12 21:20:51 +02001835static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001836{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001837 int *command = data;
1838
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001839 if (*command & FTRACE_UPDATE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001840 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001841 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001842 ftrace_replace_code(0);
1843
1844 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1845 ftrace_update_ftrace_func(ftrace_trace_function);
1846
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001847 if (*command & FTRACE_START_FUNC_RET)
1848 ftrace_enable_ftrace_graph_caller();
1849 else if (*command & FTRACE_STOP_FUNC_RET)
1850 ftrace_disable_ftrace_graph_caller();
1851
Steven Rostedtc88fd862011-08-16 09:53:39 -04001852 return 0;
1853}
1854
1855/**
1856 * ftrace_run_stop_machine, go back to the stop machine method
1857 * @command: The command to tell ftrace what to do
1858 *
1859 * If an arch needs to fall back to the stop machine method, the
1860 * it can call this function.
1861 */
1862void ftrace_run_stop_machine(int command)
1863{
1864 stop_machine(__ftrace_modify_code, &command, NULL);
1865}
1866
1867/**
1868 * arch_ftrace_update_code, modify the code to trace or not trace
1869 * @command: The command that needs to be done
1870 *
1871 * Archs can override this function if it does not need to
1872 * run stop_machine() to modify code.
1873 */
1874void __weak arch_ftrace_update_code(int command)
1875{
1876 ftrace_run_stop_machine(command);
1877}
1878
1879static void ftrace_run_update_code(int command)
1880{
1881 int ret;
1882
1883 ret = ftrace_arch_code_modify_prepare();
1884 FTRACE_WARN_ON(ret);
1885 if (ret)
1886 return;
1887 /*
1888 * Do not call function tracer while we update the code.
1889 * We are in stop machine.
1890 */
1891 function_trace_stop++;
1892
1893 /*
1894 * By default we use stop_machine() to modify the code.
1895 * But archs can do what ever they want as long as it
1896 * is safe. The stop_machine() is the safest, but also
1897 * produces the most overhead.
1898 */
1899 arch_ftrace_update_code(command);
1900
Steven Rostedt6331c282011-07-13 15:11:02 -04001901#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1902 /*
1903 * For archs that call ftrace_test_stop_func(), we must
1904 * wait till after we update all the function callers
1905 * before we update the callback. This keeps different
1906 * ops that record different functions from corrupting
1907 * each other.
1908 */
1909 __ftrace_trace_function = __ftrace_trace_function_delay;
1910#endif
1911 function_trace_stop--;
1912
Steven Rostedt000ab692009-02-17 13:35:06 -05001913 ret = ftrace_arch_code_modify_post_process();
1914 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001915}
1916
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001917static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001918static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001919static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001920
1921static void ftrace_startup_enable(int command)
1922{
1923 if (saved_ftrace_func != ftrace_trace_function) {
1924 saved_ftrace_func = ftrace_trace_function;
1925 command |= FTRACE_UPDATE_TRACE_FUNC;
1926 }
1927
1928 if (!command || !ftrace_enabled)
1929 return;
1930
1931 ftrace_run_update_code(command);
1932}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001933
Steven Rostedta1cd6172011-05-23 15:24:25 -04001934static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001935{
Steven Rostedtb8489142011-05-04 09:27:52 -04001936 bool hash_enable = true;
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001937 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04001938
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001939 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001940 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001941
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001942 ret = __register_ftrace_function(ops);
1943 if (ret)
1944 return ret;
1945
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001946 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001947 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001948
Steven Rostedtb8489142011-05-04 09:27:52 -04001949 /* ops marked global share the filter hashes */
1950 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1951 ops = &global_ops;
1952 /* Don't update hash if global is already set */
1953 if (global_start_up)
1954 hash_enable = false;
1955 global_start_up++;
1956 }
1957
Steven Rostedted926f92011-05-03 13:25:24 -04001958 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001959 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001960 ftrace_hash_rec_enable(ops, 1);
1961
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001962 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001963
1964 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001965}
1966
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001967static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001968{
Steven Rostedtb8489142011-05-04 09:27:52 -04001969 bool hash_disable = true;
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001970 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04001971
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001972 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001973 return -ENODEV;
1974
1975 ret = __unregister_ftrace_function(ops);
1976 if (ret)
1977 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001978
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001979 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001980 /*
1981 * Just warn in case of unbalance, no need to kill ftrace, it's not
1982 * critical but the ftrace_call callers may be never nopped again after
1983 * further ftrace uses.
1984 */
1985 WARN_ON_ONCE(ftrace_start_up < 0);
1986
Steven Rostedtb8489142011-05-04 09:27:52 -04001987 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1988 ops = &global_ops;
1989 global_start_up--;
1990 WARN_ON_ONCE(global_start_up < 0);
1991 /* Don't update hash if global still has users */
1992 if (global_start_up) {
1993 WARN_ON_ONCE(!ftrace_start_up);
1994 hash_disable = false;
1995 }
1996 }
1997
1998 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04001999 ftrace_hash_rec_disable(ops, 1);
2000
Steven Rostedtb8489142011-05-04 09:27:52 -04002001 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002002 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002003
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002004 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002005
2006 if (saved_ftrace_func != ftrace_trace_function) {
2007 saved_ftrace_func = ftrace_trace_function;
2008 command |= FTRACE_UPDATE_TRACE_FUNC;
2009 }
2010
Steven Rostedt29558662014-02-11 14:49:37 -05002011 if (!command || !ftrace_enabled) {
2012 /*
2013 * If these are control ops, they still need their
2014 * per_cpu field freed. Since, function tracing is
2015 * not currently active, we can just free them
2016 * without synchronizing all CPUs.
2017 */
2018 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2019 control_ops_free(ops);
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002020 return 0;
Steven Rostedt29558662014-02-11 14:49:37 -05002021 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002022
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002023 ftrace_run_update_code(command);
Steven Rostedt29558662014-02-11 14:49:37 -05002024
2025 /*
2026 * Dynamic ops may be freed, we must make sure that all
2027 * callers are done before leaving this function.
2028 * The same goes for freeing the per_cpu data of the control
2029 * ops.
2030 *
2031 * Again, normal synchronize_sched() is not good enough.
2032 * We need to do a hard force of sched synchronization.
2033 * This is because we use preempt_disable() to do RCU, but
2034 * the function tracers can be called where RCU is not watching
2035 * (like before user_exit()). We can not rely on the RCU
2036 * infrastructure to do the synchronization, thus we must do it
2037 * ourselves.
2038 */
2039 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2040 schedule_on_each_cpu(ftrace_sync);
2041
2042 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2043 control_ops_free(ops);
2044 }
2045
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002046 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002047}
2048
Ingo Molnare309b412008-05-12 21:20:51 +02002049static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002050{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002051 if (unlikely(ftrace_disabled))
2052 return;
2053
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002054 /* Force update next time */
2055 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002056 /* ftrace_start_up is true if we want ftrace running */
2057 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002058 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002059}
2060
Ingo Molnare309b412008-05-12 21:20:51 +02002061static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002062{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002063 if (unlikely(ftrace_disabled))
2064 return;
2065
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002066 /* ftrace_start_up is true if ftrace is running */
2067 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002068 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002069}
2070
Steven Rostedt3d083392008-05-12 21:20:42 +02002071static cycle_t ftrace_update_time;
2072static unsigned long ftrace_update_cnt;
2073unsigned long ftrace_update_tot_cnt;
2074
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002075static int ops_traces_mod(struct ftrace_ops *ops)
2076{
2077 struct ftrace_hash *hash;
2078
2079 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002080 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002081}
2082
Steven Rostedt31e88902008-11-14 16:21:19 -08002083static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002084{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002085 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002086 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302087 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002088 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002089 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002090
2091 /*
2092 * When adding a module, we need to check if tracers are
2093 * currently enabled and if they are set to trace all functions.
2094 * If they are, we need to enable the module functions as well
2095 * as update the reference counts for those function records.
2096 */
2097 if (mod) {
2098 struct ftrace_ops *ops;
2099
2100 for (ops = ftrace_ops_list;
2101 ops != &ftrace_list_end; ops = ops->next) {
2102 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2103 ops_traces_mod(ops))
2104 ref++;
2105 }
2106 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002107
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002108 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002109 ftrace_update_cnt = 0;
2110
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002111 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302112
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002113 for (i = 0; i < pg->index; i++) {
2114 /* If something went wrong, bail without enabling anything */
2115 if (unlikely(ftrace_disabled))
2116 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002117
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002118 p = &pg->records[i];
2119 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302120
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002121 /*
2122 * Do the initial record conversion from mcount jump
2123 * to the NOP instructions.
2124 */
2125 if (!ftrace_code_disable(mod, p))
2126 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002127
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002128 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002129
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002130 /*
2131 * If the tracing is enabled, go ahead and enable the record.
2132 *
2133 * The reason not to enable the record immediatelly is the
2134 * inherent check of ftrace_make_nop/ftrace_make_call for
2135 * correct previous instructions. Making first the NOP
2136 * conversion puts the module to the correct state, thus
2137 * passing the ftrace_make_call check.
2138 */
2139 if (ftrace_start_up && ref) {
2140 int failed = __ftrace_replace_code(p, 1);
2141 if (failed)
2142 ftrace_bug(failed, p->ip);
2143 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002144 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002145 }
2146
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002147 ftrace_new_pgs = NULL;
2148
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002149 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002150 ftrace_update_time = stop - start;
2151 ftrace_update_tot_cnt += ftrace_update_cnt;
2152
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002153 return 0;
2154}
2155
Steven Rostedta7900872011-12-16 16:23:44 -05002156static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002157{
Steven Rostedta7900872011-12-16 16:23:44 -05002158 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002159 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002160
Steven Rostedta7900872011-12-16 16:23:44 -05002161 if (WARN_ON(!count))
2162 return -EINVAL;
2163
2164 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002165
2166 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002167 * We want to fill as much as possible. No more than a page
2168 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002169 */
Steven Rostedta7900872011-12-16 16:23:44 -05002170 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2171 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002172
Steven Rostedta7900872011-12-16 16:23:44 -05002173 again:
2174 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2175
2176 if (!pg->records) {
2177 /* if we can't allocate this size, try something smaller */
2178 if (!order)
2179 return -ENOMEM;
2180 order >>= 1;
2181 goto again;
2182 }
2183
2184 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2185 pg->size = cnt;
2186
2187 if (cnt > count)
2188 cnt = count;
2189
2190 return cnt;
2191}
2192
2193static struct ftrace_page *
2194ftrace_allocate_pages(unsigned long num_to_init)
2195{
2196 struct ftrace_page *start_pg;
2197 struct ftrace_page *pg;
2198 int order;
2199 int cnt;
2200
2201 if (!num_to_init)
2202 return 0;
2203
2204 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2205 if (!pg)
2206 return NULL;
2207
2208 /*
2209 * Try to allocate as much as possible in one continues
2210 * location that fills in all of the space. We want to
2211 * waste as little space as possible.
2212 */
2213 for (;;) {
2214 cnt = ftrace_allocate_records(pg, num_to_init);
2215 if (cnt < 0)
2216 goto free_pages;
2217
2218 num_to_init -= cnt;
2219 if (!num_to_init)
2220 break;
2221
2222 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2223 if (!pg->next)
2224 goto free_pages;
2225
2226 pg = pg->next;
2227 }
2228
2229 return start_pg;
2230
2231 free_pages:
2232 while (start_pg) {
2233 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2234 free_pages((unsigned long)pg->records, order);
2235 start_pg = pg->next;
2236 kfree(pg);
2237 pg = start_pg;
2238 }
2239 pr_info("ftrace: FAILED to allocate memory for functions\n");
2240 return NULL;
2241}
2242
2243static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2244{
2245 int cnt;
2246
2247 if (!num_to_init) {
2248 pr_info("ftrace: No functions to be traced?\n");
2249 return -1;
2250 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002251
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002252 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002253 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002254 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002255
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002256 return 0;
2257}
2258
Steven Rostedt5072c592008-05-12 21:20:43 +02002259#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2260
2261struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002262 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002263 loff_t func_pos;
2264 struct ftrace_page *pg;
2265 struct dyn_ftrace *func;
2266 struct ftrace_func_probe *probe;
2267 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002268 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002269 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002270 int hidx;
2271 int idx;
2272 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002273};
2274
Ingo Molnare309b412008-05-12 21:20:51 +02002275static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002276t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002277{
2278 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002279 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002280 struct hlist_head *hhd;
2281
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002282 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002283 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002284
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002285 if (iter->probe)
2286 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002287 retry:
2288 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2289 return NULL;
2290
2291 hhd = &ftrace_func_hash[iter->hidx];
2292
2293 if (hlist_empty(hhd)) {
2294 iter->hidx++;
2295 hnd = NULL;
2296 goto retry;
2297 }
2298
2299 if (!hnd)
2300 hnd = hhd->first;
2301 else {
2302 hnd = hnd->next;
2303 if (!hnd) {
2304 iter->hidx++;
2305 goto retry;
2306 }
2307 }
2308
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002309 if (WARN_ON_ONCE(!hnd))
2310 return NULL;
2311
2312 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2313
2314 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002315}
2316
2317static void *t_hash_start(struct seq_file *m, loff_t *pos)
2318{
2319 struct ftrace_iterator *iter = m->private;
2320 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002321 loff_t l;
2322
Steven Rostedt69a30832011-12-19 15:21:16 -05002323 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2324 return NULL;
2325
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002326 if (iter->func_pos > *pos)
2327 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002328
Li Zefand82d6242009-06-24 09:54:54 +08002329 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002330 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002331 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002332 if (!p)
2333 break;
2334 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002335 if (!p)
2336 return NULL;
2337
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002338 /* Only set this if we have an item */
2339 iter->flags |= FTRACE_ITER_HASH;
2340
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002341 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002342}
2343
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002344static int
2345t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002346{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002347 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002348
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002349 rec = iter->probe;
2350 if (WARN_ON_ONCE(!rec))
2351 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002352
Steven Rostedt809dcf22009-02-16 23:06:01 -05002353 if (rec->ops->print)
2354 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2355
Steven Rostedtb375a112009-09-17 00:05:58 -04002356 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002357
2358 if (rec->data)
2359 seq_printf(m, ":%p", rec->data);
2360 seq_putc(m, '\n');
2361
2362 return 0;
2363}
2364
2365static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002366t_next(struct seq_file *m, void *v, loff_t *pos)
2367{
2368 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002369 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002370 struct dyn_ftrace *rec = NULL;
2371
Steven Rostedt45a4a232011-04-21 23:16:46 -04002372 if (unlikely(ftrace_disabled))
2373 return NULL;
2374
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002375 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002376 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002377
Steven Rostedt5072c592008-05-12 21:20:43 +02002378 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002379 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002380
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002381 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002382 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002383
Steven Rostedt5072c592008-05-12 21:20:43 +02002384 retry:
2385 if (iter->idx >= iter->pg->index) {
2386 if (iter->pg->next) {
2387 iter->pg = iter->pg->next;
2388 iter->idx = 0;
2389 goto retry;
2390 }
2391 } else {
2392 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002393 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002394 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002395
Steven Rostedt41c52c02008-05-22 11:46:33 -04002396 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002397 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2398
2399 ((iter->flags & FTRACE_ITER_ENABLED) &&
2400 !(rec->flags & ~FTRACE_FL_MASK))) {
2401
Steven Rostedt5072c592008-05-12 21:20:43 +02002402 rec = NULL;
2403 goto retry;
2404 }
2405 }
2406
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002407 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002408 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002409
2410 iter->func = rec;
2411
2412 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002413}
2414
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002415static void reset_iter_read(struct ftrace_iterator *iter)
2416{
2417 iter->pos = 0;
2418 iter->func_pos = 0;
Dan Carpenter455d8952012-06-09 19:10:27 +03002419 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002420}
2421
2422static void *t_start(struct seq_file *m, loff_t *pos)
2423{
2424 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002425 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002426 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002427 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002428
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002429 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002430
2431 if (unlikely(ftrace_disabled))
2432 return NULL;
2433
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002434 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002435 * If an lseek was done, then reset and start from beginning.
2436 */
2437 if (*pos < iter->pos)
2438 reset_iter_read(iter);
2439
2440 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002441 * For set_ftrace_filter reading, if we have the filter
2442 * off, we can short cut and just print out that all
2443 * functions are enabled.
2444 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002445 if (iter->flags & FTRACE_ITER_FILTER &&
2446 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002447 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002448 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002449 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002450 /* reset in case of seek/pread */
2451 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002452 return iter;
2453 }
2454
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002455 if (iter->flags & FTRACE_ITER_HASH)
2456 return t_hash_start(m, pos);
2457
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002458 /*
2459 * Unfortunately, we need to restart at ftrace_pages_start
2460 * every time we let go of the ftrace_mutex. This is because
2461 * those pointers can change without the lock.
2462 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002463 iter->pg = ftrace_pages_start;
2464 iter->idx = 0;
2465 for (l = 0; l <= *pos; ) {
2466 p = t_next(m, p, &l);
2467 if (!p)
2468 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002469 }
walimis5821e1b2008-11-15 15:19:06 +08002470
Steven Rostedt69a30832011-12-19 15:21:16 -05002471 if (!p)
2472 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002473
2474 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002475}
2476
2477static void t_stop(struct seq_file *m, void *p)
2478{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002479 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002480}
2481
2482static int t_show(struct seq_file *m, void *v)
2483{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002484 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002485 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002486
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002487 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002488 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002489
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002490 if (iter->flags & FTRACE_ITER_PRINTALL) {
2491 seq_printf(m, "#### all functions enabled ####\n");
2492 return 0;
2493 }
2494
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002495 rec = iter->func;
2496
Steven Rostedt5072c592008-05-12 21:20:43 +02002497 if (!rec)
2498 return 0;
2499
Steven Rostedt647bcd02011-05-03 14:39:21 -04002500 seq_printf(m, "%ps", (void *)rec->ip);
2501 if (iter->flags & FTRACE_ITER_ENABLED)
2502 seq_printf(m, " (%ld)",
2503 rec->flags & ~FTRACE_FL_MASK);
2504 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002505
2506 return 0;
2507}
2508
James Morris88e9d342009-09-22 16:43:43 -07002509static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002510 .start = t_start,
2511 .next = t_next,
2512 .stop = t_stop,
2513 .show = t_show,
2514};
2515
Ingo Molnare309b412008-05-12 21:20:51 +02002516static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002517ftrace_avail_open(struct inode *inode, struct file *file)
2518{
2519 struct ftrace_iterator *iter;
2520 int ret;
2521
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002522 if (unlikely(ftrace_disabled))
2523 return -ENODEV;
2524
Steven Rostedt5072c592008-05-12 21:20:43 +02002525 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2526 if (!iter)
2527 return -ENOMEM;
2528
2529 iter->pg = ftrace_pages_start;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002530 iter->ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002531
2532 ret = seq_open(file, &show_ftrace_seq_ops);
2533 if (!ret) {
2534 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002535
Steven Rostedt5072c592008-05-12 21:20:43 +02002536 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002537 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002538 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002539 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002540
2541 return ret;
2542}
2543
Steven Rostedt647bcd02011-05-03 14:39:21 -04002544static int
2545ftrace_enabled_open(struct inode *inode, struct file *file)
2546{
2547 struct ftrace_iterator *iter;
2548 int ret;
2549
2550 if (unlikely(ftrace_disabled))
2551 return -ENODEV;
2552
2553 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2554 if (!iter)
2555 return -ENOMEM;
2556
2557 iter->pg = ftrace_pages_start;
2558 iter->flags = FTRACE_ITER_ENABLED;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002559 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002560
2561 ret = seq_open(file, &show_ftrace_seq_ops);
2562 if (!ret) {
2563 struct seq_file *m = file->private_data;
2564
2565 m->private = iter;
2566 } else {
2567 kfree(iter);
2568 }
2569
2570 return ret;
2571}
2572
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002573static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002574{
Steven Rostedt52baf112009-02-14 01:15:39 -05002575 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002576 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002577 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002578}
2579
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002580/**
2581 * ftrace_regex_open - initialize function tracer filter files
2582 * @ops: The ftrace_ops that hold the hash filters
2583 * @flag: The type of filter to process
2584 * @inode: The inode, usually passed in to your open routine
2585 * @file: The file, usually passed in to your open routine
2586 *
2587 * ftrace_regex_open() initializes the filter files for the
2588 * @ops. Depending on @flag it may process the filter hash or
2589 * the notrace hash of @ops. With this called from the open
2590 * routine, you can use ftrace_filter_write() for the write
2591 * routine if @flag has FTRACE_ITER_FILTER set, or
2592 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedtbc4d36c2013-06-07 17:02:08 +08002593 * ftrace_filter_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002594 * release must call ftrace_regex_release().
2595 */
2596int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002597ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002598 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002599{
2600 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002601 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002602 int ret = 0;
2603
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002604 if (unlikely(ftrace_disabled))
2605 return -ENODEV;
2606
Steven Rostedt5072c592008-05-12 21:20:43 +02002607 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2608 if (!iter)
2609 return -ENOMEM;
2610
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002611 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2612 kfree(iter);
2613 return -ENOMEM;
2614 }
2615
Steven Rostedtf45948e2011-05-02 12:29:25 -04002616 if (flag & FTRACE_ITER_NOTRACE)
2617 hash = ops->notrace_hash;
2618 else
2619 hash = ops->filter_hash;
2620
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002621 iter->ops = ops;
2622 iter->flags = flag;
2623
2624 if (file->f_mode & FMODE_WRITE) {
2625 mutex_lock(&ftrace_lock);
2626 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2627 mutex_unlock(&ftrace_lock);
2628
2629 if (!iter->hash) {
2630 trace_parser_put(&iter->parser);
2631 kfree(iter);
2632 return -ENOMEM;
2633 }
2634 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002635
Steven Rostedt41c52c02008-05-22 11:46:33 -04002636 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002637
Steven Rostedt5072c592008-05-12 21:20:43 +02002638 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002639 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002640 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002641
2642 if (file->f_mode & FMODE_READ) {
2643 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002644
2645 ret = seq_open(file, &show_ftrace_seq_ops);
2646 if (!ret) {
2647 struct seq_file *m = file->private_data;
2648 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002649 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002650 /* Failed */
2651 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002652 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002653 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002654 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002655 } else
2656 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002657 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002658
2659 return ret;
2660}
2661
Steven Rostedt41c52c02008-05-22 11:46:33 -04002662static int
2663ftrace_filter_open(struct inode *inode, struct file *file)
2664{
Steven Rostedt69a30832011-12-19 15:21:16 -05002665 return ftrace_regex_open(&global_ops,
2666 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2667 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002668}
2669
2670static int
2671ftrace_notrace_open(struct inode *inode, struct file *file)
2672{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002673 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002674 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002675}
2676
Steven Rostedt64e7c442009-02-13 17:08:48 -05002677static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002678{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002679 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002680 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002681
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002682 switch (type) {
2683 case MATCH_FULL:
2684 if (strcmp(str, regex) == 0)
2685 matched = 1;
2686 break;
2687 case MATCH_FRONT_ONLY:
2688 if (strncmp(str, regex, len) == 0)
2689 matched = 1;
2690 break;
2691 case MATCH_MIDDLE_ONLY:
2692 if (strstr(str, regex))
2693 matched = 1;
2694 break;
2695 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002696 slen = strlen(str);
2697 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002698 matched = 1;
2699 break;
2700 }
2701
2702 return matched;
2703}
2704
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002705static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002706enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002707{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002708 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002709 int ret = 0;
2710
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002711 entry = ftrace_lookup_ip(hash, rec->ip);
2712 if (not) {
2713 /* Do nothing if it doesn't exist */
2714 if (!entry)
2715 return 0;
2716
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002717 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002718 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002719 /* Do nothing if it exists */
2720 if (entry)
2721 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002722
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002723 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002724 }
2725 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002726}
2727
Steven Rostedt64e7c442009-02-13 17:08:48 -05002728static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002729ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2730 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002731{
2732 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002733 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002734
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002735 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2736
2737 if (mod) {
2738 /* module lookup requires matching the module */
2739 if (!modname || strcmp(modname, mod))
2740 return 0;
2741
2742 /* blank search means to match all funcs in the mod */
2743 if (!len)
2744 return 1;
2745 }
2746
Steven Rostedt64e7c442009-02-13 17:08:48 -05002747 return ftrace_match(str, regex, len, type);
2748}
2749
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002750static int
2751match_records(struct ftrace_hash *hash, char *buff,
2752 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002753{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002754 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002755 struct ftrace_page *pg;
2756 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002757 int type = MATCH_FULL;
2758 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002759 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002760 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002761
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002762 if (len) {
2763 type = filter_parse_regex(buff, len, &search, &not);
2764 search_len = strlen(search);
2765 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002766
Steven Rostedt52baf112009-02-14 01:15:39 -05002767 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002768
2769 if (unlikely(ftrace_disabled))
2770 goto out_unlock;
2771
Steven Rostedt265c8312009-02-13 12:43:56 -05002772 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002773 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002774 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002775 if (ret < 0) {
2776 found = ret;
2777 goto out_unlock;
2778 }
Li Zefan311d16d2009-12-08 11:15:11 +08002779 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002780 }
2781 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002782 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002783 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002784
2785 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002786}
2787
Steven Rostedt64e7c442009-02-13 17:08:48 -05002788static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002789ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002790{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002791 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002792}
2793
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002794static int
2795ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002796{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002797 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002798
Steven Rostedt64e7c442009-02-13 17:08:48 -05002799 /* blank or '*' mean the same */
2800 if (strcmp(buff, "*") == 0)
2801 buff[0] = 0;
2802
2803 /* handle the case of 'dont filter this module' */
2804 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2805 buff[0] = 0;
2806 not = 1;
2807 }
2808
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002809 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002810}
2811
Steven Rostedtf6180772009-02-14 00:40:25 -05002812/*
2813 * We register the module command as a template to show others how
2814 * to register the a command as well.
2815 */
2816
2817static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002818ftrace_mod_callback(struct ftrace_hash *hash,
2819 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002820{
2821 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002822 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002823
2824 /*
2825 * cmd == 'mod' because we only registered this func
2826 * for the 'mod' ftrace_func_command.
2827 * But if you register one func with multiple commands,
2828 * you can tell which command was used by the cmd
2829 * parameter.
2830 */
2831
2832 /* we must have a module name */
2833 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002834 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002835
2836 mod = strsep(&param, ":");
2837 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002838 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002839
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002840 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002841 if (!ret)
2842 ret = -EINVAL;
2843 if (ret < 0)
2844 return ret;
2845
2846 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002847}
2848
2849static struct ftrace_func_command ftrace_mod_cmd = {
2850 .name = "mod",
2851 .func = ftrace_mod_callback,
2852};
2853
2854static int __init ftrace_mod_cmd_init(void)
2855{
2856 return register_ftrace_command(&ftrace_mod_cmd);
2857}
2858device_initcall(ftrace_mod_cmd_init);
2859
Steven Rostedt59df055f2009-02-14 15:29:06 -05002860static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002861function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002862{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002863 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002864 struct hlist_head *hhd;
2865 struct hlist_node *n;
2866 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002867
2868 key = hash_long(ip, FTRACE_HASH_BITS);
2869
2870 hhd = &ftrace_func_hash[key];
2871
2872 if (hlist_empty(hhd))
2873 return;
2874
2875 /*
2876 * Disable preemption for these calls to prevent a RCU grace
2877 * period. This syncs the hash iteration and freeing of items
2878 * on the hash. rcu_read_lock is too dangerous here.
2879 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002880 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002881 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2882 if (entry->ip == ip)
2883 entry->ops->func(ip, parent_ip, &entry->data);
2884 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002885 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002886}
2887
Steven Rostedtb6887d72009-02-17 12:32:04 -05002888static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002889{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002890 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002891};
2892
Steven Rostedtb6887d72009-02-17 12:32:04 -05002893static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002894
Steven Rostedtb6887d72009-02-17 12:32:04 -05002895static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002896{
Steven Rostedtb8489142011-05-04 09:27:52 -04002897 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002898 int i;
2899
Steven Rostedtb6887d72009-02-17 12:32:04 -05002900 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002901 return;
2902
2903 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2904 struct hlist_head *hhd = &ftrace_func_hash[i];
2905 if (hhd->first)
2906 break;
2907 }
2908 /* Nothing registered? */
2909 if (i == FTRACE_FUNC_HASHSIZE)
2910 return;
2911
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002912 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002913
Steven Rostedtb6887d72009-02-17 12:32:04 -05002914 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002915}
2916
Steven Rostedtb6887d72009-02-17 12:32:04 -05002917static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002918{
2919 int i;
2920
Steven Rostedtb6887d72009-02-17 12:32:04 -05002921 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002922 return;
2923
2924 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2925 struct hlist_head *hhd = &ftrace_func_hash[i];
2926 if (hhd->first)
2927 return;
2928 }
2929
2930 /* no more funcs left */
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002931 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002932
Steven Rostedtb6887d72009-02-17 12:32:04 -05002933 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002934}
2935
2936
2937static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2938{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002939 struct ftrace_func_probe *entry =
2940 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002941
2942 if (entry->ops->free)
2943 entry->ops->free(&entry->data);
2944 kfree(entry);
2945}
2946
2947
2948int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002949register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002950 void *data)
2951{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002952 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002953 struct ftrace_page *pg;
2954 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002955 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002956 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002957 int count = 0;
2958 char *search;
2959
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002960 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002961 len = strlen(search);
2962
Steven Rostedtb6887d72009-02-17 12:32:04 -05002963 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002964 if (WARN_ON(not))
2965 return -EINVAL;
2966
2967 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002968
Steven Rostedt45a4a232011-04-21 23:16:46 -04002969 if (unlikely(ftrace_disabled))
2970 goto out_unlock;
2971
Steven Rostedt59df055f2009-02-14 15:29:06 -05002972 do_for_each_ftrace_rec(pg, rec) {
2973
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002974 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002975 continue;
2976
2977 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2978 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002979 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002980 if (!count)
2981 count = -ENOMEM;
2982 goto out_unlock;
2983 }
2984
2985 count++;
2986
2987 entry->data = data;
2988
2989 /*
2990 * The caller might want to do something special
2991 * for each function we find. We call the callback
2992 * to give the caller an opportunity to do so.
2993 */
2994 if (ops->callback) {
2995 if (ops->callback(rec->ip, &entry->data) < 0) {
2996 /* caller does not like this func */
2997 kfree(entry);
2998 continue;
2999 }
3000 }
3001
3002 entry->ops = ops;
3003 entry->ip = rec->ip;
3004
3005 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3006 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3007
3008 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05003009 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003010
3011 out_unlock:
3012 mutex_unlock(&ftrace_lock);
3013
3014 return count;
3015}
3016
3017enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003018 PROBE_TEST_FUNC = 1,
3019 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003020};
3021
3022static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003023__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003024 void *data, int flags)
3025{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003026 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003027 struct hlist_node *n, *tmp;
3028 char str[KSYM_SYMBOL_LEN];
3029 int type = MATCH_FULL;
3030 int i, len = 0;
3031 char *search;
3032
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003033 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003034 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003035 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003036 int not;
3037
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003038 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003039 len = strlen(search);
3040
Steven Rostedtb6887d72009-02-17 12:32:04 -05003041 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003042 if (WARN_ON(not))
3043 return;
3044 }
3045
3046 mutex_lock(&ftrace_lock);
3047 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3048 struct hlist_head *hhd = &ftrace_func_hash[i];
3049
3050 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3051
3052 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003053 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003054 continue;
3055
Steven Rostedtb6887d72009-02-17 12:32:04 -05003056 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003057 continue;
3058
3059 /* do this last, since it is the most expensive */
3060 if (glob) {
3061 kallsyms_lookup(entry->ip, NULL, NULL,
3062 NULL, str);
3063 if (!ftrace_match(str, glob, len, type))
3064 continue;
3065 }
3066
Steven Rostedt (Red Hat)52cecaa2013-03-13 11:15:19 -04003067 hlist_del_rcu(&entry->node);
3068 call_rcu_sched(&entry->rcu, ftrace_free_entry_rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003069 }
3070 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003071 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003072 mutex_unlock(&ftrace_lock);
3073}
3074
3075void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003076unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003077 void *data)
3078{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003079 __unregister_ftrace_function_probe(glob, ops, data,
3080 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003081}
3082
3083void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003084unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003085{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003086 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003087}
3088
Steven Rostedtb6887d72009-02-17 12:32:04 -05003089void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003090{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003091 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003092}
3093
Steven Rostedtf6180772009-02-14 00:40:25 -05003094static LIST_HEAD(ftrace_commands);
3095static DEFINE_MUTEX(ftrace_cmd_mutex);
3096
3097int register_ftrace_command(struct ftrace_func_command *cmd)
3098{
3099 struct ftrace_func_command *p;
3100 int ret = 0;
3101
3102 mutex_lock(&ftrace_cmd_mutex);
3103 list_for_each_entry(p, &ftrace_commands, list) {
3104 if (strcmp(cmd->name, p->name) == 0) {
3105 ret = -EBUSY;
3106 goto out_unlock;
3107 }
3108 }
3109 list_add(&cmd->list, &ftrace_commands);
3110 out_unlock:
3111 mutex_unlock(&ftrace_cmd_mutex);
3112
3113 return ret;
3114}
3115
3116int unregister_ftrace_command(struct ftrace_func_command *cmd)
3117{
3118 struct ftrace_func_command *p, *n;
3119 int ret = -ENODEV;
3120
3121 mutex_lock(&ftrace_cmd_mutex);
3122 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3123 if (strcmp(cmd->name, p->name) == 0) {
3124 ret = 0;
3125 list_del_init(&p->list);
3126 goto out_unlock;
3127 }
3128 }
3129 out_unlock:
3130 mutex_unlock(&ftrace_cmd_mutex);
3131
3132 return ret;
3133}
3134
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003135static int ftrace_process_regex(struct ftrace_hash *hash,
3136 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003137{
Steven Rostedtf6180772009-02-14 00:40:25 -05003138 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003139 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003140 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003141
3142 func = strsep(&next, ":");
3143
3144 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003145 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003146 if (!ret)
3147 ret = -EINVAL;
3148 if (ret < 0)
3149 return ret;
3150 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003151 }
3152
Steven Rostedtf6180772009-02-14 00:40:25 -05003153 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003154
3155 command = strsep(&next, ":");
3156
Steven Rostedtf6180772009-02-14 00:40:25 -05003157 mutex_lock(&ftrace_cmd_mutex);
3158 list_for_each_entry(p, &ftrace_commands, list) {
3159 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003160 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003161 goto out_unlock;
3162 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003163 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003164 out_unlock:
3165 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003166
Steven Rostedtf6180772009-02-14 00:40:25 -05003167 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003168}
3169
Ingo Molnare309b412008-05-12 21:20:51 +02003170static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003171ftrace_regex_write(struct file *file, const char __user *ubuf,
3172 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003173{
3174 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003175 struct trace_parser *parser;
3176 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003177
Li Zefan4ba79782009-09-22 13:52:20 +08003178 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003179 return 0;
3180
Steven Rostedt41c52c02008-05-22 11:46:33 -04003181 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003182
Steven Rostedt45a4a232011-04-21 23:16:46 -04003183 ret = -ENODEV;
3184 if (unlikely(ftrace_disabled))
3185 goto out_unlock;
3186
Steven Rostedt5072c592008-05-12 21:20:43 +02003187 if (file->f_mode & FMODE_READ) {
3188 struct seq_file *m = file->private_data;
3189 iter = m->private;
3190 } else
3191 iter = file->private_data;
3192
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003193 parser = &iter->parser;
3194 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003195
Li Zefan4ba79782009-09-22 13:52:20 +08003196 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003197 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003198 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003199 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003200 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003201 if (ret)
Li Zefaned146b22009-11-03 08:55:38 +08003202 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003203 }
3204
Steven Rostedt5072c592008-05-12 21:20:43 +02003205 ret = read;
Li Zefaned146b22009-11-03 08:55:38 +08003206out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003207 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b22009-11-03 08:55:38 +08003208
Steven Rostedt5072c592008-05-12 21:20:43 +02003209 return ret;
3210}
3211
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003212ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003213ftrace_filter_write(struct file *file, const char __user *ubuf,
3214 size_t cnt, loff_t *ppos)
3215{
3216 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3217}
3218
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003219ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003220ftrace_notrace_write(struct file *file, const char __user *ubuf,
3221 size_t cnt, loff_t *ppos)
3222{
3223 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3224}
3225
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003226static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003227ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3228 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003229{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003230 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003231 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003232 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003233
Steven Rostedt936e0742011-05-05 22:54:01 -04003234 /* All global ops uses the global ops filters */
3235 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3236 ops = &global_ops;
3237
Steven Rostedt41c52c02008-05-22 11:46:33 -04003238 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003239 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003240
Steven Rostedtf45948e2011-05-02 12:29:25 -04003241 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003242 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003243 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003244 orig_hash = &ops->notrace_hash;
3245
3246 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3247 if (!hash)
3248 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003249
Steven Rostedt41c52c02008-05-22 11:46:33 -04003250 mutex_lock(&ftrace_regex_lock);
3251 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003252 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003253 if (buf && !ftrace_match_records(hash, buf, len)) {
3254 ret = -EINVAL;
3255 goto out_regex_unlock;
3256 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003257
3258 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003259 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003260 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3261 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003262 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003263
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003264 mutex_unlock(&ftrace_lock);
3265
Jiri Olsaac483c42012-01-02 10:04:14 +01003266 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003267 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003268
3269 free_ftrace_hash(hash);
3270 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003271}
3272
Steven Rostedt77a2b372008-05-12 21:20:45 +02003273/**
3274 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003275 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003276 * @buf - the string that holds the function filter text.
3277 * @len - the length of the string.
3278 * @reset - non zero to reset all filters before applying this filter.
3279 *
3280 * Filters denote which functions should be enabled when tracing is enabled.
3281 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3282 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003283int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003284 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003285{
Jiri Olsaac483c42012-01-02 10:04:14 +01003286 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003287}
Steven Rostedt936e0742011-05-05 22:54:01 -04003288EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003289
Steven Rostedt41c52c02008-05-22 11:46:33 -04003290/**
3291 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003292 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003293 * @buf - the string that holds the function notrace text.
3294 * @len - the length of the string.
3295 * @reset - non zero to reset all filters before applying this filter.
3296 *
3297 * Notrace Filters denote which functions should not be enabled when tracing
3298 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3299 * for tracing.
3300 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003301int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003302 int len, int reset)
3303{
Jiri Olsaac483c42012-01-02 10:04:14 +01003304 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003305}
3306EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3307/**
3308 * ftrace_set_filter - set a function to filter on in ftrace
3309 * @ops - the ops to set the filter with
3310 * @buf - the string that holds the function filter text.
3311 * @len - the length of the string.
3312 * @reset - non zero to reset all filters before applying this filter.
3313 *
3314 * Filters denote which functions should be enabled when tracing is enabled.
3315 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3316 */
3317void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3318{
3319 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3320}
3321EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3322
3323/**
3324 * ftrace_set_notrace - set a function to not trace in ftrace
3325 * @ops - the ops to set the notrace filter with
3326 * @buf - the string that holds the function notrace text.
3327 * @len - the length of the string.
3328 * @reset - non zero to reset all filters before applying this filter.
3329 *
3330 * Notrace Filters denote which functions should not be enabled when tracing
3331 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3332 * for tracing.
3333 */
3334void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003335{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003336 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003337}
Steven Rostedt936e0742011-05-05 22:54:01 -04003338EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003339
Steven Rostedt2af15d62009-05-28 13:37:24 -04003340/*
3341 * command line interface to allow users to set filters on boot up.
3342 */
3343#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3344static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3345static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3346
3347static int __init set_ftrace_notrace(char *str)
3348{
3349 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3350 return 1;
3351}
3352__setup("ftrace_notrace=", set_ftrace_notrace);
3353
3354static int __init set_ftrace_filter(char *str)
3355{
3356 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3357 return 1;
3358}
3359__setup("ftrace_filter=", set_ftrace_filter);
3360
Stefan Assmann369bc182009-10-12 22:17:21 +02003361#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003362static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003363static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3364
Stefan Assmann369bc182009-10-12 22:17:21 +02003365static int __init set_graph_function(char *str)
3366{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003367 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003368 return 1;
3369}
3370__setup("ftrace_graph_filter=", set_graph_function);
3371
3372static void __init set_ftrace_early_graph(char *buf)
3373{
3374 int ret;
3375 char *func;
3376
3377 while (buf) {
3378 func = strsep(&buf, ",");
3379 /* we allow only one expression at a time */
3380 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3381 func);
3382 if (ret)
3383 printk(KERN_DEBUG "ftrace: function %s not "
3384 "traceable\n", func);
3385 }
3386}
3387#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3388
Steven Rostedt2a85a372011-12-19 21:57:44 -05003389void __init
3390ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003391{
3392 char *func;
3393
3394 while (buf) {
3395 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003396 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003397 }
3398}
3399
3400static void __init set_ftrace_early_filters(void)
3401{
3402 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003403 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003404 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003405 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003406#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3407 if (ftrace_graph_buf[0])
3408 set_ftrace_early_graph(ftrace_graph_buf);
3409#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003410}
3411
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003412int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003413{
3414 struct seq_file *m = (struct seq_file *)file->private_data;
3415 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003416 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003417 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003418 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003419 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003420
Steven Rostedt41c52c02008-05-22 11:46:33 -04003421 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003422 if (file->f_mode & FMODE_READ) {
3423 iter = m->private;
3424
3425 seq_release(inode, file);
3426 } else
3427 iter = file->private_data;
3428
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003429 parser = &iter->parser;
3430 if (trace_parser_loaded(parser)) {
3431 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003432 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003433 }
3434
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003435 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003436
Steven Rostedt058e2972011-04-29 22:35:33 -04003437 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003438 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3439
3440 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003441 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003442 else
3443 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003444
Steven Rostedt058e2972011-04-29 22:35:33 -04003445 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003446 ret = ftrace_hash_move(iter->ops, filter_hash,
3447 orig_hash, iter->hash);
3448 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3449 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003450 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003451
Steven Rostedt058e2972011-04-29 22:35:33 -04003452 mutex_unlock(&ftrace_lock);
3453 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003454 free_ftrace_hash(iter->hash);
3455 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003456
Steven Rostedt41c52c02008-05-22 11:46:33 -04003457 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003458 return 0;
3459}
3460
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003461static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003462 .open = ftrace_avail_open,
3463 .read = seq_read,
3464 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003465 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003466};
3467
Steven Rostedt647bcd02011-05-03 14:39:21 -04003468static const struct file_operations ftrace_enabled_fops = {
3469 .open = ftrace_enabled_open,
3470 .read = seq_read,
3471 .llseek = seq_lseek,
3472 .release = seq_release_private,
3473};
3474
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003475static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003476 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003477 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003478 .write = ftrace_filter_write,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003479 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003480 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003481};
3482
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003483static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003484 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003485 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003486 .write = ftrace_notrace_write,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003487 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003488 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003489};
3490
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003491#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3492
3493static DEFINE_MUTEX(graph_lock);
3494
3495int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003496int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003497unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3498
3499static void *
Li Zefan85951842009-06-24 09:54:00 +08003500__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003501{
Li Zefan85951842009-06-24 09:54:00 +08003502 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003503 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003504 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003505}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003506
Li Zefan85951842009-06-24 09:54:00 +08003507static void *
3508g_next(struct seq_file *m, void *v, loff_t *pos)
3509{
3510 (*pos)++;
3511 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003512}
3513
3514static void *g_start(struct seq_file *m, loff_t *pos)
3515{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003516 mutex_lock(&graph_lock);
3517
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003518 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003519 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003520 return (void *)1;
3521
Li Zefan85951842009-06-24 09:54:00 +08003522 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003523}
3524
3525static void g_stop(struct seq_file *m, void *p)
3526{
3527 mutex_unlock(&graph_lock);
3528}
3529
3530static int g_show(struct seq_file *m, void *v)
3531{
3532 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003533
3534 if (!ptr)
3535 return 0;
3536
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003537 if (ptr == (unsigned long *)1) {
3538 seq_printf(m, "#### all functions enabled ####\n");
3539 return 0;
3540 }
3541
Steven Rostedtb375a112009-09-17 00:05:58 -04003542 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003543
3544 return 0;
3545}
3546
James Morris88e9d342009-09-22 16:43:43 -07003547static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003548 .start = g_start,
3549 .next = g_next,
3550 .stop = g_stop,
3551 .show = g_show,
3552};
3553
3554static int
3555ftrace_graph_open(struct inode *inode, struct file *file)
3556{
3557 int ret = 0;
3558
3559 if (unlikely(ftrace_disabled))
3560 return -ENODEV;
3561
3562 mutex_lock(&graph_lock);
3563 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003564 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003565 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003566 ftrace_graph_count = 0;
3567 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3568 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003569 mutex_unlock(&graph_lock);
3570
Li Zefana4ec5e02009-09-18 14:06:28 +08003571 if (file->f_mode & FMODE_READ)
3572 ret = seq_open(file, &ftrace_graph_seq_ops);
3573
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003574 return ret;
3575}
3576
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003577static int
Li Zefan87827112009-07-23 11:29:11 +08003578ftrace_graph_release(struct inode *inode, struct file *file)
3579{
3580 if (file->f_mode & FMODE_READ)
3581 seq_release(inode, file);
3582 return 0;
3583}
3584
3585static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003586ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003587{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003588 struct dyn_ftrace *rec;
3589 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003590 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003591 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003592 int type, not;
3593 char *search;
3594 bool exists;
3595 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003596
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003597 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003598 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003599 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3600 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003601
3602 search_len = strlen(search);
3603
Steven Rostedt52baf112009-02-14 01:15:39 -05003604 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003605
3606 if (unlikely(ftrace_disabled)) {
3607 mutex_unlock(&ftrace_lock);
3608 return -ENODEV;
3609 }
3610
Steven Rostedt265c8312009-02-13 12:43:56 -05003611 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003612
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003613 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003614 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003615 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003616 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003617 if (array[i] == rec->ip) {
3618 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003619 break;
3620 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003621 }
3622
3623 if (!not) {
3624 fail = 0;
3625 if (!exists) {
3626 array[(*idx)++] = rec->ip;
3627 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3628 goto out;
3629 }
3630 } else {
3631 if (exists) {
3632 array[i] = array[--(*idx)];
3633 array[*idx] = 0;
3634 fail = 0;
3635 }
3636 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003637 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003638 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003639out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003640 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003641
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003642 if (fail)
3643 return -EINVAL;
3644
Namhyung Kim761694a2013-04-11 16:01:38 +09003645 ftrace_graph_filter_enabled = !!(*idx);
3646
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003647 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003648}
3649
3650static ssize_t
3651ftrace_graph_write(struct file *file, const char __user *ubuf,
3652 size_t cnt, loff_t *ppos)
3653{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003654 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003655 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003656
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003657 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003658 return 0;
3659
3660 mutex_lock(&graph_lock);
3661
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003662 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3663 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003664 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003665 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003666
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003667 read = trace_get_user(&parser, ubuf, cnt, ppos);
3668
Li Zefan4ba79782009-09-22 13:52:20 +08003669 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003670 parser.buffer[parser.idx] = 0;
3671
3672 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003673 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003674 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003675 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003676 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003677 }
3678
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003679 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003680
3681out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003682 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003683out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003684 mutex_unlock(&graph_lock);
3685
3686 return ret;
3687}
3688
3689static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003690 .open = ftrace_graph_open,
3691 .read = seq_read,
3692 .write = ftrace_graph_write,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003693 .llseek = ftrace_filter_lseek,
Li Zefan87827112009-07-23 11:29:11 +08003694 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003695};
3696#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3697
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003698static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003699{
Steven Rostedt5072c592008-05-12 21:20:43 +02003700
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003701 trace_create_file("available_filter_functions", 0444,
3702 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003703
Steven Rostedt647bcd02011-05-03 14:39:21 -04003704 trace_create_file("enabled_functions", 0444,
3705 d_tracer, NULL, &ftrace_enabled_fops);
3706
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003707 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3708 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003709
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003710 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003711 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003712
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003713#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003714 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003715 NULL,
3716 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003717#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3718
Steven Rostedt5072c592008-05-12 21:20:43 +02003719 return 0;
3720}
3721
Steven Rostedt68950612011-12-16 17:06:45 -05003722static void ftrace_swap_recs(void *a, void *b, int size)
3723{
3724 struct dyn_ftrace *reca = a;
3725 struct dyn_ftrace *recb = b;
3726 struct dyn_ftrace t;
3727
3728 t = *reca;
3729 *reca = *recb;
3730 *recb = t;
3731}
3732
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003733static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003734 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003735 unsigned long *end)
3736{
Steven Rostedta7900872011-12-16 16:23:44 -05003737 struct ftrace_page *pg;
3738 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003739 unsigned long *p;
3740 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003741 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003742 int ret = -ENOMEM;
3743
3744 count = end - start;
3745
3746 if (!count)
3747 return 0;
3748
3749 pg = ftrace_allocate_pages(count);
3750 if (!pg)
3751 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003752
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003753 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003754
Steven Rostedt32082302011-12-16 14:42:37 -05003755 /*
3756 * Core and each module needs their own pages, as
3757 * modules will free them when they are removed.
3758 * Force a new page to be allocated for modules.
3759 */
Steven Rostedta7900872011-12-16 16:23:44 -05003760 if (!mod) {
3761 WARN_ON(ftrace_pages || ftrace_pages_start);
3762 /* First initialization */
3763 ftrace_pages = ftrace_pages_start = pg;
3764 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003765 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003766 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003767
Steven Rostedta7900872011-12-16 16:23:44 -05003768 if (WARN_ON(ftrace_pages->next)) {
3769 /* Hmm, we have free pages? */
3770 while (ftrace_pages->next)
3771 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003772 }
Steven Rostedta7900872011-12-16 16:23:44 -05003773
3774 ftrace_pages->next = pg;
3775 ftrace_pages = pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003776 }
3777
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003778 p = start;
3779 while (p < end) {
3780 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003781 /*
3782 * Some architecture linkers will pad between
3783 * the different mcount_loc sections of different
3784 * object files to satisfy alignments.
3785 * Skip any NULL pointers.
3786 */
3787 if (!addr)
3788 continue;
Steven Rostedta7900872011-12-16 16:23:44 -05003789 if (!ftrace_record_ip(addr))
3790 break;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003791 }
3792
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003793 /* These new locations need to be initialized */
3794 ftrace_new_pgs = pg;
3795
Steven Rostedt68950612011-12-16 17:06:45 -05003796 /* Make each individual set of pages sorted by ips */
3797 for (; pg; pg = pg->next)
3798 sort(pg->records, pg->index, sizeof(struct dyn_ftrace),
3799 ftrace_cmp_recs, ftrace_swap_recs);
3800
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003801 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003802 * We only need to disable interrupts on start up
3803 * because we are modifying code that an interrupt
3804 * may execute, and the modification is not atomic.
3805 * But for modules, nothing runs the code we modify
3806 * until we are finished with it, and there's no
3807 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003808 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003809 if (!mod)
3810 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003811 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003812 if (!mod)
3813 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003814 ret = 0;
3815 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003816 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003817
Steven Rostedta7900872011-12-16 16:23:44 -05003818 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003819}
3820
Steven Rostedt93eb6772009-04-15 13:24:06 -04003821#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003822
3823#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3824
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003825void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003826{
3827 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003828 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003829 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003830 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003831
Steven Rostedt93eb6772009-04-15 13:24:06 -04003832 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003833
3834 if (ftrace_disabled)
3835 goto out_unlock;
3836
Steven Rostedt32082302011-12-16 14:42:37 -05003837 /*
3838 * Each module has its own ftrace_pages, remove
3839 * them from the list.
3840 */
3841 last_pg = &ftrace_pages_start;
3842 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3843 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003844 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003845 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003846 * As core pages are first, the first
3847 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003848 */
Steven Rostedt32082302011-12-16 14:42:37 -05003849 if (WARN_ON(pg == ftrace_pages_start))
3850 goto out_unlock;
3851
3852 /* Check if we are deleting the last page */
3853 if (pg == ftrace_pages)
3854 ftrace_pages = next_to_ftrace_page(last_pg);
3855
3856 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003857 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3858 free_pages((unsigned long)pg->records, order);
3859 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05003860 } else
3861 last_pg = &pg->next;
3862 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003863 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003864 mutex_unlock(&ftrace_lock);
3865}
3866
3867static void ftrace_init_module(struct module *mod,
3868 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003869{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003870 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003871 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003872 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003873}
3874
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003875static int ftrace_module_notify_enter(struct notifier_block *self,
3876 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003877{
3878 struct module *mod = data;
3879
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003880 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003881 ftrace_init_module(mod, mod->ftrace_callsites,
3882 mod->ftrace_callsites +
3883 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003884 return 0;
3885}
3886
3887static int ftrace_module_notify_exit(struct notifier_block *self,
3888 unsigned long val, void *data)
3889{
3890 struct module *mod = data;
3891
3892 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003893 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003894
3895 return 0;
3896}
3897#else
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003898static int ftrace_module_notify_enter(struct notifier_block *self,
3899 unsigned long val, void *data)
3900{
3901 return 0;
3902}
3903static int ftrace_module_notify_exit(struct notifier_block *self,
3904 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003905{
3906 return 0;
3907}
3908#endif /* CONFIG_MODULES */
3909
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003910struct notifier_block ftrace_module_enter_nb = {
3911 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtf2a01002012-12-14 09:48:15 -05003912 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04003913};
3914
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003915struct notifier_block ftrace_module_exit_nb = {
3916 .notifier_call = ftrace_module_notify_exit,
3917 .priority = INT_MIN, /* Run after anything that can remove kprobes */
3918};
3919
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003920extern unsigned long __start_mcount_loc[];
3921extern unsigned long __stop_mcount_loc[];
3922
3923void __init ftrace_init(void)
3924{
3925 unsigned long count, addr, flags;
3926 int ret;
3927
3928 /* Keep the ftrace pointer to the stub */
3929 addr = (unsigned long)ftrace_stub;
3930
3931 local_irq_save(flags);
3932 ftrace_dyn_arch_init(&addr);
3933 local_irq_restore(flags);
3934
3935 /* ftrace_dyn_arch_init places the return code in addr */
3936 if (addr)
3937 goto failed;
3938
3939 count = __stop_mcount_loc - __start_mcount_loc;
3940
3941 ret = ftrace_dyn_table_alloc(count);
3942 if (ret)
3943 goto failed;
3944
3945 last_ftrace_enabled = ftrace_enabled = 1;
3946
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003947 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003948 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003949 __stop_mcount_loc);
3950
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003951 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003952 if (ret)
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003953 pr_warning("Failed to register trace ftrace module enter notifier\n");
3954
3955 ret = register_module_notifier(&ftrace_module_exit_nb);
3956 if (ret)
3957 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04003958
Steven Rostedt2af15d62009-05-28 13:37:24 -04003959 set_ftrace_early_filters();
3960
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003961 return;
3962 failed:
3963 ftrace_disabled = 1;
3964}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003965
Steven Rostedt3d083392008-05-12 21:20:42 +02003966#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003967
Steven Rostedt2b499382011-05-03 22:49:52 -04003968static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003969 .func = ftrace_stub,
3970};
3971
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003972static int __init ftrace_nodyn_init(void)
3973{
3974 ftrace_enabled = 1;
3975 return 0;
3976}
3977device_initcall(ftrace_nodyn_init);
3978
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003979static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3980static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003981/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05003982# define ftrace_startup(ops, command) \
3983 ({ \
3984 int ___ret = __register_ftrace_function(ops); \
3985 if (!___ret) \
3986 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3987 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003988 })
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05003989# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
3990
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003991# define ftrace_startup_sysctl() do { } while (0)
3992# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003993
3994static inline int
3995ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3996{
3997 return 1;
3998}
3999
Steven Rostedt3d083392008-05-12 21:20:42 +02004000#endif /* CONFIG_DYNAMIC_FTRACE */
4001
Steven Rostedtb8489142011-05-04 09:27:52 -04004002static void
Jiri Olsae2484912012-02-15 15:51:48 +01004003ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip)
4004{
4005 struct ftrace_ops *op;
4006
4007 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4008 return;
4009
4010 /*
4011 * Some of the ops may be dynamically allocated,
4012 * they must be freed after a synchronize_sched().
4013 */
4014 preempt_disable_notrace();
4015 trace_recursion_set(TRACE_CONTROL_BIT);
4016 op = rcu_dereference_raw(ftrace_control_list);
4017 while (op != &ftrace_list_end) {
4018 if (!ftrace_function_local_disabled(op) &&
4019 ftrace_ops_test(op, ip))
4020 op->func(ip, parent_ip);
4021
4022 op = rcu_dereference_raw(op->next);
4023 };
4024 trace_recursion_clear(TRACE_CONTROL_BIT);
4025 preempt_enable_notrace();
4026}
4027
4028static struct ftrace_ops control_ops = {
4029 .func = ftrace_ops_control_func,
4030};
4031
4032static void
Steven Rostedtb8489142011-05-04 09:27:52 -04004033ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
4034{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004035 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04004036
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004037 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4038 return;
4039
4040 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004041 /*
4042 * Some of the ops may be dynamically allocated,
4043 * they must be freed after a synchronize_sched().
4044 */
4045 preempt_disable_notrace();
4046 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04004047 while (op != &ftrace_list_end) {
4048 if (ftrace_ops_test(op, ip))
4049 op->func(ip, parent_ip);
4050 op = rcu_dereference_raw(op->next);
4051 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004052 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004053 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04004054}
4055
Steven Rostedte32d8952008-12-04 00:26:41 -05004056static void clear_ftrace_swapper(void)
4057{
4058 struct task_struct *p;
4059 int cpu;
4060
4061 get_online_cpus();
4062 for_each_online_cpu(cpu) {
4063 p = idle_task(cpu);
4064 clear_tsk_trace_trace(p);
4065 }
4066 put_online_cpus();
4067}
4068
4069static void set_ftrace_swapper(void)
4070{
4071 struct task_struct *p;
4072 int cpu;
4073
4074 get_online_cpus();
4075 for_each_online_cpu(cpu) {
4076 p = idle_task(cpu);
4077 set_tsk_trace_trace(p);
4078 }
4079 put_online_cpus();
4080}
4081
4082static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004083{
4084 struct task_struct *p;
4085
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004086 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004087 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004088 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004089 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004090 rcu_read_unlock();
4091
Steven Rostedte32d8952008-12-04 00:26:41 -05004092 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004093}
4094
Steven Rostedte32d8952008-12-04 00:26:41 -05004095static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004096{
4097 struct task_struct *p;
4098
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004099 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004100 do_each_pid_task(pid, PIDTYPE_PID, p) {
4101 set_tsk_trace_trace(p);
4102 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004103 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004104}
4105
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004106static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004107{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004108 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004109 clear_ftrace_swapper();
4110 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004111 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004112}
4113
4114static void set_ftrace_pid_task(struct pid *pid)
4115{
4116 if (pid == ftrace_swapper_pid)
4117 set_ftrace_swapper();
4118 else
4119 set_ftrace_pid(pid);
4120}
4121
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004122static int ftrace_pid_add(int p)
4123{
4124 struct pid *pid;
4125 struct ftrace_pid *fpid;
4126 int ret = -EINVAL;
4127
4128 mutex_lock(&ftrace_lock);
4129
4130 if (!p)
4131 pid = ftrace_swapper_pid;
4132 else
4133 pid = find_get_pid(p);
4134
4135 if (!pid)
4136 goto out;
4137
4138 ret = 0;
4139
4140 list_for_each_entry(fpid, &ftrace_pids, list)
4141 if (fpid->pid == pid)
4142 goto out_put;
4143
4144 ret = -ENOMEM;
4145
4146 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4147 if (!fpid)
4148 goto out_put;
4149
4150 list_add(&fpid->list, &ftrace_pids);
4151 fpid->pid = pid;
4152
4153 set_ftrace_pid_task(pid);
4154
4155 ftrace_update_pid_func();
4156 ftrace_startup_enable(0);
4157
4158 mutex_unlock(&ftrace_lock);
4159 return 0;
4160
4161out_put:
4162 if (pid != ftrace_swapper_pid)
4163 put_pid(pid);
4164
4165out:
4166 mutex_unlock(&ftrace_lock);
4167 return ret;
4168}
4169
4170static void ftrace_pid_reset(void)
4171{
4172 struct ftrace_pid *fpid, *safe;
4173
4174 mutex_lock(&ftrace_lock);
4175 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4176 struct pid *pid = fpid->pid;
4177
4178 clear_ftrace_pid_task(pid);
4179
4180 list_del(&fpid->list);
4181 kfree(fpid);
4182 }
4183
4184 ftrace_update_pid_func();
4185 ftrace_startup_enable(0);
4186
4187 mutex_unlock(&ftrace_lock);
4188}
4189
4190static void *fpid_start(struct seq_file *m, loff_t *pos)
4191{
4192 mutex_lock(&ftrace_lock);
4193
4194 if (list_empty(&ftrace_pids) && (!*pos))
4195 return (void *) 1;
4196
4197 return seq_list_start(&ftrace_pids, *pos);
4198}
4199
4200static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4201{
4202 if (v == (void *)1)
4203 return NULL;
4204
4205 return seq_list_next(v, &ftrace_pids, pos);
4206}
4207
4208static void fpid_stop(struct seq_file *m, void *p)
4209{
4210 mutex_unlock(&ftrace_lock);
4211}
4212
4213static int fpid_show(struct seq_file *m, void *v)
4214{
4215 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4216
4217 if (v == (void *)1) {
4218 seq_printf(m, "no pid\n");
4219 return 0;
4220 }
4221
4222 if (fpid->pid == ftrace_swapper_pid)
4223 seq_printf(m, "swapper tasks\n");
4224 else
4225 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4226
4227 return 0;
4228}
4229
4230static const struct seq_operations ftrace_pid_sops = {
4231 .start = fpid_start,
4232 .next = fpid_next,
4233 .stop = fpid_stop,
4234 .show = fpid_show,
4235};
4236
4237static int
4238ftrace_pid_open(struct inode *inode, struct file *file)
4239{
4240 int ret = 0;
4241
4242 if ((file->f_mode & FMODE_WRITE) &&
4243 (file->f_flags & O_TRUNC))
4244 ftrace_pid_reset();
4245
4246 if (file->f_mode & FMODE_READ)
4247 ret = seq_open(file, &ftrace_pid_sops);
4248
4249 return ret;
4250}
4251
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004252static ssize_t
4253ftrace_pid_write(struct file *filp, const char __user *ubuf,
4254 size_t cnt, loff_t *ppos)
4255{
Ingo Molnar457dc922009-11-23 11:03:28 +01004256 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004257 long val;
4258 int ret;
4259
4260 if (cnt >= sizeof(buf))
4261 return -EINVAL;
4262
4263 if (copy_from_user(&buf, ubuf, cnt))
4264 return -EFAULT;
4265
4266 buf[cnt] = 0;
4267
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004268 /*
4269 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4270 * to clean the filter quietly.
4271 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004272 tmp = strstrip(buf);
4273 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004274 return 1;
4275
Ingo Molnar457dc922009-11-23 11:03:28 +01004276 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004277 if (ret < 0)
4278 return ret;
4279
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004280 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004281
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004282 return ret ? ret : cnt;
4283}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004284
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004285static int
4286ftrace_pid_release(struct inode *inode, struct file *file)
4287{
4288 if (file->f_mode & FMODE_READ)
4289 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004290
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004291 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004292}
4293
Steven Rostedt5e2336a02009-03-05 21:44:55 -05004294static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004295 .open = ftrace_pid_open,
4296 .write = ftrace_pid_write,
4297 .read = seq_read,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08004298 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004299 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004300};
4301
4302static __init int ftrace_init_debugfs(void)
4303{
4304 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004305
4306 d_tracer = tracing_init_dentry();
4307 if (!d_tracer)
4308 return 0;
4309
4310 ftrace_init_dyn_debugfs(d_tracer);
4311
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004312 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4313 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004314
4315 ftrace_profile_debugfs(d_tracer);
4316
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004317 return 0;
4318}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004319fs_initcall(ftrace_init_debugfs);
4320
Steven Rostedt3d083392008-05-12 21:20:42 +02004321/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004322 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004323 *
4324 * This function should be used by panic code. It stops ftrace
4325 * but in a not so nice way. If you need to simply kill ftrace
4326 * from a non-atomic section, use ftrace_kill.
4327 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004328void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004329{
4330 ftrace_disabled = 1;
4331 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004332 clear_ftrace_function();
4333}
4334
4335/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004336 * Test if ftrace is dead or not.
4337 */
4338int ftrace_is_dead(void)
4339{
4340 return ftrace_disabled;
4341}
4342
4343/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004344 * register_ftrace_function - register a function for profiling
4345 * @ops - ops structure that holds the function for profiling.
4346 *
4347 * Register a function to be called by all functions in the
4348 * kernel.
4349 *
4350 * Note: @ops->func and all the functions it calls must be labeled
4351 * with "notrace", otherwise it will go into a
4352 * recursive loop.
4353 */
4354int register_ftrace_function(struct ftrace_ops *ops)
4355{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004356 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004357
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004358 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004359
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004360 ret = ftrace_startup(ops, 0);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004361
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004362 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004363 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004364}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004365EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004366
4367/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004368 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004369 * @ops - ops structure that holds the function to unregister
4370 *
4371 * Unregister a function that was added to be called by ftrace profiling.
4372 */
4373int unregister_ftrace_function(struct ftrace_ops *ops)
4374{
4375 int ret;
4376
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004377 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004378 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004379 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004380
4381 return ret;
4382}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004383EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004384
Ingo Molnare309b412008-05-12 21:20:51 +02004385int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004386ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004387 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004388 loff_t *ppos)
4389{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004390 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004391
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004392 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004393
Steven Rostedt45a4a232011-04-21 23:16:46 -04004394 if (unlikely(ftrace_disabled))
4395 goto out;
4396
4397 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004398
Li Zefana32c7762009-06-26 16:55:51 +08004399 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004400 goto out;
4401
Li Zefana32c7762009-06-26 16:55:51 +08004402 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004403
4404 if (ftrace_enabled) {
4405
4406 ftrace_startup_sysctl();
4407
4408 /* we are starting ftrace again */
Jan Kiszkab81d3242013-03-26 17:53:03 +01004409 if (ftrace_ops_list != &ftrace_list_end)
4410 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004411
4412 } else {
4413 /* stopping ftrace calls (just send to ftrace_stub) */
4414 ftrace_trace_function = ftrace_stub;
4415
4416 ftrace_shutdown_sysctl();
4417 }
4418
4419 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004420 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004421 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004422}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004423
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004424#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004425
Steven Rostedt597af812009-04-03 15:24:12 -04004426static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004427static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004428
Steven Rostedte49dc192008-12-02 23:50:05 -05004429int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4430{
4431 return 0;
4432}
4433
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004434/* The callbacks that hook a function */
4435trace_func_graph_ret_t ftrace_graph_return =
4436 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004437trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004438
4439/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4440static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4441{
4442 int i;
4443 int ret = 0;
4444 unsigned long flags;
4445 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4446 struct task_struct *g, *t;
4447
4448 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4449 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4450 * sizeof(struct ftrace_ret_stack),
4451 GFP_KERNEL);
4452 if (!ret_stack_list[i]) {
4453 start = 0;
4454 end = i;
4455 ret = -ENOMEM;
4456 goto free;
4457 }
4458 }
4459
4460 read_lock_irqsave(&tasklist_lock, flags);
4461 do_each_thread(g, t) {
4462 if (start == end) {
4463 ret = -EAGAIN;
4464 goto unlock;
4465 }
4466
4467 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004468 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004469 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004470 t->curr_ret_stack = -1;
4471 /* Make sure the tasks see the -1 first: */
4472 smp_wmb();
4473 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004474 }
4475 } while_each_thread(g, t);
4476
4477unlock:
4478 read_unlock_irqrestore(&tasklist_lock, flags);
4479free:
4480 for (i = start; i < end; i++)
4481 kfree(ret_stack_list[i]);
4482 return ret;
4483}
4484
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004485static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004486ftrace_graph_probe_sched_switch(void *ignore,
4487 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004488{
4489 unsigned long long timestamp;
4490 int index;
4491
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004492 /*
4493 * Does the user want to count the time a function was asleep.
4494 * If so, do not update the time stamps.
4495 */
4496 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4497 return;
4498
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004499 timestamp = trace_clock_local();
4500
4501 prev->ftrace_timestamp = timestamp;
4502
4503 /* only process tasks that we timestamped */
4504 if (!next->ftrace_timestamp)
4505 return;
4506
4507 /*
4508 * Update all the counters in next to make up for the
4509 * time next was sleeping.
4510 */
4511 timestamp -= next->ftrace_timestamp;
4512
4513 for (index = next->curr_ret_stack; index >= 0; index--)
4514 next->ret_stack[index].calltime += timestamp;
4515}
4516
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004517/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004518static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004519{
4520 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004521 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004522
4523 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4524 sizeof(struct ftrace_ret_stack *),
4525 GFP_KERNEL);
4526
4527 if (!ret_stack_list)
4528 return -ENOMEM;
4529
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004530 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004531 for_each_online_cpu(cpu) {
4532 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004533 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004534 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004535
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004536 do {
4537 ret = alloc_retstack_tasklist(ret_stack_list);
4538 } while (ret == -EAGAIN);
4539
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004540 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004541 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004542 if (ret)
4543 pr_info("ftrace_graph: Couldn't activate tracepoint"
4544 " probe to kernel_sched_switch\n");
4545 }
4546
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004547 kfree(ret_stack_list);
4548 return ret;
4549}
4550
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004551/*
4552 * Hibernation protection.
4553 * The state of the current task is too much unstable during
4554 * suspend/restore to disk. We want to protect against that.
4555 */
4556static int
4557ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4558 void *unused)
4559{
4560 switch (state) {
4561 case PM_HIBERNATION_PREPARE:
4562 pause_graph_tracing();
4563 break;
4564
4565 case PM_POST_HIBERNATION:
4566 unpause_graph_tracing();
4567 break;
4568 }
4569 return NOTIFY_DONE;
4570}
4571
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004572/* Just a place holder for function graph */
4573static struct ftrace_ops fgraph_ops __read_mostly = {
4574 .func = ftrace_stub,
4575 .flags = FTRACE_OPS_FL_GLOBAL,
4576};
4577
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004578int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4579 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004580{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004581 int ret = 0;
4582
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004583 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004584
Steven Rostedt05ce5812009-03-24 00:18:31 -04004585 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004586 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004587 ret = -EBUSY;
4588 goto out;
4589 }
4590
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004591 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4592 register_pm_notifier(&ftrace_suspend_notifier);
4593
Steven Rostedt597af812009-04-03 15:24:12 -04004594 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004595 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004596 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004597 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004598 goto out;
4599 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004600
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004601 ftrace_graph_return = retfunc;
4602 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004603
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004604 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004605
4606out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004607 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004608 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004609}
4610
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004611void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004612{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004613 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004614
Steven Rostedt597af812009-04-03 15:24:12 -04004615 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004616 goto out;
4617
Steven Rostedt597af812009-04-03 15:24:12 -04004618 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004619 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004620 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004621 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004622 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004623 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004624
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004625 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004626 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004627}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004628
Steven Rostedt868baf02011-02-10 21:26:13 -05004629static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4630
4631static void
4632graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4633{
4634 atomic_set(&t->tracing_graph_pause, 0);
4635 atomic_set(&t->trace_overrun, 0);
4636 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004637 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004638 smp_wmb();
4639 t->ret_stack = ret_stack;
4640}
4641
4642/*
4643 * Allocate a return stack for the idle task. May be the first
4644 * time through, or it may be done by CPU hotplug online.
4645 */
4646void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4647{
4648 t->curr_ret_stack = -1;
4649 /*
4650 * The idle task has no parent, it either has its own
4651 * stack or no stack at all.
4652 */
4653 if (t->ret_stack)
4654 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4655
4656 if (ftrace_graph_active) {
4657 struct ftrace_ret_stack *ret_stack;
4658
4659 ret_stack = per_cpu(idle_ret_stack, cpu);
4660 if (!ret_stack) {
4661 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4662 * sizeof(struct ftrace_ret_stack),
4663 GFP_KERNEL);
4664 if (!ret_stack)
4665 return;
4666 per_cpu(idle_ret_stack, cpu) = ret_stack;
4667 }
4668 graph_init_task(t, ret_stack);
4669 }
4670}
4671
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004672/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004673void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004674{
Steven Rostedt84047e32009-06-02 16:51:55 -04004675 /* Make sure we do not use the parent ret_stack */
4676 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004677 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004678
Steven Rostedt597af812009-04-03 15:24:12 -04004679 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004680 struct ftrace_ret_stack *ret_stack;
4681
4682 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004683 * sizeof(struct ftrace_ret_stack),
4684 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004685 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004686 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004687 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004688 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004689}
4690
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004691void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004692{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004693 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4694
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004695 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004696 /* NULL must become visible to IRQs before we free it: */
4697 barrier();
4698
4699 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004700}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004701
4702void ftrace_graph_stop(void)
4703{
4704 ftrace_stop();
4705}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004706#endif