blob: 64b6143a33bab4fec7ecb20b6c2a58e6ff2523d0 [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 Rostedtf7bc8b62011-07-14 23:02:27 -04002083static int ops_traces_mod(struct ftrace_ops *ops)
2084{
2085 struct ftrace_hash *hash;
2086
2087 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002088 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002089}
2090
Steven Rostedt31e88902008-11-14 16:21:19 -08002091static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002092{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002093 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002094 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302095 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002096 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002097 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002098
2099 /*
2100 * When adding a module, we need to check if tracers are
2101 * currently enabled and if they are set to trace all functions.
2102 * If they are, we need to enable the module functions as well
2103 * as update the reference counts for those function records.
2104 */
2105 if (mod) {
2106 struct ftrace_ops *ops;
2107
2108 for (ops = ftrace_ops_list;
2109 ops != &ftrace_list_end; ops = ops->next) {
2110 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2111 ops_traces_mod(ops))
2112 ref++;
2113 }
2114 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002115
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002116 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002117 ftrace_update_cnt = 0;
2118
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002119 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302120
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002121 for (i = 0; i < pg->index; i++) {
2122 /* If something went wrong, bail without enabling anything */
2123 if (unlikely(ftrace_disabled))
2124 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002125
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002126 p = &pg->records[i];
2127 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302128
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002129 /*
2130 * Do the initial record conversion from mcount jump
2131 * to the NOP instructions.
2132 */
2133 if (!ftrace_code_disable(mod, p))
2134 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002135
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002136 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002137
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002138 /*
2139 * If the tracing is enabled, go ahead and enable the record.
2140 *
2141 * The reason not to enable the record immediatelly is the
2142 * inherent check of ftrace_make_nop/ftrace_make_call for
2143 * correct previous instructions. Making first the NOP
2144 * conversion puts the module to the correct state, thus
2145 * passing the ftrace_make_call check.
2146 */
2147 if (ftrace_start_up && ref) {
2148 int failed = __ftrace_replace_code(p, 1);
2149 if (failed)
2150 ftrace_bug(failed, p->ip);
2151 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002152 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002153 }
2154
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002155 ftrace_new_pgs = NULL;
2156
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002157 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002158 ftrace_update_time = stop - start;
2159 ftrace_update_tot_cnt += ftrace_update_cnt;
2160
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002161 return 0;
2162}
2163
Steven Rostedta7900872011-12-16 16:23:44 -05002164static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002165{
Steven Rostedta7900872011-12-16 16:23:44 -05002166 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002167 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002168
Steven Rostedta7900872011-12-16 16:23:44 -05002169 if (WARN_ON(!count))
2170 return -EINVAL;
2171
2172 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002173
2174 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002175 * We want to fill as much as possible. No more than a page
2176 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002177 */
Steven Rostedta7900872011-12-16 16:23:44 -05002178 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2179 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002180
Steven Rostedta7900872011-12-16 16:23:44 -05002181 again:
2182 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2183
2184 if (!pg->records) {
2185 /* if we can't allocate this size, try something smaller */
2186 if (!order)
2187 return -ENOMEM;
2188 order >>= 1;
2189 goto again;
2190 }
2191
2192 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2193 pg->size = cnt;
2194
2195 if (cnt > count)
2196 cnt = count;
2197
2198 return cnt;
2199}
2200
2201static struct ftrace_page *
2202ftrace_allocate_pages(unsigned long num_to_init)
2203{
2204 struct ftrace_page *start_pg;
2205 struct ftrace_page *pg;
2206 int order;
2207 int cnt;
2208
2209 if (!num_to_init)
2210 return 0;
2211
2212 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2213 if (!pg)
2214 return NULL;
2215
2216 /*
2217 * Try to allocate as much as possible in one continues
2218 * location that fills in all of the space. We want to
2219 * waste as little space as possible.
2220 */
2221 for (;;) {
2222 cnt = ftrace_allocate_records(pg, num_to_init);
2223 if (cnt < 0)
2224 goto free_pages;
2225
2226 num_to_init -= cnt;
2227 if (!num_to_init)
2228 break;
2229
2230 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2231 if (!pg->next)
2232 goto free_pages;
2233
2234 pg = pg->next;
2235 }
2236
2237 return start_pg;
2238
2239 free_pages:
2240 while (start_pg) {
2241 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2242 free_pages((unsigned long)pg->records, order);
2243 start_pg = pg->next;
2244 kfree(pg);
2245 pg = start_pg;
2246 }
2247 pr_info("ftrace: FAILED to allocate memory for functions\n");
2248 return NULL;
2249}
2250
2251static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2252{
2253 int cnt;
2254
2255 if (!num_to_init) {
2256 pr_info("ftrace: No functions to be traced?\n");
2257 return -1;
2258 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002259
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002260 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002261 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002262 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002263
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002264 return 0;
2265}
2266
Steven Rostedt5072c592008-05-12 21:20:43 +02002267#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2268
2269struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002270 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002271 loff_t func_pos;
2272 struct ftrace_page *pg;
2273 struct dyn_ftrace *func;
2274 struct ftrace_func_probe *probe;
2275 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002276 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002277 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002278 int hidx;
2279 int idx;
2280 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002281};
2282
Ingo Molnare309b412008-05-12 21:20:51 +02002283static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002284t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002285{
2286 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002287 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002288 struct hlist_head *hhd;
2289
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002290 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002291 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002292
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002293 if (iter->probe)
2294 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002295 retry:
2296 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2297 return NULL;
2298
2299 hhd = &ftrace_func_hash[iter->hidx];
2300
2301 if (hlist_empty(hhd)) {
2302 iter->hidx++;
2303 hnd = NULL;
2304 goto retry;
2305 }
2306
2307 if (!hnd)
2308 hnd = hhd->first;
2309 else {
2310 hnd = hnd->next;
2311 if (!hnd) {
2312 iter->hidx++;
2313 goto retry;
2314 }
2315 }
2316
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002317 if (WARN_ON_ONCE(!hnd))
2318 return NULL;
2319
2320 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2321
2322 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002323}
2324
2325static void *t_hash_start(struct seq_file *m, loff_t *pos)
2326{
2327 struct ftrace_iterator *iter = m->private;
2328 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002329 loff_t l;
2330
Steven Rostedt69a30832011-12-19 15:21:16 -05002331 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2332 return NULL;
2333
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002334 if (iter->func_pos > *pos)
2335 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002336
Li Zefand82d6242009-06-24 09:54:54 +08002337 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002338 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002339 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002340 if (!p)
2341 break;
2342 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002343 if (!p)
2344 return NULL;
2345
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002346 /* Only set this if we have an item */
2347 iter->flags |= FTRACE_ITER_HASH;
2348
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002349 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002350}
2351
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002352static int
2353t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002354{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002355 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002356
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002357 rec = iter->probe;
2358 if (WARN_ON_ONCE(!rec))
2359 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002360
Steven Rostedt809dcf22009-02-16 23:06:01 -05002361 if (rec->ops->print)
2362 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2363
Steven Rostedtb375a112009-09-17 00:05:58 -04002364 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002365
2366 if (rec->data)
2367 seq_printf(m, ":%p", rec->data);
2368 seq_putc(m, '\n');
2369
2370 return 0;
2371}
2372
2373static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002374t_next(struct seq_file *m, void *v, loff_t *pos)
2375{
2376 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002377 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002378 struct dyn_ftrace *rec = NULL;
2379
Steven Rostedt45a4a232011-04-21 23:16:46 -04002380 if (unlikely(ftrace_disabled))
2381 return NULL;
2382
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002383 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002384 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002385
Steven Rostedt5072c592008-05-12 21:20:43 +02002386 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002387 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002388
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002389 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002390 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002391
Steven Rostedt5072c592008-05-12 21:20:43 +02002392 retry:
2393 if (iter->idx >= iter->pg->index) {
2394 if (iter->pg->next) {
2395 iter->pg = iter->pg->next;
2396 iter->idx = 0;
2397 goto retry;
2398 }
2399 } else {
2400 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002401 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002402 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002403
Steven Rostedt41c52c02008-05-22 11:46:33 -04002404 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002405 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2406
2407 ((iter->flags & FTRACE_ITER_ENABLED) &&
2408 !(rec->flags & ~FTRACE_FL_MASK))) {
2409
Steven Rostedt5072c592008-05-12 21:20:43 +02002410 rec = NULL;
2411 goto retry;
2412 }
2413 }
2414
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002415 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002416 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002417
2418 iter->func = rec;
2419
2420 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002421}
2422
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002423static void reset_iter_read(struct ftrace_iterator *iter)
2424{
2425 iter->pos = 0;
2426 iter->func_pos = 0;
Dan Carpenter455d8952012-06-09 19:10:27 +03002427 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002428}
2429
2430static void *t_start(struct seq_file *m, loff_t *pos)
2431{
2432 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002433 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002434 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002435 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002436
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002437 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002438
2439 if (unlikely(ftrace_disabled))
2440 return NULL;
2441
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002442 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002443 * If an lseek was done, then reset and start from beginning.
2444 */
2445 if (*pos < iter->pos)
2446 reset_iter_read(iter);
2447
2448 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002449 * For set_ftrace_filter reading, if we have the filter
2450 * off, we can short cut and just print out that all
2451 * functions are enabled.
2452 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002453 if (iter->flags & FTRACE_ITER_FILTER &&
2454 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002455 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002456 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002457 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002458 /* reset in case of seek/pread */
2459 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002460 return iter;
2461 }
2462
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002463 if (iter->flags & FTRACE_ITER_HASH)
2464 return t_hash_start(m, pos);
2465
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002466 /*
2467 * Unfortunately, we need to restart at ftrace_pages_start
2468 * every time we let go of the ftrace_mutex. This is because
2469 * those pointers can change without the lock.
2470 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002471 iter->pg = ftrace_pages_start;
2472 iter->idx = 0;
2473 for (l = 0; l <= *pos; ) {
2474 p = t_next(m, p, &l);
2475 if (!p)
2476 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002477 }
walimis5821e1b2008-11-15 15:19:06 +08002478
Steven Rostedt69a30832011-12-19 15:21:16 -05002479 if (!p)
2480 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002481
2482 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002483}
2484
2485static void t_stop(struct seq_file *m, void *p)
2486{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002487 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002488}
2489
2490static int t_show(struct seq_file *m, void *v)
2491{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002492 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002493 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002494
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002495 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002496 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002497
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002498 if (iter->flags & FTRACE_ITER_PRINTALL) {
2499 seq_printf(m, "#### all functions enabled ####\n");
2500 return 0;
2501 }
2502
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002503 rec = iter->func;
2504
Steven Rostedt5072c592008-05-12 21:20:43 +02002505 if (!rec)
2506 return 0;
2507
Steven Rostedt647bcd02011-05-03 14:39:21 -04002508 seq_printf(m, "%ps", (void *)rec->ip);
2509 if (iter->flags & FTRACE_ITER_ENABLED)
2510 seq_printf(m, " (%ld)",
2511 rec->flags & ~FTRACE_FL_MASK);
2512 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002513
2514 return 0;
2515}
2516
James Morris88e9d342009-09-22 16:43:43 -07002517static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002518 .start = t_start,
2519 .next = t_next,
2520 .stop = t_stop,
2521 .show = t_show,
2522};
2523
Ingo Molnare309b412008-05-12 21:20:51 +02002524static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002525ftrace_avail_open(struct inode *inode, struct file *file)
2526{
2527 struct ftrace_iterator *iter;
2528 int ret;
2529
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002530 if (unlikely(ftrace_disabled))
2531 return -ENODEV;
2532
Steven Rostedt5072c592008-05-12 21:20:43 +02002533 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2534 if (!iter)
2535 return -ENOMEM;
2536
2537 iter->pg = ftrace_pages_start;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002538 iter->ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002539
2540 ret = seq_open(file, &show_ftrace_seq_ops);
2541 if (!ret) {
2542 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002543
Steven Rostedt5072c592008-05-12 21:20:43 +02002544 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002545 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002546 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002547 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002548
2549 return ret;
2550}
2551
Steven Rostedt647bcd02011-05-03 14:39:21 -04002552static int
2553ftrace_enabled_open(struct inode *inode, struct file *file)
2554{
2555 struct ftrace_iterator *iter;
2556 int ret;
2557
2558 if (unlikely(ftrace_disabled))
2559 return -ENODEV;
2560
2561 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2562 if (!iter)
2563 return -ENOMEM;
2564
2565 iter->pg = ftrace_pages_start;
2566 iter->flags = FTRACE_ITER_ENABLED;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002567 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002568
2569 ret = seq_open(file, &show_ftrace_seq_ops);
2570 if (!ret) {
2571 struct seq_file *m = file->private_data;
2572
2573 m->private = iter;
2574 } else {
2575 kfree(iter);
2576 }
2577
2578 return ret;
2579}
2580
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002581static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002582{
Steven Rostedt52baf112009-02-14 01:15:39 -05002583 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002584 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002585 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002586}
2587
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002588/**
2589 * ftrace_regex_open - initialize function tracer filter files
2590 * @ops: The ftrace_ops that hold the hash filters
2591 * @flag: The type of filter to process
2592 * @inode: The inode, usually passed in to your open routine
2593 * @file: The file, usually passed in to your open routine
2594 *
2595 * ftrace_regex_open() initializes the filter files for the
2596 * @ops. Depending on @flag it may process the filter hash or
2597 * the notrace hash of @ops. With this called from the open
2598 * routine, you can use ftrace_filter_write() for the write
2599 * routine if @flag has FTRACE_ITER_FILTER set, or
2600 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedtbc4d36c2013-06-07 17:02:08 +08002601 * ftrace_filter_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002602 * release must call ftrace_regex_release().
2603 */
2604int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002605ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002606 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002607{
2608 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002609 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002610 int ret = 0;
2611
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002612 if (unlikely(ftrace_disabled))
2613 return -ENODEV;
2614
Steven Rostedt5072c592008-05-12 21:20:43 +02002615 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2616 if (!iter)
2617 return -ENOMEM;
2618
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002619 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2620 kfree(iter);
2621 return -ENOMEM;
2622 }
2623
Steven Rostedtf45948e2011-05-02 12:29:25 -04002624 if (flag & FTRACE_ITER_NOTRACE)
2625 hash = ops->notrace_hash;
2626 else
2627 hash = ops->filter_hash;
2628
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002629 iter->ops = ops;
2630 iter->flags = flag;
2631
2632 if (file->f_mode & FMODE_WRITE) {
2633 mutex_lock(&ftrace_lock);
2634 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2635 mutex_unlock(&ftrace_lock);
2636
2637 if (!iter->hash) {
2638 trace_parser_put(&iter->parser);
2639 kfree(iter);
2640 return -ENOMEM;
2641 }
2642 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002643
Steven Rostedt41c52c02008-05-22 11:46:33 -04002644 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002645
Steven Rostedt5072c592008-05-12 21:20:43 +02002646 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002647 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002648 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002649
2650 if (file->f_mode & FMODE_READ) {
2651 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002652
2653 ret = seq_open(file, &show_ftrace_seq_ops);
2654 if (!ret) {
2655 struct seq_file *m = file->private_data;
2656 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002657 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002658 /* Failed */
2659 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002660 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002661 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002662 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002663 } else
2664 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002665 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002666
2667 return ret;
2668}
2669
Steven Rostedt41c52c02008-05-22 11:46:33 -04002670static int
2671ftrace_filter_open(struct inode *inode, struct file *file)
2672{
Steven Rostedt69a30832011-12-19 15:21:16 -05002673 return ftrace_regex_open(&global_ops,
2674 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2675 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002676}
2677
2678static int
2679ftrace_notrace_open(struct inode *inode, struct file *file)
2680{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002681 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002682 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002683}
2684
Steven Rostedt64e7c442009-02-13 17:08:48 -05002685static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002686{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002687 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002688 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002689
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002690 switch (type) {
2691 case MATCH_FULL:
2692 if (strcmp(str, regex) == 0)
2693 matched = 1;
2694 break;
2695 case MATCH_FRONT_ONLY:
2696 if (strncmp(str, regex, len) == 0)
2697 matched = 1;
2698 break;
2699 case MATCH_MIDDLE_ONLY:
2700 if (strstr(str, regex))
2701 matched = 1;
2702 break;
2703 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002704 slen = strlen(str);
2705 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002706 matched = 1;
2707 break;
2708 }
2709
2710 return matched;
2711}
2712
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002713static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002714enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002715{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002716 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002717 int ret = 0;
2718
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002719 entry = ftrace_lookup_ip(hash, rec->ip);
2720 if (not) {
2721 /* Do nothing if it doesn't exist */
2722 if (!entry)
2723 return 0;
2724
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002725 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002726 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002727 /* Do nothing if it exists */
2728 if (entry)
2729 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002730
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002731 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002732 }
2733 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002734}
2735
Steven Rostedt64e7c442009-02-13 17:08:48 -05002736static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002737ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2738 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002739{
2740 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002741 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002742
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002743 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2744
2745 if (mod) {
2746 /* module lookup requires matching the module */
2747 if (!modname || strcmp(modname, mod))
2748 return 0;
2749
2750 /* blank search means to match all funcs in the mod */
2751 if (!len)
2752 return 1;
2753 }
2754
Steven Rostedt64e7c442009-02-13 17:08:48 -05002755 return ftrace_match(str, regex, len, type);
2756}
2757
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002758static int
2759match_records(struct ftrace_hash *hash, char *buff,
2760 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002761{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002762 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002763 struct ftrace_page *pg;
2764 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002765 int type = MATCH_FULL;
2766 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002767 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002768 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002769
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002770 if (len) {
2771 type = filter_parse_regex(buff, len, &search, &not);
2772 search_len = strlen(search);
2773 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002774
Steven Rostedt52baf112009-02-14 01:15:39 -05002775 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002776
2777 if (unlikely(ftrace_disabled))
2778 goto out_unlock;
2779
Steven Rostedt265c8312009-02-13 12:43:56 -05002780 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002781 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002782 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002783 if (ret < 0) {
2784 found = ret;
2785 goto out_unlock;
2786 }
Li Zefan311d16d2009-12-08 11:15:11 +08002787 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002788 }
2789 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002790 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002791 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002792
2793 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002794}
2795
Steven Rostedt64e7c442009-02-13 17:08:48 -05002796static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002797ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002798{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002799 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002800}
2801
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002802static int
2803ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002804{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002805 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002806
Steven Rostedt64e7c442009-02-13 17:08:48 -05002807 /* blank or '*' mean the same */
2808 if (strcmp(buff, "*") == 0)
2809 buff[0] = 0;
2810
2811 /* handle the case of 'dont filter this module' */
2812 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2813 buff[0] = 0;
2814 not = 1;
2815 }
2816
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002817 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002818}
2819
Steven Rostedtf6180772009-02-14 00:40:25 -05002820/*
2821 * We register the module command as a template to show others how
2822 * to register the a command as well.
2823 */
2824
2825static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002826ftrace_mod_callback(struct ftrace_hash *hash,
2827 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002828{
2829 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002830 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002831
2832 /*
2833 * cmd == 'mod' because we only registered this func
2834 * for the 'mod' ftrace_func_command.
2835 * But if you register one func with multiple commands,
2836 * you can tell which command was used by the cmd
2837 * parameter.
2838 */
2839
2840 /* we must have a module name */
2841 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002842 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002843
2844 mod = strsep(&param, ":");
2845 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002846 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002847
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002848 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002849 if (!ret)
2850 ret = -EINVAL;
2851 if (ret < 0)
2852 return ret;
2853
2854 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002855}
2856
2857static struct ftrace_func_command ftrace_mod_cmd = {
2858 .name = "mod",
2859 .func = ftrace_mod_callback,
2860};
2861
2862static int __init ftrace_mod_cmd_init(void)
2863{
2864 return register_ftrace_command(&ftrace_mod_cmd);
2865}
2866device_initcall(ftrace_mod_cmd_init);
2867
Steven Rostedt59df055f2009-02-14 15:29:06 -05002868static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002869function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002870{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002871 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002872 struct hlist_head *hhd;
2873 struct hlist_node *n;
2874 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002875
2876 key = hash_long(ip, FTRACE_HASH_BITS);
2877
2878 hhd = &ftrace_func_hash[key];
2879
2880 if (hlist_empty(hhd))
2881 return;
2882
2883 /*
2884 * Disable preemption for these calls to prevent a RCU grace
2885 * period. This syncs the hash iteration and freeing of items
2886 * on the hash. rcu_read_lock is too dangerous here.
2887 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002888 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002889 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2890 if (entry->ip == ip)
2891 entry->ops->func(ip, parent_ip, &entry->data);
2892 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002893 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002894}
2895
Steven Rostedtb6887d72009-02-17 12:32:04 -05002896static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002897{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002898 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002899};
2900
Steven Rostedtb6887d72009-02-17 12:32:04 -05002901static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002902
Steven Rostedtb6887d72009-02-17 12:32:04 -05002903static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002904{
Steven Rostedtb8489142011-05-04 09:27:52 -04002905 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002906 int i;
2907
Steven Rostedtb6887d72009-02-17 12:32:04 -05002908 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002909 return;
2910
2911 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2912 struct hlist_head *hhd = &ftrace_func_hash[i];
2913 if (hhd->first)
2914 break;
2915 }
2916 /* Nothing registered? */
2917 if (i == FTRACE_FUNC_HASHSIZE)
2918 return;
2919
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002920 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002921
Steven Rostedtb6887d72009-02-17 12:32:04 -05002922 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002923}
2924
Steven Rostedtb6887d72009-02-17 12:32:04 -05002925static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002926{
2927 int i;
2928
Steven Rostedtb6887d72009-02-17 12:32:04 -05002929 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002930 return;
2931
2932 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2933 struct hlist_head *hhd = &ftrace_func_hash[i];
2934 if (hhd->first)
2935 return;
2936 }
2937
2938 /* no more funcs left */
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002939 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002940
Steven Rostedtb6887d72009-02-17 12:32:04 -05002941 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002942}
2943
2944
2945static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2946{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002947 struct ftrace_func_probe *entry =
2948 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002949
2950 if (entry->ops->free)
2951 entry->ops->free(&entry->data);
2952 kfree(entry);
2953}
2954
2955
2956int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002957register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002958 void *data)
2959{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002960 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002961 struct ftrace_page *pg;
2962 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002963 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002964 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002965 int count = 0;
2966 char *search;
2967
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002968 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002969 len = strlen(search);
2970
Steven Rostedtb6887d72009-02-17 12:32:04 -05002971 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002972 if (WARN_ON(not))
2973 return -EINVAL;
2974
2975 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002976
Steven Rostedt45a4a232011-04-21 23:16:46 -04002977 if (unlikely(ftrace_disabled))
2978 goto out_unlock;
2979
Steven Rostedt59df055f2009-02-14 15:29:06 -05002980 do_for_each_ftrace_rec(pg, rec) {
2981
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002982 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002983 continue;
2984
2985 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2986 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002987 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002988 if (!count)
2989 count = -ENOMEM;
2990 goto out_unlock;
2991 }
2992
2993 count++;
2994
2995 entry->data = data;
2996
2997 /*
2998 * The caller might want to do something special
2999 * for each function we find. We call the callback
3000 * to give the caller an opportunity to do so.
3001 */
3002 if (ops->callback) {
3003 if (ops->callback(rec->ip, &entry->data) < 0) {
3004 /* caller does not like this func */
3005 kfree(entry);
3006 continue;
3007 }
3008 }
3009
3010 entry->ops = ops;
3011 entry->ip = rec->ip;
3012
3013 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3014 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3015
3016 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05003017 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003018
3019 out_unlock:
3020 mutex_unlock(&ftrace_lock);
3021
3022 return count;
3023}
3024
3025enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003026 PROBE_TEST_FUNC = 1,
3027 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003028};
3029
3030static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003031__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003032 void *data, int flags)
3033{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003034 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003035 struct hlist_node *n, *tmp;
3036 char str[KSYM_SYMBOL_LEN];
3037 int type = MATCH_FULL;
3038 int i, len = 0;
3039 char *search;
3040
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003041 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003042 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003043 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003044 int not;
3045
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003046 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003047 len = strlen(search);
3048
Steven Rostedtb6887d72009-02-17 12:32:04 -05003049 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003050 if (WARN_ON(not))
3051 return;
3052 }
3053
3054 mutex_lock(&ftrace_lock);
3055 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3056 struct hlist_head *hhd = &ftrace_func_hash[i];
3057
3058 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3059
3060 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003061 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003062 continue;
3063
Steven Rostedtb6887d72009-02-17 12:32:04 -05003064 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003065 continue;
3066
3067 /* do this last, since it is the most expensive */
3068 if (glob) {
3069 kallsyms_lookup(entry->ip, NULL, NULL,
3070 NULL, str);
3071 if (!ftrace_match(str, glob, len, type))
3072 continue;
3073 }
3074
Steven Rostedt (Red Hat)52cecaa2013-03-13 11:15:19 -04003075 hlist_del_rcu(&entry->node);
3076 call_rcu_sched(&entry->rcu, ftrace_free_entry_rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003077 }
3078 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003079 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003080 mutex_unlock(&ftrace_lock);
3081}
3082
3083void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003084unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003085 void *data)
3086{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003087 __unregister_ftrace_function_probe(glob, ops, data,
3088 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003089}
3090
3091void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003092unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003093{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003094 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003095}
3096
Steven Rostedtb6887d72009-02-17 12:32:04 -05003097void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003098{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003099 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003100}
3101
Steven Rostedtf6180772009-02-14 00:40:25 -05003102static LIST_HEAD(ftrace_commands);
3103static DEFINE_MUTEX(ftrace_cmd_mutex);
3104
3105int register_ftrace_command(struct ftrace_func_command *cmd)
3106{
3107 struct ftrace_func_command *p;
3108 int ret = 0;
3109
3110 mutex_lock(&ftrace_cmd_mutex);
3111 list_for_each_entry(p, &ftrace_commands, list) {
3112 if (strcmp(cmd->name, p->name) == 0) {
3113 ret = -EBUSY;
3114 goto out_unlock;
3115 }
3116 }
3117 list_add(&cmd->list, &ftrace_commands);
3118 out_unlock:
3119 mutex_unlock(&ftrace_cmd_mutex);
3120
3121 return ret;
3122}
3123
3124int unregister_ftrace_command(struct ftrace_func_command *cmd)
3125{
3126 struct ftrace_func_command *p, *n;
3127 int ret = -ENODEV;
3128
3129 mutex_lock(&ftrace_cmd_mutex);
3130 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3131 if (strcmp(cmd->name, p->name) == 0) {
3132 ret = 0;
3133 list_del_init(&p->list);
3134 goto out_unlock;
3135 }
3136 }
3137 out_unlock:
3138 mutex_unlock(&ftrace_cmd_mutex);
3139
3140 return ret;
3141}
3142
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003143static int ftrace_process_regex(struct ftrace_hash *hash,
3144 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003145{
Steven Rostedtf6180772009-02-14 00:40:25 -05003146 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003147 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003148 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003149
3150 func = strsep(&next, ":");
3151
3152 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003153 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003154 if (!ret)
3155 ret = -EINVAL;
3156 if (ret < 0)
3157 return ret;
3158 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003159 }
3160
Steven Rostedtf6180772009-02-14 00:40:25 -05003161 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003162
3163 command = strsep(&next, ":");
3164
Steven Rostedtf6180772009-02-14 00:40:25 -05003165 mutex_lock(&ftrace_cmd_mutex);
3166 list_for_each_entry(p, &ftrace_commands, list) {
3167 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003168 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003169 goto out_unlock;
3170 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003171 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003172 out_unlock:
3173 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003174
Steven Rostedtf6180772009-02-14 00:40:25 -05003175 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003176}
3177
Ingo Molnare309b412008-05-12 21:20:51 +02003178static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003179ftrace_regex_write(struct file *file, const char __user *ubuf,
3180 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003181{
3182 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003183 struct trace_parser *parser;
3184 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003185
Li Zefan4ba79782009-09-22 13:52:20 +08003186 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003187 return 0;
3188
Steven Rostedt41c52c02008-05-22 11:46:33 -04003189 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003190
Steven Rostedt45a4a232011-04-21 23:16:46 -04003191 ret = -ENODEV;
3192 if (unlikely(ftrace_disabled))
3193 goto out_unlock;
3194
Steven Rostedt5072c592008-05-12 21:20:43 +02003195 if (file->f_mode & FMODE_READ) {
3196 struct seq_file *m = file->private_data;
3197 iter = m->private;
3198 } else
3199 iter = file->private_data;
3200
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003201 parser = &iter->parser;
3202 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003203
Li Zefan4ba79782009-09-22 13:52:20 +08003204 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003205 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003206 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003207 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003208 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003209 if (ret)
Li Zefaned146b22009-11-03 08:55:38 +08003210 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003211 }
3212
Steven Rostedt5072c592008-05-12 21:20:43 +02003213 ret = read;
Li Zefaned146b22009-11-03 08:55:38 +08003214out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003215 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b22009-11-03 08:55:38 +08003216
Steven Rostedt5072c592008-05-12 21:20:43 +02003217 return ret;
3218}
3219
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003220ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003221ftrace_filter_write(struct file *file, const char __user *ubuf,
3222 size_t cnt, loff_t *ppos)
3223{
3224 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3225}
3226
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003227ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003228ftrace_notrace_write(struct file *file, const char __user *ubuf,
3229 size_t cnt, loff_t *ppos)
3230{
3231 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3232}
3233
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003234static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003235ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3236 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003237{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003238 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003239 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003240 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003241
Steven Rostedt936e0742011-05-05 22:54:01 -04003242 /* All global ops uses the global ops filters */
3243 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3244 ops = &global_ops;
3245
Steven Rostedt41c52c02008-05-22 11:46:33 -04003246 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003247 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003248
Steven Rostedtf45948e2011-05-02 12:29:25 -04003249 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003250 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003251 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003252 orig_hash = &ops->notrace_hash;
3253
3254 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3255 if (!hash)
3256 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003257
Steven Rostedt41c52c02008-05-22 11:46:33 -04003258 mutex_lock(&ftrace_regex_lock);
3259 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003260 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003261 if (buf && !ftrace_match_records(hash, buf, len)) {
3262 ret = -EINVAL;
3263 goto out_regex_unlock;
3264 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003265
3266 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003267 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003268 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3269 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003270 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003271
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003272 mutex_unlock(&ftrace_lock);
3273
Jiri Olsaac483c42012-01-02 10:04:14 +01003274 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003275 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003276
3277 free_ftrace_hash(hash);
3278 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003279}
3280
Steven Rostedt77a2b372008-05-12 21:20:45 +02003281/**
3282 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003283 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003284 * @buf - the string that holds the function filter text.
3285 * @len - the length of the string.
3286 * @reset - non zero to reset all filters before applying this filter.
3287 *
3288 * Filters denote which functions should be enabled when tracing is enabled.
3289 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3290 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003291int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003292 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003293{
Jiri Olsaac483c42012-01-02 10:04:14 +01003294 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003295}
Steven Rostedt936e0742011-05-05 22:54:01 -04003296EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003297
Steven Rostedt41c52c02008-05-22 11:46:33 -04003298/**
3299 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003300 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003301 * @buf - the string that holds the function notrace text.
3302 * @len - the length of the string.
3303 * @reset - non zero to reset all filters before applying this filter.
3304 *
3305 * Notrace Filters denote which functions should not be enabled when tracing
3306 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3307 * for tracing.
3308 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003309int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003310 int len, int reset)
3311{
Jiri Olsaac483c42012-01-02 10:04:14 +01003312 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003313}
3314EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3315/**
3316 * ftrace_set_filter - set a function to filter on in ftrace
3317 * @ops - the ops to set the filter with
3318 * @buf - the string that holds the function filter text.
3319 * @len - the length of the string.
3320 * @reset - non zero to reset all filters before applying this filter.
3321 *
3322 * Filters denote which functions should be enabled when tracing is enabled.
3323 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3324 */
3325void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3326{
3327 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3328}
3329EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3330
3331/**
3332 * ftrace_set_notrace - set a function to not trace in ftrace
3333 * @ops - the ops to set the notrace filter with
3334 * @buf - the string that holds the function notrace text.
3335 * @len - the length of the string.
3336 * @reset - non zero to reset all filters before applying this filter.
3337 *
3338 * Notrace Filters denote which functions should not be enabled when tracing
3339 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3340 * for tracing.
3341 */
3342void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003343{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003344 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003345}
Steven Rostedt936e0742011-05-05 22:54:01 -04003346EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003347
Steven Rostedt2af15d62009-05-28 13:37:24 -04003348/*
3349 * command line interface to allow users to set filters on boot up.
3350 */
3351#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3352static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3353static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3354
3355static int __init set_ftrace_notrace(char *str)
3356{
3357 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3358 return 1;
3359}
3360__setup("ftrace_notrace=", set_ftrace_notrace);
3361
3362static int __init set_ftrace_filter(char *str)
3363{
3364 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3365 return 1;
3366}
3367__setup("ftrace_filter=", set_ftrace_filter);
3368
Stefan Assmann369bc182009-10-12 22:17:21 +02003369#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003370static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003371static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3372
Stefan Assmann369bc182009-10-12 22:17:21 +02003373static int __init set_graph_function(char *str)
3374{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003375 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003376 return 1;
3377}
3378__setup("ftrace_graph_filter=", set_graph_function);
3379
3380static void __init set_ftrace_early_graph(char *buf)
3381{
3382 int ret;
3383 char *func;
3384
3385 while (buf) {
3386 func = strsep(&buf, ",");
3387 /* we allow only one expression at a time */
3388 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3389 func);
3390 if (ret)
3391 printk(KERN_DEBUG "ftrace: function %s not "
3392 "traceable\n", func);
3393 }
3394}
3395#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3396
Steven Rostedt2a85a372011-12-19 21:57:44 -05003397void __init
3398ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003399{
3400 char *func;
3401
3402 while (buf) {
3403 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003404 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003405 }
3406}
3407
3408static void __init set_ftrace_early_filters(void)
3409{
3410 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003411 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003412 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003413 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003414#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3415 if (ftrace_graph_buf[0])
3416 set_ftrace_early_graph(ftrace_graph_buf);
3417#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003418}
3419
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003420int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003421{
3422 struct seq_file *m = (struct seq_file *)file->private_data;
3423 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003424 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003425 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003426 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003427 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003428
Steven Rostedt41c52c02008-05-22 11:46:33 -04003429 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003430 if (file->f_mode & FMODE_READ) {
3431 iter = m->private;
3432
3433 seq_release(inode, file);
3434 } else
3435 iter = file->private_data;
3436
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003437 parser = &iter->parser;
3438 if (trace_parser_loaded(parser)) {
3439 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003440 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003441 }
3442
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003443 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003444
Steven Rostedt058e2972011-04-29 22:35:33 -04003445 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003446 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3447
3448 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003449 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003450 else
3451 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003452
Steven Rostedt058e2972011-04-29 22:35:33 -04003453 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003454 ret = ftrace_hash_move(iter->ops, filter_hash,
3455 orig_hash, iter->hash);
3456 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3457 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003458 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003459
Steven Rostedt058e2972011-04-29 22:35:33 -04003460 mutex_unlock(&ftrace_lock);
3461 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003462 free_ftrace_hash(iter->hash);
3463 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003464
Steven Rostedt41c52c02008-05-22 11:46:33 -04003465 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003466 return 0;
3467}
3468
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003469static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003470 .open = ftrace_avail_open,
3471 .read = seq_read,
3472 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003473 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003474};
3475
Steven Rostedt647bcd02011-05-03 14:39:21 -04003476static const struct file_operations ftrace_enabled_fops = {
3477 .open = ftrace_enabled_open,
3478 .read = seq_read,
3479 .llseek = seq_lseek,
3480 .release = seq_release_private,
3481};
3482
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003483static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003484 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003485 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003486 .write = ftrace_filter_write,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003487 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003488 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003489};
3490
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003491static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003492 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003493 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003494 .write = ftrace_notrace_write,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003495 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003496 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003497};
3498
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003499#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3500
3501static DEFINE_MUTEX(graph_lock);
3502
3503int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003504int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003505unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3506
3507static void *
Li Zefan85951842009-06-24 09:54:00 +08003508__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003509{
Li Zefan85951842009-06-24 09:54:00 +08003510 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003511 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003512 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003513}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003514
Li Zefan85951842009-06-24 09:54:00 +08003515static void *
3516g_next(struct seq_file *m, void *v, loff_t *pos)
3517{
3518 (*pos)++;
3519 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003520}
3521
3522static void *g_start(struct seq_file *m, loff_t *pos)
3523{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003524 mutex_lock(&graph_lock);
3525
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003526 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003527 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003528 return (void *)1;
3529
Li Zefan85951842009-06-24 09:54:00 +08003530 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003531}
3532
3533static void g_stop(struct seq_file *m, void *p)
3534{
3535 mutex_unlock(&graph_lock);
3536}
3537
3538static int g_show(struct seq_file *m, void *v)
3539{
3540 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003541
3542 if (!ptr)
3543 return 0;
3544
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003545 if (ptr == (unsigned long *)1) {
3546 seq_printf(m, "#### all functions enabled ####\n");
3547 return 0;
3548 }
3549
Steven Rostedtb375a112009-09-17 00:05:58 -04003550 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003551
3552 return 0;
3553}
3554
James Morris88e9d342009-09-22 16:43:43 -07003555static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003556 .start = g_start,
3557 .next = g_next,
3558 .stop = g_stop,
3559 .show = g_show,
3560};
3561
3562static int
3563ftrace_graph_open(struct inode *inode, struct file *file)
3564{
3565 int ret = 0;
3566
3567 if (unlikely(ftrace_disabled))
3568 return -ENODEV;
3569
3570 mutex_lock(&graph_lock);
3571 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003572 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003573 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003574 ftrace_graph_count = 0;
3575 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3576 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003577 mutex_unlock(&graph_lock);
3578
Li Zefana4ec5e02009-09-18 14:06:28 +08003579 if (file->f_mode & FMODE_READ)
3580 ret = seq_open(file, &ftrace_graph_seq_ops);
3581
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003582 return ret;
3583}
3584
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003585static int
Li Zefan87827112009-07-23 11:29:11 +08003586ftrace_graph_release(struct inode *inode, struct file *file)
3587{
3588 if (file->f_mode & FMODE_READ)
3589 seq_release(inode, file);
3590 return 0;
3591}
3592
3593static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003594ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003595{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003596 struct dyn_ftrace *rec;
3597 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003598 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003599 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003600 int type, not;
3601 char *search;
3602 bool exists;
3603 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003604
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003605 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003606 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003607 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3608 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003609
3610 search_len = strlen(search);
3611
Steven Rostedt52baf112009-02-14 01:15:39 -05003612 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003613
3614 if (unlikely(ftrace_disabled)) {
3615 mutex_unlock(&ftrace_lock);
3616 return -ENODEV;
3617 }
3618
Steven Rostedt265c8312009-02-13 12:43:56 -05003619 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003620
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003621 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003622 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003623 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003624 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003625 if (array[i] == rec->ip) {
3626 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003627 break;
3628 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003629 }
3630
3631 if (!not) {
3632 fail = 0;
3633 if (!exists) {
3634 array[(*idx)++] = rec->ip;
3635 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3636 goto out;
3637 }
3638 } else {
3639 if (exists) {
3640 array[i] = array[--(*idx)];
3641 array[*idx] = 0;
3642 fail = 0;
3643 }
3644 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003645 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003646 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003647out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003648 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003649
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003650 if (fail)
3651 return -EINVAL;
3652
Namhyung Kim761694a2013-04-11 16:01:38 +09003653 ftrace_graph_filter_enabled = !!(*idx);
3654
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003655 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003656}
3657
3658static ssize_t
3659ftrace_graph_write(struct file *file, const char __user *ubuf,
3660 size_t cnt, loff_t *ppos)
3661{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003662 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003663 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003664
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003665 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003666 return 0;
3667
3668 mutex_lock(&graph_lock);
3669
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003670 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3671 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003672 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003673 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003674
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003675 read = trace_get_user(&parser, ubuf, cnt, ppos);
3676
Li Zefan4ba79782009-09-22 13:52:20 +08003677 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003678 parser.buffer[parser.idx] = 0;
3679
3680 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003681 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003682 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003683 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003684 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003685 }
3686
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003687 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003688
3689out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003690 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003691out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003692 mutex_unlock(&graph_lock);
3693
3694 return ret;
3695}
3696
3697static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003698 .open = ftrace_graph_open,
3699 .read = seq_read,
3700 .write = ftrace_graph_write,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003701 .llseek = ftrace_filter_lseek,
Li Zefan87827112009-07-23 11:29:11 +08003702 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003703};
3704#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3705
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003706static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003707{
Steven Rostedt5072c592008-05-12 21:20:43 +02003708
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003709 trace_create_file("available_filter_functions", 0444,
3710 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003711
Steven Rostedt647bcd02011-05-03 14:39:21 -04003712 trace_create_file("enabled_functions", 0444,
3713 d_tracer, NULL, &ftrace_enabled_fops);
3714
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003715 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3716 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003717
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003718 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003719 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003720
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003721#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003722 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003723 NULL,
3724 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003725#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3726
Steven Rostedt5072c592008-05-12 21:20:43 +02003727 return 0;
3728}
3729
Steven Rostedt68950612011-12-16 17:06:45 -05003730static void ftrace_swap_recs(void *a, void *b, int size)
3731{
3732 struct dyn_ftrace *reca = a;
3733 struct dyn_ftrace *recb = b;
3734 struct dyn_ftrace t;
3735
3736 t = *reca;
3737 *reca = *recb;
3738 *recb = t;
3739}
3740
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003741static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003742 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003743 unsigned long *end)
3744{
Steven Rostedta7900872011-12-16 16:23:44 -05003745 struct ftrace_page *pg;
3746 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003747 unsigned long *p;
3748 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003749 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003750 int ret = -ENOMEM;
3751
3752 count = end - start;
3753
3754 if (!count)
3755 return 0;
3756
3757 pg = ftrace_allocate_pages(count);
3758 if (!pg)
3759 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003760
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003761 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003762
Steven Rostedt32082302011-12-16 14:42:37 -05003763 /*
3764 * Core and each module needs their own pages, as
3765 * modules will free them when they are removed.
3766 * Force a new page to be allocated for modules.
3767 */
Steven Rostedta7900872011-12-16 16:23:44 -05003768 if (!mod) {
3769 WARN_ON(ftrace_pages || ftrace_pages_start);
3770 /* First initialization */
3771 ftrace_pages = ftrace_pages_start = pg;
3772 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003773 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003774 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003775
Steven Rostedta7900872011-12-16 16:23:44 -05003776 if (WARN_ON(ftrace_pages->next)) {
3777 /* Hmm, we have free pages? */
3778 while (ftrace_pages->next)
3779 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003780 }
Steven Rostedta7900872011-12-16 16:23:44 -05003781
3782 ftrace_pages->next = pg;
3783 ftrace_pages = pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003784 }
3785
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003786 p = start;
3787 while (p < end) {
3788 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003789 /*
3790 * Some architecture linkers will pad between
3791 * the different mcount_loc sections of different
3792 * object files to satisfy alignments.
3793 * Skip any NULL pointers.
3794 */
3795 if (!addr)
3796 continue;
Steven Rostedta7900872011-12-16 16:23:44 -05003797 if (!ftrace_record_ip(addr))
3798 break;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003799 }
3800
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003801 /* These new locations need to be initialized */
3802 ftrace_new_pgs = pg;
3803
Steven Rostedt68950612011-12-16 17:06:45 -05003804 /* Make each individual set of pages sorted by ips */
3805 for (; pg; pg = pg->next)
3806 sort(pg->records, pg->index, sizeof(struct dyn_ftrace),
3807 ftrace_cmp_recs, ftrace_swap_recs);
3808
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003809 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003810 * We only need to disable interrupts on start up
3811 * because we are modifying code that an interrupt
3812 * may execute, and the modification is not atomic.
3813 * But for modules, nothing runs the code we modify
3814 * until we are finished with it, and there's no
3815 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003816 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003817 if (!mod)
3818 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003819 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003820 if (!mod)
3821 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003822 ret = 0;
3823 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003824 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003825
Steven Rostedta7900872011-12-16 16:23:44 -05003826 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003827}
3828
Steven Rostedt93eb6772009-04-15 13:24:06 -04003829#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003830
3831#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3832
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003833void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003834{
3835 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003836 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003837 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003838 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003839
Steven Rostedt93eb6772009-04-15 13:24:06 -04003840 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003841
3842 if (ftrace_disabled)
3843 goto out_unlock;
3844
Steven Rostedt32082302011-12-16 14:42:37 -05003845 /*
3846 * Each module has its own ftrace_pages, remove
3847 * them from the list.
3848 */
3849 last_pg = &ftrace_pages_start;
3850 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3851 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003852 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003853 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003854 * As core pages are first, the first
3855 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003856 */
Steven Rostedt32082302011-12-16 14:42:37 -05003857 if (WARN_ON(pg == ftrace_pages_start))
3858 goto out_unlock;
3859
3860 /* Check if we are deleting the last page */
3861 if (pg == ftrace_pages)
3862 ftrace_pages = next_to_ftrace_page(last_pg);
3863
3864 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003865 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3866 free_pages((unsigned long)pg->records, order);
3867 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05003868 } else
3869 last_pg = &pg->next;
3870 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003871 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003872 mutex_unlock(&ftrace_lock);
3873}
3874
3875static void ftrace_init_module(struct module *mod,
3876 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003877{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003878 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003879 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003880 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003881}
3882
Steven Rostedt (Red Hat)8f4c0e82014-04-24 10:40:12 -04003883void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003884{
Steven Rostedt (Red Hat)8f4c0e82014-04-24 10:40:12 -04003885 ftrace_init_module(mod, mod->ftrace_callsites,
3886 mod->ftrace_callsites +
3887 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003888}
3889
3890static int ftrace_module_notify_exit(struct notifier_block *self,
3891 unsigned long val, void *data)
3892{
3893 struct module *mod = data;
3894
3895 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003896 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003897
3898 return 0;
3899}
3900#else
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003901static int ftrace_module_notify_exit(struct notifier_block *self,
3902 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003903{
3904 return 0;
3905}
3906#endif /* CONFIG_MODULES */
3907
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003908struct notifier_block ftrace_module_exit_nb = {
3909 .notifier_call = ftrace_module_notify_exit,
3910 .priority = INT_MIN, /* Run after anything that can remove kprobes */
3911};
3912
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003913extern unsigned long __start_mcount_loc[];
3914extern unsigned long __stop_mcount_loc[];
3915
3916void __init ftrace_init(void)
3917{
3918 unsigned long count, addr, flags;
3919 int ret;
3920
3921 /* Keep the ftrace pointer to the stub */
3922 addr = (unsigned long)ftrace_stub;
3923
3924 local_irq_save(flags);
3925 ftrace_dyn_arch_init(&addr);
3926 local_irq_restore(flags);
3927
3928 /* ftrace_dyn_arch_init places the return code in addr */
3929 if (addr)
3930 goto failed;
3931
3932 count = __stop_mcount_loc - __start_mcount_loc;
3933
3934 ret = ftrace_dyn_table_alloc(count);
3935 if (ret)
3936 goto failed;
3937
3938 last_ftrace_enabled = ftrace_enabled = 1;
3939
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003940 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003941 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003942 __stop_mcount_loc);
3943
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003944 ret = register_module_notifier(&ftrace_module_exit_nb);
3945 if (ret)
3946 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04003947
Steven Rostedt2af15d62009-05-28 13:37:24 -04003948 set_ftrace_early_filters();
3949
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003950 return;
3951 failed:
3952 ftrace_disabled = 1;
3953}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003954
Steven Rostedt3d083392008-05-12 21:20:42 +02003955#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003956
Steven Rostedt2b499382011-05-03 22:49:52 -04003957static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003958 .func = ftrace_stub,
3959};
3960
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003961static int __init ftrace_nodyn_init(void)
3962{
3963 ftrace_enabled = 1;
3964 return 0;
3965}
3966device_initcall(ftrace_nodyn_init);
3967
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003968static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3969static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003970/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05003971# define ftrace_startup(ops, command) \
3972 ({ \
3973 int ___ret = __register_ftrace_function(ops); \
3974 if (!___ret) \
3975 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3976 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003977 })
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05003978# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
3979
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003980# define ftrace_startup_sysctl() do { } while (0)
3981# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003982
3983static inline int
3984ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3985{
3986 return 1;
3987}
3988
Steven Rostedt3d083392008-05-12 21:20:42 +02003989#endif /* CONFIG_DYNAMIC_FTRACE */
3990
Steven Rostedtb8489142011-05-04 09:27:52 -04003991static void
Jiri Olsae2484912012-02-15 15:51:48 +01003992ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip)
3993{
3994 struct ftrace_ops *op;
3995
3996 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
3997 return;
3998
3999 /*
4000 * Some of the ops may be dynamically allocated,
4001 * they must be freed after a synchronize_sched().
4002 */
4003 preempt_disable_notrace();
4004 trace_recursion_set(TRACE_CONTROL_BIT);
4005 op = rcu_dereference_raw(ftrace_control_list);
4006 while (op != &ftrace_list_end) {
4007 if (!ftrace_function_local_disabled(op) &&
4008 ftrace_ops_test(op, ip))
4009 op->func(ip, parent_ip);
4010
4011 op = rcu_dereference_raw(op->next);
4012 };
4013 trace_recursion_clear(TRACE_CONTROL_BIT);
4014 preempt_enable_notrace();
4015}
4016
4017static struct ftrace_ops control_ops = {
4018 .func = ftrace_ops_control_func,
4019};
4020
4021static void
Steven Rostedtb8489142011-05-04 09:27:52 -04004022ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
4023{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004024 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04004025
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004026 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4027 return;
4028
4029 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004030 /*
4031 * Some of the ops may be dynamically allocated,
4032 * they must be freed after a synchronize_sched().
4033 */
4034 preempt_disable_notrace();
4035 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04004036 while (op != &ftrace_list_end) {
4037 if (ftrace_ops_test(op, ip))
4038 op->func(ip, parent_ip);
4039 op = rcu_dereference_raw(op->next);
4040 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004041 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004042 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04004043}
4044
Steven Rostedte32d8952008-12-04 00:26:41 -05004045static void clear_ftrace_swapper(void)
4046{
4047 struct task_struct *p;
4048 int cpu;
4049
4050 get_online_cpus();
4051 for_each_online_cpu(cpu) {
4052 p = idle_task(cpu);
4053 clear_tsk_trace_trace(p);
4054 }
4055 put_online_cpus();
4056}
4057
4058static void set_ftrace_swapper(void)
4059{
4060 struct task_struct *p;
4061 int cpu;
4062
4063 get_online_cpus();
4064 for_each_online_cpu(cpu) {
4065 p = idle_task(cpu);
4066 set_tsk_trace_trace(p);
4067 }
4068 put_online_cpus();
4069}
4070
4071static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004072{
4073 struct task_struct *p;
4074
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004075 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004076 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004077 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004078 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004079 rcu_read_unlock();
4080
Steven Rostedte32d8952008-12-04 00:26:41 -05004081 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004082}
4083
Steven Rostedte32d8952008-12-04 00:26:41 -05004084static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004085{
4086 struct task_struct *p;
4087
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004088 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004089 do_each_pid_task(pid, PIDTYPE_PID, p) {
4090 set_tsk_trace_trace(p);
4091 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004092 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004093}
4094
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004095static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004096{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004097 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004098 clear_ftrace_swapper();
4099 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004100 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004101}
4102
4103static void set_ftrace_pid_task(struct pid *pid)
4104{
4105 if (pid == ftrace_swapper_pid)
4106 set_ftrace_swapper();
4107 else
4108 set_ftrace_pid(pid);
4109}
4110
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004111static int ftrace_pid_add(int p)
4112{
4113 struct pid *pid;
4114 struct ftrace_pid *fpid;
4115 int ret = -EINVAL;
4116
4117 mutex_lock(&ftrace_lock);
4118
4119 if (!p)
4120 pid = ftrace_swapper_pid;
4121 else
4122 pid = find_get_pid(p);
4123
4124 if (!pid)
4125 goto out;
4126
4127 ret = 0;
4128
4129 list_for_each_entry(fpid, &ftrace_pids, list)
4130 if (fpid->pid == pid)
4131 goto out_put;
4132
4133 ret = -ENOMEM;
4134
4135 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4136 if (!fpid)
4137 goto out_put;
4138
4139 list_add(&fpid->list, &ftrace_pids);
4140 fpid->pid = pid;
4141
4142 set_ftrace_pid_task(pid);
4143
4144 ftrace_update_pid_func();
4145 ftrace_startup_enable(0);
4146
4147 mutex_unlock(&ftrace_lock);
4148 return 0;
4149
4150out_put:
4151 if (pid != ftrace_swapper_pid)
4152 put_pid(pid);
4153
4154out:
4155 mutex_unlock(&ftrace_lock);
4156 return ret;
4157}
4158
4159static void ftrace_pid_reset(void)
4160{
4161 struct ftrace_pid *fpid, *safe;
4162
4163 mutex_lock(&ftrace_lock);
4164 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4165 struct pid *pid = fpid->pid;
4166
4167 clear_ftrace_pid_task(pid);
4168
4169 list_del(&fpid->list);
4170 kfree(fpid);
4171 }
4172
4173 ftrace_update_pid_func();
4174 ftrace_startup_enable(0);
4175
4176 mutex_unlock(&ftrace_lock);
4177}
4178
4179static void *fpid_start(struct seq_file *m, loff_t *pos)
4180{
4181 mutex_lock(&ftrace_lock);
4182
4183 if (list_empty(&ftrace_pids) && (!*pos))
4184 return (void *) 1;
4185
4186 return seq_list_start(&ftrace_pids, *pos);
4187}
4188
4189static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4190{
4191 if (v == (void *)1)
4192 return NULL;
4193
4194 return seq_list_next(v, &ftrace_pids, pos);
4195}
4196
4197static void fpid_stop(struct seq_file *m, void *p)
4198{
4199 mutex_unlock(&ftrace_lock);
4200}
4201
4202static int fpid_show(struct seq_file *m, void *v)
4203{
4204 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4205
4206 if (v == (void *)1) {
4207 seq_printf(m, "no pid\n");
4208 return 0;
4209 }
4210
4211 if (fpid->pid == ftrace_swapper_pid)
4212 seq_printf(m, "swapper tasks\n");
4213 else
4214 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4215
4216 return 0;
4217}
4218
4219static const struct seq_operations ftrace_pid_sops = {
4220 .start = fpid_start,
4221 .next = fpid_next,
4222 .stop = fpid_stop,
4223 .show = fpid_show,
4224};
4225
4226static int
4227ftrace_pid_open(struct inode *inode, struct file *file)
4228{
4229 int ret = 0;
4230
4231 if ((file->f_mode & FMODE_WRITE) &&
4232 (file->f_flags & O_TRUNC))
4233 ftrace_pid_reset();
4234
4235 if (file->f_mode & FMODE_READ)
4236 ret = seq_open(file, &ftrace_pid_sops);
4237
4238 return ret;
4239}
4240
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004241static ssize_t
4242ftrace_pid_write(struct file *filp, const char __user *ubuf,
4243 size_t cnt, loff_t *ppos)
4244{
Ingo Molnar457dc922009-11-23 11:03:28 +01004245 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004246 long val;
4247 int ret;
4248
4249 if (cnt >= sizeof(buf))
4250 return -EINVAL;
4251
4252 if (copy_from_user(&buf, ubuf, cnt))
4253 return -EFAULT;
4254
4255 buf[cnt] = 0;
4256
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004257 /*
4258 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4259 * to clean the filter quietly.
4260 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004261 tmp = strstrip(buf);
4262 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004263 return 1;
4264
Ingo Molnar457dc922009-11-23 11:03:28 +01004265 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004266 if (ret < 0)
4267 return ret;
4268
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004269 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004270
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004271 return ret ? ret : cnt;
4272}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004273
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004274static int
4275ftrace_pid_release(struct inode *inode, struct file *file)
4276{
4277 if (file->f_mode & FMODE_READ)
4278 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004279
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004280 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004281}
4282
Steven Rostedt5e2336a02009-03-05 21:44:55 -05004283static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004284 .open = ftrace_pid_open,
4285 .write = ftrace_pid_write,
4286 .read = seq_read,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08004287 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004288 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004289};
4290
4291static __init int ftrace_init_debugfs(void)
4292{
4293 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004294
4295 d_tracer = tracing_init_dentry();
4296 if (!d_tracer)
4297 return 0;
4298
4299 ftrace_init_dyn_debugfs(d_tracer);
4300
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004301 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4302 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004303
4304 ftrace_profile_debugfs(d_tracer);
4305
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004306 return 0;
4307}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004308fs_initcall(ftrace_init_debugfs);
4309
Steven Rostedt3d083392008-05-12 21:20:42 +02004310/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004311 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004312 *
4313 * This function should be used by panic code. It stops ftrace
4314 * but in a not so nice way. If you need to simply kill ftrace
4315 * from a non-atomic section, use ftrace_kill.
4316 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004317void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004318{
4319 ftrace_disabled = 1;
4320 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004321 clear_ftrace_function();
4322}
4323
4324/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004325 * Test if ftrace is dead or not.
4326 */
4327int ftrace_is_dead(void)
4328{
4329 return ftrace_disabled;
4330}
4331
4332/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004333 * register_ftrace_function - register a function for profiling
4334 * @ops - ops structure that holds the function for profiling.
4335 *
4336 * Register a function to be called by all functions in the
4337 * kernel.
4338 *
4339 * Note: @ops->func and all the functions it calls must be labeled
4340 * with "notrace", otherwise it will go into a
4341 * recursive loop.
4342 */
4343int register_ftrace_function(struct ftrace_ops *ops)
4344{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004345 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004346
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004347 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004348
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004349 ret = ftrace_startup(ops, 0);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004350
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004351 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004352 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004353}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004354EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004355
4356/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004357 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004358 * @ops - ops structure that holds the function to unregister
4359 *
4360 * Unregister a function that was added to be called by ftrace profiling.
4361 */
4362int unregister_ftrace_function(struct ftrace_ops *ops)
4363{
4364 int ret;
4365
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004366 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004367 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004368 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004369
4370 return ret;
4371}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004372EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004373
Ingo Molnare309b412008-05-12 21:20:51 +02004374int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004375ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004376 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004377 loff_t *ppos)
4378{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004379 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004380
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004381 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004382
Steven Rostedt45a4a232011-04-21 23:16:46 -04004383 if (unlikely(ftrace_disabled))
4384 goto out;
4385
4386 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004387
Li Zefana32c7762009-06-26 16:55:51 +08004388 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004389 goto out;
4390
Li Zefana32c7762009-06-26 16:55:51 +08004391 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004392
4393 if (ftrace_enabled) {
4394
4395 ftrace_startup_sysctl();
4396
4397 /* we are starting ftrace again */
Jan Kiszkab81d3242013-03-26 17:53:03 +01004398 if (ftrace_ops_list != &ftrace_list_end)
4399 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004400
4401 } else {
4402 /* stopping ftrace calls (just send to ftrace_stub) */
4403 ftrace_trace_function = ftrace_stub;
4404
4405 ftrace_shutdown_sysctl();
4406 }
4407
4408 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004409 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004410 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004411}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004412
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004413#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004414
Steven Rostedt597af812009-04-03 15:24:12 -04004415static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004416static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004417
Steven Rostedte49dc192008-12-02 23:50:05 -05004418int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4419{
4420 return 0;
4421}
4422
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004423/* The callbacks that hook a function */
4424trace_func_graph_ret_t ftrace_graph_return =
4425 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004426trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt1c2bd0d2014-02-11 14:50:01 -05004427static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004428
4429/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4430static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4431{
4432 int i;
4433 int ret = 0;
4434 unsigned long flags;
4435 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4436 struct task_struct *g, *t;
4437
4438 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4439 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4440 * sizeof(struct ftrace_ret_stack),
4441 GFP_KERNEL);
4442 if (!ret_stack_list[i]) {
4443 start = 0;
4444 end = i;
4445 ret = -ENOMEM;
4446 goto free;
4447 }
4448 }
4449
4450 read_lock_irqsave(&tasklist_lock, flags);
4451 do_each_thread(g, t) {
4452 if (start == end) {
4453 ret = -EAGAIN;
4454 goto unlock;
4455 }
4456
4457 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004458 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004459 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004460 t->curr_ret_stack = -1;
4461 /* Make sure the tasks see the -1 first: */
4462 smp_wmb();
4463 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004464 }
4465 } while_each_thread(g, t);
4466
4467unlock:
4468 read_unlock_irqrestore(&tasklist_lock, flags);
4469free:
4470 for (i = start; i < end; i++)
4471 kfree(ret_stack_list[i]);
4472 return ret;
4473}
4474
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004475static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004476ftrace_graph_probe_sched_switch(void *ignore,
4477 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004478{
4479 unsigned long long timestamp;
4480 int index;
4481
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004482 /*
4483 * Does the user want to count the time a function was asleep.
4484 * If so, do not update the time stamps.
4485 */
4486 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4487 return;
4488
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004489 timestamp = trace_clock_local();
4490
4491 prev->ftrace_timestamp = timestamp;
4492
4493 /* only process tasks that we timestamped */
4494 if (!next->ftrace_timestamp)
4495 return;
4496
4497 /*
4498 * Update all the counters in next to make up for the
4499 * time next was sleeping.
4500 */
4501 timestamp -= next->ftrace_timestamp;
4502
4503 for (index = next->curr_ret_stack; index >= 0; index--)
4504 next->ret_stack[index].calltime += timestamp;
4505}
4506
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004507/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004508static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004509{
4510 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004511 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004512
4513 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4514 sizeof(struct ftrace_ret_stack *),
4515 GFP_KERNEL);
4516
4517 if (!ret_stack_list)
4518 return -ENOMEM;
4519
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004520 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004521 for_each_online_cpu(cpu) {
4522 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004523 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004524 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004525
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004526 do {
4527 ret = alloc_retstack_tasklist(ret_stack_list);
4528 } while (ret == -EAGAIN);
4529
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004530 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004531 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004532 if (ret)
4533 pr_info("ftrace_graph: Couldn't activate tracepoint"
4534 " probe to kernel_sched_switch\n");
4535 }
4536
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004537 kfree(ret_stack_list);
4538 return ret;
4539}
4540
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004541/*
4542 * Hibernation protection.
4543 * The state of the current task is too much unstable during
4544 * suspend/restore to disk. We want to protect against that.
4545 */
4546static int
4547ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4548 void *unused)
4549{
4550 switch (state) {
4551 case PM_HIBERNATION_PREPARE:
4552 pause_graph_tracing();
4553 break;
4554
4555 case PM_POST_HIBERNATION:
4556 unpause_graph_tracing();
4557 break;
4558 }
4559 return NOTIFY_DONE;
4560}
4561
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004562/* Just a place holder for function graph */
4563static struct ftrace_ops fgraph_ops __read_mostly = {
4564 .func = ftrace_stub,
4565 .flags = FTRACE_OPS_FL_GLOBAL,
4566};
4567
Steven Rostedt1c2bd0d2014-02-11 14:50:01 -05004568static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
4569{
4570 if (!ftrace_ops_test(&global_ops, trace->func))
4571 return 0;
4572 return __ftrace_graph_entry(trace);
4573}
4574
4575/*
4576 * The function graph tracer should only trace the functions defined
4577 * by set_ftrace_filter and set_ftrace_notrace. If another function
4578 * tracer ops is registered, the graph tracer requires testing the
4579 * function against the global ops, and not just trace any function
4580 * that any ftrace_ops registered.
4581 */
4582static void update_function_graph_func(void)
4583{
4584 if (ftrace_ops_list == &ftrace_list_end ||
4585 (ftrace_ops_list == &global_ops &&
4586 global_ops.next == &ftrace_list_end))
4587 ftrace_graph_entry = __ftrace_graph_entry;
4588 else
4589 ftrace_graph_entry = ftrace_graph_entry_test;
4590}
4591
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004592int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4593 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004594{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004595 int ret = 0;
4596
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004597 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004598
Steven Rostedt05ce5812009-03-24 00:18:31 -04004599 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004600 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004601 ret = -EBUSY;
4602 goto out;
4603 }
4604
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004605 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4606 register_pm_notifier(&ftrace_suspend_notifier);
4607
Steven Rostedt597af812009-04-03 15:24:12 -04004608 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004609 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004610 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004611 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004612 goto out;
4613 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004614
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004615 ftrace_graph_return = retfunc;
Steven Rostedt1c2bd0d2014-02-11 14:50:01 -05004616
4617 /*
4618 * Update the indirect function to the entryfunc, and the
4619 * function that gets called to the entry_test first. Then
4620 * call the update fgraph entry function to determine if
4621 * the entryfunc should be called directly or not.
4622 */
4623 __ftrace_graph_entry = entryfunc;
4624 ftrace_graph_entry = ftrace_graph_entry_test;
4625 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05004626
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004627 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004628
4629out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004630 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004631 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004632}
4633
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004634void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004635{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004636 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004637
Steven Rostedt597af812009-04-03 15:24:12 -04004638 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004639 goto out;
4640
Steven Rostedt597af812009-04-03 15:24:12 -04004641 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004642 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004643 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt1c2bd0d2014-02-11 14:50:01 -05004644 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004645 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004646 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004647 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004648
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004649 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004650 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004651}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004652
Steven Rostedt868baf02011-02-10 21:26:13 -05004653static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4654
4655static void
4656graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4657{
4658 atomic_set(&t->tracing_graph_pause, 0);
4659 atomic_set(&t->trace_overrun, 0);
4660 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004661 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004662 smp_wmb();
4663 t->ret_stack = ret_stack;
4664}
4665
4666/*
4667 * Allocate a return stack for the idle task. May be the first
4668 * time through, or it may be done by CPU hotplug online.
4669 */
4670void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4671{
4672 t->curr_ret_stack = -1;
4673 /*
4674 * The idle task has no parent, it either has its own
4675 * stack or no stack at all.
4676 */
4677 if (t->ret_stack)
4678 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4679
4680 if (ftrace_graph_active) {
4681 struct ftrace_ret_stack *ret_stack;
4682
4683 ret_stack = per_cpu(idle_ret_stack, cpu);
4684 if (!ret_stack) {
4685 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4686 * sizeof(struct ftrace_ret_stack),
4687 GFP_KERNEL);
4688 if (!ret_stack)
4689 return;
4690 per_cpu(idle_ret_stack, cpu) = ret_stack;
4691 }
4692 graph_init_task(t, ret_stack);
4693 }
4694}
4695
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004696/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004697void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004698{
Steven Rostedt84047e32009-06-02 16:51:55 -04004699 /* Make sure we do not use the parent ret_stack */
4700 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004701 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004702
Steven Rostedt597af812009-04-03 15:24:12 -04004703 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004704 struct ftrace_ret_stack *ret_stack;
4705
4706 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004707 * sizeof(struct ftrace_ret_stack),
4708 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004709 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004710 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004711 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004712 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004713}
4714
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004715void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004716{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004717 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4718
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004719 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004720 /* NULL must become visible to IRQs before we free it: */
4721 barrier();
4722
4723 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004724}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004725
4726void ftrace_graph_stop(void)
4727{
4728 ftrace_stop();
4729}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004730#endif