blob: 683d559a0eefef4cc9495c1b97fcbee0836065e1 [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 Rostedt6912896e2008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Steven Rostedt4eebcc82008-05-12 21:20:48 +020065/* ftrace_enabled is a method to turn ftrace on or off */
66int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020067static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020068
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050069/* Quick disabling of function tracer. */
70int function_trace_stop;
71
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040072/* List for set_ftrace_pid's pids. */
73LIST_HEAD(ftrace_pids);
74struct ftrace_pid {
75 struct list_head list;
76 struct pid *pid;
77};
78
Steven Rostedt4eebcc82008-05-12 21:20:48 +020079/*
80 * ftrace_disabled is set when an anomaly is discovered.
81 * ftrace_disabled is much stronger than ftrace_enabled.
82 */
83static int ftrace_disabled __read_mostly;
84
Steven Rostedt52baf112009-02-14 01:15:39 -050085static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020086
Paul McQuadebd38c0e2011-05-31 20:51:55 +010087static struct ftrace_ops ftrace_list_end __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -040088 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020089};
90
Steven Rostedtb8489142011-05-04 09:27:52 -040091static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
92static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020093ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt6331c282011-07-13 15:11:02 -040094static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050095ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050096ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -040097static struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020098
Steven Rostedtb8489142011-05-04 09:27:52 -040099static void
100ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
101
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800102/*
Steven Rostedtb8489142011-05-04 09:27:52 -0400103 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800104 * can use rcu_dereference_raw() is that elements removed from this list
105 * are simply leaked, so there is no need to interact with a grace-period
106 * mechanism. The rcu_dereference_raw() calls are needed to handle
Steven Rostedtb8489142011-05-04 09:27:52 -0400107 * concurrent insertions into the ftrace_global_list.
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800108 *
109 * Silly Alpha and silly pointer-speculation compiler optimizations!
110 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400111static void ftrace_global_list_func(unsigned long ip,
112 unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200113{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400114 struct ftrace_ops *op;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200115
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400116 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
117 return;
118
119 trace_recursion_set(TRACE_GLOBAL_BIT);
120 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200121 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200122 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800123 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200124 };
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400125 trace_recursion_clear(TRACE_GLOBAL_BIT);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200126}
127
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500128static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
129{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500130 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500131 return;
132
133 ftrace_pid_function(ip, parent_ip);
134}
135
136static void set_ftrace_pid_function(ftrace_func_t func)
137{
138 /* do not set ftrace_pid_function to itself! */
139 if (func != ftrace_pid_func)
140 ftrace_pid_function = func;
141}
142
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200143/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200144 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200145 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200146 * This NULLs the ftrace function and in essence stops
147 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200148 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200149void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200150{
Steven Rostedt3d083392008-05-12 21:20:42 +0200151 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500152 __ftrace_trace_function = ftrace_stub;
Steven Rostedt6331c282011-07-13 15:11:02 -0400153 __ftrace_trace_function_delay = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500154 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200155}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200156
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500157#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
158/*
159 * For those archs that do not test ftrace_trace_stop in their
160 * mcount call site, we need to do it from C.
161 */
162static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
163{
164 if (function_trace_stop)
165 return;
166
167 __ftrace_trace_function(ip, parent_ip);
168}
169#endif
170
Steven Rostedt2b499382011-05-03 22:49:52 -0400171static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400172{
173 ftrace_func_t func;
174
175 /*
176 * If there's only one function registered, then call that
177 * function directly. Otherwise, we need to iterate over the
178 * registered callers.
179 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400180 if (ftrace_global_list == &ftrace_list_end ||
181 ftrace_global_list->next == &ftrace_list_end)
182 func = ftrace_global_list->func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400183 else
Steven Rostedtb8489142011-05-04 09:27:52 -0400184 func = ftrace_global_list_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400185
186 /* If we filter on pids, update to use the pid function */
187 if (!list_empty(&ftrace_pids)) {
188 set_ftrace_pid_function(func);
189 func = ftrace_pid_func;
190 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400191
192 global_ops.func = func;
193}
194
195static void update_ftrace_function(void)
196{
197 ftrace_func_t func;
198
199 update_global_ops();
200
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400201 /*
202 * If we are at the end of the list and this ops is
203 * not dynamic, then have the mcount trampoline call
204 * the function directly
205 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400206 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400207 (ftrace_ops_list->next == &ftrace_list_end &&
208 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
Steven Rostedtb8489142011-05-04 09:27:52 -0400209 func = ftrace_ops_list->func;
210 else
211 func = ftrace_ops_list_func;
Steven Rostedt2b499382011-05-03 22:49:52 -0400212
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400213#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
214 ftrace_trace_function = func;
215#else
Steven Rostedt6331c282011-07-13 15:11:02 -0400216#ifdef CONFIG_DYNAMIC_FTRACE
217 /* do not update till all functions have been modified */
218 __ftrace_trace_function_delay = func;
219#else
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400220 __ftrace_trace_function = func;
Steven Rostedt6331c282011-07-13 15:11:02 -0400221#endif
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400222 ftrace_trace_function = ftrace_test_stop_func;
223#endif
224}
225
Steven Rostedt2b499382011-05-03 22:49:52 -0400226static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200227{
Steven Rostedt2b499382011-05-03 22:49:52 -0400228 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200229 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400230 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200231 * CPU might be walking that list. We need to make sure
232 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400233 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200234 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400235 rcu_assign_pointer(*list, ops);
236}
Steven Rostedt3d083392008-05-12 21:20:42 +0200237
Steven Rostedt2b499382011-05-03 22:49:52 -0400238static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
239{
240 struct ftrace_ops **p;
241
242 /*
243 * If we are removing the last function, then simply point
244 * to the ftrace_stub.
245 */
246 if (*list == ops && ops->next == &ftrace_list_end) {
247 *list = &ftrace_list_end;
248 return 0;
249 }
250
251 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
252 if (*p == ops)
253 break;
254
255 if (*p != ops)
256 return -1;
257
258 *p = (*p)->next;
259 return 0;
260}
261
262static int __register_ftrace_function(struct ftrace_ops *ops)
263{
264 if (ftrace_disabled)
265 return -ENODEV;
266
267 if (FTRACE_WARN_ON(ops == &global_ops))
268 return -EINVAL;
269
Steven Rostedtb8489142011-05-04 09:27:52 -0400270 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
271 return -EBUSY;
272
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400273 if (!core_kernel_data((unsigned long)ops))
274 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
275
Steven Rostedtb8489142011-05-04 09:27:52 -0400276 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
277 int first = ftrace_global_list == &ftrace_list_end;
278 add_ftrace_ops(&ftrace_global_list, ops);
279 ops->flags |= FTRACE_OPS_FL_ENABLED;
280 if (first)
281 add_ftrace_ops(&ftrace_ops_list, &global_ops);
282 } else
283 add_ftrace_ops(&ftrace_ops_list, ops);
284
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400285 if (ftrace_enabled)
286 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200287
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200288 return 0;
289}
290
Ingo Molnare309b412008-05-12 21:20:51 +0200291static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200292{
Steven Rostedt2b499382011-05-03 22:49:52 -0400293 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200294
Steven Rostedt2b499382011-05-03 22:49:52 -0400295 if (ftrace_disabled)
296 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200297
Steven Rostedtb8489142011-05-04 09:27:52 -0400298 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
299 return -EBUSY;
300
Steven Rostedt2b499382011-05-03 22:49:52 -0400301 if (FTRACE_WARN_ON(ops == &global_ops))
302 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200303
Steven Rostedtb8489142011-05-04 09:27:52 -0400304 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
305 ret = remove_ftrace_ops(&ftrace_global_list, ops);
306 if (!ret && ftrace_global_list == &ftrace_list_end)
307 ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops);
308 if (!ret)
309 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
310 } else
311 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
312
Steven Rostedt2b499382011-05-03 22:49:52 -0400313 if (ret < 0)
314 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400315
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400316 if (ftrace_enabled)
317 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200318
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400319 /*
320 * Dynamic ops may be freed, we must make sure that all
321 * callers are done before leaving this function.
322 */
323 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
324 synchronize_sched();
325
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500326 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200327}
328
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500329static void ftrace_update_pid_func(void)
330{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400331 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500332 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900333 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500334
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400335 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500336}
337
Steven Rostedt493762f2009-03-23 17:12:36 -0400338#ifdef CONFIG_FUNCTION_PROFILER
339struct ftrace_profile {
340 struct hlist_node node;
341 unsigned long ip;
342 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400343#ifdef CONFIG_FUNCTION_GRAPH_TRACER
344 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400345 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400346#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400347};
348
349struct ftrace_profile_page {
350 struct ftrace_profile_page *next;
351 unsigned long index;
352 struct ftrace_profile records[];
353};
354
Steven Rostedtcafb1682009-03-24 20:50:39 -0400355struct ftrace_profile_stat {
356 atomic_t disabled;
357 struct hlist_head *hash;
358 struct ftrace_profile_page *pages;
359 struct ftrace_profile_page *start;
360 struct tracer_stat stat;
361};
362
Steven Rostedt493762f2009-03-23 17:12:36 -0400363#define PROFILE_RECORDS_SIZE \
364 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
365
366#define PROFILES_PER_PAGE \
367 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
368
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400369static int ftrace_profile_bits __read_mostly;
370static int ftrace_profile_enabled __read_mostly;
371
372/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400373static DEFINE_MUTEX(ftrace_profile_lock);
374
Steven Rostedtcafb1682009-03-24 20:50:39 -0400375static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400376
377#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
378
Steven Rostedt493762f2009-03-23 17:12:36 -0400379static void *
380function_stat_next(void *v, int idx)
381{
382 struct ftrace_profile *rec = v;
383 struct ftrace_profile_page *pg;
384
385 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
386
387 again:
Li Zefan0296e422009-06-26 11:15:37 +0800388 if (idx != 0)
389 rec++;
390
Steven Rostedt493762f2009-03-23 17:12:36 -0400391 if ((void *)rec >= (void *)&pg->records[pg->index]) {
392 pg = pg->next;
393 if (!pg)
394 return NULL;
395 rec = &pg->records[0];
396 if (!rec->counter)
397 goto again;
398 }
399
400 return rec;
401}
402
403static void *function_stat_start(struct tracer_stat *trace)
404{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400405 struct ftrace_profile_stat *stat =
406 container_of(trace, struct ftrace_profile_stat, stat);
407
408 if (!stat || !stat->start)
409 return NULL;
410
411 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400412}
413
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400414#ifdef CONFIG_FUNCTION_GRAPH_TRACER
415/* function graph compares on total time */
416static int function_stat_cmp(void *p1, void *p2)
417{
418 struct ftrace_profile *a = p1;
419 struct ftrace_profile *b = p2;
420
421 if (a->time < b->time)
422 return -1;
423 if (a->time > b->time)
424 return 1;
425 else
426 return 0;
427}
428#else
429/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400430static int function_stat_cmp(void *p1, void *p2)
431{
432 struct ftrace_profile *a = p1;
433 struct ftrace_profile *b = p2;
434
435 if (a->counter < b->counter)
436 return -1;
437 if (a->counter > b->counter)
438 return 1;
439 else
440 return 0;
441}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400442#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400443
444static int function_stat_headers(struct seq_file *m)
445{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400446#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400447 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400448 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400449 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400450 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400451#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400452 seq_printf(m, " Function Hit\n"
453 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400454#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400455 return 0;
456}
457
458static int function_stat_show(struct seq_file *m, void *v)
459{
460 struct ftrace_profile *rec = v;
461 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800462 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400463#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400464 static struct trace_seq s;
465 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400466 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400467#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800468 mutex_lock(&ftrace_profile_lock);
469
470 /* we raced with function_profile_reset() */
471 if (unlikely(rec->counter == 0)) {
472 ret = -EBUSY;
473 goto out;
474 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400475
476 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400477 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400478
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400479#ifdef CONFIG_FUNCTION_GRAPH_TRACER
480 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400481 avg = rec->time;
482 do_div(avg, rec->counter);
483
Chase Douglase330b3b2010-04-26 14:02:05 -0400484 /* Sample standard deviation (s^2) */
485 if (rec->counter <= 1)
486 stddev = 0;
487 else {
488 stddev = rec->time_squared - rec->counter * avg * avg;
489 /*
490 * Divide only 1000 for ns^2 -> us^2 conversion.
491 * trace_print_graph_duration will divide 1000 again.
492 */
493 do_div(stddev, (rec->counter - 1) * 1000);
494 }
495
Steven Rostedt34886c82009-03-25 21:00:47 -0400496 trace_seq_init(&s);
497 trace_print_graph_duration(rec->time, &s);
498 trace_seq_puts(&s, " ");
499 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400500 trace_seq_puts(&s, " ");
501 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400502 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400503#endif
504 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800505out:
506 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400507
Li Zefan3aaba202010-08-23 16:50:12 +0800508 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400509}
510
Steven Rostedtcafb1682009-03-24 20:50:39 -0400511static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400512{
513 struct ftrace_profile_page *pg;
514
Steven Rostedtcafb1682009-03-24 20:50:39 -0400515 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400516
517 while (pg) {
518 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
519 pg->index = 0;
520 pg = pg->next;
521 }
522
Steven Rostedtcafb1682009-03-24 20:50:39 -0400523 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400524 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
525}
526
Steven Rostedtcafb1682009-03-24 20:50:39 -0400527int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400528{
529 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400530 int functions;
531 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400532 int i;
533
534 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400535 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400536 return 0;
537
Steven Rostedtcafb1682009-03-24 20:50:39 -0400538 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
539 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400540 return -ENOMEM;
541
Steven Rostedt318e0a72009-03-25 20:06:34 -0400542#ifdef CONFIG_DYNAMIC_FTRACE
543 functions = ftrace_update_tot_cnt;
544#else
545 /*
546 * We do not know the number of functions that exist because
547 * dynamic tracing is what counts them. With past experience
548 * we have around 20K functions. That should be more than enough.
549 * It is highly unlikely we will execute every function in
550 * the kernel.
551 */
552 functions = 20000;
553#endif
554
Steven Rostedtcafb1682009-03-24 20:50:39 -0400555 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400556
Steven Rostedt318e0a72009-03-25 20:06:34 -0400557 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
558
559 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400560 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400561 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400562 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400563 pg = pg->next;
564 }
565
566 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400567
568 out_free:
569 pg = stat->start;
570 while (pg) {
571 unsigned long tmp = (unsigned long)pg;
572
573 pg = pg->next;
574 free_page(tmp);
575 }
576
577 free_page((unsigned long)stat->pages);
578 stat->pages = NULL;
579 stat->start = NULL;
580
581 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400582}
583
Steven Rostedtcafb1682009-03-24 20:50:39 -0400584static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400585{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400586 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400587 int size;
588
Steven Rostedtcafb1682009-03-24 20:50:39 -0400589 stat = &per_cpu(ftrace_profile_stats, cpu);
590
591 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400592 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400593 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400594 return 0;
595 }
596
597 /*
598 * We are profiling all functions, but usually only a few thousand
599 * functions are hit. We'll make a hash of 1024 items.
600 */
601 size = FTRACE_PROFILE_HASH_SIZE;
602
Steven Rostedtcafb1682009-03-24 20:50:39 -0400603 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400604
Steven Rostedtcafb1682009-03-24 20:50:39 -0400605 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400606 return -ENOMEM;
607
Steven Rostedtcafb1682009-03-24 20:50:39 -0400608 if (!ftrace_profile_bits) {
609 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400610
Steven Rostedtcafb1682009-03-24 20:50:39 -0400611 for (; size; size >>= 1)
612 ftrace_profile_bits++;
613 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400614
Steven Rostedt318e0a72009-03-25 20:06:34 -0400615 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400616 if (ftrace_profile_pages_init(stat) < 0) {
617 kfree(stat->hash);
618 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400619 return -ENOMEM;
620 }
621
622 return 0;
623}
624
Steven Rostedtcafb1682009-03-24 20:50:39 -0400625static int ftrace_profile_init(void)
626{
627 int cpu;
628 int ret = 0;
629
630 for_each_online_cpu(cpu) {
631 ret = ftrace_profile_init_cpu(cpu);
632 if (ret)
633 break;
634 }
635
636 return ret;
637}
638
Steven Rostedt493762f2009-03-23 17:12:36 -0400639/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400640static struct ftrace_profile *
641ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400642{
643 struct ftrace_profile *rec;
644 struct hlist_head *hhd;
645 struct hlist_node *n;
646 unsigned long key;
647
648 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400649 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400650
651 if (hlist_empty(hhd))
652 return NULL;
653
654 hlist_for_each_entry_rcu(rec, n, hhd, node) {
655 if (rec->ip == ip)
656 return rec;
657 }
658
659 return NULL;
660}
661
Steven Rostedtcafb1682009-03-24 20:50:39 -0400662static void ftrace_add_profile(struct ftrace_profile_stat *stat,
663 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400664{
665 unsigned long key;
666
667 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400668 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400669}
670
Steven Rostedt318e0a72009-03-25 20:06:34 -0400671/*
672 * The memory is already allocated, this simply finds a new record to use.
673 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400674static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400675ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400676{
677 struct ftrace_profile *rec = NULL;
678
Steven Rostedt318e0a72009-03-25 20:06:34 -0400679 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400680 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400681 goto out;
682
Steven Rostedt493762f2009-03-23 17:12:36 -0400683 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400684 * Try to find the function again since an NMI
685 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400686 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400687 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400688 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400689 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400690
Steven Rostedtcafb1682009-03-24 20:50:39 -0400691 if (stat->pages->index == PROFILES_PER_PAGE) {
692 if (!stat->pages->next)
693 goto out;
694 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400695 }
696
Steven Rostedtcafb1682009-03-24 20:50:39 -0400697 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400698 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400699 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400700
Steven Rostedt493762f2009-03-23 17:12:36 -0400701 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400702 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400703
704 return rec;
705}
706
Steven Rostedt493762f2009-03-23 17:12:36 -0400707static void
708function_profile_call(unsigned long ip, unsigned long parent_ip)
709{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400710 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400711 struct ftrace_profile *rec;
712 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400713
714 if (!ftrace_profile_enabled)
715 return;
716
Steven Rostedt493762f2009-03-23 17:12:36 -0400717 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400718
719 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400720 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400721 goto out;
722
723 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400724 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400725 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400726 if (!rec)
727 goto out;
728 }
729
730 rec->counter++;
731 out:
732 local_irq_restore(flags);
733}
734
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400735#ifdef CONFIG_FUNCTION_GRAPH_TRACER
736static int profile_graph_entry(struct ftrace_graph_ent *trace)
737{
738 function_profile_call(trace->func, 0);
739 return 1;
740}
741
742static void profile_graph_return(struct ftrace_graph_ret *trace)
743{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400744 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400745 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400746 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400747 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400748
749 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400751 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400752 goto out;
753
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400754 /* If the calltime was zero'd ignore it */
755 if (!trace->calltime)
756 goto out;
757
Steven Rostedta2a16d62009-03-24 23:17:58 -0400758 calltime = trace->rettime - trace->calltime;
759
760 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
761 int index;
762
763 index = trace->depth;
764
765 /* Append this call time to the parent time to subtract */
766 if (index)
767 current->ret_stack[index - 1].subtime += calltime;
768
769 if (current->ret_stack[index].subtime < calltime)
770 calltime -= current->ret_stack[index].subtime;
771 else
772 calltime = 0;
773 }
774
Steven Rostedtcafb1682009-03-24 20:50:39 -0400775 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400776 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400777 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400778 rec->time_squared += calltime * calltime;
779 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400780
Steven Rostedtcafb1682009-03-24 20:50:39 -0400781 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400782 local_irq_restore(flags);
783}
784
785static int register_ftrace_profiler(void)
786{
787 return register_ftrace_graph(&profile_graph_return,
788 &profile_graph_entry);
789}
790
791static void unregister_ftrace_profiler(void)
792{
793 unregister_ftrace_graph();
794}
795#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100796static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400797 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400798};
799
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400800static int register_ftrace_profiler(void)
801{
802 return register_ftrace_function(&ftrace_profile_ops);
803}
804
805static void unregister_ftrace_profiler(void)
806{
807 unregister_ftrace_function(&ftrace_profile_ops);
808}
809#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
810
Steven Rostedt493762f2009-03-23 17:12:36 -0400811static ssize_t
812ftrace_profile_write(struct file *filp, const char __user *ubuf,
813 size_t cnt, loff_t *ppos)
814{
815 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400816 int ret;
817
Peter Huewe22fe9b52011-06-07 21:58:27 +0200818 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
819 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400820 return ret;
821
822 val = !!val;
823
824 mutex_lock(&ftrace_profile_lock);
825 if (ftrace_profile_enabled ^ val) {
826 if (val) {
827 ret = ftrace_profile_init();
828 if (ret < 0) {
829 cnt = ret;
830 goto out;
831 }
832
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400833 ret = register_ftrace_profiler();
834 if (ret < 0) {
835 cnt = ret;
836 goto out;
837 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400838 ftrace_profile_enabled = 1;
839 } else {
840 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400841 /*
842 * unregister_ftrace_profiler calls stop_machine
843 * so this acts like an synchronize_sched.
844 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400845 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400846 }
847 }
848 out:
849 mutex_unlock(&ftrace_profile_lock);
850
Jiri Olsacf8517c2009-10-23 19:36:16 -0400851 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400852
853 return cnt;
854}
855
856static ssize_t
857ftrace_profile_read(struct file *filp, char __user *ubuf,
858 size_t cnt, loff_t *ppos)
859{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400860 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400861 int r;
862
863 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
864 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
865}
866
867static const struct file_operations ftrace_profile_fops = {
868 .open = tracing_open_generic,
869 .read = ftrace_profile_read,
870 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200871 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400872};
873
Steven Rostedtcafb1682009-03-24 20:50:39 -0400874/* used to initialize the real stat files */
875static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400876 .name = "functions",
877 .stat_start = function_stat_start,
878 .stat_next = function_stat_next,
879 .stat_cmp = function_stat_cmp,
880 .stat_headers = function_stat_headers,
881 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400882};
883
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400884static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400885{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400886 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400887 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400888 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400889 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400890 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400891
Steven Rostedtcafb1682009-03-24 20:50:39 -0400892 for_each_possible_cpu(cpu) {
893 stat = &per_cpu(ftrace_profile_stats, cpu);
894
895 /* allocate enough for function name + cpu number */
896 name = kmalloc(32, GFP_KERNEL);
897 if (!name) {
898 /*
899 * The files created are permanent, if something happens
900 * we still do not free memory.
901 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400902 WARN(1,
903 "Could not allocate stat file for cpu %d\n",
904 cpu);
905 return;
906 }
907 stat->stat = function_stats;
908 snprintf(name, 32, "function%d", cpu);
909 stat->stat.name = name;
910 ret = register_stat_tracer(&stat->stat);
911 if (ret) {
912 WARN(1,
913 "Could not register function stat for cpu %d\n",
914 cpu);
915 kfree(name);
916 return;
917 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400918 }
919
920 entry = debugfs_create_file("function_profile_enabled", 0644,
921 d_tracer, NULL, &ftrace_profile_fops);
922 if (!entry)
923 pr_warning("Could not create debugfs "
924 "'function_profile_enabled' entry\n");
925}
926
927#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400928static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400929{
930}
931#endif /* CONFIG_FUNCTION_PROFILER */
932
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100933static struct pid * const ftrace_swapper_pid = &init_struct_pid;
934
Steven Rostedt3d083392008-05-12 21:20:42 +0200935#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100936
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400937#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400938# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400939#endif
940
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500941static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
942
Steven Rostedtb6887d72009-02-17 12:32:04 -0500943struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500944 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500945 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500946 unsigned long flags;
947 unsigned long ip;
948 void *data;
949 struct rcu_head rcu;
950};
951
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400952struct ftrace_func_entry {
953 struct hlist_node hlist;
954 unsigned long ip;
955};
956
957struct ftrace_hash {
958 unsigned long size_bits;
959 struct hlist_head *buckets;
960 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -0400961 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400962};
963
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400964/*
965 * We make these constant because no one should touch them,
966 * but they are used as the default "empty hash", to avoid allocating
967 * it all the time. These are in a read only section such that if
968 * anyone does try to modify it, it will cause an exception.
969 */
970static const struct hlist_head empty_buckets[1];
971static const struct ftrace_hash empty_hash = {
972 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400973};
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400974#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +0200975
Steven Rostedt2b499382011-05-03 22:49:52 -0400976static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -0400977 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400978 .notrace_hash = EMPTY_HASH,
979 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -0400980};
981
Steven Rostedt41c52c02008-05-22 11:46:33 -0400982static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200983
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200984struct ftrace_page {
985 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -0500986 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500987 int index;
Steven Rostedta7900872011-12-16 16:23:44 -0500988 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -0700989};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200990
Steven Rostedt85ae32a2011-12-16 16:30:31 -0500991static struct ftrace_page *ftrace_new_pgs;
992
Steven Rostedta7900872011-12-16 16:23:44 -0500993#define ENTRY_SIZE sizeof(struct dyn_ftrace)
994#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200995
996/* estimate from running different kernels */
997#define NR_TO_INIT 10000
998
999static struct ftrace_page *ftrace_pages_start;
1000static struct ftrace_page *ftrace_pages;
1001
Steven Rostedt06a51d92011-12-19 19:07:36 -05001002static bool ftrace_hash_empty(struct ftrace_hash *hash)
1003{
1004 return !hash || !hash->count;
1005}
1006
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001007static struct ftrace_func_entry *
1008ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1009{
1010 unsigned long key;
1011 struct ftrace_func_entry *entry;
1012 struct hlist_head *hhd;
1013 struct hlist_node *n;
1014
Steven Rostedt06a51d92011-12-19 19:07:36 -05001015 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001016 return NULL;
1017
1018 if (hash->size_bits > 0)
1019 key = hash_long(ip, hash->size_bits);
1020 else
1021 key = 0;
1022
1023 hhd = &hash->buckets[key];
1024
1025 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1026 if (entry->ip == ip)
1027 return entry;
1028 }
1029 return NULL;
1030}
1031
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001032static void __add_hash_entry(struct ftrace_hash *hash,
1033 struct ftrace_func_entry *entry)
1034{
1035 struct hlist_head *hhd;
1036 unsigned long key;
1037
1038 if (hash->size_bits)
1039 key = hash_long(entry->ip, hash->size_bits);
1040 else
1041 key = 0;
1042
1043 hhd = &hash->buckets[key];
1044 hlist_add_head(&entry->hlist, hhd);
1045 hash->count++;
1046}
1047
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001048static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1049{
1050 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001051
1052 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1053 if (!entry)
1054 return -ENOMEM;
1055
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001056 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001057 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001058
1059 return 0;
1060}
1061
1062static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001063free_hash_entry(struct ftrace_hash *hash,
1064 struct ftrace_func_entry *entry)
1065{
1066 hlist_del(&entry->hlist);
1067 kfree(entry);
1068 hash->count--;
1069}
1070
1071static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001072remove_hash_entry(struct ftrace_hash *hash,
1073 struct ftrace_func_entry *entry)
1074{
1075 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001076 hash->count--;
1077}
1078
1079static void ftrace_hash_clear(struct ftrace_hash *hash)
1080{
1081 struct hlist_head *hhd;
1082 struct hlist_node *tp, *tn;
1083 struct ftrace_func_entry *entry;
1084 int size = 1 << hash->size_bits;
1085 int i;
1086
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001087 if (!hash->count)
1088 return;
1089
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001090 for (i = 0; i < size; i++) {
1091 hhd = &hash->buckets[i];
1092 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001093 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001094 }
1095 FTRACE_WARN_ON(hash->count);
1096}
1097
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001098static void free_ftrace_hash(struct ftrace_hash *hash)
1099{
1100 if (!hash || hash == EMPTY_HASH)
1101 return;
1102 ftrace_hash_clear(hash);
1103 kfree(hash->buckets);
1104 kfree(hash);
1105}
1106
Steven Rostedt07fd5512011-05-05 18:03:47 -04001107static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1108{
1109 struct ftrace_hash *hash;
1110
1111 hash = container_of(rcu, struct ftrace_hash, rcu);
1112 free_ftrace_hash(hash);
1113}
1114
1115static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1116{
1117 if (!hash || hash == EMPTY_HASH)
1118 return;
1119 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1120}
1121
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001122static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1123{
1124 struct ftrace_hash *hash;
1125 int size;
1126
1127 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1128 if (!hash)
1129 return NULL;
1130
1131 size = 1 << size_bits;
1132 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1133
1134 if (!hash->buckets) {
1135 kfree(hash);
1136 return NULL;
1137 }
1138
1139 hash->size_bits = size_bits;
1140
1141 return hash;
1142}
1143
1144static struct ftrace_hash *
1145alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1146{
1147 struct ftrace_func_entry *entry;
1148 struct ftrace_hash *new_hash;
1149 struct hlist_node *tp;
1150 int size;
1151 int ret;
1152 int i;
1153
1154 new_hash = alloc_ftrace_hash(size_bits);
1155 if (!new_hash)
1156 return NULL;
1157
1158 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001159 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001160 return new_hash;
1161
1162 size = 1 << hash->size_bits;
1163 for (i = 0; i < size; i++) {
1164 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1165 ret = add_hash_entry(new_hash, entry->ip);
1166 if (ret < 0)
1167 goto free_hash;
1168 }
1169 }
1170
1171 FTRACE_WARN_ON(new_hash->count != hash->count);
1172
1173 return new_hash;
1174
1175 free_hash:
1176 free_ftrace_hash(new_hash);
1177 return NULL;
1178}
1179
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001180static void
1181ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1182static void
1183ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1184
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001185static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001186ftrace_hash_move(struct ftrace_ops *ops, int enable,
1187 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001188{
1189 struct ftrace_func_entry *entry;
1190 struct hlist_node *tp, *tn;
1191 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001192 struct ftrace_hash *old_hash;
1193 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001194 unsigned long key;
1195 int size = src->count;
1196 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001197 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001198 int i;
1199
1200 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001201 * Remove the current set, update the hash and add
1202 * them back.
1203 */
1204 ftrace_hash_rec_disable(ops, enable);
1205
1206 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001207 * If the new source is empty, just free dst and assign it
1208 * the empty_hash.
1209 */
1210 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001211 free_ftrace_hash_rcu(*dst);
1212 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001213 /* still need to update the function records */
1214 ret = 0;
1215 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001216 }
1217
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001218 /*
1219 * Make the hash size about 1/2 the # found
1220 */
1221 for (size /= 2; size; size >>= 1)
1222 bits++;
1223
1224 /* Don't allocate too much */
1225 if (bits > FTRACE_HASH_MAX_BITS)
1226 bits = FTRACE_HASH_MAX_BITS;
1227
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001228 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001229 new_hash = alloc_ftrace_hash(bits);
1230 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001231 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001232
1233 size = 1 << src->size_bits;
1234 for (i = 0; i < size; i++) {
1235 hhd = &src->buckets[i];
1236 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1237 if (bits > 0)
1238 key = hash_long(entry->ip, bits);
1239 else
1240 key = 0;
1241 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001242 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001243 }
1244 }
1245
Steven Rostedt07fd5512011-05-05 18:03:47 -04001246 old_hash = *dst;
1247 rcu_assign_pointer(*dst, new_hash);
1248 free_ftrace_hash_rcu(old_hash);
1249
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001250 ret = 0;
1251 out:
1252 /*
1253 * Enable regardless of ret:
1254 * On success, we enable the new hash.
1255 * On failure, we re-enable the original hash.
1256 */
1257 ftrace_hash_rec_enable(ops, enable);
1258
1259 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001260}
1261
Steven Rostedt265c8312009-02-13 12:43:56 -05001262/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001263 * Test the hashes for this ops to see if we want to call
1264 * the ops->func or not.
1265 *
1266 * It's a match if the ip is in the ops->filter_hash or
1267 * the filter_hash does not exist or is empty,
1268 * AND
1269 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001270 *
1271 * This needs to be called with preemption disabled as
1272 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001273 */
1274static int
1275ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1276{
1277 struct ftrace_hash *filter_hash;
1278 struct ftrace_hash *notrace_hash;
1279 int ret;
1280
Steven Rostedtb8489142011-05-04 09:27:52 -04001281 filter_hash = rcu_dereference_raw(ops->filter_hash);
1282 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1283
Steven Rostedt06a51d92011-12-19 19:07:36 -05001284 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001285 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001286 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001287 !ftrace_lookup_ip(notrace_hash, ip)))
1288 ret = 1;
1289 else
1290 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001291
1292 return ret;
1293}
1294
1295/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001296 * This is a double for. Do not use 'break' to break out of the loop,
1297 * you must use a goto.
1298 */
1299#define do_for_each_ftrace_rec(pg, rec) \
1300 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1301 int _____i; \
1302 for (_____i = 0; _____i < pg->index; _____i++) { \
1303 rec = &pg->records[_____i];
1304
1305#define while_for_each_ftrace_rec() \
1306 } \
1307 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301308
Steven Rostedt5855fea2011-12-16 19:27:42 -05001309
1310static int ftrace_cmp_recs(const void *a, const void *b)
1311{
1312 const struct dyn_ftrace *reca = a;
1313 const struct dyn_ftrace *recb = b;
1314
1315 if (reca->ip > recb->ip)
1316 return 1;
1317 if (reca->ip < recb->ip)
1318 return -1;
1319 return 0;
1320}
1321
Steven Rostedtc88fd862011-08-16 09:53:39 -04001322/**
1323 * ftrace_location - return true if the ip giving is a traced location
1324 * @ip: the instruction pointer to check
1325 *
1326 * Returns 1 if @ip given is a pointer to a ftrace location.
1327 * That is, the instruction that is either a NOP or call to
1328 * the function tracer. It checks the ftrace internal tables to
1329 * determine if the address belongs or not.
1330 */
1331int ftrace_location(unsigned long ip)
1332{
1333 struct ftrace_page *pg;
1334 struct dyn_ftrace *rec;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001335 struct dyn_ftrace key;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001336
Steven Rostedt5855fea2011-12-16 19:27:42 -05001337 key.ip = ip;
1338
1339 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1340 rec = bsearch(&key, pg->records, pg->index,
1341 sizeof(struct dyn_ftrace),
1342 ftrace_cmp_recs);
1343 if (rec)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001344 return 1;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001345 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001346
1347 return 0;
1348}
1349
Steven Rostedted926f92011-05-03 13:25:24 -04001350static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1351 int filter_hash,
1352 bool inc)
1353{
1354 struct ftrace_hash *hash;
1355 struct ftrace_hash *other_hash;
1356 struct ftrace_page *pg;
1357 struct dyn_ftrace *rec;
1358 int count = 0;
1359 int all = 0;
1360
1361 /* Only update if the ops has been registered */
1362 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1363 return;
1364
1365 /*
1366 * In the filter_hash case:
1367 * If the count is zero, we update all records.
1368 * Otherwise we just update the items in the hash.
1369 *
1370 * In the notrace_hash case:
1371 * We enable the update in the hash.
1372 * As disabling notrace means enabling the tracing,
1373 * and enabling notrace means disabling, the inc variable
1374 * gets inversed.
1375 */
1376 if (filter_hash) {
1377 hash = ops->filter_hash;
1378 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001379 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001380 all = 1;
1381 } else {
1382 inc = !inc;
1383 hash = ops->notrace_hash;
1384 other_hash = ops->filter_hash;
1385 /*
1386 * If the notrace hash has no items,
1387 * then there's nothing to do.
1388 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001389 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001390 return;
1391 }
1392
1393 do_for_each_ftrace_rec(pg, rec) {
1394 int in_other_hash = 0;
1395 int in_hash = 0;
1396 int match = 0;
1397
1398 if (all) {
1399 /*
1400 * Only the filter_hash affects all records.
1401 * Update if the record is not in the notrace hash.
1402 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001403 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001404 match = 1;
1405 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001406 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1407 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001408
1409 /*
1410 *
1411 */
1412 if (filter_hash && in_hash && !in_other_hash)
1413 match = 1;
1414 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001415 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001416 match = 1;
1417 }
1418 if (!match)
1419 continue;
1420
1421 if (inc) {
1422 rec->flags++;
1423 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1424 return;
1425 } else {
1426 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1427 return;
1428 rec->flags--;
1429 }
1430 count++;
1431 /* Shortcut, if we handled all records, we are done. */
1432 if (!all && count == hash->count)
1433 return;
1434 } while_for_each_ftrace_rec();
1435}
1436
1437static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1438 int filter_hash)
1439{
1440 __ftrace_hash_rec_update(ops, filter_hash, 0);
1441}
1442
1443static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1444 int filter_hash)
1445{
1446 __ftrace_hash_rec_update(ops, filter_hash, 1);
1447}
1448
Ingo Molnare309b412008-05-12 21:20:51 +02001449static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001450{
Steven Rostedta7900872011-12-16 16:23:44 -05001451 if (ftrace_pages->index == ftrace_pages->size) {
1452 /* We should have allocated enough */
1453 if (WARN_ON(!ftrace_pages->next))
1454 return NULL;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001455 ftrace_pages = ftrace_pages->next;
1456 }
1457
1458 return &ftrace_pages->records[ftrace_pages->index++];
1459}
1460
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001461static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001462ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001463{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001464 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001465
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001466 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001467 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001468
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001469 rec = ftrace_alloc_dyn_node(ip);
1470 if (!rec)
1471 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001472
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001473 rec->ip = ip;
Steven Rostedt3d083392008-05-12 21:20:42 +02001474
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001475 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001476}
1477
Steven Rostedt05736a42008-09-22 14:55:47 -07001478static void print_ip_ins(const char *fmt, unsigned char *p)
1479{
1480 int i;
1481
1482 printk(KERN_CONT "%s", fmt);
1483
1484 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1485 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1486}
1487
Steven Rostedtc88fd862011-08-16 09:53:39 -04001488/**
1489 * ftrace_bug - report and shutdown function tracer
1490 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1491 * @ip: The address that failed
1492 *
1493 * The arch code that enables or disables the function tracing
1494 * can call ftrace_bug() when it has detected a problem in
1495 * modifying the code. @failed should be one of either:
1496 * EFAULT - if the problem happens on reading the @ip address
1497 * EINVAL - if what is read at @ip is not what was expected
1498 * EPERM - if the problem happens on writting to the @ip address
1499 */
1500void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001501{
1502 switch (failed) {
1503 case -EFAULT:
1504 FTRACE_WARN_ON_ONCE(1);
1505 pr_info("ftrace faulted on modifying ");
1506 print_ip_sym(ip);
1507 break;
1508 case -EINVAL:
1509 FTRACE_WARN_ON_ONCE(1);
1510 pr_info("ftrace failed to modify ");
1511 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001512 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001513 printk(KERN_CONT "\n");
1514 break;
1515 case -EPERM:
1516 FTRACE_WARN_ON_ONCE(1);
1517 pr_info("ftrace faulted on writing ");
1518 print_ip_sym(ip);
1519 break;
1520 default:
1521 FTRACE_WARN_ON_ONCE(1);
1522 pr_info("ftrace faulted on unknown error ");
1523 print_ip_sym(ip);
1524 }
1525}
1526
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001527
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001528/* Return 1 if the address range is reserved for ftrace */
1529int ftrace_text_reserved(void *start, void *end)
1530{
1531 struct dyn_ftrace *rec;
1532 struct ftrace_page *pg;
1533
1534 do_for_each_ftrace_rec(pg, rec) {
1535 if (rec->ip <= (unsigned long)end &&
1536 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1537 return 1;
1538 } while_for_each_ftrace_rec();
1539 return 0;
1540}
1541
Steven Rostedtc88fd862011-08-16 09:53:39 -04001542static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001543{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001544 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001545
Steven Rostedt982c3502008-11-15 16:31:41 -05001546 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001547 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001548 *
Steven Rostedted926f92011-05-03 13:25:24 -04001549 * If the record has a ref count, then we need to enable it
1550 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001551 *
Steven Rostedted926f92011-05-03 13:25:24 -04001552 * Otherwise we make sure its disabled.
1553 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001554 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001555 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001556 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001557 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001558 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001559
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001560 /* If the state of this record hasn't changed, then do nothing */
1561 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001562 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001563
1564 if (flag) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001565 if (update)
1566 rec->flags |= FTRACE_FL_ENABLED;
1567 return FTRACE_UPDATE_MAKE_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001568 }
1569
Steven Rostedtc88fd862011-08-16 09:53:39 -04001570 if (update)
1571 rec->flags &= ~FTRACE_FL_ENABLED;
1572
1573 return FTRACE_UPDATE_MAKE_NOP;
1574}
1575
1576/**
1577 * ftrace_update_record, set a record that now is tracing or not
1578 * @rec: the record to update
1579 * @enable: set to 1 if the record is tracing, zero to force disable
1580 *
1581 * The records that represent all functions that can be traced need
1582 * to be updated when tracing has been enabled.
1583 */
1584int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1585{
1586 return ftrace_check_record(rec, enable, 1);
1587}
1588
1589/**
1590 * ftrace_test_record, check if the record has been enabled or not
1591 * @rec: the record to test
1592 * @enable: set to 1 to check if enabled, 0 if it is disabled
1593 *
1594 * The arch code may need to test if a record is already set to
1595 * tracing to determine how to modify the function code that it
1596 * represents.
1597 */
1598int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1599{
1600 return ftrace_check_record(rec, enable, 0);
1601}
1602
1603static int
1604__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1605{
1606 unsigned long ftrace_addr;
1607 int ret;
1608
1609 ftrace_addr = (unsigned long)FTRACE_ADDR;
1610
1611 ret = ftrace_update_record(rec, enable);
1612
1613 switch (ret) {
1614 case FTRACE_UPDATE_IGNORE:
1615 return 0;
1616
1617 case FTRACE_UPDATE_MAKE_CALL:
1618 return ftrace_make_call(rec, ftrace_addr);
1619
1620 case FTRACE_UPDATE_MAKE_NOP:
1621 return ftrace_make_nop(NULL, rec, ftrace_addr);
1622 }
1623
1624 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001625}
1626
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001627static void ftrace_replace_code(int update)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001628{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001629 struct dyn_ftrace *rec;
1630 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001631 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001632
Steven Rostedt45a4a232011-04-21 23:16:46 -04001633 if (unlikely(ftrace_disabled))
1634 return;
1635
Steven Rostedt265c8312009-02-13 12:43:56 -05001636 do_for_each_ftrace_rec(pg, rec) {
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001637 failed = __ftrace_replace_code(rec, update);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001638 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001639 ftrace_bug(failed, rec->ip);
1640 /* Stop processing */
1641 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001642 }
1643 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001644}
1645
Steven Rostedtc88fd862011-08-16 09:53:39 -04001646struct ftrace_rec_iter {
1647 struct ftrace_page *pg;
1648 int index;
1649};
1650
1651/**
1652 * ftrace_rec_iter_start, start up iterating over traced functions
1653 *
1654 * Returns an iterator handle that is used to iterate over all
1655 * the records that represent address locations where functions
1656 * are traced.
1657 *
1658 * May return NULL if no records are available.
1659 */
1660struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1661{
1662 /*
1663 * We only use a single iterator.
1664 * Protected by the ftrace_lock mutex.
1665 */
1666 static struct ftrace_rec_iter ftrace_rec_iter;
1667 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1668
1669 iter->pg = ftrace_pages_start;
1670 iter->index = 0;
1671
1672 /* Could have empty pages */
1673 while (iter->pg && !iter->pg->index)
1674 iter->pg = iter->pg->next;
1675
1676 if (!iter->pg)
1677 return NULL;
1678
1679 return iter;
1680}
1681
1682/**
1683 * ftrace_rec_iter_next, get the next record to process.
1684 * @iter: The handle to the iterator.
1685 *
1686 * Returns the next iterator after the given iterator @iter.
1687 */
1688struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1689{
1690 iter->index++;
1691
1692 if (iter->index >= iter->pg->index) {
1693 iter->pg = iter->pg->next;
1694 iter->index = 0;
1695
1696 /* Could have empty pages */
1697 while (iter->pg && !iter->pg->index)
1698 iter->pg = iter->pg->next;
1699 }
1700
1701 if (!iter->pg)
1702 return NULL;
1703
1704 return iter;
1705}
1706
1707/**
1708 * ftrace_rec_iter_record, get the record at the iterator location
1709 * @iter: The current iterator location
1710 *
1711 * Returns the record that the current @iter is at.
1712 */
1713struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1714{
1715 return &iter->pg->records[iter->index];
1716}
1717
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301718static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001719ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001720{
1721 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001722 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001723
1724 ip = rec->ip;
1725
Steven Rostedt45a4a232011-04-21 23:16:46 -04001726 if (unlikely(ftrace_disabled))
1727 return 0;
1728
Shaohua Li25aac9d2009-01-09 11:29:40 +08001729 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001730 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001731 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301732 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001733 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301734 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001735}
1736
Steven Rostedt000ab692009-02-17 13:35:06 -05001737/*
1738 * archs can override this function if they must do something
1739 * before the modifying code is performed.
1740 */
1741int __weak ftrace_arch_code_modify_prepare(void)
1742{
1743 return 0;
1744}
1745
1746/*
1747 * archs can override this function if they must do something
1748 * after the modifying code is performed.
1749 */
1750int __weak ftrace_arch_code_modify_post_process(void)
1751{
1752 return 0;
1753}
1754
Ingo Molnare309b412008-05-12 21:20:51 +02001755static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001756{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001757 int *command = data;
1758
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001759 if (*command & FTRACE_UPDATE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001760 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001761 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001762 ftrace_replace_code(0);
1763
1764 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1765 ftrace_update_ftrace_func(ftrace_trace_function);
1766
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001767 if (*command & FTRACE_START_FUNC_RET)
1768 ftrace_enable_ftrace_graph_caller();
1769 else if (*command & FTRACE_STOP_FUNC_RET)
1770 ftrace_disable_ftrace_graph_caller();
1771
Steven Rostedtc88fd862011-08-16 09:53:39 -04001772 return 0;
1773}
1774
1775/**
1776 * ftrace_run_stop_machine, go back to the stop machine method
1777 * @command: The command to tell ftrace what to do
1778 *
1779 * If an arch needs to fall back to the stop machine method, the
1780 * it can call this function.
1781 */
1782void ftrace_run_stop_machine(int command)
1783{
1784 stop_machine(__ftrace_modify_code, &command, NULL);
1785}
1786
1787/**
1788 * arch_ftrace_update_code, modify the code to trace or not trace
1789 * @command: The command that needs to be done
1790 *
1791 * Archs can override this function if it does not need to
1792 * run stop_machine() to modify code.
1793 */
1794void __weak arch_ftrace_update_code(int command)
1795{
1796 ftrace_run_stop_machine(command);
1797}
1798
1799static void ftrace_run_update_code(int command)
1800{
1801 int ret;
1802
1803 ret = ftrace_arch_code_modify_prepare();
1804 FTRACE_WARN_ON(ret);
1805 if (ret)
1806 return;
1807 /*
1808 * Do not call function tracer while we update the code.
1809 * We are in stop machine.
1810 */
1811 function_trace_stop++;
1812
1813 /*
1814 * By default we use stop_machine() to modify the code.
1815 * But archs can do what ever they want as long as it
1816 * is safe. The stop_machine() is the safest, but also
1817 * produces the most overhead.
1818 */
1819 arch_ftrace_update_code(command);
1820
Steven Rostedt6331c282011-07-13 15:11:02 -04001821#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1822 /*
1823 * For archs that call ftrace_test_stop_func(), we must
1824 * wait till after we update all the function callers
1825 * before we update the callback. This keeps different
1826 * ops that record different functions from corrupting
1827 * each other.
1828 */
1829 __ftrace_trace_function = __ftrace_trace_function_delay;
1830#endif
1831 function_trace_stop--;
1832
Steven Rostedt000ab692009-02-17 13:35:06 -05001833 ret = ftrace_arch_code_modify_post_process();
1834 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001835}
1836
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001837static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001838static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001839static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001840
1841static void ftrace_startup_enable(int command)
1842{
1843 if (saved_ftrace_func != ftrace_trace_function) {
1844 saved_ftrace_func = ftrace_trace_function;
1845 command |= FTRACE_UPDATE_TRACE_FUNC;
1846 }
1847
1848 if (!command || !ftrace_enabled)
1849 return;
1850
1851 ftrace_run_update_code(command);
1852}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001853
Steven Rostedta1cd6172011-05-23 15:24:25 -04001854static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001855{
Steven Rostedtb8489142011-05-04 09:27:52 -04001856 bool hash_enable = true;
1857
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001858 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001859 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001860
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001861 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001862 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001863
Steven Rostedtb8489142011-05-04 09:27:52 -04001864 /* ops marked global share the filter hashes */
1865 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1866 ops = &global_ops;
1867 /* Don't update hash if global is already set */
1868 if (global_start_up)
1869 hash_enable = false;
1870 global_start_up++;
1871 }
1872
Steven Rostedted926f92011-05-03 13:25:24 -04001873 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001874 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001875 ftrace_hash_rec_enable(ops, 1);
1876
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001877 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001878
1879 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001880}
1881
Steven Rostedtbd69c302011-05-03 21:55:54 -04001882static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001883{
Steven Rostedtb8489142011-05-04 09:27:52 -04001884 bool hash_disable = true;
1885
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001886 if (unlikely(ftrace_disabled))
1887 return;
1888
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001889 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001890 /*
1891 * Just warn in case of unbalance, no need to kill ftrace, it's not
1892 * critical but the ftrace_call callers may be never nopped again after
1893 * further ftrace uses.
1894 */
1895 WARN_ON_ONCE(ftrace_start_up < 0);
1896
Steven Rostedtb8489142011-05-04 09:27:52 -04001897 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1898 ops = &global_ops;
1899 global_start_up--;
1900 WARN_ON_ONCE(global_start_up < 0);
1901 /* Don't update hash if global still has users */
1902 if (global_start_up) {
1903 WARN_ON_ONCE(!ftrace_start_up);
1904 hash_disable = false;
1905 }
1906 }
1907
1908 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04001909 ftrace_hash_rec_disable(ops, 1);
1910
Steven Rostedtb8489142011-05-04 09:27:52 -04001911 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04001912 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001913
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001914 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001915
1916 if (saved_ftrace_func != ftrace_trace_function) {
1917 saved_ftrace_func = ftrace_trace_function;
1918 command |= FTRACE_UPDATE_TRACE_FUNC;
1919 }
1920
1921 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001922 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001923
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001924 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001925}
1926
Ingo Molnare309b412008-05-12 21:20:51 +02001927static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001928{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001929 if (unlikely(ftrace_disabled))
1930 return;
1931
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001932 /* Force update next time */
1933 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001934 /* ftrace_start_up is true if we want ftrace running */
1935 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001936 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001937}
1938
Ingo Molnare309b412008-05-12 21:20:51 +02001939static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001940{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001941 if (unlikely(ftrace_disabled))
1942 return;
1943
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001944 /* ftrace_start_up is true if ftrace is running */
1945 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001946 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001947}
1948
Steven Rostedt3d083392008-05-12 21:20:42 +02001949static cycle_t ftrace_update_time;
1950static unsigned long ftrace_update_cnt;
1951unsigned long ftrace_update_tot_cnt;
1952
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001953static int ops_traces_mod(struct ftrace_ops *ops)
1954{
1955 struct ftrace_hash *hash;
1956
1957 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001958 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001959}
1960
Steven Rostedt31e88902008-11-14 16:21:19 -08001961static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001962{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001963 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001964 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301965 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001966 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001967 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001968
1969 /*
1970 * When adding a module, we need to check if tracers are
1971 * currently enabled and if they are set to trace all functions.
1972 * If they are, we need to enable the module functions as well
1973 * as update the reference counts for those function records.
1974 */
1975 if (mod) {
1976 struct ftrace_ops *ops;
1977
1978 for (ops = ftrace_ops_list;
1979 ops != &ftrace_list_end; ops = ops->next) {
1980 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
1981 ops_traces_mod(ops))
1982 ref++;
1983 }
1984 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001985
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001986 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001987 ftrace_update_cnt = 0;
1988
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001989 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301990
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001991 for (i = 0; i < pg->index; i++) {
1992 /* If something went wrong, bail without enabling anything */
1993 if (unlikely(ftrace_disabled))
1994 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001995
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001996 p = &pg->records[i];
1997 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301998
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001999 /*
2000 * Do the initial record conversion from mcount jump
2001 * to the NOP instructions.
2002 */
2003 if (!ftrace_code_disable(mod, p))
2004 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002005
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002006 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002007
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002008 /*
2009 * If the tracing is enabled, go ahead and enable the record.
2010 *
2011 * The reason not to enable the record immediatelly is the
2012 * inherent check of ftrace_make_nop/ftrace_make_call for
2013 * correct previous instructions. Making first the NOP
2014 * conversion puts the module to the correct state, thus
2015 * passing the ftrace_make_call check.
2016 */
2017 if (ftrace_start_up && ref) {
2018 int failed = __ftrace_replace_code(p, 1);
2019 if (failed)
2020 ftrace_bug(failed, p->ip);
2021 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002022 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002023 }
2024
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002025 ftrace_new_pgs = NULL;
2026
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002027 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002028 ftrace_update_time = stop - start;
2029 ftrace_update_tot_cnt += ftrace_update_cnt;
2030
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002031 return 0;
2032}
2033
Steven Rostedta7900872011-12-16 16:23:44 -05002034static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002035{
Steven Rostedta7900872011-12-16 16:23:44 -05002036 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002037 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002038
Steven Rostedta7900872011-12-16 16:23:44 -05002039 if (WARN_ON(!count))
2040 return -EINVAL;
2041
2042 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002043
2044 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002045 * We want to fill as much as possible. No more than a page
2046 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002047 */
Steven Rostedta7900872011-12-16 16:23:44 -05002048 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2049 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002050
Steven Rostedta7900872011-12-16 16:23:44 -05002051 again:
2052 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2053
2054 if (!pg->records) {
2055 /* if we can't allocate this size, try something smaller */
2056 if (!order)
2057 return -ENOMEM;
2058 order >>= 1;
2059 goto again;
2060 }
2061
2062 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2063 pg->size = cnt;
2064
2065 if (cnt > count)
2066 cnt = count;
2067
2068 return cnt;
2069}
2070
2071static struct ftrace_page *
2072ftrace_allocate_pages(unsigned long num_to_init)
2073{
2074 struct ftrace_page *start_pg;
2075 struct ftrace_page *pg;
2076 int order;
2077 int cnt;
2078
2079 if (!num_to_init)
2080 return 0;
2081
2082 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2083 if (!pg)
2084 return NULL;
2085
2086 /*
2087 * Try to allocate as much as possible in one continues
2088 * location that fills in all of the space. We want to
2089 * waste as little space as possible.
2090 */
2091 for (;;) {
2092 cnt = ftrace_allocate_records(pg, num_to_init);
2093 if (cnt < 0)
2094 goto free_pages;
2095
2096 num_to_init -= cnt;
2097 if (!num_to_init)
2098 break;
2099
2100 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2101 if (!pg->next)
2102 goto free_pages;
2103
2104 pg = pg->next;
2105 }
2106
2107 return start_pg;
2108
2109 free_pages:
2110 while (start_pg) {
2111 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2112 free_pages((unsigned long)pg->records, order);
2113 start_pg = pg->next;
2114 kfree(pg);
2115 pg = start_pg;
2116 }
2117 pr_info("ftrace: FAILED to allocate memory for functions\n");
2118 return NULL;
2119}
2120
2121static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2122{
2123 int cnt;
2124
2125 if (!num_to_init) {
2126 pr_info("ftrace: No functions to be traced?\n");
2127 return -1;
2128 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002129
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002130 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002131 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002132 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002133
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002134 return 0;
2135}
2136
Steven Rostedt5072c592008-05-12 21:20:43 +02002137#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2138
2139struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002140 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002141 loff_t func_pos;
2142 struct ftrace_page *pg;
2143 struct dyn_ftrace *func;
2144 struct ftrace_func_probe *probe;
2145 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002146 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002147 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002148 int hidx;
2149 int idx;
2150 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002151};
2152
Ingo Molnare309b412008-05-12 21:20:51 +02002153static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002154t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002155{
2156 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002157 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002158 struct hlist_head *hhd;
2159
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002160 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002161 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002162
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002163 if (iter->probe)
2164 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002165 retry:
2166 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2167 return NULL;
2168
2169 hhd = &ftrace_func_hash[iter->hidx];
2170
2171 if (hlist_empty(hhd)) {
2172 iter->hidx++;
2173 hnd = NULL;
2174 goto retry;
2175 }
2176
2177 if (!hnd)
2178 hnd = hhd->first;
2179 else {
2180 hnd = hnd->next;
2181 if (!hnd) {
2182 iter->hidx++;
2183 goto retry;
2184 }
2185 }
2186
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002187 if (WARN_ON_ONCE(!hnd))
2188 return NULL;
2189
2190 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2191
2192 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002193}
2194
2195static void *t_hash_start(struct seq_file *m, loff_t *pos)
2196{
2197 struct ftrace_iterator *iter = m->private;
2198 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002199 loff_t l;
2200
Steven Rostedt69a30832011-12-19 15:21:16 -05002201 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2202 return NULL;
2203
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002204 if (iter->func_pos > *pos)
2205 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002206
Li Zefand82d6242009-06-24 09:54:54 +08002207 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002208 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002209 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002210 if (!p)
2211 break;
2212 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002213 if (!p)
2214 return NULL;
2215
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002216 /* Only set this if we have an item */
2217 iter->flags |= FTRACE_ITER_HASH;
2218
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002219 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002220}
2221
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002222static int
2223t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002224{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002225 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002226
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002227 rec = iter->probe;
2228 if (WARN_ON_ONCE(!rec))
2229 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002230
Steven Rostedt809dcf22009-02-16 23:06:01 -05002231 if (rec->ops->print)
2232 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2233
Steven Rostedtb375a112009-09-17 00:05:58 -04002234 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002235
2236 if (rec->data)
2237 seq_printf(m, ":%p", rec->data);
2238 seq_putc(m, '\n');
2239
2240 return 0;
2241}
2242
2243static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002244t_next(struct seq_file *m, void *v, loff_t *pos)
2245{
2246 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002247 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002248 struct dyn_ftrace *rec = NULL;
2249
Steven Rostedt45a4a232011-04-21 23:16:46 -04002250 if (unlikely(ftrace_disabled))
2251 return NULL;
2252
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002253 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002254 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002255
Steven Rostedt5072c592008-05-12 21:20:43 +02002256 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002257 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002258
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002259 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002260 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002261
Steven Rostedt5072c592008-05-12 21:20:43 +02002262 retry:
2263 if (iter->idx >= iter->pg->index) {
2264 if (iter->pg->next) {
2265 iter->pg = iter->pg->next;
2266 iter->idx = 0;
2267 goto retry;
2268 }
2269 } else {
2270 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05002271 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002272 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05002273
Steven Rostedt41c52c02008-05-22 11:46:33 -04002274 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002275 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2276
2277 ((iter->flags & FTRACE_ITER_ENABLED) &&
2278 !(rec->flags & ~FTRACE_FL_MASK))) {
2279
Steven Rostedt5072c592008-05-12 21:20:43 +02002280 rec = NULL;
2281 goto retry;
2282 }
2283 }
2284
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002285 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002286 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002287
2288 iter->func = rec;
2289
2290 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002291}
2292
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002293static void reset_iter_read(struct ftrace_iterator *iter)
2294{
2295 iter->pos = 0;
2296 iter->func_pos = 0;
2297 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002298}
2299
2300static void *t_start(struct seq_file *m, loff_t *pos)
2301{
2302 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002303 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002304 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002305 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002306
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002307 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002308
2309 if (unlikely(ftrace_disabled))
2310 return NULL;
2311
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002312 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002313 * If an lseek was done, then reset and start from beginning.
2314 */
2315 if (*pos < iter->pos)
2316 reset_iter_read(iter);
2317
2318 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002319 * For set_ftrace_filter reading, if we have the filter
2320 * off, we can short cut and just print out that all
2321 * functions are enabled.
2322 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002323 if (iter->flags & FTRACE_ITER_FILTER &&
2324 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002325 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002326 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002327 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002328 /* reset in case of seek/pread */
2329 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002330 return iter;
2331 }
2332
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002333 if (iter->flags & FTRACE_ITER_HASH)
2334 return t_hash_start(m, pos);
2335
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002336 /*
2337 * Unfortunately, we need to restart at ftrace_pages_start
2338 * every time we let go of the ftrace_mutex. This is because
2339 * those pointers can change without the lock.
2340 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002341 iter->pg = ftrace_pages_start;
2342 iter->idx = 0;
2343 for (l = 0; l <= *pos; ) {
2344 p = t_next(m, p, &l);
2345 if (!p)
2346 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002347 }
walimis5821e1b2008-11-15 15:19:06 +08002348
Steven Rostedt69a30832011-12-19 15:21:16 -05002349 if (!p)
2350 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002351
2352 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002353}
2354
2355static void t_stop(struct seq_file *m, void *p)
2356{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002357 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002358}
2359
2360static int t_show(struct seq_file *m, void *v)
2361{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002362 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002363 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002364
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002365 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002366 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002367
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002368 if (iter->flags & FTRACE_ITER_PRINTALL) {
2369 seq_printf(m, "#### all functions enabled ####\n");
2370 return 0;
2371 }
2372
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002373 rec = iter->func;
2374
Steven Rostedt5072c592008-05-12 21:20:43 +02002375 if (!rec)
2376 return 0;
2377
Steven Rostedt647bcd02011-05-03 14:39:21 -04002378 seq_printf(m, "%ps", (void *)rec->ip);
2379 if (iter->flags & FTRACE_ITER_ENABLED)
2380 seq_printf(m, " (%ld)",
2381 rec->flags & ~FTRACE_FL_MASK);
2382 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002383
2384 return 0;
2385}
2386
James Morris88e9d342009-09-22 16:43:43 -07002387static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002388 .start = t_start,
2389 .next = t_next,
2390 .stop = t_stop,
2391 .show = t_show,
2392};
2393
Ingo Molnare309b412008-05-12 21:20:51 +02002394static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002395ftrace_avail_open(struct inode *inode, struct file *file)
2396{
2397 struct ftrace_iterator *iter;
2398 int ret;
2399
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002400 if (unlikely(ftrace_disabled))
2401 return -ENODEV;
2402
Steven Rostedt5072c592008-05-12 21:20:43 +02002403 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2404 if (!iter)
2405 return -ENOMEM;
2406
2407 iter->pg = ftrace_pages_start;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002408 iter->ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002409
2410 ret = seq_open(file, &show_ftrace_seq_ops);
2411 if (!ret) {
2412 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002413
Steven Rostedt5072c592008-05-12 21:20:43 +02002414 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002415 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002416 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002417 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002418
2419 return ret;
2420}
2421
Steven Rostedt647bcd02011-05-03 14:39:21 -04002422static int
2423ftrace_enabled_open(struct inode *inode, struct file *file)
2424{
2425 struct ftrace_iterator *iter;
2426 int ret;
2427
2428 if (unlikely(ftrace_disabled))
2429 return -ENODEV;
2430
2431 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2432 if (!iter)
2433 return -ENOMEM;
2434
2435 iter->pg = ftrace_pages_start;
2436 iter->flags = FTRACE_ITER_ENABLED;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002437 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002438
2439 ret = seq_open(file, &show_ftrace_seq_ops);
2440 if (!ret) {
2441 struct seq_file *m = file->private_data;
2442
2443 m->private = iter;
2444 } else {
2445 kfree(iter);
2446 }
2447
2448 return ret;
2449}
2450
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002451static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002452{
Steven Rostedt52baf112009-02-14 01:15:39 -05002453 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002454 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002455 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002456}
2457
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002458/**
2459 * ftrace_regex_open - initialize function tracer filter files
2460 * @ops: The ftrace_ops that hold the hash filters
2461 * @flag: The type of filter to process
2462 * @inode: The inode, usually passed in to your open routine
2463 * @file: The file, usually passed in to your open routine
2464 *
2465 * ftrace_regex_open() initializes the filter files for the
2466 * @ops. Depending on @flag it may process the filter hash or
2467 * the notrace hash of @ops. With this called from the open
2468 * routine, you can use ftrace_filter_write() for the write
2469 * routine if @flag has FTRACE_ITER_FILTER set, or
2470 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2471 * ftrace_regex_lseek() should be used as the lseek routine, and
2472 * release must call ftrace_regex_release().
2473 */
2474int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002475ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002476 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002477{
2478 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002479 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002480 int ret = 0;
2481
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002482 if (unlikely(ftrace_disabled))
2483 return -ENODEV;
2484
Steven Rostedt5072c592008-05-12 21:20:43 +02002485 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2486 if (!iter)
2487 return -ENOMEM;
2488
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002489 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2490 kfree(iter);
2491 return -ENOMEM;
2492 }
2493
Steven Rostedtf45948e2011-05-02 12:29:25 -04002494 if (flag & FTRACE_ITER_NOTRACE)
2495 hash = ops->notrace_hash;
2496 else
2497 hash = ops->filter_hash;
2498
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002499 iter->ops = ops;
2500 iter->flags = flag;
2501
2502 if (file->f_mode & FMODE_WRITE) {
2503 mutex_lock(&ftrace_lock);
2504 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2505 mutex_unlock(&ftrace_lock);
2506
2507 if (!iter->hash) {
2508 trace_parser_put(&iter->parser);
2509 kfree(iter);
2510 return -ENOMEM;
2511 }
2512 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002513
Steven Rostedt41c52c02008-05-22 11:46:33 -04002514 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002515
Steven Rostedt5072c592008-05-12 21:20:43 +02002516 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002517 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002518 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002519
2520 if (file->f_mode & FMODE_READ) {
2521 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002522
2523 ret = seq_open(file, &show_ftrace_seq_ops);
2524 if (!ret) {
2525 struct seq_file *m = file->private_data;
2526 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002527 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002528 /* Failed */
2529 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002530 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002531 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002532 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002533 } else
2534 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002535 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002536
2537 return ret;
2538}
2539
Steven Rostedt41c52c02008-05-22 11:46:33 -04002540static int
2541ftrace_filter_open(struct inode *inode, struct file *file)
2542{
Steven Rostedt69a30832011-12-19 15:21:16 -05002543 return ftrace_regex_open(&global_ops,
2544 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2545 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002546}
2547
2548static int
2549ftrace_notrace_open(struct inode *inode, struct file *file)
2550{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002551 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002552 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002553}
2554
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002555loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002556ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02002557{
2558 loff_t ret;
2559
2560 if (file->f_mode & FMODE_READ)
2561 ret = seq_lseek(file, offset, origin);
2562 else
2563 file->f_pos = ret = 1;
2564
2565 return ret;
2566}
2567
Steven Rostedt64e7c442009-02-13 17:08:48 -05002568static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002569{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002570 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002571 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002572
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002573 switch (type) {
2574 case MATCH_FULL:
2575 if (strcmp(str, regex) == 0)
2576 matched = 1;
2577 break;
2578 case MATCH_FRONT_ONLY:
2579 if (strncmp(str, regex, len) == 0)
2580 matched = 1;
2581 break;
2582 case MATCH_MIDDLE_ONLY:
2583 if (strstr(str, regex))
2584 matched = 1;
2585 break;
2586 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002587 slen = strlen(str);
2588 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002589 matched = 1;
2590 break;
2591 }
2592
2593 return matched;
2594}
2595
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002596static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002597enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002598{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002599 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002600 int ret = 0;
2601
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002602 entry = ftrace_lookup_ip(hash, rec->ip);
2603 if (not) {
2604 /* Do nothing if it doesn't exist */
2605 if (!entry)
2606 return 0;
2607
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002608 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002609 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002610 /* Do nothing if it exists */
2611 if (entry)
2612 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002613
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002614 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002615 }
2616 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002617}
2618
Steven Rostedt64e7c442009-02-13 17:08:48 -05002619static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002620ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2621 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002622{
2623 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002624 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002625
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002626 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2627
2628 if (mod) {
2629 /* module lookup requires matching the module */
2630 if (!modname || strcmp(modname, mod))
2631 return 0;
2632
2633 /* blank search means to match all funcs in the mod */
2634 if (!len)
2635 return 1;
2636 }
2637
Steven Rostedt64e7c442009-02-13 17:08:48 -05002638 return ftrace_match(str, regex, len, type);
2639}
2640
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002641static int
2642match_records(struct ftrace_hash *hash, char *buff,
2643 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002644{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002645 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002646 struct ftrace_page *pg;
2647 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002648 int type = MATCH_FULL;
2649 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002650 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002651 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002652
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002653 if (len) {
2654 type = filter_parse_regex(buff, len, &search, &not);
2655 search_len = strlen(search);
2656 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002657
Steven Rostedt52baf112009-02-14 01:15:39 -05002658 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002659
2660 if (unlikely(ftrace_disabled))
2661 goto out_unlock;
2662
Steven Rostedt265c8312009-02-13 12:43:56 -05002663 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002664 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002665 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002666 if (ret < 0) {
2667 found = ret;
2668 goto out_unlock;
2669 }
Li Zefan311d16d2009-12-08 11:15:11 +08002670 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002671 }
2672 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002673 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002674 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002675
2676 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002677}
2678
Steven Rostedt64e7c442009-02-13 17:08:48 -05002679static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002680ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002681{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002682 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002683}
2684
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002685static int
2686ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002687{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002688 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002689
Steven Rostedt64e7c442009-02-13 17:08:48 -05002690 /* blank or '*' mean the same */
2691 if (strcmp(buff, "*") == 0)
2692 buff[0] = 0;
2693
2694 /* handle the case of 'dont filter this module' */
2695 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2696 buff[0] = 0;
2697 not = 1;
2698 }
2699
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002700 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002701}
2702
Steven Rostedtf6180772009-02-14 00:40:25 -05002703/*
2704 * We register the module command as a template to show others how
2705 * to register the a command as well.
2706 */
2707
2708static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002709ftrace_mod_callback(struct ftrace_hash *hash,
2710 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002711{
2712 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002713 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002714
2715 /*
2716 * cmd == 'mod' because we only registered this func
2717 * for the 'mod' ftrace_func_command.
2718 * But if you register one func with multiple commands,
2719 * you can tell which command was used by the cmd
2720 * parameter.
2721 */
2722
2723 /* we must have a module name */
2724 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002725 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002726
2727 mod = strsep(&param, ":");
2728 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002729 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002730
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002731 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002732 if (!ret)
2733 ret = -EINVAL;
2734 if (ret < 0)
2735 return ret;
2736
2737 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002738}
2739
2740static struct ftrace_func_command ftrace_mod_cmd = {
2741 .name = "mod",
2742 .func = ftrace_mod_callback,
2743};
2744
2745static int __init ftrace_mod_cmd_init(void)
2746{
2747 return register_ftrace_command(&ftrace_mod_cmd);
2748}
2749device_initcall(ftrace_mod_cmd_init);
2750
Steven Rostedt59df055f2009-02-14 15:29:06 -05002751static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002752function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002753{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002754 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002755 struct hlist_head *hhd;
2756 struct hlist_node *n;
2757 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002758
2759 key = hash_long(ip, FTRACE_HASH_BITS);
2760
2761 hhd = &ftrace_func_hash[key];
2762
2763 if (hlist_empty(hhd))
2764 return;
2765
2766 /*
2767 * Disable preemption for these calls to prevent a RCU grace
2768 * period. This syncs the hash iteration and freeing of items
2769 * on the hash. rcu_read_lock is too dangerous here.
2770 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002771 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002772 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2773 if (entry->ip == ip)
2774 entry->ops->func(ip, parent_ip, &entry->data);
2775 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002776 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002777}
2778
Steven Rostedtb6887d72009-02-17 12:32:04 -05002779static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002780{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002781 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002782};
2783
Steven Rostedtb6887d72009-02-17 12:32:04 -05002784static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002785
Steven Rostedtb6887d72009-02-17 12:32:04 -05002786static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002787{
Steven Rostedtb8489142011-05-04 09:27:52 -04002788 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002789 int i;
2790
Steven Rostedtb6887d72009-02-17 12:32:04 -05002791 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002792 return;
2793
2794 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2795 struct hlist_head *hhd = &ftrace_func_hash[i];
2796 if (hhd->first)
2797 break;
2798 }
2799 /* Nothing registered? */
2800 if (i == FTRACE_FUNC_HASHSIZE)
2801 return;
2802
Steven Rostedtb8489142011-05-04 09:27:52 -04002803 ret = __register_ftrace_function(&trace_probe_ops);
2804 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002805 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002806
Steven Rostedtb6887d72009-02-17 12:32:04 -05002807 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002808}
2809
Steven Rostedtb6887d72009-02-17 12:32:04 -05002810static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002811{
Steven Rostedtb8489142011-05-04 09:27:52 -04002812 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002813 int i;
2814
Steven Rostedtb6887d72009-02-17 12:32:04 -05002815 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002816 return;
2817
2818 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2819 struct hlist_head *hhd = &ftrace_func_hash[i];
2820 if (hhd->first)
2821 return;
2822 }
2823
2824 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002825 ret = __unregister_ftrace_function(&trace_probe_ops);
2826 if (!ret)
2827 ftrace_shutdown(&trace_probe_ops, 0);
2828
Steven Rostedtb6887d72009-02-17 12:32:04 -05002829 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002830}
2831
2832
2833static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2834{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002835 struct ftrace_func_probe *entry =
2836 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002837
2838 if (entry->ops->free)
2839 entry->ops->free(&entry->data);
2840 kfree(entry);
2841}
2842
2843
2844int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002845register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002846 void *data)
2847{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002848 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002849 struct ftrace_page *pg;
2850 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002851 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002852 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002853 int count = 0;
2854 char *search;
2855
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002856 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002857 len = strlen(search);
2858
Steven Rostedtb6887d72009-02-17 12:32:04 -05002859 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002860 if (WARN_ON(not))
2861 return -EINVAL;
2862
2863 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002864
Steven Rostedt45a4a232011-04-21 23:16:46 -04002865 if (unlikely(ftrace_disabled))
2866 goto out_unlock;
2867
Steven Rostedt59df055f2009-02-14 15:29:06 -05002868 do_for_each_ftrace_rec(pg, rec) {
2869
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002870 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002871 continue;
2872
2873 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2874 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002875 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002876 if (!count)
2877 count = -ENOMEM;
2878 goto out_unlock;
2879 }
2880
2881 count++;
2882
2883 entry->data = data;
2884
2885 /*
2886 * The caller might want to do something special
2887 * for each function we find. We call the callback
2888 * to give the caller an opportunity to do so.
2889 */
2890 if (ops->callback) {
2891 if (ops->callback(rec->ip, &entry->data) < 0) {
2892 /* caller does not like this func */
2893 kfree(entry);
2894 continue;
2895 }
2896 }
2897
2898 entry->ops = ops;
2899 entry->ip = rec->ip;
2900
2901 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2902 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2903
2904 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002905 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002906
2907 out_unlock:
2908 mutex_unlock(&ftrace_lock);
2909
2910 return count;
2911}
2912
2913enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002914 PROBE_TEST_FUNC = 1,
2915 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002916};
2917
2918static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002919__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002920 void *data, int flags)
2921{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002922 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002923 struct hlist_node *n, *tmp;
2924 char str[KSYM_SYMBOL_LEN];
2925 int type = MATCH_FULL;
2926 int i, len = 0;
2927 char *search;
2928
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002929 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002930 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002931 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002932 int not;
2933
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002934 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002935 len = strlen(search);
2936
Steven Rostedtb6887d72009-02-17 12:32:04 -05002937 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002938 if (WARN_ON(not))
2939 return;
2940 }
2941
2942 mutex_lock(&ftrace_lock);
2943 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2944 struct hlist_head *hhd = &ftrace_func_hash[i];
2945
2946 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2947
2948 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002949 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002950 continue;
2951
Steven Rostedtb6887d72009-02-17 12:32:04 -05002952 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002953 continue;
2954
2955 /* do this last, since it is the most expensive */
2956 if (glob) {
2957 kallsyms_lookup(entry->ip, NULL, NULL,
2958 NULL, str);
2959 if (!ftrace_match(str, glob, len, type))
2960 continue;
2961 }
2962
2963 hlist_del(&entry->node);
2964 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2965 }
2966 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002967 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002968 mutex_unlock(&ftrace_lock);
2969}
2970
2971void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002972unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002973 void *data)
2974{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002975 __unregister_ftrace_function_probe(glob, ops, data,
2976 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002977}
2978
2979void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002980unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002981{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002982 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002983}
2984
Steven Rostedtb6887d72009-02-17 12:32:04 -05002985void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002986{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002987 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002988}
2989
Steven Rostedtf6180772009-02-14 00:40:25 -05002990static LIST_HEAD(ftrace_commands);
2991static DEFINE_MUTEX(ftrace_cmd_mutex);
2992
2993int register_ftrace_command(struct ftrace_func_command *cmd)
2994{
2995 struct ftrace_func_command *p;
2996 int ret = 0;
2997
2998 mutex_lock(&ftrace_cmd_mutex);
2999 list_for_each_entry(p, &ftrace_commands, list) {
3000 if (strcmp(cmd->name, p->name) == 0) {
3001 ret = -EBUSY;
3002 goto out_unlock;
3003 }
3004 }
3005 list_add(&cmd->list, &ftrace_commands);
3006 out_unlock:
3007 mutex_unlock(&ftrace_cmd_mutex);
3008
3009 return ret;
3010}
3011
3012int unregister_ftrace_command(struct ftrace_func_command *cmd)
3013{
3014 struct ftrace_func_command *p, *n;
3015 int ret = -ENODEV;
3016
3017 mutex_lock(&ftrace_cmd_mutex);
3018 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3019 if (strcmp(cmd->name, p->name) == 0) {
3020 ret = 0;
3021 list_del_init(&p->list);
3022 goto out_unlock;
3023 }
3024 }
3025 out_unlock:
3026 mutex_unlock(&ftrace_cmd_mutex);
3027
3028 return ret;
3029}
3030
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003031static int ftrace_process_regex(struct ftrace_hash *hash,
3032 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003033{
Steven Rostedtf6180772009-02-14 00:40:25 -05003034 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003035 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003036 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003037
3038 func = strsep(&next, ":");
3039
3040 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003041 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003042 if (!ret)
3043 ret = -EINVAL;
3044 if (ret < 0)
3045 return ret;
3046 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003047 }
3048
Steven Rostedtf6180772009-02-14 00:40:25 -05003049 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003050
3051 command = strsep(&next, ":");
3052
Steven Rostedtf6180772009-02-14 00:40:25 -05003053 mutex_lock(&ftrace_cmd_mutex);
3054 list_for_each_entry(p, &ftrace_commands, list) {
3055 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003056 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003057 goto out_unlock;
3058 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003059 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003060 out_unlock:
3061 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003062
Steven Rostedtf6180772009-02-14 00:40:25 -05003063 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003064}
3065
Ingo Molnare309b412008-05-12 21:20:51 +02003066static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003067ftrace_regex_write(struct file *file, const char __user *ubuf,
3068 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003069{
3070 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003071 struct trace_parser *parser;
3072 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003073
Li Zefan4ba79782009-09-22 13:52:20 +08003074 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003075 return 0;
3076
Steven Rostedt41c52c02008-05-22 11:46:33 -04003077 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003078
Steven Rostedt45a4a232011-04-21 23:16:46 -04003079 ret = -ENODEV;
3080 if (unlikely(ftrace_disabled))
3081 goto out_unlock;
3082
Steven Rostedt5072c592008-05-12 21:20:43 +02003083 if (file->f_mode & FMODE_READ) {
3084 struct seq_file *m = file->private_data;
3085 iter = m->private;
3086 } else
3087 iter = file->private_data;
3088
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003089 parser = &iter->parser;
3090 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003091
Li Zefan4ba79782009-09-22 13:52:20 +08003092 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003093 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003094 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003095 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003096 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003097 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003098 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003099 }
3100
Steven Rostedt5072c592008-05-12 21:20:43 +02003101 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003102out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003103 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003104
Steven Rostedt5072c592008-05-12 21:20:43 +02003105 return ret;
3106}
3107
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003108ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003109ftrace_filter_write(struct file *file, const char __user *ubuf,
3110 size_t cnt, loff_t *ppos)
3111{
3112 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3113}
3114
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003115ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003116ftrace_notrace_write(struct file *file, const char __user *ubuf,
3117 size_t cnt, loff_t *ppos)
3118{
3119 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3120}
3121
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003122static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003123ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3124 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003125{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003126 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003127 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003128 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003129
Steven Rostedt936e0742011-05-05 22:54:01 -04003130 /* All global ops uses the global ops filters */
3131 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3132 ops = &global_ops;
3133
Steven Rostedt41c52c02008-05-22 11:46:33 -04003134 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003135 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003136
Steven Rostedtf45948e2011-05-02 12:29:25 -04003137 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003138 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003139 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003140 orig_hash = &ops->notrace_hash;
3141
3142 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3143 if (!hash)
3144 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003145
Steven Rostedt41c52c02008-05-22 11:46:33 -04003146 mutex_lock(&ftrace_regex_lock);
3147 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003148 ftrace_filter_reset(hash);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003149 if (buf)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003150 ftrace_match_records(hash, buf, len);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003151
3152 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003153 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003154 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3155 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003156 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003157
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003158 mutex_unlock(&ftrace_lock);
3159
Steven Rostedt41c52c02008-05-22 11:46:33 -04003160 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003161
3162 free_ftrace_hash(hash);
3163 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003164}
3165
Steven Rostedt77a2b372008-05-12 21:20:45 +02003166/**
3167 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003168 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003169 * @buf - the string that holds the function filter text.
3170 * @len - the length of the string.
3171 * @reset - non zero to reset all filters before applying this filter.
3172 *
3173 * Filters denote which functions should be enabled when tracing is enabled.
3174 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3175 */
Steven Rostedt936e0742011-05-05 22:54:01 -04003176void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3177 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003178{
Steven Rostedt936e0742011-05-05 22:54:01 -04003179 ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003180}
Steven Rostedt936e0742011-05-05 22:54:01 -04003181EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003182
Steven Rostedt41c52c02008-05-22 11:46:33 -04003183/**
3184 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003185 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003186 * @buf - the string that holds the function notrace text.
3187 * @len - the length of the string.
3188 * @reset - non zero to reset all filters before applying this filter.
3189 *
3190 * Notrace Filters denote which functions should not be enabled when tracing
3191 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3192 * for tracing.
3193 */
Steven Rostedt936e0742011-05-05 22:54:01 -04003194void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3195 int len, int reset)
3196{
3197 ftrace_set_regex(ops, buf, len, reset, 0);
3198}
3199EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3200/**
3201 * ftrace_set_filter - set a function to filter on in ftrace
3202 * @ops - the ops to set the filter with
3203 * @buf - the string that holds the function filter text.
3204 * @len - the length of the string.
3205 * @reset - non zero to reset all filters before applying this filter.
3206 *
3207 * Filters denote which functions should be enabled when tracing is enabled.
3208 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3209 */
3210void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3211{
3212 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3213}
3214EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3215
3216/**
3217 * ftrace_set_notrace - set a function to not trace in ftrace
3218 * @ops - the ops to set the notrace filter with
3219 * @buf - the string that holds the function notrace text.
3220 * @len - the length of the string.
3221 * @reset - non zero to reset all filters before applying this filter.
3222 *
3223 * Notrace Filters denote which functions should not be enabled when tracing
3224 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3225 * for tracing.
3226 */
3227void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003228{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003229 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003230}
Steven Rostedt936e0742011-05-05 22:54:01 -04003231EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003232
Steven Rostedt2af15d62009-05-28 13:37:24 -04003233/*
3234 * command line interface to allow users to set filters on boot up.
3235 */
3236#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3237static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3238static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3239
3240static int __init set_ftrace_notrace(char *str)
3241{
3242 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3243 return 1;
3244}
3245__setup("ftrace_notrace=", set_ftrace_notrace);
3246
3247static int __init set_ftrace_filter(char *str)
3248{
3249 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3250 return 1;
3251}
3252__setup("ftrace_filter=", set_ftrace_filter);
3253
Stefan Assmann369bc182009-10-12 22:17:21 +02003254#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003255static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003256static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3257
Stefan Assmann369bc182009-10-12 22:17:21 +02003258static int __init set_graph_function(char *str)
3259{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003260 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003261 return 1;
3262}
3263__setup("ftrace_graph_filter=", set_graph_function);
3264
3265static void __init set_ftrace_early_graph(char *buf)
3266{
3267 int ret;
3268 char *func;
3269
3270 while (buf) {
3271 func = strsep(&buf, ",");
3272 /* we allow only one expression at a time */
3273 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3274 func);
3275 if (ret)
3276 printk(KERN_DEBUG "ftrace: function %s not "
3277 "traceable\n", func);
3278 }
3279}
3280#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3281
Steven Rostedt2a85a372011-12-19 21:57:44 -05003282void __init
3283ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003284{
3285 char *func;
3286
3287 while (buf) {
3288 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003289 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003290 }
3291}
3292
3293static void __init set_ftrace_early_filters(void)
3294{
3295 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003296 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003297 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003298 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003299#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3300 if (ftrace_graph_buf[0])
3301 set_ftrace_early_graph(ftrace_graph_buf);
3302#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003303}
3304
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003305int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003306{
3307 struct seq_file *m = (struct seq_file *)file->private_data;
3308 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003309 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003310 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003311 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003312 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003313
Steven Rostedt41c52c02008-05-22 11:46:33 -04003314 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003315 if (file->f_mode & FMODE_READ) {
3316 iter = m->private;
3317
3318 seq_release(inode, file);
3319 } else
3320 iter = file->private_data;
3321
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003322 parser = &iter->parser;
3323 if (trace_parser_loaded(parser)) {
3324 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003325 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003326 }
3327
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003328 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003329
Steven Rostedt058e2972011-04-29 22:35:33 -04003330 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003331 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3332
3333 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003334 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003335 else
3336 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003337
Steven Rostedt058e2972011-04-29 22:35:33 -04003338 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003339 ret = ftrace_hash_move(iter->ops, filter_hash,
3340 orig_hash, iter->hash);
3341 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3342 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003343 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003344
Steven Rostedt058e2972011-04-29 22:35:33 -04003345 mutex_unlock(&ftrace_lock);
3346 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003347 free_ftrace_hash(iter->hash);
3348 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003349
Steven Rostedt41c52c02008-05-22 11:46:33 -04003350 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003351 return 0;
3352}
3353
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003354static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003355 .open = ftrace_avail_open,
3356 .read = seq_read,
3357 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003358 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003359};
3360
Steven Rostedt647bcd02011-05-03 14:39:21 -04003361static const struct file_operations ftrace_enabled_fops = {
3362 .open = ftrace_enabled_open,
3363 .read = seq_read,
3364 .llseek = seq_lseek,
3365 .release = seq_release_private,
3366};
3367
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003368static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003369 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003370 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003371 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003372 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003373 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003374};
3375
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003376static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003377 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003378 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003379 .write = ftrace_notrace_write,
3380 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003381 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003382};
3383
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003384#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3385
3386static DEFINE_MUTEX(graph_lock);
3387
3388int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003389int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003390unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3391
3392static void *
Li Zefan85951842009-06-24 09:54:00 +08003393__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003394{
Li Zefan85951842009-06-24 09:54:00 +08003395 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003396 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003397 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003398}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003399
Li Zefan85951842009-06-24 09:54:00 +08003400static void *
3401g_next(struct seq_file *m, void *v, loff_t *pos)
3402{
3403 (*pos)++;
3404 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003405}
3406
3407static void *g_start(struct seq_file *m, loff_t *pos)
3408{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003409 mutex_lock(&graph_lock);
3410
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003411 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003412 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003413 return (void *)1;
3414
Li Zefan85951842009-06-24 09:54:00 +08003415 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003416}
3417
3418static void g_stop(struct seq_file *m, void *p)
3419{
3420 mutex_unlock(&graph_lock);
3421}
3422
3423static int g_show(struct seq_file *m, void *v)
3424{
3425 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003426
3427 if (!ptr)
3428 return 0;
3429
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003430 if (ptr == (unsigned long *)1) {
3431 seq_printf(m, "#### all functions enabled ####\n");
3432 return 0;
3433 }
3434
Steven Rostedtb375a112009-09-17 00:05:58 -04003435 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003436
3437 return 0;
3438}
3439
James Morris88e9d342009-09-22 16:43:43 -07003440static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003441 .start = g_start,
3442 .next = g_next,
3443 .stop = g_stop,
3444 .show = g_show,
3445};
3446
3447static int
3448ftrace_graph_open(struct inode *inode, struct file *file)
3449{
3450 int ret = 0;
3451
3452 if (unlikely(ftrace_disabled))
3453 return -ENODEV;
3454
3455 mutex_lock(&graph_lock);
3456 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003457 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003458 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003459 ftrace_graph_count = 0;
3460 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3461 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003462 mutex_unlock(&graph_lock);
3463
Li Zefana4ec5e02009-09-18 14:06:28 +08003464 if (file->f_mode & FMODE_READ)
3465 ret = seq_open(file, &ftrace_graph_seq_ops);
3466
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003467 return ret;
3468}
3469
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003470static int
Li Zefan87827112009-07-23 11:29:11 +08003471ftrace_graph_release(struct inode *inode, struct file *file)
3472{
3473 if (file->f_mode & FMODE_READ)
3474 seq_release(inode, file);
3475 return 0;
3476}
3477
3478static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003479ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003480{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003481 struct dyn_ftrace *rec;
3482 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003483 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003484 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003485 int type, not;
3486 char *search;
3487 bool exists;
3488 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003489
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003490 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003491 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003492 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3493 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003494
3495 search_len = strlen(search);
3496
Steven Rostedt52baf112009-02-14 01:15:39 -05003497 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003498
3499 if (unlikely(ftrace_disabled)) {
3500 mutex_unlock(&ftrace_lock);
3501 return -ENODEV;
3502 }
3503
Steven Rostedt265c8312009-02-13 12:43:56 -05003504 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003505
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003506 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003507 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003508 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003509 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003510 if (array[i] == rec->ip) {
3511 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003512 break;
3513 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003514 }
3515
3516 if (!not) {
3517 fail = 0;
3518 if (!exists) {
3519 array[(*idx)++] = rec->ip;
3520 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3521 goto out;
3522 }
3523 } else {
3524 if (exists) {
3525 array[i] = array[--(*idx)];
3526 array[*idx] = 0;
3527 fail = 0;
3528 }
3529 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003530 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003531 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003532out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003533 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003534
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003535 if (fail)
3536 return -EINVAL;
3537
3538 ftrace_graph_filter_enabled = 1;
3539 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003540}
3541
3542static ssize_t
3543ftrace_graph_write(struct file *file, const char __user *ubuf,
3544 size_t cnt, loff_t *ppos)
3545{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003546 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003547 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003548
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003549 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003550 return 0;
3551
3552 mutex_lock(&graph_lock);
3553
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003554 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3555 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003556 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003557 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003558
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003559 read = trace_get_user(&parser, ubuf, cnt, ppos);
3560
Li Zefan4ba79782009-09-22 13:52:20 +08003561 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003562 parser.buffer[parser.idx] = 0;
3563
3564 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003565 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003566 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003567 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003568 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003569 }
3570
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003571 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003572
3573out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003574 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003575out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003576 mutex_unlock(&graph_lock);
3577
3578 return ret;
3579}
3580
3581static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003582 .open = ftrace_graph_open,
3583 .read = seq_read,
3584 .write = ftrace_graph_write,
3585 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003586 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003587};
3588#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3589
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003590static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003591{
Steven Rostedt5072c592008-05-12 21:20:43 +02003592
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003593 trace_create_file("available_filter_functions", 0444,
3594 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003595
Steven Rostedt647bcd02011-05-03 14:39:21 -04003596 trace_create_file("enabled_functions", 0444,
3597 d_tracer, NULL, &ftrace_enabled_fops);
3598
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003599 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3600 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003601
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003602 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003603 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003604
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003605#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003606 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003607 NULL,
3608 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003609#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3610
Steven Rostedt5072c592008-05-12 21:20:43 +02003611 return 0;
3612}
3613
Steven Rostedt68950612011-12-16 17:06:45 -05003614static void ftrace_swap_recs(void *a, void *b, int size)
3615{
3616 struct dyn_ftrace *reca = a;
3617 struct dyn_ftrace *recb = b;
3618 struct dyn_ftrace t;
3619
3620 t = *reca;
3621 *reca = *recb;
3622 *recb = t;
3623}
3624
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003625static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003626 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003627 unsigned long *end)
3628{
Steven Rostedta7900872011-12-16 16:23:44 -05003629 struct ftrace_page *pg;
3630 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003631 unsigned long *p;
3632 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003633 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003634 int ret = -ENOMEM;
3635
3636 count = end - start;
3637
3638 if (!count)
3639 return 0;
3640
3641 pg = ftrace_allocate_pages(count);
3642 if (!pg)
3643 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003644
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003645 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003646
Steven Rostedt320823092011-12-16 14:42:37 -05003647 /*
3648 * Core and each module needs their own pages, as
3649 * modules will free them when they are removed.
3650 * Force a new page to be allocated for modules.
3651 */
Steven Rostedta7900872011-12-16 16:23:44 -05003652 if (!mod) {
3653 WARN_ON(ftrace_pages || ftrace_pages_start);
3654 /* First initialization */
3655 ftrace_pages = ftrace_pages_start = pg;
3656 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05003657 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003658 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05003659
Steven Rostedta7900872011-12-16 16:23:44 -05003660 if (WARN_ON(ftrace_pages->next)) {
3661 /* Hmm, we have free pages? */
3662 while (ftrace_pages->next)
3663 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05003664 }
Steven Rostedta7900872011-12-16 16:23:44 -05003665
3666 ftrace_pages->next = pg;
3667 ftrace_pages = pg;
Steven Rostedt320823092011-12-16 14:42:37 -05003668 }
3669
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003670 p = start;
3671 while (p < end) {
3672 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003673 /*
3674 * Some architecture linkers will pad between
3675 * the different mcount_loc sections of different
3676 * object files to satisfy alignments.
3677 * Skip any NULL pointers.
3678 */
3679 if (!addr)
3680 continue;
Steven Rostedta7900872011-12-16 16:23:44 -05003681 if (!ftrace_record_ip(addr))
3682 break;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003683 }
3684
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003685 /* These new locations need to be initialized */
3686 ftrace_new_pgs = pg;
3687
Steven Rostedt68950612011-12-16 17:06:45 -05003688 /* Make each individual set of pages sorted by ips */
3689 for (; pg; pg = pg->next)
3690 sort(pg->records, pg->index, sizeof(struct dyn_ftrace),
3691 ftrace_cmp_recs, ftrace_swap_recs);
3692
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003693 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003694 * We only need to disable interrupts on start up
3695 * because we are modifying code that an interrupt
3696 * may execute, and the modification is not atomic.
3697 * But for modules, nothing runs the code we modify
3698 * until we are finished with it, and there's no
3699 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003700 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003701 if (!mod)
3702 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003703 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003704 if (!mod)
3705 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003706 ret = 0;
3707 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003708 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003709
Steven Rostedta7900872011-12-16 16:23:44 -05003710 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003711}
3712
Steven Rostedt93eb6772009-04-15 13:24:06 -04003713#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05003714
3715#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3716
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003717void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003718{
3719 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05003720 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003721 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003722 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003723
Steven Rostedt93eb6772009-04-15 13:24:06 -04003724 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003725
3726 if (ftrace_disabled)
3727 goto out_unlock;
3728
Steven Rostedt320823092011-12-16 14:42:37 -05003729 /*
3730 * Each module has its own ftrace_pages, remove
3731 * them from the list.
3732 */
3733 last_pg = &ftrace_pages_start;
3734 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3735 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003736 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003737 /*
Steven Rostedt320823092011-12-16 14:42:37 -05003738 * As core pages are first, the first
3739 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003740 */
Steven Rostedt320823092011-12-16 14:42:37 -05003741 if (WARN_ON(pg == ftrace_pages_start))
3742 goto out_unlock;
3743
3744 /* Check if we are deleting the last page */
3745 if (pg == ftrace_pages)
3746 ftrace_pages = next_to_ftrace_page(last_pg);
3747
3748 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003749 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3750 free_pages((unsigned long)pg->records, order);
3751 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05003752 } else
3753 last_pg = &pg->next;
3754 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003755 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003756 mutex_unlock(&ftrace_lock);
3757}
3758
3759static void ftrace_init_module(struct module *mod,
3760 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003761{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003762 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003763 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003764 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003765}
3766
Steven Rostedt93eb6772009-04-15 13:24:06 -04003767static int ftrace_module_notify(struct notifier_block *self,
3768 unsigned long val, void *data)
3769{
3770 struct module *mod = data;
3771
3772 switch (val) {
3773 case MODULE_STATE_COMING:
3774 ftrace_init_module(mod, mod->ftrace_callsites,
3775 mod->ftrace_callsites +
3776 mod->num_ftrace_callsites);
3777 break;
3778 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003779 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003780 break;
3781 }
3782
3783 return 0;
3784}
3785#else
3786static int ftrace_module_notify(struct notifier_block *self,
3787 unsigned long val, void *data)
3788{
3789 return 0;
3790}
3791#endif /* CONFIG_MODULES */
3792
3793struct notifier_block ftrace_module_nb = {
3794 .notifier_call = ftrace_module_notify,
3795 .priority = 0,
3796};
3797
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003798extern unsigned long __start_mcount_loc[];
3799extern unsigned long __stop_mcount_loc[];
3800
3801void __init ftrace_init(void)
3802{
3803 unsigned long count, addr, flags;
3804 int ret;
3805
3806 /* Keep the ftrace pointer to the stub */
3807 addr = (unsigned long)ftrace_stub;
3808
3809 local_irq_save(flags);
3810 ftrace_dyn_arch_init(&addr);
3811 local_irq_restore(flags);
3812
3813 /* ftrace_dyn_arch_init places the return code in addr */
3814 if (addr)
3815 goto failed;
3816
3817 count = __stop_mcount_loc - __start_mcount_loc;
3818
3819 ret = ftrace_dyn_table_alloc(count);
3820 if (ret)
3821 goto failed;
3822
3823 last_ftrace_enabled = ftrace_enabled = 1;
3824
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003825 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003826 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003827 __stop_mcount_loc);
3828
Steven Rostedt93eb6772009-04-15 13:24:06 -04003829 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003830 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003831 pr_warning("Failed to register trace ftrace module notifier\n");
3832
Steven Rostedt2af15d62009-05-28 13:37:24 -04003833 set_ftrace_early_filters();
3834
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003835 return;
3836 failed:
3837 ftrace_disabled = 1;
3838}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003839
Steven Rostedt3d083392008-05-12 21:20:42 +02003840#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003841
Steven Rostedt2b499382011-05-03 22:49:52 -04003842static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003843 .func = ftrace_stub,
3844};
3845
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003846static int __init ftrace_nodyn_init(void)
3847{
3848 ftrace_enabled = 1;
3849 return 0;
3850}
3851device_initcall(ftrace_nodyn_init);
3852
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003853static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3854static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003855/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003856# define ftrace_startup(ops, command) \
3857 ({ \
3858 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3859 0; \
3860 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04003861# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003862# define ftrace_startup_sysctl() do { } while (0)
3863# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003864
3865static inline int
3866ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3867{
3868 return 1;
3869}
3870
Steven Rostedt3d083392008-05-12 21:20:42 +02003871#endif /* CONFIG_DYNAMIC_FTRACE */
3872
Steven Rostedtb8489142011-05-04 09:27:52 -04003873static void
3874ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3875{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003876 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04003877
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003878 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3879 return;
3880
3881 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003882 /*
3883 * Some of the ops may be dynamically allocated,
3884 * they must be freed after a synchronize_sched().
3885 */
3886 preempt_disable_notrace();
3887 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04003888 while (op != &ftrace_list_end) {
3889 if (ftrace_ops_test(op, ip))
3890 op->func(ip, parent_ip);
3891 op = rcu_dereference_raw(op->next);
3892 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003893 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003894 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04003895}
3896
Steven Rostedte32d8952008-12-04 00:26:41 -05003897static void clear_ftrace_swapper(void)
3898{
3899 struct task_struct *p;
3900 int cpu;
3901
3902 get_online_cpus();
3903 for_each_online_cpu(cpu) {
3904 p = idle_task(cpu);
3905 clear_tsk_trace_trace(p);
3906 }
3907 put_online_cpus();
3908}
3909
3910static void set_ftrace_swapper(void)
3911{
3912 struct task_struct *p;
3913 int cpu;
3914
3915 get_online_cpus();
3916 for_each_online_cpu(cpu) {
3917 p = idle_task(cpu);
3918 set_tsk_trace_trace(p);
3919 }
3920 put_online_cpus();
3921}
3922
3923static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003924{
3925 struct task_struct *p;
3926
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003927 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05003928 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05003929 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05003930 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003931 rcu_read_unlock();
3932
Steven Rostedte32d8952008-12-04 00:26:41 -05003933 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003934}
3935
Steven Rostedte32d8952008-12-04 00:26:41 -05003936static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003937{
3938 struct task_struct *p;
3939
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003940 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003941 do_each_pid_task(pid, PIDTYPE_PID, p) {
3942 set_tsk_trace_trace(p);
3943 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003944 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003945}
3946
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003947static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003948{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003949 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003950 clear_ftrace_swapper();
3951 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003952 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05003953}
3954
3955static void set_ftrace_pid_task(struct pid *pid)
3956{
3957 if (pid == ftrace_swapper_pid)
3958 set_ftrace_swapper();
3959 else
3960 set_ftrace_pid(pid);
3961}
3962
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003963static int ftrace_pid_add(int p)
3964{
3965 struct pid *pid;
3966 struct ftrace_pid *fpid;
3967 int ret = -EINVAL;
3968
3969 mutex_lock(&ftrace_lock);
3970
3971 if (!p)
3972 pid = ftrace_swapper_pid;
3973 else
3974 pid = find_get_pid(p);
3975
3976 if (!pid)
3977 goto out;
3978
3979 ret = 0;
3980
3981 list_for_each_entry(fpid, &ftrace_pids, list)
3982 if (fpid->pid == pid)
3983 goto out_put;
3984
3985 ret = -ENOMEM;
3986
3987 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3988 if (!fpid)
3989 goto out_put;
3990
3991 list_add(&fpid->list, &ftrace_pids);
3992 fpid->pid = pid;
3993
3994 set_ftrace_pid_task(pid);
3995
3996 ftrace_update_pid_func();
3997 ftrace_startup_enable(0);
3998
3999 mutex_unlock(&ftrace_lock);
4000 return 0;
4001
4002out_put:
4003 if (pid != ftrace_swapper_pid)
4004 put_pid(pid);
4005
4006out:
4007 mutex_unlock(&ftrace_lock);
4008 return ret;
4009}
4010
4011static void ftrace_pid_reset(void)
4012{
4013 struct ftrace_pid *fpid, *safe;
4014
4015 mutex_lock(&ftrace_lock);
4016 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4017 struct pid *pid = fpid->pid;
4018
4019 clear_ftrace_pid_task(pid);
4020
4021 list_del(&fpid->list);
4022 kfree(fpid);
4023 }
4024
4025 ftrace_update_pid_func();
4026 ftrace_startup_enable(0);
4027
4028 mutex_unlock(&ftrace_lock);
4029}
4030
4031static void *fpid_start(struct seq_file *m, loff_t *pos)
4032{
4033 mutex_lock(&ftrace_lock);
4034
4035 if (list_empty(&ftrace_pids) && (!*pos))
4036 return (void *) 1;
4037
4038 return seq_list_start(&ftrace_pids, *pos);
4039}
4040
4041static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4042{
4043 if (v == (void *)1)
4044 return NULL;
4045
4046 return seq_list_next(v, &ftrace_pids, pos);
4047}
4048
4049static void fpid_stop(struct seq_file *m, void *p)
4050{
4051 mutex_unlock(&ftrace_lock);
4052}
4053
4054static int fpid_show(struct seq_file *m, void *v)
4055{
4056 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4057
4058 if (v == (void *)1) {
4059 seq_printf(m, "no pid\n");
4060 return 0;
4061 }
4062
4063 if (fpid->pid == ftrace_swapper_pid)
4064 seq_printf(m, "swapper tasks\n");
4065 else
4066 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4067
4068 return 0;
4069}
4070
4071static const struct seq_operations ftrace_pid_sops = {
4072 .start = fpid_start,
4073 .next = fpid_next,
4074 .stop = fpid_stop,
4075 .show = fpid_show,
4076};
4077
4078static int
4079ftrace_pid_open(struct inode *inode, struct file *file)
4080{
4081 int ret = 0;
4082
4083 if ((file->f_mode & FMODE_WRITE) &&
4084 (file->f_flags & O_TRUNC))
4085 ftrace_pid_reset();
4086
4087 if (file->f_mode & FMODE_READ)
4088 ret = seq_open(file, &ftrace_pid_sops);
4089
4090 return ret;
4091}
4092
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004093static ssize_t
4094ftrace_pid_write(struct file *filp, const char __user *ubuf,
4095 size_t cnt, loff_t *ppos)
4096{
Ingo Molnar457dc922009-11-23 11:03:28 +01004097 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004098 long val;
4099 int ret;
4100
4101 if (cnt >= sizeof(buf))
4102 return -EINVAL;
4103
4104 if (copy_from_user(&buf, ubuf, cnt))
4105 return -EFAULT;
4106
4107 buf[cnt] = 0;
4108
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004109 /*
4110 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4111 * to clean the filter quietly.
4112 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004113 tmp = strstrip(buf);
4114 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004115 return 1;
4116
Ingo Molnar457dc922009-11-23 11:03:28 +01004117 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004118 if (ret < 0)
4119 return ret;
4120
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004121 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004122
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004123 return ret ? ret : cnt;
4124}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004125
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004126static int
4127ftrace_pid_release(struct inode *inode, struct file *file)
4128{
4129 if (file->f_mode & FMODE_READ)
4130 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004131
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004132 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004133}
4134
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004135static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004136 .open = ftrace_pid_open,
4137 .write = ftrace_pid_write,
4138 .read = seq_read,
4139 .llseek = seq_lseek,
4140 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004141};
4142
4143static __init int ftrace_init_debugfs(void)
4144{
4145 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004146
4147 d_tracer = tracing_init_dentry();
4148 if (!d_tracer)
4149 return 0;
4150
4151 ftrace_init_dyn_debugfs(d_tracer);
4152
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004153 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4154 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004155
4156 ftrace_profile_debugfs(d_tracer);
4157
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004158 return 0;
4159}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004160fs_initcall(ftrace_init_debugfs);
4161
Steven Rostedt3d083392008-05-12 21:20:42 +02004162/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004163 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004164 *
4165 * This function should be used by panic code. It stops ftrace
4166 * but in a not so nice way. If you need to simply kill ftrace
4167 * from a non-atomic section, use ftrace_kill.
4168 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004169void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004170{
4171 ftrace_disabled = 1;
4172 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004173 clear_ftrace_function();
4174}
4175
4176/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004177 * Test if ftrace is dead or not.
4178 */
4179int ftrace_is_dead(void)
4180{
4181 return ftrace_disabled;
4182}
4183
4184/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004185 * register_ftrace_function - register a function for profiling
4186 * @ops - ops structure that holds the function for profiling.
4187 *
4188 * Register a function to be called by all functions in the
4189 * kernel.
4190 *
4191 * Note: @ops->func and all the functions it calls must be labeled
4192 * with "notrace", otherwise it will go into a
4193 * recursive loop.
4194 */
4195int register_ftrace_function(struct ftrace_ops *ops)
4196{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004197 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004198
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004199 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004200
Steven Rostedt45a4a232011-04-21 23:16:46 -04004201 if (unlikely(ftrace_disabled))
4202 goto out_unlock;
4203
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004204 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004205 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004206 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004207
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004208
Steven Rostedt45a4a232011-04-21 23:16:46 -04004209 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004210 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004211 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004212}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004213EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004214
4215/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004216 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004217 * @ops - ops structure that holds the function to unregister
4218 *
4219 * Unregister a function that was added to be called by ftrace profiling.
4220 */
4221int unregister_ftrace_function(struct ftrace_ops *ops)
4222{
4223 int ret;
4224
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004225 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004226 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004227 if (!ret)
4228 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004229 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004230
4231 return ret;
4232}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004233EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004234
Ingo Molnare309b412008-05-12 21:20:51 +02004235int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004236ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004237 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004238 loff_t *ppos)
4239{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004240 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004241
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004242 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004243
Steven Rostedt45a4a232011-04-21 23:16:46 -04004244 if (unlikely(ftrace_disabled))
4245 goto out;
4246
4247 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004248
Li Zefana32c7762009-06-26 16:55:51 +08004249 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004250 goto out;
4251
Li Zefana32c7762009-06-26 16:55:51 +08004252 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004253
4254 if (ftrace_enabled) {
4255
4256 ftrace_startup_sysctl();
4257
4258 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004259 if (ftrace_ops_list != &ftrace_list_end) {
4260 if (ftrace_ops_list->next == &ftrace_list_end)
4261 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004262 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004263 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004264 }
4265
4266 } else {
4267 /* stopping ftrace calls (just send to ftrace_stub) */
4268 ftrace_trace_function = ftrace_stub;
4269
4270 ftrace_shutdown_sysctl();
4271 }
4272
4273 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004274 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004275 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004276}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004277
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004278#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004279
Steven Rostedt597af812009-04-03 15:24:12 -04004280static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004281static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004282
Steven Rostedte49dc192008-12-02 23:50:05 -05004283int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4284{
4285 return 0;
4286}
4287
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004288/* The callbacks that hook a function */
4289trace_func_graph_ret_t ftrace_graph_return =
4290 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004291trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004292
4293/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4294static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4295{
4296 int i;
4297 int ret = 0;
4298 unsigned long flags;
4299 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4300 struct task_struct *g, *t;
4301
4302 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4303 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4304 * sizeof(struct ftrace_ret_stack),
4305 GFP_KERNEL);
4306 if (!ret_stack_list[i]) {
4307 start = 0;
4308 end = i;
4309 ret = -ENOMEM;
4310 goto free;
4311 }
4312 }
4313
4314 read_lock_irqsave(&tasklist_lock, flags);
4315 do_each_thread(g, t) {
4316 if (start == end) {
4317 ret = -EAGAIN;
4318 goto unlock;
4319 }
4320
4321 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004322 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004323 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004324 t->curr_ret_stack = -1;
4325 /* Make sure the tasks see the -1 first: */
4326 smp_wmb();
4327 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004328 }
4329 } while_each_thread(g, t);
4330
4331unlock:
4332 read_unlock_irqrestore(&tasklist_lock, flags);
4333free:
4334 for (i = start; i < end; i++)
4335 kfree(ret_stack_list[i]);
4336 return ret;
4337}
4338
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004339static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004340ftrace_graph_probe_sched_switch(void *ignore,
4341 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004342{
4343 unsigned long long timestamp;
4344 int index;
4345
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004346 /*
4347 * Does the user want to count the time a function was asleep.
4348 * If so, do not update the time stamps.
4349 */
4350 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4351 return;
4352
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004353 timestamp = trace_clock_local();
4354
4355 prev->ftrace_timestamp = timestamp;
4356
4357 /* only process tasks that we timestamped */
4358 if (!next->ftrace_timestamp)
4359 return;
4360
4361 /*
4362 * Update all the counters in next to make up for the
4363 * time next was sleeping.
4364 */
4365 timestamp -= next->ftrace_timestamp;
4366
4367 for (index = next->curr_ret_stack; index >= 0; index--)
4368 next->ret_stack[index].calltime += timestamp;
4369}
4370
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004371/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004372static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004373{
4374 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004375 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004376
4377 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4378 sizeof(struct ftrace_ret_stack *),
4379 GFP_KERNEL);
4380
4381 if (!ret_stack_list)
4382 return -ENOMEM;
4383
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004384 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004385 for_each_online_cpu(cpu) {
4386 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004387 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004388 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004389
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004390 do {
4391 ret = alloc_retstack_tasklist(ret_stack_list);
4392 } while (ret == -EAGAIN);
4393
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004394 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004395 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004396 if (ret)
4397 pr_info("ftrace_graph: Couldn't activate tracepoint"
4398 " probe to kernel_sched_switch\n");
4399 }
4400
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004401 kfree(ret_stack_list);
4402 return ret;
4403}
4404
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004405/*
4406 * Hibernation protection.
4407 * The state of the current task is too much unstable during
4408 * suspend/restore to disk. We want to protect against that.
4409 */
4410static int
4411ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4412 void *unused)
4413{
4414 switch (state) {
4415 case PM_HIBERNATION_PREPARE:
4416 pause_graph_tracing();
4417 break;
4418
4419 case PM_POST_HIBERNATION:
4420 unpause_graph_tracing();
4421 break;
4422 }
4423 return NOTIFY_DONE;
4424}
4425
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004426int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4427 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004428{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004429 int ret = 0;
4430
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004431 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004432
Steven Rostedt05ce5812009-03-24 00:18:31 -04004433 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004434 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004435 ret = -EBUSY;
4436 goto out;
4437 }
4438
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004439 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4440 register_pm_notifier(&ftrace_suspend_notifier);
4441
Steven Rostedt597af812009-04-03 15:24:12 -04004442 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004443 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004444 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004445 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004446 goto out;
4447 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004448
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004449 ftrace_graph_return = retfunc;
4450 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004451
Steven Rostedta1cd6172011-05-23 15:24:25 -04004452 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004453
4454out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004455 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004456 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004457}
4458
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004459void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004460{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004461 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004462
Steven Rostedt597af812009-04-03 15:24:12 -04004463 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004464 goto out;
4465
Steven Rostedt597af812009-04-03 15:24:12 -04004466 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004467 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004468 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004469 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004470 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004471 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004472
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004473 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004474 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004475}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004476
Steven Rostedt868baf02011-02-10 21:26:13 -05004477static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4478
4479static void
4480graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4481{
4482 atomic_set(&t->tracing_graph_pause, 0);
4483 atomic_set(&t->trace_overrun, 0);
4484 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004485 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004486 smp_wmb();
4487 t->ret_stack = ret_stack;
4488}
4489
4490/*
4491 * Allocate a return stack for the idle task. May be the first
4492 * time through, or it may be done by CPU hotplug online.
4493 */
4494void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4495{
4496 t->curr_ret_stack = -1;
4497 /*
4498 * The idle task has no parent, it either has its own
4499 * stack or no stack at all.
4500 */
4501 if (t->ret_stack)
4502 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4503
4504 if (ftrace_graph_active) {
4505 struct ftrace_ret_stack *ret_stack;
4506
4507 ret_stack = per_cpu(idle_ret_stack, cpu);
4508 if (!ret_stack) {
4509 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4510 * sizeof(struct ftrace_ret_stack),
4511 GFP_KERNEL);
4512 if (!ret_stack)
4513 return;
4514 per_cpu(idle_ret_stack, cpu) = ret_stack;
4515 }
4516 graph_init_task(t, ret_stack);
4517 }
4518}
4519
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004520/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004521void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004522{
Steven Rostedt84047e32009-06-02 16:51:55 -04004523 /* Make sure we do not use the parent ret_stack */
4524 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004525 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004526
Steven Rostedt597af812009-04-03 15:24:12 -04004527 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004528 struct ftrace_ret_stack *ret_stack;
4529
4530 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004531 * sizeof(struct ftrace_ret_stack),
4532 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004533 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004534 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004535 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004536 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004537}
4538
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004539void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004540{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004541 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4542
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004543 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004544 /* NULL must become visible to IRQs before we free it: */
4545 barrier();
4546
4547 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004548}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004549
4550void ftrace_graph_stop(void)
4551{
4552 ftrace_stop();
4553}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004554#endif