blob: 5b6bd45bc58b4db1780b948b14826276be3540e9 [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 Rostedt1c2bd0d2014-02-11 14:50:01 -0500242#ifdef CONFIG_FUNCTION_GRAPH_TRACER
243static void update_function_graph_func(void);
244#else
245static inline void update_function_graph_func(void) { }
246#endif
247
Steven Rostedt2b499382011-05-03 22:49:52 -0400248static void update_ftrace_function(void)
249{
250 ftrace_func_t func;
251
252 update_global_ops();
253
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400254 /*
255 * If we are at the end of the list and this ops is
256 * not dynamic, then have the mcount trampoline call
257 * the function directly
258 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400259 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400260 (ftrace_ops_list->next == &ftrace_list_end &&
261 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
Steven Rostedtb8489142011-05-04 09:27:52 -0400262 func = ftrace_ops_list->func;
263 else
264 func = ftrace_ops_list_func;
Steven Rostedt2b499382011-05-03 22:49:52 -0400265
Steven Rostedt1c2bd0d2014-02-11 14:50:01 -0500266 update_function_graph_func();
267
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400268#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
269 ftrace_trace_function = func;
270#else
Steven Rostedt6331c282011-07-13 15:11:02 -0400271#ifdef CONFIG_DYNAMIC_FTRACE
272 /* do not update till all functions have been modified */
273 __ftrace_trace_function_delay = func;
274#else
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400275 __ftrace_trace_function = func;
Steven Rostedt6331c282011-07-13 15:11:02 -0400276#endif
Rajesh Bhagatdb6544e2012-02-17 13:59:15 +0530277 ftrace_trace_function =
278 (func == ftrace_stub) ? func : ftrace_test_stop_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400279#endif
280}
281
Steven Rostedt2b499382011-05-03 22:49:52 -0400282static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200283{
Steven Rostedt2b499382011-05-03 22:49:52 -0400284 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200285 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400286 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200287 * CPU might be walking that list. We need to make sure
288 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400289 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200290 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400291 rcu_assign_pointer(*list, ops);
292}
Steven Rostedt3d083392008-05-12 21:20:42 +0200293
Steven Rostedt2b499382011-05-03 22:49:52 -0400294static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
295{
296 struct ftrace_ops **p;
297
298 /*
299 * If we are removing the last function, then simply point
300 * to the ftrace_stub.
301 */
302 if (*list == ops && ops->next == &ftrace_list_end) {
303 *list = &ftrace_list_end;
304 return 0;
305 }
306
307 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
308 if (*p == ops)
309 break;
310
311 if (*p != ops)
312 return -1;
313
314 *p = (*p)->next;
315 return 0;
316}
317
Jiri Olsae2484912012-02-15 15:51:48 +0100318static void add_ftrace_list_ops(struct ftrace_ops **list,
319 struct ftrace_ops *main_ops,
320 struct ftrace_ops *ops)
321{
322 int first = *list == &ftrace_list_end;
323 add_ftrace_ops(list, ops);
324 if (first)
325 add_ftrace_ops(&ftrace_ops_list, main_ops);
326}
327
328static int remove_ftrace_list_ops(struct ftrace_ops **list,
329 struct ftrace_ops *main_ops,
330 struct ftrace_ops *ops)
331{
332 int ret = remove_ftrace_ops(list, ops);
333 if (!ret && *list == &ftrace_list_end)
334 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
335 return ret;
336}
337
Steven Rostedt2b499382011-05-03 22:49:52 -0400338static int __register_ftrace_function(struct ftrace_ops *ops)
339{
Steven Rostedt2b499382011-05-03 22:49:52 -0400340 if (FTRACE_WARN_ON(ops == &global_ops))
341 return -EINVAL;
342
Steven Rostedtb8489142011-05-04 09:27:52 -0400343 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
344 return -EBUSY;
345
Jiri Olsae2484912012-02-15 15:51:48 +0100346 /* We don't support both control and global flags set. */
347 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
348 return -EINVAL;
349
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400350 if (!core_kernel_data((unsigned long)ops))
351 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
352
Steven Rostedtb8489142011-05-04 09:27:52 -0400353 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100354 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400355 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100356 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
357 if (control_ops_alloc(ops))
358 return -ENOMEM;
359 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400360 } else
361 add_ftrace_ops(&ftrace_ops_list, ops);
362
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400363 if (ftrace_enabled)
364 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200365
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200366 return 0;
367}
368
Ingo Molnare309b412008-05-12 21:20:51 +0200369static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200370{
Steven Rostedt2b499382011-05-03 22:49:52 -0400371 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200372
Steven Rostedtb8489142011-05-04 09:27:52 -0400373 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
374 return -EBUSY;
375
Steven Rostedt2b499382011-05-03 22:49:52 -0400376 if (FTRACE_WARN_ON(ops == &global_ops))
377 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200378
Steven Rostedtb8489142011-05-04 09:27:52 -0400379 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100380 ret = remove_ftrace_list_ops(&ftrace_global_list,
381 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400382 if (!ret)
383 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100384 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
385 ret = remove_ftrace_list_ops(&ftrace_control_list,
386 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400387 } else
388 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
389
Steven Rostedt2b499382011-05-03 22:49:52 -0400390 if (ret < 0)
391 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400392
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400393 if (ftrace_enabled)
394 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200395
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500396 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200397}
398
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500399static void ftrace_update_pid_func(void)
400{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400401 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500402 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900403 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500404
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400405 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500406}
407
Steven Rostedt493762f2009-03-23 17:12:36 -0400408#ifdef CONFIG_FUNCTION_PROFILER
409struct ftrace_profile {
410 struct hlist_node node;
411 unsigned long ip;
412 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400413#ifdef CONFIG_FUNCTION_GRAPH_TRACER
414 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400415 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400416#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400417};
418
419struct ftrace_profile_page {
420 struct ftrace_profile_page *next;
421 unsigned long index;
422 struct ftrace_profile records[];
423};
424
Steven Rostedtcafb1682009-03-24 20:50:39 -0400425struct ftrace_profile_stat {
426 atomic_t disabled;
427 struct hlist_head *hash;
428 struct ftrace_profile_page *pages;
429 struct ftrace_profile_page *start;
430 struct tracer_stat stat;
431};
432
Steven Rostedt493762f2009-03-23 17:12:36 -0400433#define PROFILE_RECORDS_SIZE \
434 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
435
436#define PROFILES_PER_PAGE \
437 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
438
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400439static int ftrace_profile_bits __read_mostly;
440static int ftrace_profile_enabled __read_mostly;
441
442/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400443static DEFINE_MUTEX(ftrace_profile_lock);
444
Steven Rostedtcafb1682009-03-24 20:50:39 -0400445static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400446
447#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
448
Steven Rostedt493762f2009-03-23 17:12:36 -0400449static void *
450function_stat_next(void *v, int idx)
451{
452 struct ftrace_profile *rec = v;
453 struct ftrace_profile_page *pg;
454
455 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
456
457 again:
Li Zefan0296e422009-06-26 11:15:37 +0800458 if (idx != 0)
459 rec++;
460
Steven Rostedt493762f2009-03-23 17:12:36 -0400461 if ((void *)rec >= (void *)&pg->records[pg->index]) {
462 pg = pg->next;
463 if (!pg)
464 return NULL;
465 rec = &pg->records[0];
466 if (!rec->counter)
467 goto again;
468 }
469
470 return rec;
471}
472
473static void *function_stat_start(struct tracer_stat *trace)
474{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400475 struct ftrace_profile_stat *stat =
476 container_of(trace, struct ftrace_profile_stat, stat);
477
478 if (!stat || !stat->start)
479 return NULL;
480
481 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400482}
483
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400484#ifdef CONFIG_FUNCTION_GRAPH_TRACER
485/* function graph compares on total time */
486static int function_stat_cmp(void *p1, void *p2)
487{
488 struct ftrace_profile *a = p1;
489 struct ftrace_profile *b = p2;
490
491 if (a->time < b->time)
492 return -1;
493 if (a->time > b->time)
494 return 1;
495 else
496 return 0;
497}
498#else
499/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400500static int function_stat_cmp(void *p1, void *p2)
501{
502 struct ftrace_profile *a = p1;
503 struct ftrace_profile *b = p2;
504
505 if (a->counter < b->counter)
506 return -1;
507 if (a->counter > b->counter)
508 return 1;
509 else
510 return 0;
511}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400512#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400513
514static int function_stat_headers(struct seq_file *m)
515{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400516#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400517 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400518 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400519 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400520 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400521#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400522 seq_printf(m, " Function Hit\n"
523 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400524#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400525 return 0;
526}
527
528static int function_stat_show(struct seq_file *m, void *v)
529{
530 struct ftrace_profile *rec = v;
531 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800532 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400533#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400534 static struct trace_seq s;
535 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400536 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400537#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800538 mutex_lock(&ftrace_profile_lock);
539
540 /* we raced with function_profile_reset() */
541 if (unlikely(rec->counter == 0)) {
542 ret = -EBUSY;
543 goto out;
544 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400545
546 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400547 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400548
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400549#ifdef CONFIG_FUNCTION_GRAPH_TRACER
550 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400551 avg = rec->time;
552 do_div(avg, rec->counter);
553
Chase Douglase330b3b2010-04-26 14:02:05 -0400554 /* Sample standard deviation (s^2) */
555 if (rec->counter <= 1)
556 stddev = 0;
557 else {
558 stddev = rec->time_squared - rec->counter * avg * avg;
559 /*
560 * Divide only 1000 for ns^2 -> us^2 conversion.
561 * trace_print_graph_duration will divide 1000 again.
562 */
563 do_div(stddev, (rec->counter - 1) * 1000);
564 }
565
Steven Rostedt34886c82009-03-25 21:00:47 -0400566 trace_seq_init(&s);
567 trace_print_graph_duration(rec->time, &s);
568 trace_seq_puts(&s, " ");
569 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400570 trace_seq_puts(&s, " ");
571 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400572 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400573#endif
574 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800575out:
576 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400577
Li Zefan3aaba202010-08-23 16:50:12 +0800578 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400579}
580
Steven Rostedtcafb1682009-03-24 20:50:39 -0400581static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400582{
583 struct ftrace_profile_page *pg;
584
Steven Rostedtcafb1682009-03-24 20:50:39 -0400585 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400586
587 while (pg) {
588 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
589 pg->index = 0;
590 pg = pg->next;
591 }
592
Steven Rostedtcafb1682009-03-24 20:50:39 -0400593 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400594 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
595}
596
Steven Rostedtcafb1682009-03-24 20:50:39 -0400597int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400598{
599 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400600 int functions;
601 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400602 int i;
603
604 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400605 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400606 return 0;
607
Steven Rostedtcafb1682009-03-24 20:50:39 -0400608 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
609 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400610 return -ENOMEM;
611
Steven Rostedt318e0a72009-03-25 20:06:34 -0400612#ifdef CONFIG_DYNAMIC_FTRACE
613 functions = ftrace_update_tot_cnt;
614#else
615 /*
616 * We do not know the number of functions that exist because
617 * dynamic tracing is what counts them. With past experience
618 * we have around 20K functions. That should be more than enough.
619 * It is highly unlikely we will execute every function in
620 * the kernel.
621 */
622 functions = 20000;
623#endif
624
Steven Rostedtcafb1682009-03-24 20:50:39 -0400625 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400626
Steven Rostedt318e0a72009-03-25 20:06:34 -0400627 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
628
Namhyung Kim226c8ea2013-04-01 21:46:24 +0900629 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400630 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400631 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400632 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400633 pg = pg->next;
634 }
635
636 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400637
638 out_free:
639 pg = stat->start;
640 while (pg) {
641 unsigned long tmp = (unsigned long)pg;
642
643 pg = pg->next;
644 free_page(tmp);
645 }
646
Steven Rostedt318e0a72009-03-25 20:06:34 -0400647 stat->pages = NULL;
648 stat->start = NULL;
649
650 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400651}
652
Steven Rostedtcafb1682009-03-24 20:50:39 -0400653static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400654{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400655 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400656 int size;
657
Steven Rostedtcafb1682009-03-24 20:50:39 -0400658 stat = &per_cpu(ftrace_profile_stats, cpu);
659
660 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400661 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400662 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400663 return 0;
664 }
665
666 /*
667 * We are profiling all functions, but usually only a few thousand
668 * functions are hit. We'll make a hash of 1024 items.
669 */
670 size = FTRACE_PROFILE_HASH_SIZE;
671
Steven Rostedtcafb1682009-03-24 20:50:39 -0400672 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400673
Steven Rostedtcafb1682009-03-24 20:50:39 -0400674 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400675 return -ENOMEM;
676
Steven Rostedtcafb1682009-03-24 20:50:39 -0400677 if (!ftrace_profile_bits) {
678 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400679
Steven Rostedtcafb1682009-03-24 20:50:39 -0400680 for (; size; size >>= 1)
681 ftrace_profile_bits++;
682 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400683
Steven Rostedt318e0a72009-03-25 20:06:34 -0400684 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400685 if (ftrace_profile_pages_init(stat) < 0) {
686 kfree(stat->hash);
687 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400688 return -ENOMEM;
689 }
690
691 return 0;
692}
693
Steven Rostedtcafb1682009-03-24 20:50:39 -0400694static int ftrace_profile_init(void)
695{
696 int cpu;
697 int ret = 0;
698
Miao Xie09951c92013-12-16 15:20:01 +0800699 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400700 ret = ftrace_profile_init_cpu(cpu);
701 if (ret)
702 break;
703 }
704
705 return ret;
706}
707
Steven Rostedt493762f2009-03-23 17:12:36 -0400708/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400709static struct ftrace_profile *
710ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400711{
712 struct ftrace_profile *rec;
713 struct hlist_head *hhd;
714 struct hlist_node *n;
715 unsigned long key;
716
717 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400718 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400719
720 if (hlist_empty(hhd))
721 return NULL;
722
723 hlist_for_each_entry_rcu(rec, n, hhd, node) {
724 if (rec->ip == ip)
725 return rec;
726 }
727
728 return NULL;
729}
730
Steven Rostedtcafb1682009-03-24 20:50:39 -0400731static void ftrace_add_profile(struct ftrace_profile_stat *stat,
732 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400733{
734 unsigned long key;
735
736 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400737 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400738}
739
Steven Rostedt318e0a72009-03-25 20:06:34 -0400740/*
741 * The memory is already allocated, this simply finds a new record to use.
742 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400743static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400744ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400745{
746 struct ftrace_profile *rec = NULL;
747
Steven Rostedt318e0a72009-03-25 20:06:34 -0400748 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400749 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400750 goto out;
751
Steven Rostedt493762f2009-03-23 17:12:36 -0400752 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400753 * Try to find the function again since an NMI
754 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400755 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400756 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400757 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400758 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400759
Steven Rostedtcafb1682009-03-24 20:50:39 -0400760 if (stat->pages->index == PROFILES_PER_PAGE) {
761 if (!stat->pages->next)
762 goto out;
763 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400764 }
765
Steven Rostedtcafb1682009-03-24 20:50:39 -0400766 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400767 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400768 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400769
Steven Rostedt493762f2009-03-23 17:12:36 -0400770 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400771 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400772
773 return rec;
774}
775
Steven Rostedt493762f2009-03-23 17:12:36 -0400776static void
777function_profile_call(unsigned long ip, unsigned long parent_ip)
778{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400779 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400780 struct ftrace_profile *rec;
781 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400782
783 if (!ftrace_profile_enabled)
784 return;
785
Steven Rostedt493762f2009-03-23 17:12:36 -0400786 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400787
788 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400789 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400790 goto out;
791
792 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400793 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400794 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400795 if (!rec)
796 goto out;
797 }
798
799 rec->counter++;
800 out:
801 local_irq_restore(flags);
802}
803
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400804#ifdef CONFIG_FUNCTION_GRAPH_TRACER
805static int profile_graph_entry(struct ftrace_graph_ent *trace)
806{
807 function_profile_call(trace->func, 0);
808 return 1;
809}
810
811static void profile_graph_return(struct ftrace_graph_ret *trace)
812{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400813 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400814 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400815 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400816 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400817
818 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400820 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400821 goto out;
822
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400823 /* If the calltime was zero'd ignore it */
824 if (!trace->calltime)
825 goto out;
826
Steven Rostedta2a16d62009-03-24 23:17:58 -0400827 calltime = trace->rettime - trace->calltime;
828
829 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
830 int index;
831
832 index = trace->depth;
833
834 /* Append this call time to the parent time to subtract */
835 if (index)
836 current->ret_stack[index - 1].subtime += calltime;
837
838 if (current->ret_stack[index].subtime < calltime)
839 calltime -= current->ret_stack[index].subtime;
840 else
841 calltime = 0;
842 }
843
Steven Rostedtcafb1682009-03-24 20:50:39 -0400844 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400845 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400846 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400847 rec->time_squared += calltime * calltime;
848 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400849
Steven Rostedtcafb1682009-03-24 20:50:39 -0400850 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400851 local_irq_restore(flags);
852}
853
854static int register_ftrace_profiler(void)
855{
856 return register_ftrace_graph(&profile_graph_return,
857 &profile_graph_entry);
858}
859
860static void unregister_ftrace_profiler(void)
861{
862 unregister_ftrace_graph();
863}
864#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100865static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400866 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400867};
868
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400869static int register_ftrace_profiler(void)
870{
871 return register_ftrace_function(&ftrace_profile_ops);
872}
873
874static void unregister_ftrace_profiler(void)
875{
876 unregister_ftrace_function(&ftrace_profile_ops);
877}
878#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
879
Steven Rostedt493762f2009-03-23 17:12:36 -0400880static ssize_t
881ftrace_profile_write(struct file *filp, const char __user *ubuf,
882 size_t cnt, loff_t *ppos)
883{
884 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400885 int ret;
886
Peter Huewe22fe9b52011-06-07 21:58:27 +0200887 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
888 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400889 return ret;
890
891 val = !!val;
892
893 mutex_lock(&ftrace_profile_lock);
894 if (ftrace_profile_enabled ^ val) {
895 if (val) {
896 ret = ftrace_profile_init();
897 if (ret < 0) {
898 cnt = ret;
899 goto out;
900 }
901
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400902 ret = register_ftrace_profiler();
903 if (ret < 0) {
904 cnt = ret;
905 goto out;
906 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400907 ftrace_profile_enabled = 1;
908 } else {
909 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400910 /*
911 * unregister_ftrace_profiler calls stop_machine
912 * so this acts like an synchronize_sched.
913 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400914 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400915 }
916 }
917 out:
918 mutex_unlock(&ftrace_profile_lock);
919
Jiri Olsacf8517c2009-10-23 19:36:16 -0400920 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400921
922 return cnt;
923}
924
925static ssize_t
926ftrace_profile_read(struct file *filp, char __user *ubuf,
927 size_t cnt, loff_t *ppos)
928{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400929 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400930 int r;
931
932 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
933 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
934}
935
936static const struct file_operations ftrace_profile_fops = {
937 .open = tracing_open_generic,
938 .read = ftrace_profile_read,
939 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200940 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400941};
942
Steven Rostedtcafb1682009-03-24 20:50:39 -0400943/* used to initialize the real stat files */
944static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400945 .name = "functions",
946 .stat_start = function_stat_start,
947 .stat_next = function_stat_next,
948 .stat_cmp = function_stat_cmp,
949 .stat_headers = function_stat_headers,
950 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400951};
952
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400953static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400954{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400955 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400956 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400957 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400958 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400959 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400960
Steven Rostedtcafb1682009-03-24 20:50:39 -0400961 for_each_possible_cpu(cpu) {
962 stat = &per_cpu(ftrace_profile_stats, cpu);
963
964 /* allocate enough for function name + cpu number */
965 name = kmalloc(32, GFP_KERNEL);
966 if (!name) {
967 /*
968 * The files created are permanent, if something happens
969 * we still do not free memory.
970 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400971 WARN(1,
972 "Could not allocate stat file for cpu %d\n",
973 cpu);
974 return;
975 }
976 stat->stat = function_stats;
977 snprintf(name, 32, "function%d", cpu);
978 stat->stat.name = name;
979 ret = register_stat_tracer(&stat->stat);
980 if (ret) {
981 WARN(1,
982 "Could not register function stat for cpu %d\n",
983 cpu);
984 kfree(name);
985 return;
986 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400987 }
988
989 entry = debugfs_create_file("function_profile_enabled", 0644,
990 d_tracer, NULL, &ftrace_profile_fops);
991 if (!entry)
992 pr_warning("Could not create debugfs "
993 "'function_profile_enabled' entry\n");
994}
995
996#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400997static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400998{
999}
1000#endif /* CONFIG_FUNCTION_PROFILER */
1001
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001002static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1003
Steven Rostedtbc4d36c2013-06-07 17:02:08 +08001004loff_t
1005ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1006{
1007 loff_t ret;
1008
1009 if (file->f_mode & FMODE_READ)
1010 ret = seq_lseek(file, offset, whence);
1011 else
1012 file->f_pos = ret = 1;
1013
1014 return ret;
1015}
1016
Steven Rostedt3d083392008-05-12 21:20:42 +02001017#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001018
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001019#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001020# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001021#endif
1022
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001023static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1024
Steven Rostedtb6887d72009-02-17 12:32:04 -05001025struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001026 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001027 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001028 unsigned long flags;
1029 unsigned long ip;
1030 void *data;
1031 struct rcu_head rcu;
1032};
1033
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001034struct ftrace_func_entry {
1035 struct hlist_node hlist;
1036 unsigned long ip;
1037};
1038
1039struct ftrace_hash {
1040 unsigned long size_bits;
1041 struct hlist_head *buckets;
1042 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001043 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001044};
1045
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001046/*
1047 * We make these constant because no one should touch them,
1048 * but they are used as the default "empty hash", to avoid allocating
1049 * it all the time. These are in a read only section such that if
1050 * anyone does try to modify it, it will cause an exception.
1051 */
1052static const struct hlist_head empty_buckets[1];
1053static const struct ftrace_hash empty_hash = {
1054 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001055};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001056#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001057
Steven Rostedt2b499382011-05-03 22:49:52 -04001058static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001059 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001060 .notrace_hash = EMPTY_HASH,
1061 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001062};
1063
Steven Rostedt41c52c02008-05-22 11:46:33 -04001064static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001065
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001066struct ftrace_page {
1067 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001068 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001069 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001070 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001071};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001072
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001073static struct ftrace_page *ftrace_new_pgs;
1074
Steven Rostedta7900872011-12-16 16:23:44 -05001075#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1076#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001077
1078/* estimate from running different kernels */
1079#define NR_TO_INIT 10000
1080
1081static struct ftrace_page *ftrace_pages_start;
1082static struct ftrace_page *ftrace_pages;
1083
Steven Rostedt06a51d92011-12-19 19:07:36 -05001084static bool ftrace_hash_empty(struct ftrace_hash *hash)
1085{
1086 return !hash || !hash->count;
1087}
1088
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001089static struct ftrace_func_entry *
1090ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1091{
1092 unsigned long key;
1093 struct ftrace_func_entry *entry;
1094 struct hlist_head *hhd;
1095 struct hlist_node *n;
1096
Steven Rostedt06a51d92011-12-19 19:07:36 -05001097 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001098 return NULL;
1099
1100 if (hash->size_bits > 0)
1101 key = hash_long(ip, hash->size_bits);
1102 else
1103 key = 0;
1104
1105 hhd = &hash->buckets[key];
1106
1107 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1108 if (entry->ip == ip)
1109 return entry;
1110 }
1111 return NULL;
1112}
1113
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001114static void __add_hash_entry(struct ftrace_hash *hash,
1115 struct ftrace_func_entry *entry)
1116{
1117 struct hlist_head *hhd;
1118 unsigned long key;
1119
1120 if (hash->size_bits)
1121 key = hash_long(entry->ip, hash->size_bits);
1122 else
1123 key = 0;
1124
1125 hhd = &hash->buckets[key];
1126 hlist_add_head(&entry->hlist, hhd);
1127 hash->count++;
1128}
1129
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001130static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1131{
1132 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001133
1134 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1135 if (!entry)
1136 return -ENOMEM;
1137
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001138 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001139 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001140
1141 return 0;
1142}
1143
1144static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001145free_hash_entry(struct ftrace_hash *hash,
1146 struct ftrace_func_entry *entry)
1147{
1148 hlist_del(&entry->hlist);
1149 kfree(entry);
1150 hash->count--;
1151}
1152
1153static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001154remove_hash_entry(struct ftrace_hash *hash,
1155 struct ftrace_func_entry *entry)
1156{
1157 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001158 hash->count--;
1159}
1160
1161static void ftrace_hash_clear(struct ftrace_hash *hash)
1162{
1163 struct hlist_head *hhd;
1164 struct hlist_node *tp, *tn;
1165 struct ftrace_func_entry *entry;
1166 int size = 1 << hash->size_bits;
1167 int i;
1168
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001169 if (!hash->count)
1170 return;
1171
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001172 for (i = 0; i < size; i++) {
1173 hhd = &hash->buckets[i];
1174 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001175 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001176 }
1177 FTRACE_WARN_ON(hash->count);
1178}
1179
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001180static void free_ftrace_hash(struct ftrace_hash *hash)
1181{
1182 if (!hash || hash == EMPTY_HASH)
1183 return;
1184 ftrace_hash_clear(hash);
1185 kfree(hash->buckets);
1186 kfree(hash);
1187}
1188
Steven Rostedt07fd5512011-05-05 18:03:47 -04001189static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1190{
1191 struct ftrace_hash *hash;
1192
1193 hash = container_of(rcu, struct ftrace_hash, rcu);
1194 free_ftrace_hash(hash);
1195}
1196
1197static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1198{
1199 if (!hash || hash == EMPTY_HASH)
1200 return;
1201 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1202}
1203
Jiri Olsa5500fa52012-02-15 15:51:54 +01001204void ftrace_free_filter(struct ftrace_ops *ops)
1205{
1206 free_ftrace_hash(ops->filter_hash);
1207 free_ftrace_hash(ops->notrace_hash);
1208}
1209
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001210static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1211{
1212 struct ftrace_hash *hash;
1213 int size;
1214
1215 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1216 if (!hash)
1217 return NULL;
1218
1219 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001220 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001221
1222 if (!hash->buckets) {
1223 kfree(hash);
1224 return NULL;
1225 }
1226
1227 hash->size_bits = size_bits;
1228
1229 return hash;
1230}
1231
1232static struct ftrace_hash *
1233alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1234{
1235 struct ftrace_func_entry *entry;
1236 struct ftrace_hash *new_hash;
1237 struct hlist_node *tp;
1238 int size;
1239 int ret;
1240 int i;
1241
1242 new_hash = alloc_ftrace_hash(size_bits);
1243 if (!new_hash)
1244 return NULL;
1245
1246 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001247 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001248 return new_hash;
1249
1250 size = 1 << hash->size_bits;
1251 for (i = 0; i < size; i++) {
1252 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1253 ret = add_hash_entry(new_hash, entry->ip);
1254 if (ret < 0)
1255 goto free_hash;
1256 }
1257 }
1258
1259 FTRACE_WARN_ON(new_hash->count != hash->count);
1260
1261 return new_hash;
1262
1263 free_hash:
1264 free_ftrace_hash(new_hash);
1265 return NULL;
1266}
1267
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001268static void
1269ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1270static void
1271ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1272
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001273static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001274ftrace_hash_move(struct ftrace_ops *ops, int enable,
1275 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001276{
1277 struct ftrace_func_entry *entry;
1278 struct hlist_node *tp, *tn;
1279 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001280 struct ftrace_hash *old_hash;
1281 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001282 unsigned long key;
1283 int size = src->count;
1284 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001285 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001286 int i;
1287
1288 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001289 * Remove the current set, update the hash and add
1290 * them back.
1291 */
1292 ftrace_hash_rec_disable(ops, enable);
1293
1294 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001295 * If the new source is empty, just free dst and assign it
1296 * the empty_hash.
1297 */
1298 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001299 free_ftrace_hash_rcu(*dst);
1300 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001301 /* still need to update the function records */
1302 ret = 0;
1303 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001304 }
1305
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001306 /*
1307 * Make the hash size about 1/2 the # found
1308 */
1309 for (size /= 2; size; size >>= 1)
1310 bits++;
1311
1312 /* Don't allocate too much */
1313 if (bits > FTRACE_HASH_MAX_BITS)
1314 bits = FTRACE_HASH_MAX_BITS;
1315
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001316 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001317 new_hash = alloc_ftrace_hash(bits);
1318 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001319 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001320
1321 size = 1 << src->size_bits;
1322 for (i = 0; i < size; i++) {
1323 hhd = &src->buckets[i];
1324 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1325 if (bits > 0)
1326 key = hash_long(entry->ip, bits);
1327 else
1328 key = 0;
1329 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001330 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001331 }
1332 }
1333
Steven Rostedt07fd5512011-05-05 18:03:47 -04001334 old_hash = *dst;
1335 rcu_assign_pointer(*dst, new_hash);
1336 free_ftrace_hash_rcu(old_hash);
1337
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001338 ret = 0;
1339 out:
1340 /*
1341 * Enable regardless of ret:
1342 * On success, we enable the new hash.
1343 * On failure, we re-enable the original hash.
1344 */
1345 ftrace_hash_rec_enable(ops, enable);
1346
1347 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001348}
1349
Steven Rostedt265c8312009-02-13 12:43:56 -05001350/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001351 * Test the hashes for this ops to see if we want to call
1352 * the ops->func or not.
1353 *
1354 * It's a match if the ip is in the ops->filter_hash or
1355 * the filter_hash does not exist or is empty,
1356 * AND
1357 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001358 *
1359 * This needs to be called with preemption disabled as
1360 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001361 */
1362static int
1363ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1364{
1365 struct ftrace_hash *filter_hash;
1366 struct ftrace_hash *notrace_hash;
1367 int ret;
1368
Steven Rostedtb8489142011-05-04 09:27:52 -04001369 filter_hash = rcu_dereference_raw(ops->filter_hash);
1370 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1371
Steven Rostedt06a51d92011-12-19 19:07:36 -05001372 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001373 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001374 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001375 !ftrace_lookup_ip(notrace_hash, ip)))
1376 ret = 1;
1377 else
1378 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001379
1380 return ret;
1381}
1382
1383/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001384 * This is a double for. Do not use 'break' to break out of the loop,
1385 * you must use a goto.
1386 */
1387#define do_for_each_ftrace_rec(pg, rec) \
1388 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1389 int _____i; \
1390 for (_____i = 0; _____i < pg->index; _____i++) { \
1391 rec = &pg->records[_____i];
1392
1393#define while_for_each_ftrace_rec() \
1394 } \
1395 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301396
Steven Rostedt5855fea2011-12-16 19:27:42 -05001397
1398static int ftrace_cmp_recs(const void *a, const void *b)
1399{
1400 const struct dyn_ftrace *reca = a;
1401 const struct dyn_ftrace *recb = b;
1402
1403 if (reca->ip > recb->ip)
1404 return 1;
1405 if (reca->ip < recb->ip)
1406 return -1;
1407 return 0;
1408}
1409
Steven Rostedtc88fd862011-08-16 09:53:39 -04001410/**
1411 * ftrace_location - return true if the ip giving is a traced location
1412 * @ip: the instruction pointer to check
1413 *
1414 * Returns 1 if @ip given is a pointer to a ftrace location.
1415 * That is, the instruction that is either a NOP or call to
1416 * the function tracer. It checks the ftrace internal tables to
1417 * determine if the address belongs or not.
1418 */
1419int ftrace_location(unsigned long ip)
1420{
1421 struct ftrace_page *pg;
1422 struct dyn_ftrace *rec;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001423 struct dyn_ftrace key;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001424
Steven Rostedt5855fea2011-12-16 19:27:42 -05001425 key.ip = ip;
1426
1427 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1428 rec = bsearch(&key, pg->records, pg->index,
1429 sizeof(struct dyn_ftrace),
1430 ftrace_cmp_recs);
1431 if (rec)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001432 return 1;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001433 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001434
1435 return 0;
1436}
1437
Steven Rostedted926f92011-05-03 13:25:24 -04001438static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1439 int filter_hash,
1440 bool inc)
1441{
1442 struct ftrace_hash *hash;
1443 struct ftrace_hash *other_hash;
1444 struct ftrace_page *pg;
1445 struct dyn_ftrace *rec;
1446 int count = 0;
1447 int all = 0;
1448
1449 /* Only update if the ops has been registered */
1450 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1451 return;
1452
1453 /*
1454 * In the filter_hash case:
1455 * If the count is zero, we update all records.
1456 * Otherwise we just update the items in the hash.
1457 *
1458 * In the notrace_hash case:
1459 * We enable the update in the hash.
1460 * As disabling notrace means enabling the tracing,
1461 * and enabling notrace means disabling, the inc variable
1462 * gets inversed.
1463 */
1464 if (filter_hash) {
1465 hash = ops->filter_hash;
1466 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001467 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001468 all = 1;
1469 } else {
1470 inc = !inc;
1471 hash = ops->notrace_hash;
1472 other_hash = ops->filter_hash;
1473 /*
1474 * If the notrace hash has no items,
1475 * then there's nothing to do.
1476 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001477 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001478 return;
1479 }
1480
1481 do_for_each_ftrace_rec(pg, rec) {
1482 int in_other_hash = 0;
1483 int in_hash = 0;
1484 int match = 0;
1485
1486 if (all) {
1487 /*
1488 * Only the filter_hash affects all records.
1489 * Update if the record is not in the notrace hash.
1490 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001491 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001492 match = 1;
1493 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001494 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1495 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001496
1497 /*
1498 *
1499 */
1500 if (filter_hash && in_hash && !in_other_hash)
1501 match = 1;
1502 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001503 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001504 match = 1;
1505 }
1506 if (!match)
1507 continue;
1508
1509 if (inc) {
1510 rec->flags++;
1511 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1512 return;
1513 } else {
1514 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1515 return;
1516 rec->flags--;
1517 }
1518 count++;
1519 /* Shortcut, if we handled all records, we are done. */
1520 if (!all && count == hash->count)
1521 return;
1522 } while_for_each_ftrace_rec();
1523}
1524
1525static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1526 int filter_hash)
1527{
1528 __ftrace_hash_rec_update(ops, filter_hash, 0);
1529}
1530
1531static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1532 int filter_hash)
1533{
1534 __ftrace_hash_rec_update(ops, filter_hash, 1);
1535}
1536
Ingo Molnare309b412008-05-12 21:20:51 +02001537static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001538{
Steven Rostedta7900872011-12-16 16:23:44 -05001539 if (ftrace_pages->index == ftrace_pages->size) {
1540 /* We should have allocated enough */
1541 if (WARN_ON(!ftrace_pages->next))
1542 return NULL;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001543 ftrace_pages = ftrace_pages->next;
1544 }
1545
1546 return &ftrace_pages->records[ftrace_pages->index++];
1547}
1548
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001549static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001550ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001551{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001552 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001553
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001554 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001555 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001556
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001557 rec = ftrace_alloc_dyn_node(ip);
1558 if (!rec)
1559 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001560
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001561 rec->ip = ip;
Steven Rostedt3d083392008-05-12 21:20:42 +02001562
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001563 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001564}
1565
Steven Rostedt05736a42008-09-22 14:55:47 -07001566static void print_ip_ins(const char *fmt, unsigned char *p)
1567{
1568 int i;
1569
1570 printk(KERN_CONT "%s", fmt);
1571
1572 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1573 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1574}
1575
Steven Rostedtc88fd862011-08-16 09:53:39 -04001576/**
1577 * ftrace_bug - report and shutdown function tracer
1578 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1579 * @ip: The address that failed
1580 *
1581 * The arch code that enables or disables the function tracing
1582 * can call ftrace_bug() when it has detected a problem in
1583 * modifying the code. @failed should be one of either:
1584 * EFAULT - if the problem happens on reading the @ip address
1585 * EINVAL - if what is read at @ip is not what was expected
1586 * EPERM - if the problem happens on writting to the @ip address
1587 */
1588void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001589{
1590 switch (failed) {
1591 case -EFAULT:
1592 FTRACE_WARN_ON_ONCE(1);
1593 pr_info("ftrace faulted on modifying ");
1594 print_ip_sym(ip);
1595 break;
1596 case -EINVAL:
1597 FTRACE_WARN_ON_ONCE(1);
1598 pr_info("ftrace failed to modify ");
1599 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001600 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001601 printk(KERN_CONT "\n");
1602 break;
1603 case -EPERM:
1604 FTRACE_WARN_ON_ONCE(1);
1605 pr_info("ftrace faulted on writing ");
1606 print_ip_sym(ip);
1607 break;
1608 default:
1609 FTRACE_WARN_ON_ONCE(1);
1610 pr_info("ftrace faulted on unknown error ");
1611 print_ip_sym(ip);
1612 }
1613}
1614
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001615
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001616/* Return 1 if the address range is reserved for ftrace */
1617int ftrace_text_reserved(void *start, void *end)
1618{
1619 struct dyn_ftrace *rec;
1620 struct ftrace_page *pg;
1621
1622 do_for_each_ftrace_rec(pg, rec) {
1623 if (rec->ip <= (unsigned long)end &&
1624 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1625 return 1;
1626 } while_for_each_ftrace_rec();
1627 return 0;
1628}
1629
Steven Rostedtc88fd862011-08-16 09:53:39 -04001630static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001631{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001632 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001633
Steven Rostedt982c3502008-11-15 16:31:41 -05001634 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001635 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001636 *
Steven Rostedted926f92011-05-03 13:25:24 -04001637 * If the record has a ref count, then we need to enable it
1638 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001639 *
Steven Rostedted926f92011-05-03 13:25:24 -04001640 * Otherwise we make sure its disabled.
1641 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001642 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001643 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001644 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001645 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001646 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001647
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001648 /* If the state of this record hasn't changed, then do nothing */
1649 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001650 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001651
1652 if (flag) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001653 if (update)
1654 rec->flags |= FTRACE_FL_ENABLED;
1655 return FTRACE_UPDATE_MAKE_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001656 }
1657
Steven Rostedtc88fd862011-08-16 09:53:39 -04001658 if (update)
1659 rec->flags &= ~FTRACE_FL_ENABLED;
1660
1661 return FTRACE_UPDATE_MAKE_NOP;
1662}
1663
1664/**
1665 * ftrace_update_record, set a record that now is tracing or not
1666 * @rec: the record to update
1667 * @enable: set to 1 if the record is tracing, zero to force disable
1668 *
1669 * The records that represent all functions that can be traced need
1670 * to be updated when tracing has been enabled.
1671 */
1672int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1673{
1674 return ftrace_check_record(rec, enable, 1);
1675}
1676
1677/**
1678 * ftrace_test_record, check if the record has been enabled or not
1679 * @rec: the record to test
1680 * @enable: set to 1 to check if enabled, 0 if it is disabled
1681 *
1682 * The arch code may need to test if a record is already set to
1683 * tracing to determine how to modify the function code that it
1684 * represents.
1685 */
1686int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1687{
1688 return ftrace_check_record(rec, enable, 0);
1689}
1690
1691static int
1692__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1693{
1694 unsigned long ftrace_addr;
1695 int ret;
1696
1697 ftrace_addr = (unsigned long)FTRACE_ADDR;
1698
1699 ret = ftrace_update_record(rec, enable);
1700
1701 switch (ret) {
1702 case FTRACE_UPDATE_IGNORE:
1703 return 0;
1704
1705 case FTRACE_UPDATE_MAKE_CALL:
1706 return ftrace_make_call(rec, ftrace_addr);
1707
1708 case FTRACE_UPDATE_MAKE_NOP:
1709 return ftrace_make_nop(NULL, rec, ftrace_addr);
1710 }
1711
1712 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001713}
1714
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001715static void ftrace_replace_code(int update)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001716{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001717 struct dyn_ftrace *rec;
1718 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001719 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001720
Steven Rostedt45a4a232011-04-21 23:16:46 -04001721 if (unlikely(ftrace_disabled))
1722 return;
1723
Steven Rostedt265c8312009-02-13 12:43:56 -05001724 do_for_each_ftrace_rec(pg, rec) {
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001725 failed = __ftrace_replace_code(rec, update);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001726 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001727 ftrace_bug(failed, rec->ip);
1728 /* Stop processing */
1729 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001730 }
1731 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001732}
1733
Steven Rostedtc88fd862011-08-16 09:53:39 -04001734struct ftrace_rec_iter {
1735 struct ftrace_page *pg;
1736 int index;
1737};
1738
1739/**
1740 * ftrace_rec_iter_start, start up iterating over traced functions
1741 *
1742 * Returns an iterator handle that is used to iterate over all
1743 * the records that represent address locations where functions
1744 * are traced.
1745 *
1746 * May return NULL if no records are available.
1747 */
1748struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1749{
1750 /*
1751 * We only use a single iterator.
1752 * Protected by the ftrace_lock mutex.
1753 */
1754 static struct ftrace_rec_iter ftrace_rec_iter;
1755 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1756
1757 iter->pg = ftrace_pages_start;
1758 iter->index = 0;
1759
1760 /* Could have empty pages */
1761 while (iter->pg && !iter->pg->index)
1762 iter->pg = iter->pg->next;
1763
1764 if (!iter->pg)
1765 return NULL;
1766
1767 return iter;
1768}
1769
1770/**
1771 * ftrace_rec_iter_next, get the next record to process.
1772 * @iter: The handle to the iterator.
1773 *
1774 * Returns the next iterator after the given iterator @iter.
1775 */
1776struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1777{
1778 iter->index++;
1779
1780 if (iter->index >= iter->pg->index) {
1781 iter->pg = iter->pg->next;
1782 iter->index = 0;
1783
1784 /* Could have empty pages */
1785 while (iter->pg && !iter->pg->index)
1786 iter->pg = iter->pg->next;
1787 }
1788
1789 if (!iter->pg)
1790 return NULL;
1791
1792 return iter;
1793}
1794
1795/**
1796 * ftrace_rec_iter_record, get the record at the iterator location
1797 * @iter: The current iterator location
1798 *
1799 * Returns the record that the current @iter is at.
1800 */
1801struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1802{
1803 return &iter->pg->records[iter->index];
1804}
1805
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301806static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001807ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001808{
1809 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001810 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001811
1812 ip = rec->ip;
1813
Steven Rostedt45a4a232011-04-21 23:16:46 -04001814 if (unlikely(ftrace_disabled))
1815 return 0;
1816
Shaohua Li25aac9d2009-01-09 11:29:40 +08001817 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001818 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001819 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301820 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001821 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301822 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001823}
1824
Steven Rostedt000ab692009-02-17 13:35:06 -05001825/*
1826 * archs can override this function if they must do something
1827 * before the modifying code is performed.
1828 */
1829int __weak ftrace_arch_code_modify_prepare(void)
1830{
1831 return 0;
1832}
1833
1834/*
1835 * archs can override this function if they must do something
1836 * after the modifying code is performed.
1837 */
1838int __weak ftrace_arch_code_modify_post_process(void)
1839{
1840 return 0;
1841}
1842
Ingo Molnare309b412008-05-12 21:20:51 +02001843static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001844{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001845 int *command = data;
1846
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001847 if (*command & FTRACE_UPDATE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001848 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001849 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001850 ftrace_replace_code(0);
1851
1852 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1853 ftrace_update_ftrace_func(ftrace_trace_function);
1854
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001855 if (*command & FTRACE_START_FUNC_RET)
1856 ftrace_enable_ftrace_graph_caller();
1857 else if (*command & FTRACE_STOP_FUNC_RET)
1858 ftrace_disable_ftrace_graph_caller();
1859
Steven Rostedtc88fd862011-08-16 09:53:39 -04001860 return 0;
1861}
1862
1863/**
1864 * ftrace_run_stop_machine, go back to the stop machine method
1865 * @command: The command to tell ftrace what to do
1866 *
1867 * If an arch needs to fall back to the stop machine method, the
1868 * it can call this function.
1869 */
1870void ftrace_run_stop_machine(int command)
1871{
1872 stop_machine(__ftrace_modify_code, &command, NULL);
1873}
1874
1875/**
1876 * arch_ftrace_update_code, modify the code to trace or not trace
1877 * @command: The command that needs to be done
1878 *
1879 * Archs can override this function if it does not need to
1880 * run stop_machine() to modify code.
1881 */
1882void __weak arch_ftrace_update_code(int command)
1883{
1884 ftrace_run_stop_machine(command);
1885}
1886
1887static void ftrace_run_update_code(int command)
1888{
1889 int ret;
1890
1891 ret = ftrace_arch_code_modify_prepare();
1892 FTRACE_WARN_ON(ret);
1893 if (ret)
1894 return;
1895 /*
1896 * Do not call function tracer while we update the code.
1897 * We are in stop machine.
1898 */
1899 function_trace_stop++;
1900
1901 /*
1902 * By default we use stop_machine() to modify the code.
1903 * But archs can do what ever they want as long as it
1904 * is safe. The stop_machine() is the safest, but also
1905 * produces the most overhead.
1906 */
1907 arch_ftrace_update_code(command);
1908
Steven Rostedt6331c282011-07-13 15:11:02 -04001909#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1910 /*
1911 * For archs that call ftrace_test_stop_func(), we must
1912 * wait till after we update all the function callers
1913 * before we update the callback. This keeps different
1914 * ops that record different functions from corrupting
1915 * each other.
1916 */
1917 __ftrace_trace_function = __ftrace_trace_function_delay;
1918#endif
1919 function_trace_stop--;
1920
Steven Rostedt000ab692009-02-17 13:35:06 -05001921 ret = ftrace_arch_code_modify_post_process();
1922 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001923}
1924
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001925static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001926static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001927static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001928
1929static void ftrace_startup_enable(int command)
1930{
1931 if (saved_ftrace_func != ftrace_trace_function) {
1932 saved_ftrace_func = ftrace_trace_function;
1933 command |= FTRACE_UPDATE_TRACE_FUNC;
1934 }
1935
1936 if (!command || !ftrace_enabled)
1937 return;
1938
1939 ftrace_run_update_code(command);
1940}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001941
Steven Rostedta1cd6172011-05-23 15:24:25 -04001942static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001943{
Steven Rostedtb8489142011-05-04 09:27:52 -04001944 bool hash_enable = true;
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001945 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04001946
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001947 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001948 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001949
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001950 ret = __register_ftrace_function(ops);
1951 if (ret)
1952 return ret;
1953
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001954 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001955 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001956
Steven Rostedtb8489142011-05-04 09:27:52 -04001957 /* ops marked global share the filter hashes */
1958 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1959 ops = &global_ops;
1960 /* Don't update hash if global is already set */
1961 if (global_start_up)
1962 hash_enable = false;
1963 global_start_up++;
1964 }
1965
Steven Rostedted926f92011-05-03 13:25:24 -04001966 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001967 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001968 ftrace_hash_rec_enable(ops, 1);
1969
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001970 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001971
1972 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001973}
1974
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001975static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001976{
Steven Rostedtb8489142011-05-04 09:27:52 -04001977 bool hash_disable = true;
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001978 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04001979
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001980 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001981 return -ENODEV;
1982
1983 ret = __unregister_ftrace_function(ops);
1984 if (ret)
1985 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001986
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001987 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001988 /*
1989 * Just warn in case of unbalance, no need to kill ftrace, it's not
1990 * critical but the ftrace_call callers may be never nopped again after
1991 * further ftrace uses.
1992 */
1993 WARN_ON_ONCE(ftrace_start_up < 0);
1994
Steven Rostedtb8489142011-05-04 09:27:52 -04001995 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1996 ops = &global_ops;
1997 global_start_up--;
1998 WARN_ON_ONCE(global_start_up < 0);
1999 /* Don't update hash if global still has users */
2000 if (global_start_up) {
2001 WARN_ON_ONCE(!ftrace_start_up);
2002 hash_disable = false;
2003 }
2004 }
2005
2006 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002007 ftrace_hash_rec_disable(ops, 1);
2008
Steven Rostedtb8489142011-05-04 09:27:52 -04002009 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002010 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002011
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002012 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002013
2014 if (saved_ftrace_func != ftrace_trace_function) {
2015 saved_ftrace_func = ftrace_trace_function;
2016 command |= FTRACE_UPDATE_TRACE_FUNC;
2017 }
2018
Steven Rostedt29558662014-02-11 14:49:37 -05002019 if (!command || !ftrace_enabled) {
2020 /*
2021 * If these are control ops, they still need their
2022 * per_cpu field freed. Since, function tracing is
2023 * not currently active, we can just free them
2024 * without synchronizing all CPUs.
2025 */
2026 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2027 control_ops_free(ops);
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002028 return 0;
Steven Rostedt29558662014-02-11 14:49:37 -05002029 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002030
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002031 ftrace_run_update_code(command);
Steven Rostedt29558662014-02-11 14:49:37 -05002032
2033 /*
2034 * Dynamic ops may be freed, we must make sure that all
2035 * callers are done before leaving this function.
2036 * The same goes for freeing the per_cpu data of the control
2037 * ops.
2038 *
2039 * Again, normal synchronize_sched() is not good enough.
2040 * We need to do a hard force of sched synchronization.
2041 * This is because we use preempt_disable() to do RCU, but
2042 * the function tracers can be called where RCU is not watching
2043 * (like before user_exit()). We can not rely on the RCU
2044 * infrastructure to do the synchronization, thus we must do it
2045 * ourselves.
2046 */
2047 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2048 schedule_on_each_cpu(ftrace_sync);
2049
2050 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2051 control_ops_free(ops);
2052 }
2053
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002054 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002055}
2056
Ingo Molnare309b412008-05-12 21:20:51 +02002057static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002058{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002059 if (unlikely(ftrace_disabled))
2060 return;
2061
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002062 /* Force update next time */
2063 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002064 /* ftrace_start_up is true if we want ftrace running */
2065 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002066 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002067}
2068
Ingo Molnare309b412008-05-12 21:20:51 +02002069static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002070{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002071 if (unlikely(ftrace_disabled))
2072 return;
2073
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002074 /* ftrace_start_up is true if ftrace is running */
2075 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002076 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002077}
2078
Steven Rostedt3d083392008-05-12 21:20:42 +02002079static cycle_t ftrace_update_time;
2080static unsigned long ftrace_update_cnt;
2081unsigned long ftrace_update_tot_cnt;
2082
Steven Rostedt (Red Hat)74d86ed2013-07-30 00:04:32 -04002083static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002084{
Steven Rostedt (Red Hat)74d86ed2013-07-30 00:04:32 -04002085 /*
2086 * Filter_hash being empty will default to trace module.
2087 * But notrace hash requires a test of individual module functions.
2088 */
2089 return ftrace_hash_empty(ops->filter_hash) &&
2090 ftrace_hash_empty(ops->notrace_hash);
2091}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002092
Steven Rostedt (Red Hat)74d86ed2013-07-30 00:04:32 -04002093/*
2094 * Check if the current ops references the record.
2095 *
2096 * If the ops traces all functions, then it was already accounted for.
2097 * If the ops does not trace the current record function, skip it.
2098 * If the ops ignores the function via notrace filter, skip it.
2099 */
2100static inline bool
2101ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2102{
2103 /* If ops isn't enabled, ignore it */
2104 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2105 return 0;
2106
2107 /* If ops traces all mods, we already accounted for it */
2108 if (ops_traces_mod(ops))
2109 return 0;
2110
2111 /* The function must be in the filter */
2112 if (!ftrace_hash_empty(ops->filter_hash) &&
2113 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2114 return 0;
2115
2116 /* If in notrace hash, we ignore it too */
2117 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2118 return 0;
2119
2120 return 1;
2121}
2122
2123static int referenced_filters(struct dyn_ftrace *rec)
2124{
2125 struct ftrace_ops *ops;
2126 int cnt = 0;
2127
2128 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2129 if (ops_references_rec(ops, rec))
2130 cnt++;
2131 }
2132
2133 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002134}
2135
Steven Rostedt31e88902008-11-14 16:21:19 -08002136static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002137{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002138 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002139 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302140 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002141 unsigned long ref = 0;
Steven Rostedt (Red Hat)74d86ed2013-07-30 00:04:32 -04002142 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002143 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002144
2145 /*
2146 * When adding a module, we need to check if tracers are
2147 * currently enabled and if they are set to trace all functions.
2148 * If they are, we need to enable the module functions as well
2149 * as update the reference counts for those function records.
2150 */
2151 if (mod) {
2152 struct ftrace_ops *ops;
2153
2154 for (ops = ftrace_ops_list;
2155 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)74d86ed2013-07-30 00:04:32 -04002156 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2157 if (ops_traces_mod(ops))
2158 ref++;
2159 else
2160 test = true;
2161 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002162 }
2163 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002164
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002165 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002166 ftrace_update_cnt = 0;
2167
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002168 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302169
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002170 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)74d86ed2013-07-30 00:04:32 -04002171 int cnt = ref;
2172
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002173 /* If something went wrong, bail without enabling anything */
2174 if (unlikely(ftrace_disabled))
2175 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002176
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002177 p = &pg->records[i];
Steven Rostedt (Red Hat)74d86ed2013-07-30 00:04:32 -04002178 if (test)
2179 cnt += referenced_filters(p);
2180 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302181
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002182 /*
2183 * Do the initial record conversion from mcount jump
2184 * to the NOP instructions.
2185 */
2186 if (!ftrace_code_disable(mod, p))
2187 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002188
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002189 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002190
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002191 /*
2192 * If the tracing is enabled, go ahead and enable the record.
2193 *
2194 * The reason not to enable the record immediatelly is the
2195 * inherent check of ftrace_make_nop/ftrace_make_call for
2196 * correct previous instructions. Making first the NOP
2197 * conversion puts the module to the correct state, thus
2198 * passing the ftrace_make_call check.
2199 */
Steven Rostedt (Red Hat)74d86ed2013-07-30 00:04:32 -04002200 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002201 int failed = __ftrace_replace_code(p, 1);
2202 if (failed)
2203 ftrace_bug(failed, p->ip);
2204 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002205 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002206 }
2207
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002208 ftrace_new_pgs = NULL;
2209
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002210 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002211 ftrace_update_time = stop - start;
2212 ftrace_update_tot_cnt += ftrace_update_cnt;
2213
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002214 return 0;
2215}
2216
Steven Rostedta7900872011-12-16 16:23:44 -05002217static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002218{
Steven Rostedta7900872011-12-16 16:23:44 -05002219 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002220 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002221
Steven Rostedta7900872011-12-16 16:23:44 -05002222 if (WARN_ON(!count))
2223 return -EINVAL;
2224
2225 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002226
2227 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002228 * We want to fill as much as possible. No more than a page
2229 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002230 */
Steven Rostedta7900872011-12-16 16:23:44 -05002231 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2232 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002233
Steven Rostedta7900872011-12-16 16:23:44 -05002234 again:
2235 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2236
2237 if (!pg->records) {
2238 /* if we can't allocate this size, try something smaller */
2239 if (!order)
2240 return -ENOMEM;
2241 order >>= 1;
2242 goto again;
2243 }
2244
2245 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2246 pg->size = cnt;
2247
2248 if (cnt > count)
2249 cnt = count;
2250
2251 return cnt;
2252}
2253
2254static struct ftrace_page *
2255ftrace_allocate_pages(unsigned long num_to_init)
2256{
2257 struct ftrace_page *start_pg;
2258 struct ftrace_page *pg;
2259 int order;
2260 int cnt;
2261
2262 if (!num_to_init)
2263 return 0;
2264
2265 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2266 if (!pg)
2267 return NULL;
2268
2269 /*
2270 * Try to allocate as much as possible in one continues
2271 * location that fills in all of the space. We want to
2272 * waste as little space as possible.
2273 */
2274 for (;;) {
2275 cnt = ftrace_allocate_records(pg, num_to_init);
2276 if (cnt < 0)
2277 goto free_pages;
2278
2279 num_to_init -= cnt;
2280 if (!num_to_init)
2281 break;
2282
2283 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2284 if (!pg->next)
2285 goto free_pages;
2286
2287 pg = pg->next;
2288 }
2289
2290 return start_pg;
2291
2292 free_pages:
2293 while (start_pg) {
2294 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2295 free_pages((unsigned long)pg->records, order);
2296 start_pg = pg->next;
2297 kfree(pg);
2298 pg = start_pg;
2299 }
2300 pr_info("ftrace: FAILED to allocate memory for functions\n");
2301 return NULL;
2302}
2303
2304static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2305{
2306 int cnt;
2307
2308 if (!num_to_init) {
2309 pr_info("ftrace: No functions to be traced?\n");
2310 return -1;
2311 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002312
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002313 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002314 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002315 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002316
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002317 return 0;
2318}
2319
Steven Rostedt5072c592008-05-12 21:20:43 +02002320#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2321
2322struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002323 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002324 loff_t func_pos;
2325 struct ftrace_page *pg;
2326 struct dyn_ftrace *func;
2327 struct ftrace_func_probe *probe;
2328 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002329 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002330 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002331 int hidx;
2332 int idx;
2333 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002334};
2335
Ingo Molnare309b412008-05-12 21:20:51 +02002336static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002337t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002338{
2339 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002340 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002341 struct hlist_head *hhd;
2342
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002343 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002344 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002345
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002346 if (iter->probe)
2347 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002348 retry:
2349 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2350 return NULL;
2351
2352 hhd = &ftrace_func_hash[iter->hidx];
2353
2354 if (hlist_empty(hhd)) {
2355 iter->hidx++;
2356 hnd = NULL;
2357 goto retry;
2358 }
2359
2360 if (!hnd)
2361 hnd = hhd->first;
2362 else {
2363 hnd = hnd->next;
2364 if (!hnd) {
2365 iter->hidx++;
2366 goto retry;
2367 }
2368 }
2369
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002370 if (WARN_ON_ONCE(!hnd))
2371 return NULL;
2372
2373 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2374
2375 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002376}
2377
2378static void *t_hash_start(struct seq_file *m, loff_t *pos)
2379{
2380 struct ftrace_iterator *iter = m->private;
2381 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002382 loff_t l;
2383
Steven Rostedt69a30832011-12-19 15:21:16 -05002384 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2385 return NULL;
2386
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002387 if (iter->func_pos > *pos)
2388 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002389
Li Zefand82d6242009-06-24 09:54:54 +08002390 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002391 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002392 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002393 if (!p)
2394 break;
2395 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002396 if (!p)
2397 return NULL;
2398
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002399 /* Only set this if we have an item */
2400 iter->flags |= FTRACE_ITER_HASH;
2401
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002402 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002403}
2404
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002405static int
2406t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002407{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002408 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002409
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002410 rec = iter->probe;
2411 if (WARN_ON_ONCE(!rec))
2412 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002413
Steven Rostedt809dcf22009-02-16 23:06:01 -05002414 if (rec->ops->print)
2415 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2416
Steven Rostedtb375a112009-09-17 00:05:58 -04002417 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002418
2419 if (rec->data)
2420 seq_printf(m, ":%p", rec->data);
2421 seq_putc(m, '\n');
2422
2423 return 0;
2424}
2425
2426static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002427t_next(struct seq_file *m, void *v, loff_t *pos)
2428{
2429 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002430 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002431 struct dyn_ftrace *rec = NULL;
2432
Steven Rostedt45a4a232011-04-21 23:16:46 -04002433 if (unlikely(ftrace_disabled))
2434 return NULL;
2435
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002436 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002437 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002438
Steven Rostedt5072c592008-05-12 21:20:43 +02002439 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002440 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002441
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002442 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002443 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002444
Steven Rostedt5072c592008-05-12 21:20:43 +02002445 retry:
2446 if (iter->idx >= iter->pg->index) {
2447 if (iter->pg->next) {
2448 iter->pg = iter->pg->next;
2449 iter->idx = 0;
2450 goto retry;
2451 }
2452 } else {
2453 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002454 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002455 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002456
Steven Rostedt41c52c02008-05-22 11:46:33 -04002457 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002458 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2459
2460 ((iter->flags & FTRACE_ITER_ENABLED) &&
2461 !(rec->flags & ~FTRACE_FL_MASK))) {
2462
Steven Rostedt5072c592008-05-12 21:20:43 +02002463 rec = NULL;
2464 goto retry;
2465 }
2466 }
2467
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002468 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002469 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002470
2471 iter->func = rec;
2472
2473 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002474}
2475
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002476static void reset_iter_read(struct ftrace_iterator *iter)
2477{
2478 iter->pos = 0;
2479 iter->func_pos = 0;
Dan Carpenter455d8952012-06-09 19:10:27 +03002480 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002481}
2482
2483static void *t_start(struct seq_file *m, loff_t *pos)
2484{
2485 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002486 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002487 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002488 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002489
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002490 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002491
2492 if (unlikely(ftrace_disabled))
2493 return NULL;
2494
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002495 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002496 * If an lseek was done, then reset and start from beginning.
2497 */
2498 if (*pos < iter->pos)
2499 reset_iter_read(iter);
2500
2501 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002502 * For set_ftrace_filter reading, if we have the filter
2503 * off, we can short cut and just print out that all
2504 * functions are enabled.
2505 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002506 if (iter->flags & FTRACE_ITER_FILTER &&
2507 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002508 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002509 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002510 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002511 /* reset in case of seek/pread */
2512 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002513 return iter;
2514 }
2515
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002516 if (iter->flags & FTRACE_ITER_HASH)
2517 return t_hash_start(m, pos);
2518
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002519 /*
2520 * Unfortunately, we need to restart at ftrace_pages_start
2521 * every time we let go of the ftrace_mutex. This is because
2522 * those pointers can change without the lock.
2523 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002524 iter->pg = ftrace_pages_start;
2525 iter->idx = 0;
2526 for (l = 0; l <= *pos; ) {
2527 p = t_next(m, p, &l);
2528 if (!p)
2529 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002530 }
walimis5821e1b2008-11-15 15:19:06 +08002531
Steven Rostedt69a30832011-12-19 15:21:16 -05002532 if (!p)
2533 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002534
2535 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002536}
2537
2538static void t_stop(struct seq_file *m, void *p)
2539{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002540 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002541}
2542
2543static int t_show(struct seq_file *m, void *v)
2544{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002545 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002546 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002547
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002548 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002549 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002550
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002551 if (iter->flags & FTRACE_ITER_PRINTALL) {
2552 seq_printf(m, "#### all functions enabled ####\n");
2553 return 0;
2554 }
2555
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002556 rec = iter->func;
2557
Steven Rostedt5072c592008-05-12 21:20:43 +02002558 if (!rec)
2559 return 0;
2560
Steven Rostedt647bcd02011-05-03 14:39:21 -04002561 seq_printf(m, "%ps", (void *)rec->ip);
2562 if (iter->flags & FTRACE_ITER_ENABLED)
2563 seq_printf(m, " (%ld)",
2564 rec->flags & ~FTRACE_FL_MASK);
2565 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002566
2567 return 0;
2568}
2569
James Morris88e9d342009-09-22 16:43:43 -07002570static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002571 .start = t_start,
2572 .next = t_next,
2573 .stop = t_stop,
2574 .show = t_show,
2575};
2576
Ingo Molnare309b412008-05-12 21:20:51 +02002577static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002578ftrace_avail_open(struct inode *inode, struct file *file)
2579{
2580 struct ftrace_iterator *iter;
2581 int ret;
2582
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002583 if (unlikely(ftrace_disabled))
2584 return -ENODEV;
2585
Steven Rostedt5072c592008-05-12 21:20:43 +02002586 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2587 if (!iter)
2588 return -ENOMEM;
2589
2590 iter->pg = ftrace_pages_start;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002591 iter->ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002592
2593 ret = seq_open(file, &show_ftrace_seq_ops);
2594 if (!ret) {
2595 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002596
Steven Rostedt5072c592008-05-12 21:20:43 +02002597 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002598 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002599 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002600 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002601
2602 return ret;
2603}
2604
Steven Rostedt647bcd02011-05-03 14:39:21 -04002605static int
2606ftrace_enabled_open(struct inode *inode, struct file *file)
2607{
2608 struct ftrace_iterator *iter;
2609 int ret;
2610
2611 if (unlikely(ftrace_disabled))
2612 return -ENODEV;
2613
2614 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2615 if (!iter)
2616 return -ENOMEM;
2617
2618 iter->pg = ftrace_pages_start;
2619 iter->flags = FTRACE_ITER_ENABLED;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002620 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002621
2622 ret = seq_open(file, &show_ftrace_seq_ops);
2623 if (!ret) {
2624 struct seq_file *m = file->private_data;
2625
2626 m->private = iter;
2627 } else {
2628 kfree(iter);
2629 }
2630
2631 return ret;
2632}
2633
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002634static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002635{
Steven Rostedt52baf112009-02-14 01:15:39 -05002636 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002637 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002638 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002639}
2640
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002641/**
2642 * ftrace_regex_open - initialize function tracer filter files
2643 * @ops: The ftrace_ops that hold the hash filters
2644 * @flag: The type of filter to process
2645 * @inode: The inode, usually passed in to your open routine
2646 * @file: The file, usually passed in to your open routine
2647 *
2648 * ftrace_regex_open() initializes the filter files for the
2649 * @ops. Depending on @flag it may process the filter hash or
2650 * the notrace hash of @ops. With this called from the open
2651 * routine, you can use ftrace_filter_write() for the write
2652 * routine if @flag has FTRACE_ITER_FILTER set, or
2653 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedtbc4d36c2013-06-07 17:02:08 +08002654 * ftrace_filter_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002655 * release must call ftrace_regex_release().
2656 */
2657int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002658ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002659 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002660{
2661 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002662 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002663 int ret = 0;
2664
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002665 if (unlikely(ftrace_disabled))
2666 return -ENODEV;
2667
Steven Rostedt5072c592008-05-12 21:20:43 +02002668 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2669 if (!iter)
2670 return -ENOMEM;
2671
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002672 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2673 kfree(iter);
2674 return -ENOMEM;
2675 }
2676
Steven Rostedtf45948e2011-05-02 12:29:25 -04002677 if (flag & FTRACE_ITER_NOTRACE)
2678 hash = ops->notrace_hash;
2679 else
2680 hash = ops->filter_hash;
2681
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002682 iter->ops = ops;
2683 iter->flags = flag;
2684
2685 if (file->f_mode & FMODE_WRITE) {
2686 mutex_lock(&ftrace_lock);
2687 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2688 mutex_unlock(&ftrace_lock);
2689
2690 if (!iter->hash) {
2691 trace_parser_put(&iter->parser);
2692 kfree(iter);
2693 return -ENOMEM;
2694 }
2695 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002696
Steven Rostedt41c52c02008-05-22 11:46:33 -04002697 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002698
Steven Rostedt5072c592008-05-12 21:20:43 +02002699 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002700 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002701 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002702
2703 if (file->f_mode & FMODE_READ) {
2704 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002705
2706 ret = seq_open(file, &show_ftrace_seq_ops);
2707 if (!ret) {
2708 struct seq_file *m = file->private_data;
2709 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002710 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002711 /* Failed */
2712 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002713 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002714 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002715 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002716 } else
2717 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002718 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002719
2720 return ret;
2721}
2722
Steven Rostedt41c52c02008-05-22 11:46:33 -04002723static int
2724ftrace_filter_open(struct inode *inode, struct file *file)
2725{
Steven Rostedt69a30832011-12-19 15:21:16 -05002726 return ftrace_regex_open(&global_ops,
2727 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2728 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002729}
2730
2731static int
2732ftrace_notrace_open(struct inode *inode, struct file *file)
2733{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002734 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002735 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002736}
2737
Steven Rostedt64e7c442009-02-13 17:08:48 -05002738static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002739{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002740 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002741 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002742
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002743 switch (type) {
2744 case MATCH_FULL:
2745 if (strcmp(str, regex) == 0)
2746 matched = 1;
2747 break;
2748 case MATCH_FRONT_ONLY:
2749 if (strncmp(str, regex, len) == 0)
2750 matched = 1;
2751 break;
2752 case MATCH_MIDDLE_ONLY:
2753 if (strstr(str, regex))
2754 matched = 1;
2755 break;
2756 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002757 slen = strlen(str);
2758 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002759 matched = 1;
2760 break;
2761 }
2762
2763 return matched;
2764}
2765
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002766static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002767enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002768{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002769 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002770 int ret = 0;
2771
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002772 entry = ftrace_lookup_ip(hash, rec->ip);
2773 if (not) {
2774 /* Do nothing if it doesn't exist */
2775 if (!entry)
2776 return 0;
2777
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002778 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002779 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002780 /* Do nothing if it exists */
2781 if (entry)
2782 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002783
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002784 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002785 }
2786 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002787}
2788
Steven Rostedt64e7c442009-02-13 17:08:48 -05002789static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002790ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2791 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002792{
2793 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002794 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002795
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002796 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2797
2798 if (mod) {
2799 /* module lookup requires matching the module */
2800 if (!modname || strcmp(modname, mod))
2801 return 0;
2802
2803 /* blank search means to match all funcs in the mod */
2804 if (!len)
2805 return 1;
2806 }
2807
Steven Rostedt64e7c442009-02-13 17:08:48 -05002808 return ftrace_match(str, regex, len, type);
2809}
2810
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002811static int
2812match_records(struct ftrace_hash *hash, char *buff,
2813 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002814{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002815 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002816 struct ftrace_page *pg;
2817 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002818 int type = MATCH_FULL;
2819 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002820 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002821 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002822
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002823 if (len) {
2824 type = filter_parse_regex(buff, len, &search, &not);
2825 search_len = strlen(search);
2826 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002827
Steven Rostedt52baf112009-02-14 01:15:39 -05002828 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002829
2830 if (unlikely(ftrace_disabled))
2831 goto out_unlock;
2832
Steven Rostedt265c8312009-02-13 12:43:56 -05002833 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002834 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002835 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002836 if (ret < 0) {
2837 found = ret;
2838 goto out_unlock;
2839 }
Li Zefan311d16d2009-12-08 11:15:11 +08002840 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002841 }
2842 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002843 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002844 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002845
2846 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002847}
2848
Steven Rostedt64e7c442009-02-13 17:08:48 -05002849static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002850ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002851{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002852 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002853}
2854
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002855static int
2856ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002857{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002858 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002859
Steven Rostedt64e7c442009-02-13 17:08:48 -05002860 /* blank or '*' mean the same */
2861 if (strcmp(buff, "*") == 0)
2862 buff[0] = 0;
2863
2864 /* handle the case of 'dont filter this module' */
2865 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2866 buff[0] = 0;
2867 not = 1;
2868 }
2869
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002870 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002871}
2872
Steven Rostedtf6180772009-02-14 00:40:25 -05002873/*
2874 * We register the module command as a template to show others how
2875 * to register the a command as well.
2876 */
2877
2878static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002879ftrace_mod_callback(struct ftrace_hash *hash,
2880 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002881{
2882 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002883 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002884
2885 /*
2886 * cmd == 'mod' because we only registered this func
2887 * for the 'mod' ftrace_func_command.
2888 * But if you register one func with multiple commands,
2889 * you can tell which command was used by the cmd
2890 * parameter.
2891 */
2892
2893 /* we must have a module name */
2894 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002895 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002896
2897 mod = strsep(&param, ":");
2898 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002899 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002900
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002901 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002902 if (!ret)
2903 ret = -EINVAL;
2904 if (ret < 0)
2905 return ret;
2906
2907 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002908}
2909
2910static struct ftrace_func_command ftrace_mod_cmd = {
2911 .name = "mod",
2912 .func = ftrace_mod_callback,
2913};
2914
2915static int __init ftrace_mod_cmd_init(void)
2916{
2917 return register_ftrace_command(&ftrace_mod_cmd);
2918}
2919device_initcall(ftrace_mod_cmd_init);
2920
Steven Rostedt59df055f2009-02-14 15:29:06 -05002921static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002922function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002923{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002924 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002925 struct hlist_head *hhd;
2926 struct hlist_node *n;
2927 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002928
2929 key = hash_long(ip, FTRACE_HASH_BITS);
2930
2931 hhd = &ftrace_func_hash[key];
2932
2933 if (hlist_empty(hhd))
2934 return;
2935
2936 /*
2937 * Disable preemption for these calls to prevent a RCU grace
2938 * period. This syncs the hash iteration and freeing of items
2939 * on the hash. rcu_read_lock is too dangerous here.
2940 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002941 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002942 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2943 if (entry->ip == ip)
2944 entry->ops->func(ip, parent_ip, &entry->data);
2945 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002946 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002947}
2948
Steven Rostedtb6887d72009-02-17 12:32:04 -05002949static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002950{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002951 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002952};
2953
Steven Rostedtb6887d72009-02-17 12:32:04 -05002954static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002955
Steven Rostedtb6887d72009-02-17 12:32:04 -05002956static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002957{
Steven Rostedtb8489142011-05-04 09:27:52 -04002958 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002959 int i;
2960
Steven Rostedtb6887d72009-02-17 12:32:04 -05002961 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002962 return;
2963
2964 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2965 struct hlist_head *hhd = &ftrace_func_hash[i];
2966 if (hhd->first)
2967 break;
2968 }
2969 /* Nothing registered? */
2970 if (i == FTRACE_FUNC_HASHSIZE)
2971 return;
2972
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002973 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002974
Steven Rostedtb6887d72009-02-17 12:32:04 -05002975 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002976}
2977
Steven Rostedtb6887d72009-02-17 12:32:04 -05002978static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002979{
2980 int i;
2981
Steven Rostedtb6887d72009-02-17 12:32:04 -05002982 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002983 return;
2984
2985 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2986 struct hlist_head *hhd = &ftrace_func_hash[i];
2987 if (hhd->first)
2988 return;
2989 }
2990
2991 /* no more funcs left */
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002992 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002993
Steven Rostedtb6887d72009-02-17 12:32:04 -05002994 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002995}
2996
2997
2998static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2999{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003000 struct ftrace_func_probe *entry =
3001 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003002
3003 if (entry->ops->free)
3004 entry->ops->free(&entry->data);
3005 kfree(entry);
3006}
3007
3008
3009int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003010register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003011 void *data)
3012{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003013 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003014 struct ftrace_page *pg;
3015 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003016 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003017 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003018 int count = 0;
3019 char *search;
3020
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003021 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003022 len = strlen(search);
3023
Steven Rostedtb6887d72009-02-17 12:32:04 -05003024 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003025 if (WARN_ON(not))
3026 return -EINVAL;
3027
3028 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003029
Steven Rostedt45a4a232011-04-21 23:16:46 -04003030 if (unlikely(ftrace_disabled))
3031 goto out_unlock;
3032
Steven Rostedt59df055f2009-02-14 15:29:06 -05003033 do_for_each_ftrace_rec(pg, rec) {
3034
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003035 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003036 continue;
3037
3038 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3039 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003040 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003041 if (!count)
3042 count = -ENOMEM;
3043 goto out_unlock;
3044 }
3045
3046 count++;
3047
3048 entry->data = data;
3049
3050 /*
3051 * The caller might want to do something special
3052 * for each function we find. We call the callback
3053 * to give the caller an opportunity to do so.
3054 */
3055 if (ops->callback) {
3056 if (ops->callback(rec->ip, &entry->data) < 0) {
3057 /* caller does not like this func */
3058 kfree(entry);
3059 continue;
3060 }
3061 }
3062
3063 entry->ops = ops;
3064 entry->ip = rec->ip;
3065
3066 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3067 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3068
3069 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05003070 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003071
3072 out_unlock:
3073 mutex_unlock(&ftrace_lock);
3074
3075 return count;
3076}
3077
3078enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003079 PROBE_TEST_FUNC = 1,
3080 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003081};
3082
3083static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003084__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003085 void *data, int flags)
3086{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003087 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003088 struct hlist_node *n, *tmp;
3089 char str[KSYM_SYMBOL_LEN];
3090 int type = MATCH_FULL;
3091 int i, len = 0;
3092 char *search;
3093
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003094 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003095 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003096 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003097 int not;
3098
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003099 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003100 len = strlen(search);
3101
Steven Rostedtb6887d72009-02-17 12:32:04 -05003102 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003103 if (WARN_ON(not))
3104 return;
3105 }
3106
3107 mutex_lock(&ftrace_lock);
3108 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3109 struct hlist_head *hhd = &ftrace_func_hash[i];
3110
3111 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3112
3113 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003114 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003115 continue;
3116
Steven Rostedtb6887d72009-02-17 12:32:04 -05003117 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003118 continue;
3119
3120 /* do this last, since it is the most expensive */
3121 if (glob) {
3122 kallsyms_lookup(entry->ip, NULL, NULL,
3123 NULL, str);
3124 if (!ftrace_match(str, glob, len, type))
3125 continue;
3126 }
3127
Steven Rostedt (Red Hat)52cecaa2013-03-13 11:15:19 -04003128 hlist_del_rcu(&entry->node);
3129 call_rcu_sched(&entry->rcu, ftrace_free_entry_rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003130 }
3131 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003132 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003133 mutex_unlock(&ftrace_lock);
3134}
3135
3136void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003137unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003138 void *data)
3139{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003140 __unregister_ftrace_function_probe(glob, ops, data,
3141 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003142}
3143
3144void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003145unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003146{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003147 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003148}
3149
Steven Rostedtb6887d72009-02-17 12:32:04 -05003150void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003151{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003152 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003153}
3154
Steven Rostedtf6180772009-02-14 00:40:25 -05003155static LIST_HEAD(ftrace_commands);
3156static DEFINE_MUTEX(ftrace_cmd_mutex);
3157
3158int register_ftrace_command(struct ftrace_func_command *cmd)
3159{
3160 struct ftrace_func_command *p;
3161 int ret = 0;
3162
3163 mutex_lock(&ftrace_cmd_mutex);
3164 list_for_each_entry(p, &ftrace_commands, list) {
3165 if (strcmp(cmd->name, p->name) == 0) {
3166 ret = -EBUSY;
3167 goto out_unlock;
3168 }
3169 }
3170 list_add(&cmd->list, &ftrace_commands);
3171 out_unlock:
3172 mutex_unlock(&ftrace_cmd_mutex);
3173
3174 return ret;
3175}
3176
3177int unregister_ftrace_command(struct ftrace_func_command *cmd)
3178{
3179 struct ftrace_func_command *p, *n;
3180 int ret = -ENODEV;
3181
3182 mutex_lock(&ftrace_cmd_mutex);
3183 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3184 if (strcmp(cmd->name, p->name) == 0) {
3185 ret = 0;
3186 list_del_init(&p->list);
3187 goto out_unlock;
3188 }
3189 }
3190 out_unlock:
3191 mutex_unlock(&ftrace_cmd_mutex);
3192
3193 return ret;
3194}
3195
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003196static int ftrace_process_regex(struct ftrace_hash *hash,
3197 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003198{
Steven Rostedtf6180772009-02-14 00:40:25 -05003199 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003200 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003201 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003202
3203 func = strsep(&next, ":");
3204
3205 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003206 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003207 if (!ret)
3208 ret = -EINVAL;
3209 if (ret < 0)
3210 return ret;
3211 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003212 }
3213
Steven Rostedtf6180772009-02-14 00:40:25 -05003214 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003215
3216 command = strsep(&next, ":");
3217
Steven Rostedtf6180772009-02-14 00:40:25 -05003218 mutex_lock(&ftrace_cmd_mutex);
3219 list_for_each_entry(p, &ftrace_commands, list) {
3220 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003221 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003222 goto out_unlock;
3223 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003224 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003225 out_unlock:
3226 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003227
Steven Rostedtf6180772009-02-14 00:40:25 -05003228 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003229}
3230
Ingo Molnare309b412008-05-12 21:20:51 +02003231static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003232ftrace_regex_write(struct file *file, const char __user *ubuf,
3233 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003234{
3235 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003236 struct trace_parser *parser;
3237 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003238
Li Zefan4ba79782009-09-22 13:52:20 +08003239 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003240 return 0;
3241
Steven Rostedt41c52c02008-05-22 11:46:33 -04003242 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003243
Steven Rostedt45a4a232011-04-21 23:16:46 -04003244 ret = -ENODEV;
3245 if (unlikely(ftrace_disabled))
3246 goto out_unlock;
3247
Steven Rostedt5072c592008-05-12 21:20:43 +02003248 if (file->f_mode & FMODE_READ) {
3249 struct seq_file *m = file->private_data;
3250 iter = m->private;
3251 } else
3252 iter = file->private_data;
3253
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003254 parser = &iter->parser;
3255 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003256
Li Zefan4ba79782009-09-22 13:52:20 +08003257 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003258 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003259 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003260 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003261 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003262 if (ret)
Li Zefaned146b22009-11-03 08:55:38 +08003263 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003264 }
3265
Steven Rostedt5072c592008-05-12 21:20:43 +02003266 ret = read;
Li Zefaned146b22009-11-03 08:55:38 +08003267out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003268 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b22009-11-03 08:55:38 +08003269
Steven Rostedt5072c592008-05-12 21:20:43 +02003270 return ret;
3271}
3272
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003273ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003274ftrace_filter_write(struct file *file, const char __user *ubuf,
3275 size_t cnt, loff_t *ppos)
3276{
3277 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3278}
3279
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003280ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003281ftrace_notrace_write(struct file *file, const char __user *ubuf,
3282 size_t cnt, loff_t *ppos)
3283{
3284 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3285}
3286
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003287static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003288ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3289 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003290{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003291 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003292 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003293 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003294
Steven Rostedt936e0742011-05-05 22:54:01 -04003295 /* All global ops uses the global ops filters */
3296 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3297 ops = &global_ops;
3298
Steven Rostedt41c52c02008-05-22 11:46:33 -04003299 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003300 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003301
Steven Rostedtf45948e2011-05-02 12:29:25 -04003302 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003303 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003304 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003305 orig_hash = &ops->notrace_hash;
3306
3307 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3308 if (!hash)
3309 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003310
Steven Rostedt41c52c02008-05-22 11:46:33 -04003311 mutex_lock(&ftrace_regex_lock);
3312 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003313 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003314 if (buf && !ftrace_match_records(hash, buf, len)) {
3315 ret = -EINVAL;
3316 goto out_regex_unlock;
3317 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003318
3319 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003320 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003321 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3322 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003323 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003324
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003325 mutex_unlock(&ftrace_lock);
3326
Jiri Olsaac483c42012-01-02 10:04:14 +01003327 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003328 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003329
3330 free_ftrace_hash(hash);
3331 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003332}
3333
Steven Rostedt77a2b372008-05-12 21:20:45 +02003334/**
3335 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003336 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003337 * @buf - the string that holds the function filter text.
3338 * @len - the length of the string.
3339 * @reset - non zero to reset all filters before applying this filter.
3340 *
3341 * Filters denote which functions should be enabled when tracing is enabled.
3342 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3343 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003344int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003345 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003346{
Jiri Olsaac483c42012-01-02 10:04:14 +01003347 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003348}
Steven Rostedt936e0742011-05-05 22:54:01 -04003349EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003350
Steven Rostedt41c52c02008-05-22 11:46:33 -04003351/**
3352 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003353 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003354 * @buf - the string that holds the function notrace text.
3355 * @len - the length of the string.
3356 * @reset - non zero to reset all filters before applying this filter.
3357 *
3358 * Notrace Filters denote which functions should not be enabled when tracing
3359 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3360 * for tracing.
3361 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003362int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003363 int len, int reset)
3364{
Jiri Olsaac483c42012-01-02 10:04:14 +01003365 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003366}
3367EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3368/**
3369 * ftrace_set_filter - set a function to filter on in ftrace
3370 * @ops - the ops to set the filter with
3371 * @buf - the string that holds the function filter text.
3372 * @len - the length of the string.
3373 * @reset - non zero to reset all filters before applying this filter.
3374 *
3375 * Filters denote which functions should be enabled when tracing is enabled.
3376 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3377 */
3378void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3379{
3380 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3381}
3382EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3383
3384/**
3385 * ftrace_set_notrace - set a function to not trace in ftrace
3386 * @ops - the ops to set the notrace filter with
3387 * @buf - the string that holds the function notrace text.
3388 * @len - the length of the string.
3389 * @reset - non zero to reset all filters before applying this filter.
3390 *
3391 * Notrace Filters denote which functions should not be enabled when tracing
3392 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3393 * for tracing.
3394 */
3395void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003396{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003397 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003398}
Steven Rostedt936e0742011-05-05 22:54:01 -04003399EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003400
Steven Rostedt2af15d62009-05-28 13:37:24 -04003401/*
3402 * command line interface to allow users to set filters on boot up.
3403 */
3404#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3405static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3406static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3407
3408static int __init set_ftrace_notrace(char *str)
3409{
3410 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3411 return 1;
3412}
3413__setup("ftrace_notrace=", set_ftrace_notrace);
3414
3415static int __init set_ftrace_filter(char *str)
3416{
3417 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3418 return 1;
3419}
3420__setup("ftrace_filter=", set_ftrace_filter);
3421
Stefan Assmann369bc182009-10-12 22:17:21 +02003422#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003423static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003424static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3425
Stefan Assmann369bc182009-10-12 22:17:21 +02003426static int __init set_graph_function(char *str)
3427{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003428 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003429 return 1;
3430}
3431__setup("ftrace_graph_filter=", set_graph_function);
3432
3433static void __init set_ftrace_early_graph(char *buf)
3434{
3435 int ret;
3436 char *func;
3437
3438 while (buf) {
3439 func = strsep(&buf, ",");
3440 /* we allow only one expression at a time */
3441 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3442 func);
3443 if (ret)
3444 printk(KERN_DEBUG "ftrace: function %s not "
3445 "traceable\n", func);
3446 }
3447}
3448#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3449
Steven Rostedt2a85a372011-12-19 21:57:44 -05003450void __init
3451ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003452{
3453 char *func;
3454
3455 while (buf) {
3456 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003457 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003458 }
3459}
3460
3461static void __init set_ftrace_early_filters(void)
3462{
3463 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003464 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003465 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003466 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003467#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3468 if (ftrace_graph_buf[0])
3469 set_ftrace_early_graph(ftrace_graph_buf);
3470#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003471}
3472
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003473int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003474{
3475 struct seq_file *m = (struct seq_file *)file->private_data;
3476 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003477 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003478 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003479 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003480 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003481
Steven Rostedt41c52c02008-05-22 11:46:33 -04003482 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003483 if (file->f_mode & FMODE_READ) {
3484 iter = m->private;
3485
3486 seq_release(inode, file);
3487 } else
3488 iter = file->private_data;
3489
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003490 parser = &iter->parser;
3491 if (trace_parser_loaded(parser)) {
3492 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003493 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003494 }
3495
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003496 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003497
Steven Rostedt058e2972011-04-29 22:35:33 -04003498 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003499 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3500
3501 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003502 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003503 else
3504 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003505
Steven Rostedt058e2972011-04-29 22:35:33 -04003506 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003507 ret = ftrace_hash_move(iter->ops, filter_hash,
3508 orig_hash, iter->hash);
3509 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3510 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003511 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003512
Steven Rostedt058e2972011-04-29 22:35:33 -04003513 mutex_unlock(&ftrace_lock);
3514 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003515 free_ftrace_hash(iter->hash);
3516 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003517
Steven Rostedt41c52c02008-05-22 11:46:33 -04003518 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003519 return 0;
3520}
3521
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003522static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003523 .open = ftrace_avail_open,
3524 .read = seq_read,
3525 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003526 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003527};
3528
Steven Rostedt647bcd02011-05-03 14:39:21 -04003529static const struct file_operations ftrace_enabled_fops = {
3530 .open = ftrace_enabled_open,
3531 .read = seq_read,
3532 .llseek = seq_lseek,
3533 .release = seq_release_private,
3534};
3535
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003536static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003537 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003538 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003539 .write = ftrace_filter_write,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003540 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003541 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003542};
3543
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003544static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003545 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003546 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003547 .write = ftrace_notrace_write,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003548 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003549 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003550};
3551
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003552#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3553
3554static DEFINE_MUTEX(graph_lock);
3555
3556int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003557int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003558unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3559
3560static void *
Li Zefan85951842009-06-24 09:54:00 +08003561__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003562{
Li Zefan85951842009-06-24 09:54:00 +08003563 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003564 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003565 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003566}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003567
Li Zefan85951842009-06-24 09:54:00 +08003568static void *
3569g_next(struct seq_file *m, void *v, loff_t *pos)
3570{
3571 (*pos)++;
3572 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003573}
3574
3575static void *g_start(struct seq_file *m, loff_t *pos)
3576{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003577 mutex_lock(&graph_lock);
3578
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003579 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003580 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003581 return (void *)1;
3582
Li Zefan85951842009-06-24 09:54:00 +08003583 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003584}
3585
3586static void g_stop(struct seq_file *m, void *p)
3587{
3588 mutex_unlock(&graph_lock);
3589}
3590
3591static int g_show(struct seq_file *m, void *v)
3592{
3593 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003594
3595 if (!ptr)
3596 return 0;
3597
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003598 if (ptr == (unsigned long *)1) {
3599 seq_printf(m, "#### all functions enabled ####\n");
3600 return 0;
3601 }
3602
Steven Rostedtb375a112009-09-17 00:05:58 -04003603 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003604
3605 return 0;
3606}
3607
James Morris88e9d342009-09-22 16:43:43 -07003608static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003609 .start = g_start,
3610 .next = g_next,
3611 .stop = g_stop,
3612 .show = g_show,
3613};
3614
3615static int
3616ftrace_graph_open(struct inode *inode, struct file *file)
3617{
3618 int ret = 0;
3619
3620 if (unlikely(ftrace_disabled))
3621 return -ENODEV;
3622
3623 mutex_lock(&graph_lock);
3624 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003625 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003626 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003627 ftrace_graph_count = 0;
3628 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3629 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003630 mutex_unlock(&graph_lock);
3631
Li Zefana4ec5e02009-09-18 14:06:28 +08003632 if (file->f_mode & FMODE_READ)
3633 ret = seq_open(file, &ftrace_graph_seq_ops);
3634
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003635 return ret;
3636}
3637
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003638static int
Li Zefan87827112009-07-23 11:29:11 +08003639ftrace_graph_release(struct inode *inode, struct file *file)
3640{
3641 if (file->f_mode & FMODE_READ)
3642 seq_release(inode, file);
3643 return 0;
3644}
3645
3646static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003647ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003648{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003649 struct dyn_ftrace *rec;
3650 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003651 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003652 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003653 int type, not;
3654 char *search;
3655 bool exists;
3656 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003657
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003658 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003659 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003660 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3661 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003662
3663 search_len = strlen(search);
3664
Steven Rostedt52baf112009-02-14 01:15:39 -05003665 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003666
3667 if (unlikely(ftrace_disabled)) {
3668 mutex_unlock(&ftrace_lock);
3669 return -ENODEV;
3670 }
3671
Steven Rostedt265c8312009-02-13 12:43:56 -05003672 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003673
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003674 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003675 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003676 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003677 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003678 if (array[i] == rec->ip) {
3679 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003680 break;
3681 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003682 }
3683
3684 if (!not) {
3685 fail = 0;
3686 if (!exists) {
3687 array[(*idx)++] = rec->ip;
3688 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3689 goto out;
3690 }
3691 } else {
3692 if (exists) {
3693 array[i] = array[--(*idx)];
3694 array[*idx] = 0;
3695 fail = 0;
3696 }
3697 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003698 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003699 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003700out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003701 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003702
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003703 if (fail)
3704 return -EINVAL;
3705
Namhyung Kim761694a2013-04-11 16:01:38 +09003706 ftrace_graph_filter_enabled = !!(*idx);
3707
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003708 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003709}
3710
3711static ssize_t
3712ftrace_graph_write(struct file *file, const char __user *ubuf,
3713 size_t cnt, loff_t *ppos)
3714{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003715 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003716 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003717
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003718 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003719 return 0;
3720
3721 mutex_lock(&graph_lock);
3722
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003723 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3724 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003725 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003726 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003727
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003728 read = trace_get_user(&parser, ubuf, cnt, ppos);
3729
Li Zefan4ba79782009-09-22 13:52:20 +08003730 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003731 parser.buffer[parser.idx] = 0;
3732
3733 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003734 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003735 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003736 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003737 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003738 }
3739
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003740 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003741
3742out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003743 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003744out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003745 mutex_unlock(&graph_lock);
3746
3747 return ret;
3748}
3749
3750static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003751 .open = ftrace_graph_open,
3752 .read = seq_read,
3753 .write = ftrace_graph_write,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003754 .llseek = ftrace_filter_lseek,
Li Zefan87827112009-07-23 11:29:11 +08003755 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003756};
3757#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3758
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003759static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003760{
Steven Rostedt5072c592008-05-12 21:20:43 +02003761
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003762 trace_create_file("available_filter_functions", 0444,
3763 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003764
Steven Rostedt647bcd02011-05-03 14:39:21 -04003765 trace_create_file("enabled_functions", 0444,
3766 d_tracer, NULL, &ftrace_enabled_fops);
3767
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003768 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3769 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003770
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003771 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003772 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003773
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003774#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003775 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003776 NULL,
3777 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003778#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3779
Steven Rostedt5072c592008-05-12 21:20:43 +02003780 return 0;
3781}
3782
Steven Rostedt68950612011-12-16 17:06:45 -05003783static void ftrace_swap_recs(void *a, void *b, int size)
3784{
3785 struct dyn_ftrace *reca = a;
3786 struct dyn_ftrace *recb = b;
3787 struct dyn_ftrace t;
3788
3789 t = *reca;
3790 *reca = *recb;
3791 *recb = t;
3792}
3793
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003794static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003795 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003796 unsigned long *end)
3797{
Steven Rostedta7900872011-12-16 16:23:44 -05003798 struct ftrace_page *pg;
3799 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003800 unsigned long *p;
3801 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003802 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003803 int ret = -ENOMEM;
3804
3805 count = end - start;
3806
3807 if (!count)
3808 return 0;
3809
3810 pg = ftrace_allocate_pages(count);
3811 if (!pg)
3812 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003813
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003814 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003815
Steven Rostedt32082302011-12-16 14:42:37 -05003816 /*
3817 * Core and each module needs their own pages, as
3818 * modules will free them when they are removed.
3819 * Force a new page to be allocated for modules.
3820 */
Steven Rostedta7900872011-12-16 16:23:44 -05003821 if (!mod) {
3822 WARN_ON(ftrace_pages || ftrace_pages_start);
3823 /* First initialization */
3824 ftrace_pages = ftrace_pages_start = pg;
3825 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003826 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003827 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003828
Steven Rostedta7900872011-12-16 16:23:44 -05003829 if (WARN_ON(ftrace_pages->next)) {
3830 /* Hmm, we have free pages? */
3831 while (ftrace_pages->next)
3832 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003833 }
Steven Rostedta7900872011-12-16 16:23:44 -05003834
3835 ftrace_pages->next = pg;
3836 ftrace_pages = pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003837 }
3838
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003839 p = start;
3840 while (p < end) {
3841 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003842 /*
3843 * Some architecture linkers will pad between
3844 * the different mcount_loc sections of different
3845 * object files to satisfy alignments.
3846 * Skip any NULL pointers.
3847 */
3848 if (!addr)
3849 continue;
Steven Rostedta7900872011-12-16 16:23:44 -05003850 if (!ftrace_record_ip(addr))
3851 break;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003852 }
3853
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003854 /* These new locations need to be initialized */
3855 ftrace_new_pgs = pg;
3856
Steven Rostedt68950612011-12-16 17:06:45 -05003857 /* Make each individual set of pages sorted by ips */
3858 for (; pg; pg = pg->next)
3859 sort(pg->records, pg->index, sizeof(struct dyn_ftrace),
3860 ftrace_cmp_recs, ftrace_swap_recs);
3861
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003862 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003863 * We only need to disable interrupts on start up
3864 * because we are modifying code that an interrupt
3865 * may execute, and the modification is not atomic.
3866 * But for modules, nothing runs the code we modify
3867 * until we are finished with it, and there's no
3868 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003869 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003870 if (!mod)
3871 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003872 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003873 if (!mod)
3874 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003875 ret = 0;
3876 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003877 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003878
Steven Rostedta7900872011-12-16 16:23:44 -05003879 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003880}
3881
Steven Rostedt93eb6772009-04-15 13:24:06 -04003882#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003883
3884#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3885
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003886void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003887{
3888 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003889 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003890 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003891 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003892
Steven Rostedt93eb6772009-04-15 13:24:06 -04003893 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003894
3895 if (ftrace_disabled)
3896 goto out_unlock;
3897
Steven Rostedt32082302011-12-16 14:42:37 -05003898 /*
3899 * Each module has its own ftrace_pages, remove
3900 * them from the list.
3901 */
3902 last_pg = &ftrace_pages_start;
3903 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3904 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003905 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003906 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003907 * As core pages are first, the first
3908 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003909 */
Steven Rostedt32082302011-12-16 14:42:37 -05003910 if (WARN_ON(pg == ftrace_pages_start))
3911 goto out_unlock;
3912
3913 /* Check if we are deleting the last page */
3914 if (pg == ftrace_pages)
3915 ftrace_pages = next_to_ftrace_page(last_pg);
3916
3917 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003918 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3919 free_pages((unsigned long)pg->records, order);
3920 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05003921 } else
3922 last_pg = &pg->next;
3923 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003924 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003925 mutex_unlock(&ftrace_lock);
3926}
3927
3928static void ftrace_init_module(struct module *mod,
3929 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003930{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003931 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003932 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003933 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003934}
3935
Steven Rostedt (Red Hat)8f4c0e82014-04-24 10:40:12 -04003936void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003937{
Steven Rostedt (Red Hat)8f4c0e82014-04-24 10:40:12 -04003938 ftrace_init_module(mod, mod->ftrace_callsites,
3939 mod->ftrace_callsites +
3940 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003941}
3942
3943static int ftrace_module_notify_exit(struct notifier_block *self,
3944 unsigned long val, void *data)
3945{
3946 struct module *mod = data;
3947
3948 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003949 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003950
3951 return 0;
3952}
3953#else
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003954static int ftrace_module_notify_exit(struct notifier_block *self,
3955 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003956{
3957 return 0;
3958}
3959#endif /* CONFIG_MODULES */
3960
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003961struct notifier_block ftrace_module_exit_nb = {
3962 .notifier_call = ftrace_module_notify_exit,
3963 .priority = INT_MIN, /* Run after anything that can remove kprobes */
3964};
3965
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003966extern unsigned long __start_mcount_loc[];
3967extern unsigned long __stop_mcount_loc[];
3968
3969void __init ftrace_init(void)
3970{
3971 unsigned long count, addr, flags;
3972 int ret;
3973
3974 /* Keep the ftrace pointer to the stub */
3975 addr = (unsigned long)ftrace_stub;
3976
3977 local_irq_save(flags);
3978 ftrace_dyn_arch_init(&addr);
3979 local_irq_restore(flags);
3980
3981 /* ftrace_dyn_arch_init places the return code in addr */
3982 if (addr)
3983 goto failed;
3984
3985 count = __stop_mcount_loc - __start_mcount_loc;
3986
3987 ret = ftrace_dyn_table_alloc(count);
3988 if (ret)
3989 goto failed;
3990
3991 last_ftrace_enabled = ftrace_enabled = 1;
3992
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003993 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003994 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003995 __stop_mcount_loc);
3996
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003997 ret = register_module_notifier(&ftrace_module_exit_nb);
3998 if (ret)
3999 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004000
Steven Rostedt2af15d62009-05-28 13:37:24 -04004001 set_ftrace_early_filters();
4002
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004003 return;
4004 failed:
4005 ftrace_disabled = 1;
4006}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004007
Steven Rostedt3d083392008-05-12 21:20:42 +02004008#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004009
Steven Rostedt2b499382011-05-03 22:49:52 -04004010static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004011 .func = ftrace_stub,
4012};
4013
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004014static int __init ftrace_nodyn_init(void)
4015{
4016 ftrace_enabled = 1;
4017 return 0;
4018}
4019device_initcall(ftrace_nodyn_init);
4020
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004021static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4022static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004023/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004024# define ftrace_startup(ops, command) \
4025 ({ \
4026 int ___ret = __register_ftrace_function(ops); \
4027 if (!___ret) \
4028 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4029 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004030 })
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004031# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4032
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004033# define ftrace_startup_sysctl() do { } while (0)
4034# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004035
4036static inline int
4037ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4038{
4039 return 1;
4040}
4041
Steven Rostedt3d083392008-05-12 21:20:42 +02004042#endif /* CONFIG_DYNAMIC_FTRACE */
4043
Steven Rostedtb8489142011-05-04 09:27:52 -04004044static void
Jiri Olsae2484912012-02-15 15:51:48 +01004045ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip)
4046{
4047 struct ftrace_ops *op;
4048
4049 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4050 return;
4051
4052 /*
4053 * Some of the ops may be dynamically allocated,
4054 * they must be freed after a synchronize_sched().
4055 */
4056 preempt_disable_notrace();
4057 trace_recursion_set(TRACE_CONTROL_BIT);
4058 op = rcu_dereference_raw(ftrace_control_list);
4059 while (op != &ftrace_list_end) {
4060 if (!ftrace_function_local_disabled(op) &&
4061 ftrace_ops_test(op, ip))
4062 op->func(ip, parent_ip);
4063
4064 op = rcu_dereference_raw(op->next);
4065 };
4066 trace_recursion_clear(TRACE_CONTROL_BIT);
4067 preempt_enable_notrace();
4068}
4069
4070static struct ftrace_ops control_ops = {
4071 .func = ftrace_ops_control_func,
4072};
4073
4074static void
Steven Rostedtb8489142011-05-04 09:27:52 -04004075ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
4076{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004077 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04004078
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004079 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4080 return;
4081
4082 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004083 /*
4084 * Some of the ops may be dynamically allocated,
4085 * they must be freed after a synchronize_sched().
4086 */
4087 preempt_disable_notrace();
4088 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04004089 while (op != &ftrace_list_end) {
4090 if (ftrace_ops_test(op, ip))
4091 op->func(ip, parent_ip);
4092 op = rcu_dereference_raw(op->next);
4093 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004094 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004095 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04004096}
4097
Steven Rostedte32d8952008-12-04 00:26:41 -05004098static void clear_ftrace_swapper(void)
4099{
4100 struct task_struct *p;
4101 int cpu;
4102
4103 get_online_cpus();
4104 for_each_online_cpu(cpu) {
4105 p = idle_task(cpu);
4106 clear_tsk_trace_trace(p);
4107 }
4108 put_online_cpus();
4109}
4110
4111static void set_ftrace_swapper(void)
4112{
4113 struct task_struct *p;
4114 int cpu;
4115
4116 get_online_cpus();
4117 for_each_online_cpu(cpu) {
4118 p = idle_task(cpu);
4119 set_tsk_trace_trace(p);
4120 }
4121 put_online_cpus();
4122}
4123
4124static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004125{
4126 struct task_struct *p;
4127
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004128 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004129 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004130 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004131 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004132 rcu_read_unlock();
4133
Steven Rostedte32d8952008-12-04 00:26:41 -05004134 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004135}
4136
Steven Rostedte32d8952008-12-04 00:26:41 -05004137static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004138{
4139 struct task_struct *p;
4140
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004141 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004142 do_each_pid_task(pid, PIDTYPE_PID, p) {
4143 set_tsk_trace_trace(p);
4144 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004145 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004146}
4147
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004148static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004149{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004150 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004151 clear_ftrace_swapper();
4152 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004153 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004154}
4155
4156static void set_ftrace_pid_task(struct pid *pid)
4157{
4158 if (pid == ftrace_swapper_pid)
4159 set_ftrace_swapper();
4160 else
4161 set_ftrace_pid(pid);
4162}
4163
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004164static int ftrace_pid_add(int p)
4165{
4166 struct pid *pid;
4167 struct ftrace_pid *fpid;
4168 int ret = -EINVAL;
4169
4170 mutex_lock(&ftrace_lock);
4171
4172 if (!p)
4173 pid = ftrace_swapper_pid;
4174 else
4175 pid = find_get_pid(p);
4176
4177 if (!pid)
4178 goto out;
4179
4180 ret = 0;
4181
4182 list_for_each_entry(fpid, &ftrace_pids, list)
4183 if (fpid->pid == pid)
4184 goto out_put;
4185
4186 ret = -ENOMEM;
4187
4188 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4189 if (!fpid)
4190 goto out_put;
4191
4192 list_add(&fpid->list, &ftrace_pids);
4193 fpid->pid = pid;
4194
4195 set_ftrace_pid_task(pid);
4196
4197 ftrace_update_pid_func();
4198 ftrace_startup_enable(0);
4199
4200 mutex_unlock(&ftrace_lock);
4201 return 0;
4202
4203out_put:
4204 if (pid != ftrace_swapper_pid)
4205 put_pid(pid);
4206
4207out:
4208 mutex_unlock(&ftrace_lock);
4209 return ret;
4210}
4211
4212static void ftrace_pid_reset(void)
4213{
4214 struct ftrace_pid *fpid, *safe;
4215
4216 mutex_lock(&ftrace_lock);
4217 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4218 struct pid *pid = fpid->pid;
4219
4220 clear_ftrace_pid_task(pid);
4221
4222 list_del(&fpid->list);
4223 kfree(fpid);
4224 }
4225
4226 ftrace_update_pid_func();
4227 ftrace_startup_enable(0);
4228
4229 mutex_unlock(&ftrace_lock);
4230}
4231
4232static void *fpid_start(struct seq_file *m, loff_t *pos)
4233{
4234 mutex_lock(&ftrace_lock);
4235
4236 if (list_empty(&ftrace_pids) && (!*pos))
4237 return (void *) 1;
4238
4239 return seq_list_start(&ftrace_pids, *pos);
4240}
4241
4242static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4243{
4244 if (v == (void *)1)
4245 return NULL;
4246
4247 return seq_list_next(v, &ftrace_pids, pos);
4248}
4249
4250static void fpid_stop(struct seq_file *m, void *p)
4251{
4252 mutex_unlock(&ftrace_lock);
4253}
4254
4255static int fpid_show(struct seq_file *m, void *v)
4256{
4257 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4258
4259 if (v == (void *)1) {
4260 seq_printf(m, "no pid\n");
4261 return 0;
4262 }
4263
4264 if (fpid->pid == ftrace_swapper_pid)
4265 seq_printf(m, "swapper tasks\n");
4266 else
4267 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4268
4269 return 0;
4270}
4271
4272static const struct seq_operations ftrace_pid_sops = {
4273 .start = fpid_start,
4274 .next = fpid_next,
4275 .stop = fpid_stop,
4276 .show = fpid_show,
4277};
4278
4279static int
4280ftrace_pid_open(struct inode *inode, struct file *file)
4281{
4282 int ret = 0;
4283
4284 if ((file->f_mode & FMODE_WRITE) &&
4285 (file->f_flags & O_TRUNC))
4286 ftrace_pid_reset();
4287
4288 if (file->f_mode & FMODE_READ)
4289 ret = seq_open(file, &ftrace_pid_sops);
4290
4291 return ret;
4292}
4293
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004294static ssize_t
4295ftrace_pid_write(struct file *filp, const char __user *ubuf,
4296 size_t cnt, loff_t *ppos)
4297{
Ingo Molnar457dc922009-11-23 11:03:28 +01004298 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004299 long val;
4300 int ret;
4301
4302 if (cnt >= sizeof(buf))
4303 return -EINVAL;
4304
4305 if (copy_from_user(&buf, ubuf, cnt))
4306 return -EFAULT;
4307
4308 buf[cnt] = 0;
4309
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004310 /*
4311 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4312 * to clean the filter quietly.
4313 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004314 tmp = strstrip(buf);
4315 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004316 return 1;
4317
Ingo Molnar457dc922009-11-23 11:03:28 +01004318 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004319 if (ret < 0)
4320 return ret;
4321
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004322 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004323
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004324 return ret ? ret : cnt;
4325}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004326
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004327static int
4328ftrace_pid_release(struct inode *inode, struct file *file)
4329{
4330 if (file->f_mode & FMODE_READ)
4331 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004332
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004333 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004334}
4335
Steven Rostedt5e2336a02009-03-05 21:44:55 -05004336static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004337 .open = ftrace_pid_open,
4338 .write = ftrace_pid_write,
4339 .read = seq_read,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08004340 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004341 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004342};
4343
4344static __init int ftrace_init_debugfs(void)
4345{
4346 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004347
4348 d_tracer = tracing_init_dentry();
4349 if (!d_tracer)
4350 return 0;
4351
4352 ftrace_init_dyn_debugfs(d_tracer);
4353
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004354 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4355 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004356
4357 ftrace_profile_debugfs(d_tracer);
4358
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004359 return 0;
4360}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004361fs_initcall(ftrace_init_debugfs);
4362
Steven Rostedt3d083392008-05-12 21:20:42 +02004363/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004364 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004365 *
4366 * This function should be used by panic code. It stops ftrace
4367 * but in a not so nice way. If you need to simply kill ftrace
4368 * from a non-atomic section, use ftrace_kill.
4369 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004370void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004371{
4372 ftrace_disabled = 1;
4373 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004374 clear_ftrace_function();
4375}
4376
4377/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004378 * Test if ftrace is dead or not.
4379 */
4380int ftrace_is_dead(void)
4381{
4382 return ftrace_disabled;
4383}
4384
4385/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004386 * register_ftrace_function - register a function for profiling
4387 * @ops - ops structure that holds the function for profiling.
4388 *
4389 * Register a function to be called by all functions in the
4390 * kernel.
4391 *
4392 * Note: @ops->func and all the functions it calls must be labeled
4393 * with "notrace", otherwise it will go into a
4394 * recursive loop.
4395 */
4396int register_ftrace_function(struct ftrace_ops *ops)
4397{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004398 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004399
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004400 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004401
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004402 ret = ftrace_startup(ops, 0);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004403
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004404 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004405 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004406}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004407EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004408
4409/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004410 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004411 * @ops - ops structure that holds the function to unregister
4412 *
4413 * Unregister a function that was added to be called by ftrace profiling.
4414 */
4415int unregister_ftrace_function(struct ftrace_ops *ops)
4416{
4417 int ret;
4418
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004419 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004420 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004421 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004422
4423 return ret;
4424}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004425EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004426
Ingo Molnare309b412008-05-12 21:20:51 +02004427int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004428ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004429 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004430 loff_t *ppos)
4431{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004432 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004433
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004434 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004435
Steven Rostedt45a4a232011-04-21 23:16:46 -04004436 if (unlikely(ftrace_disabled))
4437 goto out;
4438
4439 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004440
Li Zefana32c7762009-06-26 16:55:51 +08004441 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004442 goto out;
4443
Li Zefana32c7762009-06-26 16:55:51 +08004444 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004445
4446 if (ftrace_enabled) {
4447
4448 ftrace_startup_sysctl();
4449
4450 /* we are starting ftrace again */
Jan Kiszkab81d3242013-03-26 17:53:03 +01004451 if (ftrace_ops_list != &ftrace_list_end)
4452 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004453
4454 } else {
4455 /* stopping ftrace calls (just send to ftrace_stub) */
4456 ftrace_trace_function = ftrace_stub;
4457
4458 ftrace_shutdown_sysctl();
4459 }
4460
4461 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004462 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004463 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004464}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004465
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004466#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004467
Steven Rostedt597af812009-04-03 15:24:12 -04004468static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004469static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004470
Steven Rostedte49dc192008-12-02 23:50:05 -05004471int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4472{
4473 return 0;
4474}
4475
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004476/* The callbacks that hook a function */
4477trace_func_graph_ret_t ftrace_graph_return =
4478 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004479trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt1c2bd0d2014-02-11 14:50:01 -05004480static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004481
4482/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4483static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4484{
4485 int i;
4486 int ret = 0;
4487 unsigned long flags;
4488 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4489 struct task_struct *g, *t;
4490
4491 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4492 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4493 * sizeof(struct ftrace_ret_stack),
4494 GFP_KERNEL);
4495 if (!ret_stack_list[i]) {
4496 start = 0;
4497 end = i;
4498 ret = -ENOMEM;
4499 goto free;
4500 }
4501 }
4502
4503 read_lock_irqsave(&tasklist_lock, flags);
4504 do_each_thread(g, t) {
4505 if (start == end) {
4506 ret = -EAGAIN;
4507 goto unlock;
4508 }
4509
4510 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004511 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004512 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004513 t->curr_ret_stack = -1;
4514 /* Make sure the tasks see the -1 first: */
4515 smp_wmb();
4516 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004517 }
4518 } while_each_thread(g, t);
4519
4520unlock:
4521 read_unlock_irqrestore(&tasklist_lock, flags);
4522free:
4523 for (i = start; i < end; i++)
4524 kfree(ret_stack_list[i]);
4525 return ret;
4526}
4527
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004528static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004529ftrace_graph_probe_sched_switch(void *ignore,
4530 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004531{
4532 unsigned long long timestamp;
4533 int index;
4534
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004535 /*
4536 * Does the user want to count the time a function was asleep.
4537 * If so, do not update the time stamps.
4538 */
4539 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4540 return;
4541
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004542 timestamp = trace_clock_local();
4543
4544 prev->ftrace_timestamp = timestamp;
4545
4546 /* only process tasks that we timestamped */
4547 if (!next->ftrace_timestamp)
4548 return;
4549
4550 /*
4551 * Update all the counters in next to make up for the
4552 * time next was sleeping.
4553 */
4554 timestamp -= next->ftrace_timestamp;
4555
4556 for (index = next->curr_ret_stack; index >= 0; index--)
4557 next->ret_stack[index].calltime += timestamp;
4558}
4559
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004560/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004561static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004562{
4563 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004564 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004565
4566 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4567 sizeof(struct ftrace_ret_stack *),
4568 GFP_KERNEL);
4569
4570 if (!ret_stack_list)
4571 return -ENOMEM;
4572
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004573 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004574 for_each_online_cpu(cpu) {
4575 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004576 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004577 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004578
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004579 do {
4580 ret = alloc_retstack_tasklist(ret_stack_list);
4581 } while (ret == -EAGAIN);
4582
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004583 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004584 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004585 if (ret)
4586 pr_info("ftrace_graph: Couldn't activate tracepoint"
4587 " probe to kernel_sched_switch\n");
4588 }
4589
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004590 kfree(ret_stack_list);
4591 return ret;
4592}
4593
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004594/*
4595 * Hibernation protection.
4596 * The state of the current task is too much unstable during
4597 * suspend/restore to disk. We want to protect against that.
4598 */
4599static int
4600ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4601 void *unused)
4602{
4603 switch (state) {
4604 case PM_HIBERNATION_PREPARE:
4605 pause_graph_tracing();
4606 break;
4607
4608 case PM_POST_HIBERNATION:
4609 unpause_graph_tracing();
4610 break;
4611 }
4612 return NOTIFY_DONE;
4613}
4614
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004615/* Just a place holder for function graph */
4616static struct ftrace_ops fgraph_ops __read_mostly = {
4617 .func = ftrace_stub,
4618 .flags = FTRACE_OPS_FL_GLOBAL,
4619};
4620
Steven Rostedt1c2bd0d2014-02-11 14:50:01 -05004621static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
4622{
4623 if (!ftrace_ops_test(&global_ops, trace->func))
4624 return 0;
4625 return __ftrace_graph_entry(trace);
4626}
4627
4628/*
4629 * The function graph tracer should only trace the functions defined
4630 * by set_ftrace_filter and set_ftrace_notrace. If another function
4631 * tracer ops is registered, the graph tracer requires testing the
4632 * function against the global ops, and not just trace any function
4633 * that any ftrace_ops registered.
4634 */
4635static void update_function_graph_func(void)
4636{
4637 if (ftrace_ops_list == &ftrace_list_end ||
4638 (ftrace_ops_list == &global_ops &&
4639 global_ops.next == &ftrace_list_end))
4640 ftrace_graph_entry = __ftrace_graph_entry;
4641 else
4642 ftrace_graph_entry = ftrace_graph_entry_test;
4643}
4644
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004645int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4646 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004647{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004648 int ret = 0;
4649
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004650 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004651
Steven Rostedt05ce5812009-03-24 00:18:31 -04004652 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004653 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004654 ret = -EBUSY;
4655 goto out;
4656 }
4657
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004658 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4659 register_pm_notifier(&ftrace_suspend_notifier);
4660
Steven Rostedt597af812009-04-03 15:24:12 -04004661 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004662 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004663 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004664 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004665 goto out;
4666 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004667
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004668 ftrace_graph_return = retfunc;
Steven Rostedt1c2bd0d2014-02-11 14:50:01 -05004669
4670 /*
4671 * Update the indirect function to the entryfunc, and the
4672 * function that gets called to the entry_test first. Then
4673 * call the update fgraph entry function to determine if
4674 * the entryfunc should be called directly or not.
4675 */
4676 __ftrace_graph_entry = entryfunc;
4677 ftrace_graph_entry = ftrace_graph_entry_test;
4678 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05004679
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004680 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004681
4682out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004683 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004684 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004685}
4686
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004687void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004688{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004689 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004690
Steven Rostedt597af812009-04-03 15:24:12 -04004691 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004692 goto out;
4693
Steven Rostedt597af812009-04-03 15:24:12 -04004694 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004695 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004696 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt1c2bd0d2014-02-11 14:50:01 -05004697 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004698 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004699 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004700 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004701
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004702 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004703 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004704}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004705
Steven Rostedt868baf02011-02-10 21:26:13 -05004706static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4707
4708static void
4709graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4710{
4711 atomic_set(&t->tracing_graph_pause, 0);
4712 atomic_set(&t->trace_overrun, 0);
4713 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004714 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004715 smp_wmb();
4716 t->ret_stack = ret_stack;
4717}
4718
4719/*
4720 * Allocate a return stack for the idle task. May be the first
4721 * time through, or it may be done by CPU hotplug online.
4722 */
4723void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4724{
4725 t->curr_ret_stack = -1;
4726 /*
4727 * The idle task has no parent, it either has its own
4728 * stack or no stack at all.
4729 */
4730 if (t->ret_stack)
4731 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4732
4733 if (ftrace_graph_active) {
4734 struct ftrace_ret_stack *ret_stack;
4735
4736 ret_stack = per_cpu(idle_ret_stack, cpu);
4737 if (!ret_stack) {
4738 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4739 * sizeof(struct ftrace_ret_stack),
4740 GFP_KERNEL);
4741 if (!ret_stack)
4742 return;
4743 per_cpu(idle_ret_stack, cpu) = ret_stack;
4744 }
4745 graph_init_task(t, ret_stack);
4746 }
4747}
4748
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004749/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004750void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004751{
Steven Rostedt84047e32009-06-02 16:51:55 -04004752 /* Make sure we do not use the parent ret_stack */
4753 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004754 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004755
Steven Rostedt597af812009-04-03 15:24:12 -04004756 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004757 struct ftrace_ret_stack *ret_stack;
4758
4759 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004760 * sizeof(struct ftrace_ret_stack),
4761 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004762 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004763 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004764 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004765 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004766}
4767
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004768void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004769{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004770 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4771
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004772 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004773 /* NULL must become visible to IRQs before we free it: */
4774 barrier();
4775
4776 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004777}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004778
4779void ftrace_graph_stop(void)
4780{
4781 ftrace_stop();
4782}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004783#endif