blob: be6888f40d2b146fb2d017b54cafbcdeee5f63c3 [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>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040025#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010026#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020027#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020029#include <linux/ctype.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020030#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050031#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080032#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020033
Steven Rostedtad8d75f2009-04-14 19:39:12 -040034#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040035
Steven Rostedt2af15d62009-05-28 13:37:24 -040036#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053037
Steven Rostedt0706f1c2009-03-23 23:12:58 -040038#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040039#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020040
Steven Rostedt69128962008-10-23 09:33:03 -040041#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040042 ({ \
43 int ___r = cond; \
44 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040045 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040046 ___r; \
47 })
Steven Rostedt69128962008-10-23 09:33:03 -040048
49#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040050 ({ \
51 int ___r = cond; \
52 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040053 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040054 ___r; \
55 })
Steven Rostedt69128962008-10-23 09:33:03 -040056
Steven Rostedt8fc0c702009-02-16 15:28:00 -050057/* hash bits for specific function selection */
58#define FTRACE_HASH_BITS 7
59#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040060#define FTRACE_HASH_DEFAULT_BITS 10
61#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050062
Steven Rostedt4eebcc82008-05-12 21:20:48 +020063/* ftrace_enabled is a method to turn ftrace on or off */
64int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020065static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020066
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050067/* Quick disabling of function tracer. */
68int function_trace_stop;
69
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040070/* List for set_ftrace_pid's pids. */
71LIST_HEAD(ftrace_pids);
72struct ftrace_pid {
73 struct list_head list;
74 struct pid *pid;
75};
76
Steven Rostedt4eebcc82008-05-12 21:20:48 +020077/*
78 * ftrace_disabled is set when an anomaly is discovered.
79 * ftrace_disabled is much stronger than ftrace_enabled.
80 */
81static int ftrace_disabled __read_mostly;
82
Steven Rostedt52baf112009-02-14 01:15:39 -050083static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020084
Paul McQuadebd38c0e2011-05-31 20:51:55 +010085static struct ftrace_ops ftrace_list_end __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -040086 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020087};
88
Steven Rostedtb8489142011-05-04 09:27:52 -040089static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
90static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020091ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt6331c282011-07-13 15:11:02 -040092static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050093ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050094ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -040095static struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020096
Steven Rostedtb8489142011-05-04 09:27:52 -040097static void
98ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
99
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800100/*
Steven Rostedtb8489142011-05-04 09:27:52 -0400101 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800102 * can use rcu_dereference_raw() is that elements removed from this list
103 * are simply leaked, so there is no need to interact with a grace-period
104 * mechanism. The rcu_dereference_raw() calls are needed to handle
Steven Rostedtb8489142011-05-04 09:27:52 -0400105 * concurrent insertions into the ftrace_global_list.
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800106 *
107 * Silly Alpha and silly pointer-speculation compiler optimizations!
108 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400109static void ftrace_global_list_func(unsigned long ip,
110 unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200111{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400112 struct ftrace_ops *op;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200113
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400114 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
115 return;
116
117 trace_recursion_set(TRACE_GLOBAL_BIT);
118 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200119 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200120 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800121 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200122 };
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400123 trace_recursion_clear(TRACE_GLOBAL_BIT);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200124}
125
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500126static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
127{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500128 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500129 return;
130
131 ftrace_pid_function(ip, parent_ip);
132}
133
134static void set_ftrace_pid_function(ftrace_func_t func)
135{
136 /* do not set ftrace_pid_function to itself! */
137 if (func != ftrace_pid_func)
138 ftrace_pid_function = func;
139}
140
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200141/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200142 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200143 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200144 * This NULLs the ftrace function and in essence stops
145 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200146 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200147void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200148{
Steven Rostedt3d083392008-05-12 21:20:42 +0200149 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500150 __ftrace_trace_function = ftrace_stub;
Steven Rostedt6331c282011-07-13 15:11:02 -0400151 __ftrace_trace_function_delay = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500152 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200153}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200154
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500155#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
156/*
157 * For those archs that do not test ftrace_trace_stop in their
158 * mcount call site, we need to do it from C.
159 */
160static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
161{
162 if (function_trace_stop)
163 return;
164
165 __ftrace_trace_function(ip, parent_ip);
166}
167#endif
168
Steven Rostedt2b499382011-05-03 22:49:52 -0400169static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400170{
171 ftrace_func_t func;
172
173 /*
174 * If there's only one function registered, then call that
175 * function directly. Otherwise, we need to iterate over the
176 * registered callers.
177 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400178 if (ftrace_global_list == &ftrace_list_end ||
179 ftrace_global_list->next == &ftrace_list_end)
180 func = ftrace_global_list->func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400181 else
Steven Rostedtb8489142011-05-04 09:27:52 -0400182 func = ftrace_global_list_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400183
184 /* If we filter on pids, update to use the pid function */
185 if (!list_empty(&ftrace_pids)) {
186 set_ftrace_pid_function(func);
187 func = ftrace_pid_func;
188 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400189
190 global_ops.func = func;
191}
192
193static void update_ftrace_function(void)
194{
195 ftrace_func_t func;
196
197 update_global_ops();
198
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400199 /*
200 * If we are at the end of the list and this ops is
201 * not dynamic, then have the mcount trampoline call
202 * the function directly
203 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400204 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400205 (ftrace_ops_list->next == &ftrace_list_end &&
206 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
Steven Rostedtb8489142011-05-04 09:27:52 -0400207 func = ftrace_ops_list->func;
208 else
209 func = ftrace_ops_list_func;
Steven Rostedt2b499382011-05-03 22:49:52 -0400210
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400211#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
212 ftrace_trace_function = func;
213#else
Steven Rostedt6331c282011-07-13 15:11:02 -0400214#ifdef CONFIG_DYNAMIC_FTRACE
215 /* do not update till all functions have been modified */
216 __ftrace_trace_function_delay = func;
217#else
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400218 __ftrace_trace_function = func;
Steven Rostedt6331c282011-07-13 15:11:02 -0400219#endif
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400220 ftrace_trace_function = ftrace_test_stop_func;
221#endif
222}
223
Steven Rostedt2b499382011-05-03 22:49:52 -0400224static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200225{
Steven Rostedt2b499382011-05-03 22:49:52 -0400226 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200227 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400228 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200229 * CPU might be walking that list. We need to make sure
230 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400231 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200232 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400233 rcu_assign_pointer(*list, ops);
234}
Steven Rostedt3d083392008-05-12 21:20:42 +0200235
Steven Rostedt2b499382011-05-03 22:49:52 -0400236static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
237{
238 struct ftrace_ops **p;
239
240 /*
241 * If we are removing the last function, then simply point
242 * to the ftrace_stub.
243 */
244 if (*list == ops && ops->next == &ftrace_list_end) {
245 *list = &ftrace_list_end;
246 return 0;
247 }
248
249 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
250 if (*p == ops)
251 break;
252
253 if (*p != ops)
254 return -1;
255
256 *p = (*p)->next;
257 return 0;
258}
259
260static int __register_ftrace_function(struct ftrace_ops *ops)
261{
262 if (ftrace_disabled)
263 return -ENODEV;
264
265 if (FTRACE_WARN_ON(ops == &global_ops))
266 return -EINVAL;
267
Steven Rostedtb8489142011-05-04 09:27:52 -0400268 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
269 return -EBUSY;
270
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400271 if (!core_kernel_data((unsigned long)ops))
272 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
273
Steven Rostedtb8489142011-05-04 09:27:52 -0400274 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
275 int first = ftrace_global_list == &ftrace_list_end;
276 add_ftrace_ops(&ftrace_global_list, ops);
277 ops->flags |= FTRACE_OPS_FL_ENABLED;
278 if (first)
279 add_ftrace_ops(&ftrace_ops_list, &global_ops);
280 } else
281 add_ftrace_ops(&ftrace_ops_list, ops);
282
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400283 if (ftrace_enabled)
284 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200285
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200286 return 0;
287}
288
Ingo Molnare309b412008-05-12 21:20:51 +0200289static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200290{
Steven Rostedt2b499382011-05-03 22:49:52 -0400291 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200292
Steven Rostedt2b499382011-05-03 22:49:52 -0400293 if (ftrace_disabled)
294 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200295
Steven Rostedtb8489142011-05-04 09:27:52 -0400296 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
297 return -EBUSY;
298
Steven Rostedt2b499382011-05-03 22:49:52 -0400299 if (FTRACE_WARN_ON(ops == &global_ops))
300 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200301
Steven Rostedtb8489142011-05-04 09:27:52 -0400302 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
303 ret = remove_ftrace_ops(&ftrace_global_list, ops);
304 if (!ret && ftrace_global_list == &ftrace_list_end)
305 ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops);
306 if (!ret)
307 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
308 } else
309 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
310
Steven Rostedt2b499382011-05-03 22:49:52 -0400311 if (ret < 0)
312 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400313
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400314 if (ftrace_enabled)
315 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200316
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400317 /*
318 * Dynamic ops may be freed, we must make sure that all
319 * callers are done before leaving this function.
320 */
321 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
322 synchronize_sched();
323
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500324 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200325}
326
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500327static void ftrace_update_pid_func(void)
328{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400329 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500330 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900331 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500332
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400333 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500334}
335
Steven Rostedt493762f2009-03-23 17:12:36 -0400336#ifdef CONFIG_FUNCTION_PROFILER
337struct ftrace_profile {
338 struct hlist_node node;
339 unsigned long ip;
340 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400341#ifdef CONFIG_FUNCTION_GRAPH_TRACER
342 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400343 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400344#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400345};
346
347struct ftrace_profile_page {
348 struct ftrace_profile_page *next;
349 unsigned long index;
350 struct ftrace_profile records[];
351};
352
Steven Rostedtcafb1682009-03-24 20:50:39 -0400353struct ftrace_profile_stat {
354 atomic_t disabled;
355 struct hlist_head *hash;
356 struct ftrace_profile_page *pages;
357 struct ftrace_profile_page *start;
358 struct tracer_stat stat;
359};
360
Steven Rostedt493762f2009-03-23 17:12:36 -0400361#define PROFILE_RECORDS_SIZE \
362 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
363
364#define PROFILES_PER_PAGE \
365 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
366
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400367static int ftrace_profile_bits __read_mostly;
368static int ftrace_profile_enabled __read_mostly;
369
370/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400371static DEFINE_MUTEX(ftrace_profile_lock);
372
Steven Rostedtcafb1682009-03-24 20:50:39 -0400373static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400374
375#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
376
Steven Rostedt493762f2009-03-23 17:12:36 -0400377static void *
378function_stat_next(void *v, int idx)
379{
380 struct ftrace_profile *rec = v;
381 struct ftrace_profile_page *pg;
382
383 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
384
385 again:
Li Zefan0296e422009-06-26 11:15:37 +0800386 if (idx != 0)
387 rec++;
388
Steven Rostedt493762f2009-03-23 17:12:36 -0400389 if ((void *)rec >= (void *)&pg->records[pg->index]) {
390 pg = pg->next;
391 if (!pg)
392 return NULL;
393 rec = &pg->records[0];
394 if (!rec->counter)
395 goto again;
396 }
397
398 return rec;
399}
400
401static void *function_stat_start(struct tracer_stat *trace)
402{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400403 struct ftrace_profile_stat *stat =
404 container_of(trace, struct ftrace_profile_stat, stat);
405
406 if (!stat || !stat->start)
407 return NULL;
408
409 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400410}
411
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400412#ifdef CONFIG_FUNCTION_GRAPH_TRACER
413/* function graph compares on total time */
414static int function_stat_cmp(void *p1, void *p2)
415{
416 struct ftrace_profile *a = p1;
417 struct ftrace_profile *b = p2;
418
419 if (a->time < b->time)
420 return -1;
421 if (a->time > b->time)
422 return 1;
423 else
424 return 0;
425}
426#else
427/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400428static int function_stat_cmp(void *p1, void *p2)
429{
430 struct ftrace_profile *a = p1;
431 struct ftrace_profile *b = p2;
432
433 if (a->counter < b->counter)
434 return -1;
435 if (a->counter > b->counter)
436 return 1;
437 else
438 return 0;
439}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400440#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400441
442static int function_stat_headers(struct seq_file *m)
443{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400444#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400445 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400446 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400447 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400448 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400449#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400450 seq_printf(m, " Function Hit\n"
451 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400452#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400453 return 0;
454}
455
456static int function_stat_show(struct seq_file *m, void *v)
457{
458 struct ftrace_profile *rec = v;
459 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800460 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400461#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400462 static struct trace_seq s;
463 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400464 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400465#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800466 mutex_lock(&ftrace_profile_lock);
467
468 /* we raced with function_profile_reset() */
469 if (unlikely(rec->counter == 0)) {
470 ret = -EBUSY;
471 goto out;
472 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400473
474 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400475 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400476
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400477#ifdef CONFIG_FUNCTION_GRAPH_TRACER
478 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400479 avg = rec->time;
480 do_div(avg, rec->counter);
481
Chase Douglase330b3b2010-04-26 14:02:05 -0400482 /* Sample standard deviation (s^2) */
483 if (rec->counter <= 1)
484 stddev = 0;
485 else {
486 stddev = rec->time_squared - rec->counter * avg * avg;
487 /*
488 * Divide only 1000 for ns^2 -> us^2 conversion.
489 * trace_print_graph_duration will divide 1000 again.
490 */
491 do_div(stddev, (rec->counter - 1) * 1000);
492 }
493
Steven Rostedt34886c82009-03-25 21:00:47 -0400494 trace_seq_init(&s);
495 trace_print_graph_duration(rec->time, &s);
496 trace_seq_puts(&s, " ");
497 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400498 trace_seq_puts(&s, " ");
499 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400500 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400501#endif
502 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800503out:
504 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400505
Li Zefan3aaba202010-08-23 16:50:12 +0800506 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400507}
508
Steven Rostedtcafb1682009-03-24 20:50:39 -0400509static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400510{
511 struct ftrace_profile_page *pg;
512
Steven Rostedtcafb1682009-03-24 20:50:39 -0400513 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400514
515 while (pg) {
516 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
517 pg->index = 0;
518 pg = pg->next;
519 }
520
Steven Rostedtcafb1682009-03-24 20:50:39 -0400521 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400522 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
523}
524
Steven Rostedtcafb1682009-03-24 20:50:39 -0400525int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400526{
527 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400528 int functions;
529 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400530 int i;
531
532 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400533 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400534 return 0;
535
Steven Rostedtcafb1682009-03-24 20:50:39 -0400536 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
537 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400538 return -ENOMEM;
539
Steven Rostedt318e0a72009-03-25 20:06:34 -0400540#ifdef CONFIG_DYNAMIC_FTRACE
541 functions = ftrace_update_tot_cnt;
542#else
543 /*
544 * We do not know the number of functions that exist because
545 * dynamic tracing is what counts them. With past experience
546 * we have around 20K functions. That should be more than enough.
547 * It is highly unlikely we will execute every function in
548 * the kernel.
549 */
550 functions = 20000;
551#endif
552
Steven Rostedtcafb1682009-03-24 20:50:39 -0400553 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400554
Steven Rostedt318e0a72009-03-25 20:06:34 -0400555 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
556
557 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400558 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400559 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400560 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400561 pg = pg->next;
562 }
563
564 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400565
566 out_free:
567 pg = stat->start;
568 while (pg) {
569 unsigned long tmp = (unsigned long)pg;
570
571 pg = pg->next;
572 free_page(tmp);
573 }
574
575 free_page((unsigned long)stat->pages);
576 stat->pages = NULL;
577 stat->start = NULL;
578
579 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400580}
581
Steven Rostedtcafb1682009-03-24 20:50:39 -0400582static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400583{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400584 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400585 int size;
586
Steven Rostedtcafb1682009-03-24 20:50:39 -0400587 stat = &per_cpu(ftrace_profile_stats, cpu);
588
589 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400590 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400591 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400592 return 0;
593 }
594
595 /*
596 * We are profiling all functions, but usually only a few thousand
597 * functions are hit. We'll make a hash of 1024 items.
598 */
599 size = FTRACE_PROFILE_HASH_SIZE;
600
Steven Rostedtcafb1682009-03-24 20:50:39 -0400601 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400602
Steven Rostedtcafb1682009-03-24 20:50:39 -0400603 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400604 return -ENOMEM;
605
Steven Rostedtcafb1682009-03-24 20:50:39 -0400606 if (!ftrace_profile_bits) {
607 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400608
Steven Rostedtcafb1682009-03-24 20:50:39 -0400609 for (; size; size >>= 1)
610 ftrace_profile_bits++;
611 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400612
Steven Rostedt318e0a72009-03-25 20:06:34 -0400613 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400614 if (ftrace_profile_pages_init(stat) < 0) {
615 kfree(stat->hash);
616 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400617 return -ENOMEM;
618 }
619
620 return 0;
621}
622
Steven Rostedtcafb1682009-03-24 20:50:39 -0400623static int ftrace_profile_init(void)
624{
625 int cpu;
626 int ret = 0;
627
628 for_each_online_cpu(cpu) {
629 ret = ftrace_profile_init_cpu(cpu);
630 if (ret)
631 break;
632 }
633
634 return ret;
635}
636
Steven Rostedt493762f2009-03-23 17:12:36 -0400637/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400638static struct ftrace_profile *
639ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400640{
641 struct ftrace_profile *rec;
642 struct hlist_head *hhd;
643 struct hlist_node *n;
644 unsigned long key;
645
646 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400647 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400648
649 if (hlist_empty(hhd))
650 return NULL;
651
652 hlist_for_each_entry_rcu(rec, n, hhd, node) {
653 if (rec->ip == ip)
654 return rec;
655 }
656
657 return NULL;
658}
659
Steven Rostedtcafb1682009-03-24 20:50:39 -0400660static void ftrace_add_profile(struct ftrace_profile_stat *stat,
661 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400662{
663 unsigned long key;
664
665 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400666 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400667}
668
Steven Rostedt318e0a72009-03-25 20:06:34 -0400669/*
670 * The memory is already allocated, this simply finds a new record to use.
671 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400672static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400673ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400674{
675 struct ftrace_profile *rec = NULL;
676
Steven Rostedt318e0a72009-03-25 20:06:34 -0400677 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400678 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400679 goto out;
680
Steven Rostedt493762f2009-03-23 17:12:36 -0400681 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400682 * Try to find the function again since an NMI
683 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400684 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400685 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400686 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400687 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400688
Steven Rostedtcafb1682009-03-24 20:50:39 -0400689 if (stat->pages->index == PROFILES_PER_PAGE) {
690 if (!stat->pages->next)
691 goto out;
692 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400693 }
694
Steven Rostedtcafb1682009-03-24 20:50:39 -0400695 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400696 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400697 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400698
Steven Rostedt493762f2009-03-23 17:12:36 -0400699 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400700 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400701
702 return rec;
703}
704
Steven Rostedt493762f2009-03-23 17:12:36 -0400705static void
706function_profile_call(unsigned long ip, unsigned long parent_ip)
707{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400708 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400709 struct ftrace_profile *rec;
710 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400711
712 if (!ftrace_profile_enabled)
713 return;
714
Steven Rostedt493762f2009-03-23 17:12:36 -0400715 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716
717 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400718 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400719 goto out;
720
721 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400722 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400723 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400724 if (!rec)
725 goto out;
726 }
727
728 rec->counter++;
729 out:
730 local_irq_restore(flags);
731}
732
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400733#ifdef CONFIG_FUNCTION_GRAPH_TRACER
734static int profile_graph_entry(struct ftrace_graph_ent *trace)
735{
736 function_profile_call(trace->func, 0);
737 return 1;
738}
739
740static void profile_graph_return(struct ftrace_graph_ret *trace)
741{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400742 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400743 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400744 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400745 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400746
747 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400749 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750 goto out;
751
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400752 /* If the calltime was zero'd ignore it */
753 if (!trace->calltime)
754 goto out;
755
Steven Rostedta2a16d62009-03-24 23:17:58 -0400756 calltime = trace->rettime - trace->calltime;
757
758 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
759 int index;
760
761 index = trace->depth;
762
763 /* Append this call time to the parent time to subtract */
764 if (index)
765 current->ret_stack[index - 1].subtime += calltime;
766
767 if (current->ret_stack[index].subtime < calltime)
768 calltime -= current->ret_stack[index].subtime;
769 else
770 calltime = 0;
771 }
772
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400774 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400775 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400776 rec->time_squared += calltime * calltime;
777 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400778
Steven Rostedtcafb1682009-03-24 20:50:39 -0400779 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400780 local_irq_restore(flags);
781}
782
783static int register_ftrace_profiler(void)
784{
785 return register_ftrace_graph(&profile_graph_return,
786 &profile_graph_entry);
787}
788
789static void unregister_ftrace_profiler(void)
790{
791 unregister_ftrace_graph();
792}
793#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100794static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400795 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400796};
797
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400798static int register_ftrace_profiler(void)
799{
800 return register_ftrace_function(&ftrace_profile_ops);
801}
802
803static void unregister_ftrace_profiler(void)
804{
805 unregister_ftrace_function(&ftrace_profile_ops);
806}
807#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
808
Steven Rostedt493762f2009-03-23 17:12:36 -0400809static ssize_t
810ftrace_profile_write(struct file *filp, const char __user *ubuf,
811 size_t cnt, loff_t *ppos)
812{
813 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400814 int ret;
815
Peter Huewe22fe9b52011-06-07 21:58:27 +0200816 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
817 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400818 return ret;
819
820 val = !!val;
821
822 mutex_lock(&ftrace_profile_lock);
823 if (ftrace_profile_enabled ^ val) {
824 if (val) {
825 ret = ftrace_profile_init();
826 if (ret < 0) {
827 cnt = ret;
828 goto out;
829 }
830
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400831 ret = register_ftrace_profiler();
832 if (ret < 0) {
833 cnt = ret;
834 goto out;
835 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400836 ftrace_profile_enabled = 1;
837 } else {
838 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400839 /*
840 * unregister_ftrace_profiler calls stop_machine
841 * so this acts like an synchronize_sched.
842 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400843 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400844 }
845 }
846 out:
847 mutex_unlock(&ftrace_profile_lock);
848
Jiri Olsacf8517c2009-10-23 19:36:16 -0400849 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400850
851 return cnt;
852}
853
854static ssize_t
855ftrace_profile_read(struct file *filp, char __user *ubuf,
856 size_t cnt, loff_t *ppos)
857{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400858 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400859 int r;
860
861 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
862 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
863}
864
865static const struct file_operations ftrace_profile_fops = {
866 .open = tracing_open_generic,
867 .read = ftrace_profile_read,
868 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200869 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400870};
871
Steven Rostedtcafb1682009-03-24 20:50:39 -0400872/* used to initialize the real stat files */
873static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400874 .name = "functions",
875 .stat_start = function_stat_start,
876 .stat_next = function_stat_next,
877 .stat_cmp = function_stat_cmp,
878 .stat_headers = function_stat_headers,
879 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400880};
881
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400882static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400883{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400884 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400885 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400886 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400887 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400888 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400889
Steven Rostedtcafb1682009-03-24 20:50:39 -0400890 for_each_possible_cpu(cpu) {
891 stat = &per_cpu(ftrace_profile_stats, cpu);
892
893 /* allocate enough for function name + cpu number */
894 name = kmalloc(32, GFP_KERNEL);
895 if (!name) {
896 /*
897 * The files created are permanent, if something happens
898 * we still do not free memory.
899 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400900 WARN(1,
901 "Could not allocate stat file for cpu %d\n",
902 cpu);
903 return;
904 }
905 stat->stat = function_stats;
906 snprintf(name, 32, "function%d", cpu);
907 stat->stat.name = name;
908 ret = register_stat_tracer(&stat->stat);
909 if (ret) {
910 WARN(1,
911 "Could not register function stat for cpu %d\n",
912 cpu);
913 kfree(name);
914 return;
915 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400916 }
917
918 entry = debugfs_create_file("function_profile_enabled", 0644,
919 d_tracer, NULL, &ftrace_profile_fops);
920 if (!entry)
921 pr_warning("Could not create debugfs "
922 "'function_profile_enabled' entry\n");
923}
924
925#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400926static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400927{
928}
929#endif /* CONFIG_FUNCTION_PROFILER */
930
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100931static struct pid * const ftrace_swapper_pid = &init_struct_pid;
932
Steven Rostedt3d083392008-05-12 21:20:42 +0200933#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100934
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400935#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400936# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400937#endif
938
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500939static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
940
Steven Rostedtb6887d72009-02-17 12:32:04 -0500941struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500942 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500943 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500944 unsigned long flags;
945 unsigned long ip;
946 void *data;
947 struct rcu_head rcu;
948};
949
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400950struct ftrace_func_entry {
951 struct hlist_node hlist;
952 unsigned long ip;
953};
954
955struct ftrace_hash {
956 unsigned long size_bits;
957 struct hlist_head *buckets;
958 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -0400959 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400960};
961
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400962/*
963 * We make these constant because no one should touch them,
964 * but they are used as the default "empty hash", to avoid allocating
965 * it all the time. These are in a read only section such that if
966 * anyone does try to modify it, it will cause an exception.
967 */
968static const struct hlist_head empty_buckets[1];
969static const struct ftrace_hash empty_hash = {
970 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400971};
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400972#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +0200973
Steven Rostedt2b499382011-05-03 22:49:52 -0400974static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -0400975 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400976 .notrace_hash = EMPTY_HASH,
977 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -0400978};
979
Lai Jiangshane94142a2009-03-13 17:51:27 +0800980static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200981
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 Rostedt431aa3f2009-01-06 12:43:01 -0500986 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200987 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700988};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200989
990#define ENTRIES_PER_PAGE \
991 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
992
993/* estimate from running different kernels */
994#define NR_TO_INIT 10000
995
996static struct ftrace_page *ftrace_pages_start;
997static struct ftrace_page *ftrace_pages;
998
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400999static struct ftrace_func_entry *
1000ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1001{
1002 unsigned long key;
1003 struct ftrace_func_entry *entry;
1004 struct hlist_head *hhd;
1005 struct hlist_node *n;
1006
1007 if (!hash->count)
1008 return NULL;
1009
1010 if (hash->size_bits > 0)
1011 key = hash_long(ip, hash->size_bits);
1012 else
1013 key = 0;
1014
1015 hhd = &hash->buckets[key];
1016
1017 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1018 if (entry->ip == ip)
1019 return entry;
1020 }
1021 return NULL;
1022}
1023
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001024static void __add_hash_entry(struct ftrace_hash *hash,
1025 struct ftrace_func_entry *entry)
1026{
1027 struct hlist_head *hhd;
1028 unsigned long key;
1029
1030 if (hash->size_bits)
1031 key = hash_long(entry->ip, hash->size_bits);
1032 else
1033 key = 0;
1034
1035 hhd = &hash->buckets[key];
1036 hlist_add_head(&entry->hlist, hhd);
1037 hash->count++;
1038}
1039
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001040static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1041{
1042 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001043
1044 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1045 if (!entry)
1046 return -ENOMEM;
1047
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001048 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001049 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001050
1051 return 0;
1052}
1053
1054static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001055free_hash_entry(struct ftrace_hash *hash,
1056 struct ftrace_func_entry *entry)
1057{
1058 hlist_del(&entry->hlist);
1059 kfree(entry);
1060 hash->count--;
1061}
1062
1063static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001064remove_hash_entry(struct ftrace_hash *hash,
1065 struct ftrace_func_entry *entry)
1066{
1067 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001068 hash->count--;
1069}
1070
1071static void ftrace_hash_clear(struct ftrace_hash *hash)
1072{
1073 struct hlist_head *hhd;
1074 struct hlist_node *tp, *tn;
1075 struct ftrace_func_entry *entry;
1076 int size = 1 << hash->size_bits;
1077 int i;
1078
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001079 if (!hash->count)
1080 return;
1081
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001082 for (i = 0; i < size; i++) {
1083 hhd = &hash->buckets[i];
1084 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001085 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001086 }
1087 FTRACE_WARN_ON(hash->count);
1088}
1089
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001090static void free_ftrace_hash(struct ftrace_hash *hash)
1091{
1092 if (!hash || hash == EMPTY_HASH)
1093 return;
1094 ftrace_hash_clear(hash);
1095 kfree(hash->buckets);
1096 kfree(hash);
1097}
1098
Steven Rostedt07fd5512011-05-05 18:03:47 -04001099static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1100{
1101 struct ftrace_hash *hash;
1102
1103 hash = container_of(rcu, struct ftrace_hash, rcu);
1104 free_ftrace_hash(hash);
1105}
1106
1107static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1108{
1109 if (!hash || hash == EMPTY_HASH)
1110 return;
1111 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1112}
1113
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001114static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1115{
1116 struct ftrace_hash *hash;
1117 int size;
1118
1119 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1120 if (!hash)
1121 return NULL;
1122
1123 size = 1 << size_bits;
1124 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1125
1126 if (!hash->buckets) {
1127 kfree(hash);
1128 return NULL;
1129 }
1130
1131 hash->size_bits = size_bits;
1132
1133 return hash;
1134}
1135
1136static struct ftrace_hash *
1137alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1138{
1139 struct ftrace_func_entry *entry;
1140 struct ftrace_hash *new_hash;
1141 struct hlist_node *tp;
1142 int size;
1143 int ret;
1144 int i;
1145
1146 new_hash = alloc_ftrace_hash(size_bits);
1147 if (!new_hash)
1148 return NULL;
1149
1150 /* Empty hash? */
1151 if (!hash || !hash->count)
1152 return new_hash;
1153
1154 size = 1 << hash->size_bits;
1155 for (i = 0; i < size; i++) {
1156 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1157 ret = add_hash_entry(new_hash, entry->ip);
1158 if (ret < 0)
1159 goto free_hash;
1160 }
1161 }
1162
1163 FTRACE_WARN_ON(new_hash->count != hash->count);
1164
1165 return new_hash;
1166
1167 free_hash:
1168 free_ftrace_hash(new_hash);
1169 return NULL;
1170}
1171
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001172static void
1173ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1174static void
1175ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1176
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001177static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001178ftrace_hash_move(struct ftrace_ops *ops, int enable,
1179 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001180{
1181 struct ftrace_func_entry *entry;
1182 struct hlist_node *tp, *tn;
1183 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001184 struct ftrace_hash *old_hash;
1185 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001186 unsigned long key;
1187 int size = src->count;
1188 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001189 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001190 int i;
1191
1192 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001193 * Remove the current set, update the hash and add
1194 * them back.
1195 */
1196 ftrace_hash_rec_disable(ops, enable);
1197
1198 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001199 * If the new source is empty, just free dst and assign it
1200 * the empty_hash.
1201 */
1202 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001203 free_ftrace_hash_rcu(*dst);
1204 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001205 /* still need to update the function records */
1206 ret = 0;
1207 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001208 }
1209
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001210 /*
1211 * Make the hash size about 1/2 the # found
1212 */
1213 for (size /= 2; size; size >>= 1)
1214 bits++;
1215
1216 /* Don't allocate too much */
1217 if (bits > FTRACE_HASH_MAX_BITS)
1218 bits = FTRACE_HASH_MAX_BITS;
1219
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001220 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001221 new_hash = alloc_ftrace_hash(bits);
1222 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001223 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001224
1225 size = 1 << src->size_bits;
1226 for (i = 0; i < size; i++) {
1227 hhd = &src->buckets[i];
1228 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1229 if (bits > 0)
1230 key = hash_long(entry->ip, bits);
1231 else
1232 key = 0;
1233 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001234 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001235 }
1236 }
1237
Steven Rostedt07fd5512011-05-05 18:03:47 -04001238 old_hash = *dst;
1239 rcu_assign_pointer(*dst, new_hash);
1240 free_ftrace_hash_rcu(old_hash);
1241
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001242 ret = 0;
1243 out:
1244 /*
1245 * Enable regardless of ret:
1246 * On success, we enable the new hash.
1247 * On failure, we re-enable the original hash.
1248 */
1249 ftrace_hash_rec_enable(ops, enable);
1250
1251 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001252}
1253
Steven Rostedt265c8312009-02-13 12:43:56 -05001254/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001255 * Test the hashes for this ops to see if we want to call
1256 * the ops->func or not.
1257 *
1258 * It's a match if the ip is in the ops->filter_hash or
1259 * the filter_hash does not exist or is empty,
1260 * AND
1261 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001262 *
1263 * This needs to be called with preemption disabled as
1264 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001265 */
1266static int
1267ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1268{
1269 struct ftrace_hash *filter_hash;
1270 struct ftrace_hash *notrace_hash;
1271 int ret;
1272
Steven Rostedtb8489142011-05-04 09:27:52 -04001273 filter_hash = rcu_dereference_raw(ops->filter_hash);
1274 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1275
1276 if ((!filter_hash || !filter_hash->count ||
1277 ftrace_lookup_ip(filter_hash, ip)) &&
1278 (!notrace_hash || !notrace_hash->count ||
1279 !ftrace_lookup_ip(notrace_hash, ip)))
1280 ret = 1;
1281 else
1282 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001283
1284 return ret;
1285}
1286
1287/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001288 * This is a double for. Do not use 'break' to break out of the loop,
1289 * you must use a goto.
1290 */
1291#define do_for_each_ftrace_rec(pg, rec) \
1292 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1293 int _____i; \
1294 for (_____i = 0; _____i < pg->index; _____i++) { \
1295 rec = &pg->records[_____i];
1296
1297#define while_for_each_ftrace_rec() \
1298 } \
1299 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301300
Steven Rostedtc88fd862011-08-16 09:53:39 -04001301/**
1302 * ftrace_location - return true if the ip giving is a traced location
1303 * @ip: the instruction pointer to check
1304 *
1305 * Returns 1 if @ip given is a pointer to a ftrace location.
1306 * That is, the instruction that is either a NOP or call to
1307 * the function tracer. It checks the ftrace internal tables to
1308 * determine if the address belongs or not.
1309 */
1310int ftrace_location(unsigned long ip)
1311{
1312 struct ftrace_page *pg;
1313 struct dyn_ftrace *rec;
1314
1315 do_for_each_ftrace_rec(pg, rec) {
1316 if (rec->ip == ip)
1317 return 1;
1318 } while_for_each_ftrace_rec();
1319
1320 return 0;
1321}
1322
Steven Rostedted926f92011-05-03 13:25:24 -04001323static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1324 int filter_hash,
1325 bool inc)
1326{
1327 struct ftrace_hash *hash;
1328 struct ftrace_hash *other_hash;
1329 struct ftrace_page *pg;
1330 struct dyn_ftrace *rec;
1331 int count = 0;
1332 int all = 0;
1333
1334 /* Only update if the ops has been registered */
1335 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1336 return;
1337
1338 /*
1339 * In the filter_hash case:
1340 * If the count is zero, we update all records.
1341 * Otherwise we just update the items in the hash.
1342 *
1343 * In the notrace_hash case:
1344 * We enable the update in the hash.
1345 * As disabling notrace means enabling the tracing,
1346 * and enabling notrace means disabling, the inc variable
1347 * gets inversed.
1348 */
1349 if (filter_hash) {
1350 hash = ops->filter_hash;
1351 other_hash = ops->notrace_hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001352 if (!hash || !hash->count)
Steven Rostedted926f92011-05-03 13:25:24 -04001353 all = 1;
1354 } else {
1355 inc = !inc;
1356 hash = ops->notrace_hash;
1357 other_hash = ops->filter_hash;
1358 /*
1359 * If the notrace hash has no items,
1360 * then there's nothing to do.
1361 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001362 if (hash && !hash->count)
Steven Rostedted926f92011-05-03 13:25:24 -04001363 return;
1364 }
1365
1366 do_for_each_ftrace_rec(pg, rec) {
1367 int in_other_hash = 0;
1368 int in_hash = 0;
1369 int match = 0;
1370
1371 if (all) {
1372 /*
1373 * Only the filter_hash affects all records.
1374 * Update if the record is not in the notrace hash.
1375 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001376 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001377 match = 1;
1378 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -04001379 in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip);
1380 in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001381
1382 /*
1383 *
1384 */
1385 if (filter_hash && in_hash && !in_other_hash)
1386 match = 1;
1387 else if (!filter_hash && in_hash &&
1388 (in_other_hash || !other_hash->count))
1389 match = 1;
1390 }
1391 if (!match)
1392 continue;
1393
1394 if (inc) {
1395 rec->flags++;
1396 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1397 return;
1398 } else {
1399 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1400 return;
1401 rec->flags--;
1402 }
1403 count++;
1404 /* Shortcut, if we handled all records, we are done. */
1405 if (!all && count == hash->count)
1406 return;
1407 } while_for_each_ftrace_rec();
1408}
1409
1410static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1411 int filter_hash)
1412{
1413 __ftrace_hash_rec_update(ops, filter_hash, 0);
1414}
1415
1416static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1417 int filter_hash)
1418{
1419 __ftrace_hash_rec_update(ops, filter_hash, 1);
1420}
1421
Ingo Molnare309b412008-05-12 21:20:51 +02001422static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001423{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001424 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001425 if (!ftrace_pages->next) {
1426 /* allocate another page */
1427 ftrace_pages->next =
1428 (void *)get_zeroed_page(GFP_KERNEL);
1429 if (!ftrace_pages->next)
1430 return NULL;
1431 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001432 ftrace_pages = ftrace_pages->next;
1433 }
1434
1435 return &ftrace_pages->records[ftrace_pages->index++];
1436}
1437
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001438static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001439ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001440{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001441 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001442
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001443 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001444 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001445
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001446 rec = ftrace_alloc_dyn_node(ip);
1447 if (!rec)
1448 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001449
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001450 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001451 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001452 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001453
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001454 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001455}
1456
Steven Rostedt05736a42008-09-22 14:55:47 -07001457static void print_ip_ins(const char *fmt, unsigned char *p)
1458{
1459 int i;
1460
1461 printk(KERN_CONT "%s", fmt);
1462
1463 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1464 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1465}
1466
Steven Rostedtc88fd862011-08-16 09:53:39 -04001467/**
1468 * ftrace_bug - report and shutdown function tracer
1469 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1470 * @ip: The address that failed
1471 *
1472 * The arch code that enables or disables the function tracing
1473 * can call ftrace_bug() when it has detected a problem in
1474 * modifying the code. @failed should be one of either:
1475 * EFAULT - if the problem happens on reading the @ip address
1476 * EINVAL - if what is read at @ip is not what was expected
1477 * EPERM - if the problem happens on writting to the @ip address
1478 */
1479void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001480{
1481 switch (failed) {
1482 case -EFAULT:
1483 FTRACE_WARN_ON_ONCE(1);
1484 pr_info("ftrace faulted on modifying ");
1485 print_ip_sym(ip);
1486 break;
1487 case -EINVAL:
1488 FTRACE_WARN_ON_ONCE(1);
1489 pr_info("ftrace failed to modify ");
1490 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001491 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001492 printk(KERN_CONT "\n");
1493 break;
1494 case -EPERM:
1495 FTRACE_WARN_ON_ONCE(1);
1496 pr_info("ftrace faulted on writing ");
1497 print_ip_sym(ip);
1498 break;
1499 default:
1500 FTRACE_WARN_ON_ONCE(1);
1501 pr_info("ftrace faulted on unknown error ");
1502 print_ip_sym(ip);
1503 }
1504}
1505
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001506
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001507/* Return 1 if the address range is reserved for ftrace */
1508int ftrace_text_reserved(void *start, void *end)
1509{
1510 struct dyn_ftrace *rec;
1511 struct ftrace_page *pg;
1512
1513 do_for_each_ftrace_rec(pg, rec) {
1514 if (rec->ip <= (unsigned long)end &&
1515 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1516 return 1;
1517 } while_for_each_ftrace_rec();
1518 return 0;
1519}
1520
Steven Rostedtc88fd862011-08-16 09:53:39 -04001521static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001522{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001523 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001524
Steven Rostedt982c3502008-11-15 16:31:41 -05001525 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001526 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001527 *
Steven Rostedted926f92011-05-03 13:25:24 -04001528 * If the record has a ref count, then we need to enable it
1529 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001530 *
Steven Rostedted926f92011-05-03 13:25:24 -04001531 * Otherwise we make sure its disabled.
1532 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001533 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001534 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001535 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001536 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001537 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001538
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001539 /* If the state of this record hasn't changed, then do nothing */
1540 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001541 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001542
1543 if (flag) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001544 if (update)
1545 rec->flags |= FTRACE_FL_ENABLED;
1546 return FTRACE_UPDATE_MAKE_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001547 }
1548
Steven Rostedtc88fd862011-08-16 09:53:39 -04001549 if (update)
1550 rec->flags &= ~FTRACE_FL_ENABLED;
1551
1552 return FTRACE_UPDATE_MAKE_NOP;
1553}
1554
1555/**
1556 * ftrace_update_record, set a record that now is tracing or not
1557 * @rec: the record to update
1558 * @enable: set to 1 if the record is tracing, zero to force disable
1559 *
1560 * The records that represent all functions that can be traced need
1561 * to be updated when tracing has been enabled.
1562 */
1563int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1564{
1565 return ftrace_check_record(rec, enable, 1);
1566}
1567
1568/**
1569 * ftrace_test_record, check if the record has been enabled or not
1570 * @rec: the record to test
1571 * @enable: set to 1 to check if enabled, 0 if it is disabled
1572 *
1573 * The arch code may need to test if a record is already set to
1574 * tracing to determine how to modify the function code that it
1575 * represents.
1576 */
1577int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1578{
1579 return ftrace_check_record(rec, enable, 0);
1580}
1581
1582static int
1583__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1584{
1585 unsigned long ftrace_addr;
1586 int ret;
1587
1588 ftrace_addr = (unsigned long)FTRACE_ADDR;
1589
1590 ret = ftrace_update_record(rec, enable);
1591
1592 switch (ret) {
1593 case FTRACE_UPDATE_IGNORE:
1594 return 0;
1595
1596 case FTRACE_UPDATE_MAKE_CALL:
1597 return ftrace_make_call(rec, ftrace_addr);
1598
1599 case FTRACE_UPDATE_MAKE_NOP:
1600 return ftrace_make_nop(NULL, rec, ftrace_addr);
1601 }
1602
1603 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001604}
1605
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001606static void ftrace_replace_code(int update)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001607{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001608 struct dyn_ftrace *rec;
1609 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001610 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001611
Steven Rostedt45a4a232011-04-21 23:16:46 -04001612 if (unlikely(ftrace_disabled))
1613 return;
1614
Steven Rostedt265c8312009-02-13 12:43:56 -05001615 do_for_each_ftrace_rec(pg, rec) {
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001616 failed = __ftrace_replace_code(rec, update);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001617 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001618 ftrace_bug(failed, rec->ip);
1619 /* Stop processing */
1620 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001621 }
1622 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001623}
1624
Steven Rostedtc88fd862011-08-16 09:53:39 -04001625struct ftrace_rec_iter {
1626 struct ftrace_page *pg;
1627 int index;
1628};
1629
1630/**
1631 * ftrace_rec_iter_start, start up iterating over traced functions
1632 *
1633 * Returns an iterator handle that is used to iterate over all
1634 * the records that represent address locations where functions
1635 * are traced.
1636 *
1637 * May return NULL if no records are available.
1638 */
1639struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1640{
1641 /*
1642 * We only use a single iterator.
1643 * Protected by the ftrace_lock mutex.
1644 */
1645 static struct ftrace_rec_iter ftrace_rec_iter;
1646 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1647
1648 iter->pg = ftrace_pages_start;
1649 iter->index = 0;
1650
1651 /* Could have empty pages */
1652 while (iter->pg && !iter->pg->index)
1653 iter->pg = iter->pg->next;
1654
1655 if (!iter->pg)
1656 return NULL;
1657
1658 return iter;
1659}
1660
1661/**
1662 * ftrace_rec_iter_next, get the next record to process.
1663 * @iter: The handle to the iterator.
1664 *
1665 * Returns the next iterator after the given iterator @iter.
1666 */
1667struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1668{
1669 iter->index++;
1670
1671 if (iter->index >= iter->pg->index) {
1672 iter->pg = iter->pg->next;
1673 iter->index = 0;
1674
1675 /* Could have empty pages */
1676 while (iter->pg && !iter->pg->index)
1677 iter->pg = iter->pg->next;
1678 }
1679
1680 if (!iter->pg)
1681 return NULL;
1682
1683 return iter;
1684}
1685
1686/**
1687 * ftrace_rec_iter_record, get the record at the iterator location
1688 * @iter: The current iterator location
1689 *
1690 * Returns the record that the current @iter is at.
1691 */
1692struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1693{
1694 return &iter->pg->records[iter->index];
1695}
1696
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301697static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001698ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001699{
1700 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001701 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001702
1703 ip = rec->ip;
1704
Steven Rostedt45a4a232011-04-21 23:16:46 -04001705 if (unlikely(ftrace_disabled))
1706 return 0;
1707
Shaohua Li25aac9d2009-01-09 11:29:40 +08001708 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001709 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001710 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301711 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001712 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301713 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001714}
1715
Steven Rostedt000ab692009-02-17 13:35:06 -05001716/*
1717 * archs can override this function if they must do something
1718 * before the modifying code is performed.
1719 */
1720int __weak ftrace_arch_code_modify_prepare(void)
1721{
1722 return 0;
1723}
1724
1725/*
1726 * archs can override this function if they must do something
1727 * after the modifying code is performed.
1728 */
1729int __weak ftrace_arch_code_modify_post_process(void)
1730{
1731 return 0;
1732}
1733
Ingo Molnare309b412008-05-12 21:20:51 +02001734static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001735{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001736 int *command = data;
1737
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001738 if (*command & FTRACE_UPDATE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001739 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001740 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001741 ftrace_replace_code(0);
1742
1743 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1744 ftrace_update_ftrace_func(ftrace_trace_function);
1745
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001746 if (*command & FTRACE_START_FUNC_RET)
1747 ftrace_enable_ftrace_graph_caller();
1748 else if (*command & FTRACE_STOP_FUNC_RET)
1749 ftrace_disable_ftrace_graph_caller();
1750
Steven Rostedtc88fd862011-08-16 09:53:39 -04001751 return 0;
1752}
1753
1754/**
1755 * ftrace_run_stop_machine, go back to the stop machine method
1756 * @command: The command to tell ftrace what to do
1757 *
1758 * If an arch needs to fall back to the stop machine method, the
1759 * it can call this function.
1760 */
1761void ftrace_run_stop_machine(int command)
1762{
1763 stop_machine(__ftrace_modify_code, &command, NULL);
1764}
1765
1766/**
1767 * arch_ftrace_update_code, modify the code to trace or not trace
1768 * @command: The command that needs to be done
1769 *
1770 * Archs can override this function if it does not need to
1771 * run stop_machine() to modify code.
1772 */
1773void __weak arch_ftrace_update_code(int command)
1774{
1775 ftrace_run_stop_machine(command);
1776}
1777
1778static void ftrace_run_update_code(int command)
1779{
1780 int ret;
1781
1782 ret = ftrace_arch_code_modify_prepare();
1783 FTRACE_WARN_ON(ret);
1784 if (ret)
1785 return;
1786 /*
1787 * Do not call function tracer while we update the code.
1788 * We are in stop machine.
1789 */
1790 function_trace_stop++;
1791
1792 /*
1793 * By default we use stop_machine() to modify the code.
1794 * But archs can do what ever they want as long as it
1795 * is safe. The stop_machine() is the safest, but also
1796 * produces the most overhead.
1797 */
1798 arch_ftrace_update_code(command);
1799
Steven Rostedt6331c282011-07-13 15:11:02 -04001800#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1801 /*
1802 * For archs that call ftrace_test_stop_func(), we must
1803 * wait till after we update all the function callers
1804 * before we update the callback. This keeps different
1805 * ops that record different functions from corrupting
1806 * each other.
1807 */
1808 __ftrace_trace_function = __ftrace_trace_function_delay;
1809#endif
1810 function_trace_stop--;
1811
Steven Rostedt000ab692009-02-17 13:35:06 -05001812 ret = ftrace_arch_code_modify_post_process();
1813 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001814}
1815
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001816static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001817static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001818static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001819
1820static void ftrace_startup_enable(int command)
1821{
1822 if (saved_ftrace_func != ftrace_trace_function) {
1823 saved_ftrace_func = ftrace_trace_function;
1824 command |= FTRACE_UPDATE_TRACE_FUNC;
1825 }
1826
1827 if (!command || !ftrace_enabled)
1828 return;
1829
1830 ftrace_run_update_code(command);
1831}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001832
Steven Rostedta1cd6172011-05-23 15:24:25 -04001833static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001834{
Steven Rostedtb8489142011-05-04 09:27:52 -04001835 bool hash_enable = true;
1836
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001837 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001838 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001839
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001840 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001841 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001842
Steven Rostedtb8489142011-05-04 09:27:52 -04001843 /* ops marked global share the filter hashes */
1844 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1845 ops = &global_ops;
1846 /* Don't update hash if global is already set */
1847 if (global_start_up)
1848 hash_enable = false;
1849 global_start_up++;
1850 }
1851
Steven Rostedted926f92011-05-03 13:25:24 -04001852 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001853 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001854 ftrace_hash_rec_enable(ops, 1);
1855
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001856 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001857
1858 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001859}
1860
Steven Rostedtbd69c302011-05-03 21:55:54 -04001861static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001862{
Steven Rostedtb8489142011-05-04 09:27:52 -04001863 bool hash_disable = true;
1864
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001865 if (unlikely(ftrace_disabled))
1866 return;
1867
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001868 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001869 /*
1870 * Just warn in case of unbalance, no need to kill ftrace, it's not
1871 * critical but the ftrace_call callers may be never nopped again after
1872 * further ftrace uses.
1873 */
1874 WARN_ON_ONCE(ftrace_start_up < 0);
1875
Steven Rostedtb8489142011-05-04 09:27:52 -04001876 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1877 ops = &global_ops;
1878 global_start_up--;
1879 WARN_ON_ONCE(global_start_up < 0);
1880 /* Don't update hash if global still has users */
1881 if (global_start_up) {
1882 WARN_ON_ONCE(!ftrace_start_up);
1883 hash_disable = false;
1884 }
1885 }
1886
1887 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04001888 ftrace_hash_rec_disable(ops, 1);
1889
Steven Rostedtb8489142011-05-04 09:27:52 -04001890 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04001891 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001892
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001893 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001894
1895 if (saved_ftrace_func != ftrace_trace_function) {
1896 saved_ftrace_func = ftrace_trace_function;
1897 command |= FTRACE_UPDATE_TRACE_FUNC;
1898 }
1899
1900 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001901 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001902
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001903 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001904}
1905
Ingo Molnare309b412008-05-12 21:20:51 +02001906static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001907{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001908 if (unlikely(ftrace_disabled))
1909 return;
1910
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001911 /* Force update next time */
1912 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001913 /* ftrace_start_up is true if we want ftrace running */
1914 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001915 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001916}
1917
Ingo Molnare309b412008-05-12 21:20:51 +02001918static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001919{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001920 if (unlikely(ftrace_disabled))
1921 return;
1922
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001923 /* ftrace_start_up is true if ftrace is running */
1924 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001925 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001926}
1927
Steven Rostedt3d083392008-05-12 21:20:42 +02001928static cycle_t ftrace_update_time;
1929static unsigned long ftrace_update_cnt;
1930unsigned long ftrace_update_tot_cnt;
1931
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001932static int ops_traces_mod(struct ftrace_ops *ops)
1933{
1934 struct ftrace_hash *hash;
1935
1936 hash = ops->filter_hash;
1937 return !!(!hash || !hash->count);
1938}
1939
Steven Rostedt31e88902008-11-14 16:21:19 -08001940static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001941{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001942 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301943 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001944 unsigned long ref = 0;
1945
1946 /*
1947 * When adding a module, we need to check if tracers are
1948 * currently enabled and if they are set to trace all functions.
1949 * If they are, we need to enable the module functions as well
1950 * as update the reference counts for those function records.
1951 */
1952 if (mod) {
1953 struct ftrace_ops *ops;
1954
1955 for (ops = ftrace_ops_list;
1956 ops != &ftrace_list_end; ops = ops->next) {
1957 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
1958 ops_traces_mod(ops))
1959 ref++;
1960 }
1961 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001962
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001963 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001964 ftrace_update_cnt = 0;
1965
Lai Jiangshane94142a2009-03-13 17:51:27 +08001966 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301967
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001968 /* If something went wrong, bail without enabling anything */
1969 if (unlikely(ftrace_disabled))
1970 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001971
Lai Jiangshane94142a2009-03-13 17:51:27 +08001972 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001973 ftrace_new_addrs = p->newlist;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001974 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301975
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001976 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001977 * Do the initial record conversion from mcount jump
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001978 * to the NOP instructions.
1979 */
Steven Rostedt32082302011-12-16 14:42:37 -05001980 if (!ftrace_code_disable(mod, p))
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001981 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001982
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001983 ftrace_update_cnt++;
1984
1985 /*
1986 * If the tracing is enabled, go ahead and enable the record.
1987 *
1988 * The reason not to enable the record immediatelly is the
1989 * inherent check of ftrace_make_nop/ftrace_make_call for
1990 * correct previous instructions. Making first the NOP
1991 * conversion puts the module to the correct state, thus
1992 * passing the ftrace_make_call check.
1993 */
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001994 if (ftrace_start_up && ref) {
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001995 int failed = __ftrace_replace_code(p, 1);
Steven Rostedt32082302011-12-16 14:42:37 -05001996 if (failed)
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001997 ftrace_bug(failed, p->ip);
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001998 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001999 }
2000
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002001 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002002 ftrace_update_time = stop - start;
2003 ftrace_update_tot_cnt += ftrace_update_cnt;
2004
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002005 return 0;
2006}
2007
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002008static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002009{
2010 struct ftrace_page *pg;
2011 int cnt;
2012 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002013
2014 /* allocate a few pages */
2015 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
2016 if (!ftrace_pages_start)
2017 return -1;
2018
2019 /*
2020 * Allocate a few more pages.
2021 *
2022 * TODO: have some parser search vmlinux before
2023 * final linking to find all calls to ftrace.
2024 * Then we can:
2025 * a) know how many pages to allocate.
2026 * and/or
2027 * b) set up the table then.
2028 *
2029 * The dynamic code is still necessary for
2030 * modules.
2031 */
2032
2033 pg = ftrace_pages = ftrace_pages_start;
2034
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002035 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002036 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002037 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002038
2039 for (i = 0; i < cnt; i++) {
2040 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
2041
2042 /* If we fail, we'll try later anyway */
2043 if (!pg->next)
2044 break;
2045
2046 pg = pg->next;
2047 }
2048
2049 return 0;
2050}
2051
Steven Rostedt5072c592008-05-12 21:20:43 +02002052enum {
2053 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002054 FTRACE_ITER_NOTRACE = (1 << 1),
Steven Rostedt3499e462011-04-21 22:59:12 -04002055 FTRACE_ITER_PRINTALL = (1 << 2),
2056 FTRACE_ITER_HASH = (1 << 3),
Steven Rostedt647bcd02011-05-03 14:39:21 -04002057 FTRACE_ITER_ENABLED = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02002058};
2059
2060#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2061
2062struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002063 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002064 loff_t func_pos;
2065 struct ftrace_page *pg;
2066 struct dyn_ftrace *func;
2067 struct ftrace_func_probe *probe;
2068 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002069 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002070 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002071 int hidx;
2072 int idx;
2073 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002074};
2075
Ingo Molnare309b412008-05-12 21:20:51 +02002076static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002077t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002078{
2079 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002080 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002081 struct hlist_head *hhd;
2082
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002083 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002084 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002085
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002086 if (iter->probe)
2087 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002088 retry:
2089 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2090 return NULL;
2091
2092 hhd = &ftrace_func_hash[iter->hidx];
2093
2094 if (hlist_empty(hhd)) {
2095 iter->hidx++;
2096 hnd = NULL;
2097 goto retry;
2098 }
2099
2100 if (!hnd)
2101 hnd = hhd->first;
2102 else {
2103 hnd = hnd->next;
2104 if (!hnd) {
2105 iter->hidx++;
2106 goto retry;
2107 }
2108 }
2109
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002110 if (WARN_ON_ONCE(!hnd))
2111 return NULL;
2112
2113 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2114
2115 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002116}
2117
2118static void *t_hash_start(struct seq_file *m, loff_t *pos)
2119{
2120 struct ftrace_iterator *iter = m->private;
2121 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002122 loff_t l;
2123
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002124 if (iter->func_pos > *pos)
2125 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002126
Li Zefand82d6242009-06-24 09:54:54 +08002127 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002128 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002129 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002130 if (!p)
2131 break;
2132 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002133 if (!p)
2134 return NULL;
2135
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002136 /* Only set this if we have an item */
2137 iter->flags |= FTRACE_ITER_HASH;
2138
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002139 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002140}
2141
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002142static int
2143t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002144{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002145 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002146
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002147 rec = iter->probe;
2148 if (WARN_ON_ONCE(!rec))
2149 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002150
Steven Rostedt809dcf22009-02-16 23:06:01 -05002151 if (rec->ops->print)
2152 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2153
Steven Rostedtb375a112009-09-17 00:05:58 -04002154 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002155
2156 if (rec->data)
2157 seq_printf(m, ":%p", rec->data);
2158 seq_putc(m, '\n');
2159
2160 return 0;
2161}
2162
2163static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002164t_next(struct seq_file *m, void *v, loff_t *pos)
2165{
2166 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002167 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002168 struct dyn_ftrace *rec = NULL;
2169
Steven Rostedt45a4a232011-04-21 23:16:46 -04002170 if (unlikely(ftrace_disabled))
2171 return NULL;
2172
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002173 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002174 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002175
Steven Rostedt5072c592008-05-12 21:20:43 +02002176 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002177 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002178
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002179 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002180 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002181
Steven Rostedt5072c592008-05-12 21:20:43 +02002182 retry:
2183 if (iter->idx >= iter->pg->index) {
2184 if (iter->pg->next) {
2185 iter->pg = iter->pg->next;
2186 iter->idx = 0;
2187 goto retry;
2188 }
2189 } else {
2190 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002191 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002192 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002193
Steven Rostedt41c52c02008-05-22 11:46:33 -04002194 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002195 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2196
2197 ((iter->flags & FTRACE_ITER_ENABLED) &&
2198 !(rec->flags & ~FTRACE_FL_MASK))) {
2199
Steven Rostedt5072c592008-05-12 21:20:43 +02002200 rec = NULL;
2201 goto retry;
2202 }
2203 }
2204
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002205 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002206 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002207
2208 iter->func = rec;
2209
2210 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002211}
2212
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002213static void reset_iter_read(struct ftrace_iterator *iter)
2214{
2215 iter->pos = 0;
2216 iter->func_pos = 0;
2217 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002218}
2219
2220static void *t_start(struct seq_file *m, loff_t *pos)
2221{
2222 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002223 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002224 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002225 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002226
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002227 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002228
2229 if (unlikely(ftrace_disabled))
2230 return NULL;
2231
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002232 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002233 * If an lseek was done, then reset and start from beginning.
2234 */
2235 if (*pos < iter->pos)
2236 reset_iter_read(iter);
2237
2238 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002239 * For set_ftrace_filter reading, if we have the filter
2240 * off, we can short cut and just print out that all
2241 * functions are enabled.
2242 */
Steven Rostedtf45948e2011-05-02 12:29:25 -04002243 if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002244 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002245 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002246 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002247 /* reset in case of seek/pread */
2248 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002249 return iter;
2250 }
2251
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002252 if (iter->flags & FTRACE_ITER_HASH)
2253 return t_hash_start(m, pos);
2254
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002255 /*
2256 * Unfortunately, we need to restart at ftrace_pages_start
2257 * every time we let go of the ftrace_mutex. This is because
2258 * those pointers can change without the lock.
2259 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002260 iter->pg = ftrace_pages_start;
2261 iter->idx = 0;
2262 for (l = 0; l <= *pos; ) {
2263 p = t_next(m, p, &l);
2264 if (!p)
2265 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002266 }
walimis5821e1b2008-11-15 15:19:06 +08002267
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002268 if (!p) {
2269 if (iter->flags & FTRACE_ITER_FILTER)
2270 return t_hash_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002271
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002272 return NULL;
2273 }
2274
2275 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002276}
2277
2278static void t_stop(struct seq_file *m, void *p)
2279{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002280 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002281}
2282
2283static int t_show(struct seq_file *m, void *v)
2284{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002285 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002286 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002287
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002288 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002289 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002290
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002291 if (iter->flags & FTRACE_ITER_PRINTALL) {
2292 seq_printf(m, "#### all functions enabled ####\n");
2293 return 0;
2294 }
2295
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002296 rec = iter->func;
2297
Steven Rostedt5072c592008-05-12 21:20:43 +02002298 if (!rec)
2299 return 0;
2300
Steven Rostedt647bcd02011-05-03 14:39:21 -04002301 seq_printf(m, "%ps", (void *)rec->ip);
2302 if (iter->flags & FTRACE_ITER_ENABLED)
2303 seq_printf(m, " (%ld)",
2304 rec->flags & ~FTRACE_FL_MASK);
2305 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002306
2307 return 0;
2308}
2309
James Morris88e9d342009-09-22 16:43:43 -07002310static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002311 .start = t_start,
2312 .next = t_next,
2313 .stop = t_stop,
2314 .show = t_show,
2315};
2316
Ingo Molnare309b412008-05-12 21:20:51 +02002317static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002318ftrace_avail_open(struct inode *inode, struct file *file)
2319{
2320 struct ftrace_iterator *iter;
2321 int ret;
2322
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002323 if (unlikely(ftrace_disabled))
2324 return -ENODEV;
2325
Steven Rostedt5072c592008-05-12 21:20:43 +02002326 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2327 if (!iter)
2328 return -ENOMEM;
2329
2330 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002331
2332 ret = seq_open(file, &show_ftrace_seq_ops);
2333 if (!ret) {
2334 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002335
Steven Rostedt5072c592008-05-12 21:20:43 +02002336 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002337 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002338 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002339 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002340
2341 return ret;
2342}
2343
Steven Rostedt647bcd02011-05-03 14:39:21 -04002344static int
2345ftrace_enabled_open(struct inode *inode, struct file *file)
2346{
2347 struct ftrace_iterator *iter;
2348 int ret;
2349
2350 if (unlikely(ftrace_disabled))
2351 return -ENODEV;
2352
2353 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2354 if (!iter)
2355 return -ENOMEM;
2356
2357 iter->pg = ftrace_pages_start;
2358 iter->flags = FTRACE_ITER_ENABLED;
2359
2360 ret = seq_open(file, &show_ftrace_seq_ops);
2361 if (!ret) {
2362 struct seq_file *m = file->private_data;
2363
2364 m->private = iter;
2365 } else {
2366 kfree(iter);
2367 }
2368
2369 return ret;
2370}
2371
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002372static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002373{
Steven Rostedt52baf112009-02-14 01:15:39 -05002374 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002375 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002376 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002377}
2378
Ingo Molnare309b412008-05-12 21:20:51 +02002379static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002380ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002381 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002382{
2383 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002384 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002385 int ret = 0;
2386
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002387 if (unlikely(ftrace_disabled))
2388 return -ENODEV;
2389
Steven Rostedt5072c592008-05-12 21:20:43 +02002390 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2391 if (!iter)
2392 return -ENOMEM;
2393
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002394 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2395 kfree(iter);
2396 return -ENOMEM;
2397 }
2398
Steven Rostedtf45948e2011-05-02 12:29:25 -04002399 if (flag & FTRACE_ITER_NOTRACE)
2400 hash = ops->notrace_hash;
2401 else
2402 hash = ops->filter_hash;
2403
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002404 iter->ops = ops;
2405 iter->flags = flag;
2406
2407 if (file->f_mode & FMODE_WRITE) {
2408 mutex_lock(&ftrace_lock);
2409 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2410 mutex_unlock(&ftrace_lock);
2411
2412 if (!iter->hash) {
2413 trace_parser_put(&iter->parser);
2414 kfree(iter);
2415 return -ENOMEM;
2416 }
2417 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002418
Steven Rostedt41c52c02008-05-22 11:46:33 -04002419 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002420
Steven Rostedt5072c592008-05-12 21:20:43 +02002421 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002422 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002423 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002424
2425 if (file->f_mode & FMODE_READ) {
2426 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002427
2428 ret = seq_open(file, &show_ftrace_seq_ops);
2429 if (!ret) {
2430 struct seq_file *m = file->private_data;
2431 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002432 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002433 /* Failed */
2434 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002435 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002436 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002437 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002438 } else
2439 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002440 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002441
2442 return ret;
2443}
2444
Steven Rostedt41c52c02008-05-22 11:46:33 -04002445static int
2446ftrace_filter_open(struct inode *inode, struct file *file)
2447{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002448 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002449 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002450}
2451
2452static int
2453ftrace_notrace_open(struct inode *inode, struct file *file)
2454{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002455 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002456 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002457}
2458
Ingo Molnare309b412008-05-12 21:20:51 +02002459static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002460ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02002461{
2462 loff_t ret;
2463
2464 if (file->f_mode & FMODE_READ)
2465 ret = seq_lseek(file, offset, origin);
2466 else
2467 file->f_pos = ret = 1;
2468
2469 return ret;
2470}
2471
Steven Rostedt64e7c442009-02-13 17:08:48 -05002472static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002473{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002474 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002475 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002476
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002477 switch (type) {
2478 case MATCH_FULL:
2479 if (strcmp(str, regex) == 0)
2480 matched = 1;
2481 break;
2482 case MATCH_FRONT_ONLY:
2483 if (strncmp(str, regex, len) == 0)
2484 matched = 1;
2485 break;
2486 case MATCH_MIDDLE_ONLY:
2487 if (strstr(str, regex))
2488 matched = 1;
2489 break;
2490 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002491 slen = strlen(str);
2492 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002493 matched = 1;
2494 break;
2495 }
2496
2497 return matched;
2498}
2499
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002500static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002501enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002502{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002503 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002504 int ret = 0;
2505
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002506 entry = ftrace_lookup_ip(hash, rec->ip);
2507 if (not) {
2508 /* Do nothing if it doesn't exist */
2509 if (!entry)
2510 return 0;
2511
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002512 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002513 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002514 /* Do nothing if it exists */
2515 if (entry)
2516 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002517
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002518 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002519 }
2520 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002521}
2522
Steven Rostedt64e7c442009-02-13 17:08:48 -05002523static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002524ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2525 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002526{
2527 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002528 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002529
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002530 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2531
2532 if (mod) {
2533 /* module lookup requires matching the module */
2534 if (!modname || strcmp(modname, mod))
2535 return 0;
2536
2537 /* blank search means to match all funcs in the mod */
2538 if (!len)
2539 return 1;
2540 }
2541
Steven Rostedt64e7c442009-02-13 17:08:48 -05002542 return ftrace_match(str, regex, len, type);
2543}
2544
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002545static int
2546match_records(struct ftrace_hash *hash, char *buff,
2547 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002548{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002549 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002550 struct ftrace_page *pg;
2551 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002552 int type = MATCH_FULL;
2553 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002554 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002555 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002556
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002557 if (len) {
2558 type = filter_parse_regex(buff, len, &search, &not);
2559 search_len = strlen(search);
2560 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002561
Steven Rostedt52baf112009-02-14 01:15:39 -05002562 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002563
2564 if (unlikely(ftrace_disabled))
2565 goto out_unlock;
2566
Steven Rostedt265c8312009-02-13 12:43:56 -05002567 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002568 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002569 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002570 if (ret < 0) {
2571 found = ret;
2572 goto out_unlock;
2573 }
Li Zefan311d16d2009-12-08 11:15:11 +08002574 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002575 }
2576 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002577 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002578 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002579
2580 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002581}
2582
Steven Rostedt64e7c442009-02-13 17:08:48 -05002583static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002584ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002585{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002586 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002587}
2588
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002589static int
2590ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002591{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002592 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002593
Steven Rostedt64e7c442009-02-13 17:08:48 -05002594 /* blank or '*' mean the same */
2595 if (strcmp(buff, "*") == 0)
2596 buff[0] = 0;
2597
2598 /* handle the case of 'dont filter this module' */
2599 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2600 buff[0] = 0;
2601 not = 1;
2602 }
2603
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002604 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002605}
2606
Steven Rostedtf6180772009-02-14 00:40:25 -05002607/*
2608 * We register the module command as a template to show others how
2609 * to register the a command as well.
2610 */
2611
2612static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002613ftrace_mod_callback(struct ftrace_hash *hash,
2614 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002615{
2616 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002617 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002618
2619 /*
2620 * cmd == 'mod' because we only registered this func
2621 * for the 'mod' ftrace_func_command.
2622 * But if you register one func with multiple commands,
2623 * you can tell which command was used by the cmd
2624 * parameter.
2625 */
2626
2627 /* we must have a module name */
2628 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002629 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002630
2631 mod = strsep(&param, ":");
2632 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002633 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002634
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002635 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002636 if (!ret)
2637 ret = -EINVAL;
2638 if (ret < 0)
2639 return ret;
2640
2641 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002642}
2643
2644static struct ftrace_func_command ftrace_mod_cmd = {
2645 .name = "mod",
2646 .func = ftrace_mod_callback,
2647};
2648
2649static int __init ftrace_mod_cmd_init(void)
2650{
2651 return register_ftrace_command(&ftrace_mod_cmd);
2652}
2653device_initcall(ftrace_mod_cmd_init);
2654
Steven Rostedt59df055f2009-02-14 15:29:06 -05002655static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002656function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002657{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002658 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002659 struct hlist_head *hhd;
2660 struct hlist_node *n;
2661 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002662
2663 key = hash_long(ip, FTRACE_HASH_BITS);
2664
2665 hhd = &ftrace_func_hash[key];
2666
2667 if (hlist_empty(hhd))
2668 return;
2669
2670 /*
2671 * Disable preemption for these calls to prevent a RCU grace
2672 * period. This syncs the hash iteration and freeing of items
2673 * on the hash. rcu_read_lock is too dangerous here.
2674 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002675 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002676 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2677 if (entry->ip == ip)
2678 entry->ops->func(ip, parent_ip, &entry->data);
2679 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002680 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002681}
2682
Steven Rostedtb6887d72009-02-17 12:32:04 -05002683static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002684{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002685 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002686};
2687
Steven Rostedtb6887d72009-02-17 12:32:04 -05002688static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002689
Steven Rostedtb6887d72009-02-17 12:32:04 -05002690static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002691{
Steven Rostedtb8489142011-05-04 09:27:52 -04002692 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002693 int i;
2694
Steven Rostedtb6887d72009-02-17 12:32:04 -05002695 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002696 return;
2697
2698 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2699 struct hlist_head *hhd = &ftrace_func_hash[i];
2700 if (hhd->first)
2701 break;
2702 }
2703 /* Nothing registered? */
2704 if (i == FTRACE_FUNC_HASHSIZE)
2705 return;
2706
Steven Rostedtb8489142011-05-04 09:27:52 -04002707 ret = __register_ftrace_function(&trace_probe_ops);
2708 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002709 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002710
Steven Rostedtb6887d72009-02-17 12:32:04 -05002711 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002712}
2713
Steven Rostedtb6887d72009-02-17 12:32:04 -05002714static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002715{
Steven Rostedtb8489142011-05-04 09:27:52 -04002716 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002717 int i;
2718
Steven Rostedtb6887d72009-02-17 12:32:04 -05002719 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002720 return;
2721
2722 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2723 struct hlist_head *hhd = &ftrace_func_hash[i];
2724 if (hhd->first)
2725 return;
2726 }
2727
2728 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002729 ret = __unregister_ftrace_function(&trace_probe_ops);
2730 if (!ret)
2731 ftrace_shutdown(&trace_probe_ops, 0);
2732
Steven Rostedtb6887d72009-02-17 12:32:04 -05002733 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002734}
2735
2736
2737static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2738{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002739 struct ftrace_func_probe *entry =
2740 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002741
2742 if (entry->ops->free)
2743 entry->ops->free(&entry->data);
2744 kfree(entry);
2745}
2746
2747
2748int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002749register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002750 void *data)
2751{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002752 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002753 struct ftrace_page *pg;
2754 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002755 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002756 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002757 int count = 0;
2758 char *search;
2759
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002760 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002761 len = strlen(search);
2762
Steven Rostedtb6887d72009-02-17 12:32:04 -05002763 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002764 if (WARN_ON(not))
2765 return -EINVAL;
2766
2767 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002768
Steven Rostedt45a4a232011-04-21 23:16:46 -04002769 if (unlikely(ftrace_disabled))
2770 goto out_unlock;
2771
Steven Rostedt59df055f2009-02-14 15:29:06 -05002772 do_for_each_ftrace_rec(pg, rec) {
2773
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002774 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002775 continue;
2776
2777 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2778 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002779 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002780 if (!count)
2781 count = -ENOMEM;
2782 goto out_unlock;
2783 }
2784
2785 count++;
2786
2787 entry->data = data;
2788
2789 /*
2790 * The caller might want to do something special
2791 * for each function we find. We call the callback
2792 * to give the caller an opportunity to do so.
2793 */
2794 if (ops->callback) {
2795 if (ops->callback(rec->ip, &entry->data) < 0) {
2796 /* caller does not like this func */
2797 kfree(entry);
2798 continue;
2799 }
2800 }
2801
2802 entry->ops = ops;
2803 entry->ip = rec->ip;
2804
2805 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2806 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2807
2808 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002809 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002810
2811 out_unlock:
2812 mutex_unlock(&ftrace_lock);
2813
2814 return count;
2815}
2816
2817enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002818 PROBE_TEST_FUNC = 1,
2819 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002820};
2821
2822static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002823__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002824 void *data, int flags)
2825{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002826 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002827 struct hlist_node *n, *tmp;
2828 char str[KSYM_SYMBOL_LEN];
2829 int type = MATCH_FULL;
2830 int i, len = 0;
2831 char *search;
2832
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002833 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002834 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002835 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002836 int not;
2837
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002838 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002839 len = strlen(search);
2840
Steven Rostedtb6887d72009-02-17 12:32:04 -05002841 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002842 if (WARN_ON(not))
2843 return;
2844 }
2845
2846 mutex_lock(&ftrace_lock);
2847 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2848 struct hlist_head *hhd = &ftrace_func_hash[i];
2849
2850 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2851
2852 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002853 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002854 continue;
2855
Steven Rostedtb6887d72009-02-17 12:32:04 -05002856 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002857 continue;
2858
2859 /* do this last, since it is the most expensive */
2860 if (glob) {
2861 kallsyms_lookup(entry->ip, NULL, NULL,
2862 NULL, str);
2863 if (!ftrace_match(str, glob, len, type))
2864 continue;
2865 }
2866
2867 hlist_del(&entry->node);
2868 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2869 }
2870 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002871 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002872 mutex_unlock(&ftrace_lock);
2873}
2874
2875void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002876unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002877 void *data)
2878{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002879 __unregister_ftrace_function_probe(glob, ops, data,
2880 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002881}
2882
2883void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002884unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002885{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002886 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002887}
2888
Steven Rostedtb6887d72009-02-17 12:32:04 -05002889void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002890{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002891 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002892}
2893
Steven Rostedtf6180772009-02-14 00:40:25 -05002894static LIST_HEAD(ftrace_commands);
2895static DEFINE_MUTEX(ftrace_cmd_mutex);
2896
2897int register_ftrace_command(struct ftrace_func_command *cmd)
2898{
2899 struct ftrace_func_command *p;
2900 int ret = 0;
2901
2902 mutex_lock(&ftrace_cmd_mutex);
2903 list_for_each_entry(p, &ftrace_commands, list) {
2904 if (strcmp(cmd->name, p->name) == 0) {
2905 ret = -EBUSY;
2906 goto out_unlock;
2907 }
2908 }
2909 list_add(&cmd->list, &ftrace_commands);
2910 out_unlock:
2911 mutex_unlock(&ftrace_cmd_mutex);
2912
2913 return ret;
2914}
2915
2916int unregister_ftrace_command(struct ftrace_func_command *cmd)
2917{
2918 struct ftrace_func_command *p, *n;
2919 int ret = -ENODEV;
2920
2921 mutex_lock(&ftrace_cmd_mutex);
2922 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2923 if (strcmp(cmd->name, p->name) == 0) {
2924 ret = 0;
2925 list_del_init(&p->list);
2926 goto out_unlock;
2927 }
2928 }
2929 out_unlock:
2930 mutex_unlock(&ftrace_cmd_mutex);
2931
2932 return ret;
2933}
2934
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002935static int ftrace_process_regex(struct ftrace_hash *hash,
2936 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002937{
Steven Rostedtf6180772009-02-14 00:40:25 -05002938 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002939 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08002940 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002941
2942 func = strsep(&next, ":");
2943
2944 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002945 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002946 if (!ret)
2947 ret = -EINVAL;
2948 if (ret < 0)
2949 return ret;
2950 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002951 }
2952
Steven Rostedtf6180772009-02-14 00:40:25 -05002953 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002954
2955 command = strsep(&next, ":");
2956
Steven Rostedtf6180772009-02-14 00:40:25 -05002957 mutex_lock(&ftrace_cmd_mutex);
2958 list_for_each_entry(p, &ftrace_commands, list) {
2959 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002960 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05002961 goto out_unlock;
2962 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002963 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002964 out_unlock:
2965 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002966
Steven Rostedtf6180772009-02-14 00:40:25 -05002967 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002968}
2969
Ingo Molnare309b412008-05-12 21:20:51 +02002970static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002971ftrace_regex_write(struct file *file, const char __user *ubuf,
2972 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002973{
2974 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002975 struct trace_parser *parser;
2976 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002977
Li Zefan4ba79782009-09-22 13:52:20 +08002978 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002979 return 0;
2980
Steven Rostedt41c52c02008-05-22 11:46:33 -04002981 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002982
Steven Rostedt45a4a232011-04-21 23:16:46 -04002983 ret = -ENODEV;
2984 if (unlikely(ftrace_disabled))
2985 goto out_unlock;
2986
Steven Rostedt5072c592008-05-12 21:20:43 +02002987 if (file->f_mode & FMODE_READ) {
2988 struct seq_file *m = file->private_data;
2989 iter = m->private;
2990 } else
2991 iter = file->private_data;
2992
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002993 parser = &iter->parser;
2994 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002995
Li Zefan4ba79782009-09-22 13:52:20 +08002996 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002997 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002998 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002999 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003000 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003001 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003002 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003003 }
3004
Steven Rostedt5072c592008-05-12 21:20:43 +02003005 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003006out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003007 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003008
Steven Rostedt5072c592008-05-12 21:20:43 +02003009 return ret;
3010}
3011
Steven Rostedt41c52c02008-05-22 11:46:33 -04003012static ssize_t
3013ftrace_filter_write(struct file *file, const char __user *ubuf,
3014 size_t cnt, loff_t *ppos)
3015{
3016 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3017}
3018
3019static ssize_t
3020ftrace_notrace_write(struct file *file, const char __user *ubuf,
3021 size_t cnt, loff_t *ppos)
3022{
3023 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3024}
3025
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003026static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003027ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3028 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003029{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003030 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003031 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003032 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003033
Steven Rostedt936e0742011-05-05 22:54:01 -04003034 /* All global ops uses the global ops filters */
3035 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3036 ops = &global_ops;
3037
Steven Rostedt41c52c02008-05-22 11:46:33 -04003038 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003039 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003040
Steven Rostedtf45948e2011-05-02 12:29:25 -04003041 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003042 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003043 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003044 orig_hash = &ops->notrace_hash;
3045
3046 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3047 if (!hash)
3048 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003049
Steven Rostedt41c52c02008-05-22 11:46:33 -04003050 mutex_lock(&ftrace_regex_lock);
3051 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003052 ftrace_filter_reset(hash);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003053 if (buf)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003054 ftrace_match_records(hash, buf, len);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003055
3056 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003057 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003058 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3059 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003060 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003061
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003062 mutex_unlock(&ftrace_lock);
3063
Steven Rostedt41c52c02008-05-22 11:46:33 -04003064 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003065
3066 free_ftrace_hash(hash);
3067 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003068}
3069
Steven Rostedt77a2b372008-05-12 21:20:45 +02003070/**
3071 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003072 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003073 * @buf - the string that holds the function filter text.
3074 * @len - the length of the string.
3075 * @reset - non zero to reset all filters before applying this filter.
3076 *
3077 * Filters denote which functions should be enabled when tracing is enabled.
3078 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3079 */
Steven Rostedt936e0742011-05-05 22:54:01 -04003080void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3081 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003082{
Steven Rostedt936e0742011-05-05 22:54:01 -04003083 ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003084}
Steven Rostedt936e0742011-05-05 22:54:01 -04003085EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003086
Steven Rostedt41c52c02008-05-22 11:46:33 -04003087/**
3088 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003089 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003090 * @buf - the string that holds the function notrace text.
3091 * @len - the length of the string.
3092 * @reset - non zero to reset all filters before applying this filter.
3093 *
3094 * Notrace Filters denote which functions should not be enabled when tracing
3095 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3096 * for tracing.
3097 */
Steven Rostedt936e0742011-05-05 22:54:01 -04003098void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3099 int len, int reset)
3100{
3101 ftrace_set_regex(ops, buf, len, reset, 0);
3102}
3103EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3104/**
3105 * ftrace_set_filter - set a function to filter on in ftrace
3106 * @ops - the ops to set the filter with
3107 * @buf - the string that holds the function filter text.
3108 * @len - the length of the string.
3109 * @reset - non zero to reset all filters before applying this filter.
3110 *
3111 * Filters denote which functions should be enabled when tracing is enabled.
3112 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3113 */
3114void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3115{
3116 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3117}
3118EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3119
3120/**
3121 * ftrace_set_notrace - set a function to not trace in ftrace
3122 * @ops - the ops to set the notrace filter with
3123 * @buf - the string that holds the function notrace text.
3124 * @len - the length of the string.
3125 * @reset - non zero to reset all filters before applying this filter.
3126 *
3127 * Notrace Filters denote which functions should not be enabled when tracing
3128 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3129 * for tracing.
3130 */
3131void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003132{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003133 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003134}
Steven Rostedt936e0742011-05-05 22:54:01 -04003135EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003136
Steven Rostedt2af15d62009-05-28 13:37:24 -04003137/*
3138 * command line interface to allow users to set filters on boot up.
3139 */
3140#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3141static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3142static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3143
3144static int __init set_ftrace_notrace(char *str)
3145{
3146 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3147 return 1;
3148}
3149__setup("ftrace_notrace=", set_ftrace_notrace);
3150
3151static int __init set_ftrace_filter(char *str)
3152{
3153 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3154 return 1;
3155}
3156__setup("ftrace_filter=", set_ftrace_filter);
3157
Stefan Assmann369bc182009-10-12 22:17:21 +02003158#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003159static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003160static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3161
Stefan Assmann369bc182009-10-12 22:17:21 +02003162static int __init set_graph_function(char *str)
3163{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003164 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003165 return 1;
3166}
3167__setup("ftrace_graph_filter=", set_graph_function);
3168
3169static void __init set_ftrace_early_graph(char *buf)
3170{
3171 int ret;
3172 char *func;
3173
3174 while (buf) {
3175 func = strsep(&buf, ",");
3176 /* we allow only one expression at a time */
3177 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3178 func);
3179 if (ret)
3180 printk(KERN_DEBUG "ftrace: function %s not "
3181 "traceable\n", func);
3182 }
3183}
3184#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3185
Steven Rostedtf45948e2011-05-02 12:29:25 -04003186static void __init
3187set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003188{
3189 char *func;
3190
3191 while (buf) {
3192 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003193 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003194 }
3195}
3196
3197static void __init set_ftrace_early_filters(void)
3198{
3199 if (ftrace_filter_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04003200 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003201 if (ftrace_notrace_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04003202 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003203#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3204 if (ftrace_graph_buf[0])
3205 set_ftrace_early_graph(ftrace_graph_buf);
3206#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003207}
3208
Ingo Molnare309b412008-05-12 21:20:51 +02003209static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003210ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003211{
3212 struct seq_file *m = (struct seq_file *)file->private_data;
3213 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003214 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003215 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003216 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003217 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003218
Steven Rostedt41c52c02008-05-22 11:46:33 -04003219 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003220 if (file->f_mode & FMODE_READ) {
3221 iter = m->private;
3222
3223 seq_release(inode, file);
3224 } else
3225 iter = file->private_data;
3226
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003227 parser = &iter->parser;
3228 if (trace_parser_loaded(parser)) {
3229 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003230 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003231 }
3232
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003233 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003234
Steven Rostedt058e2972011-04-29 22:35:33 -04003235 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003236 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3237
3238 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003239 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003240 else
3241 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003242
Steven Rostedt058e2972011-04-29 22:35:33 -04003243 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003244 ret = ftrace_hash_move(iter->ops, filter_hash,
3245 orig_hash, iter->hash);
3246 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3247 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003248 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003249
Steven Rostedt058e2972011-04-29 22:35:33 -04003250 mutex_unlock(&ftrace_lock);
3251 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003252 free_ftrace_hash(iter->hash);
3253 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003254
Steven Rostedt41c52c02008-05-22 11:46:33 -04003255 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003256 return 0;
3257}
3258
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003259static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003260 .open = ftrace_avail_open,
3261 .read = seq_read,
3262 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003263 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003264};
3265
Steven Rostedt647bcd02011-05-03 14:39:21 -04003266static const struct file_operations ftrace_enabled_fops = {
3267 .open = ftrace_enabled_open,
3268 .read = seq_read,
3269 .llseek = seq_lseek,
3270 .release = seq_release_private,
3271};
3272
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003273static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003274 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003275 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003276 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003277 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003278 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003279};
3280
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003281static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003282 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003283 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003284 .write = ftrace_notrace_write,
3285 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003286 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003287};
3288
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003289#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3290
3291static DEFINE_MUTEX(graph_lock);
3292
3293int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003294int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003295unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3296
3297static void *
Li Zefan85951842009-06-24 09:54:00 +08003298__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003299{
Li Zefan85951842009-06-24 09:54:00 +08003300 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003301 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003302 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003303}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003304
Li Zefan85951842009-06-24 09:54:00 +08003305static void *
3306g_next(struct seq_file *m, void *v, loff_t *pos)
3307{
3308 (*pos)++;
3309 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003310}
3311
3312static void *g_start(struct seq_file *m, loff_t *pos)
3313{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003314 mutex_lock(&graph_lock);
3315
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003316 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003317 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003318 return (void *)1;
3319
Li Zefan85951842009-06-24 09:54:00 +08003320 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003321}
3322
3323static void g_stop(struct seq_file *m, void *p)
3324{
3325 mutex_unlock(&graph_lock);
3326}
3327
3328static int g_show(struct seq_file *m, void *v)
3329{
3330 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003331
3332 if (!ptr)
3333 return 0;
3334
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003335 if (ptr == (unsigned long *)1) {
3336 seq_printf(m, "#### all functions enabled ####\n");
3337 return 0;
3338 }
3339
Steven Rostedtb375a112009-09-17 00:05:58 -04003340 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003341
3342 return 0;
3343}
3344
James Morris88e9d342009-09-22 16:43:43 -07003345static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003346 .start = g_start,
3347 .next = g_next,
3348 .stop = g_stop,
3349 .show = g_show,
3350};
3351
3352static int
3353ftrace_graph_open(struct inode *inode, struct file *file)
3354{
3355 int ret = 0;
3356
3357 if (unlikely(ftrace_disabled))
3358 return -ENODEV;
3359
3360 mutex_lock(&graph_lock);
3361 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003362 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003363 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003364 ftrace_graph_count = 0;
3365 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3366 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003367 mutex_unlock(&graph_lock);
3368
Li Zefana4ec5e02009-09-18 14:06:28 +08003369 if (file->f_mode & FMODE_READ)
3370 ret = seq_open(file, &ftrace_graph_seq_ops);
3371
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003372 return ret;
3373}
3374
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003375static int
Li Zefan87827112009-07-23 11:29:11 +08003376ftrace_graph_release(struct inode *inode, struct file *file)
3377{
3378 if (file->f_mode & FMODE_READ)
3379 seq_release(inode, file);
3380 return 0;
3381}
3382
3383static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003384ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003385{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003386 struct dyn_ftrace *rec;
3387 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003388 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003389 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003390 int type, not;
3391 char *search;
3392 bool exists;
3393 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003394
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003395 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003396 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003397 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3398 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003399
3400 search_len = strlen(search);
3401
Steven Rostedt52baf112009-02-14 01:15:39 -05003402 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003403
3404 if (unlikely(ftrace_disabled)) {
3405 mutex_unlock(&ftrace_lock);
3406 return -ENODEV;
3407 }
3408
Steven Rostedt265c8312009-02-13 12:43:56 -05003409 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003410
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003411 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003412 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003413 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003414 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003415 if (array[i] == rec->ip) {
3416 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003417 break;
3418 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003419 }
3420
3421 if (!not) {
3422 fail = 0;
3423 if (!exists) {
3424 array[(*idx)++] = rec->ip;
3425 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3426 goto out;
3427 }
3428 } else {
3429 if (exists) {
3430 array[i] = array[--(*idx)];
3431 array[*idx] = 0;
3432 fail = 0;
3433 }
3434 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003435 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003436 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003437out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003438 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003439
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003440 if (fail)
3441 return -EINVAL;
3442
3443 ftrace_graph_filter_enabled = 1;
3444 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003445}
3446
3447static ssize_t
3448ftrace_graph_write(struct file *file, const char __user *ubuf,
3449 size_t cnt, loff_t *ppos)
3450{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003451 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003452 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003453
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003454 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003455 return 0;
3456
3457 mutex_lock(&graph_lock);
3458
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003459 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3460 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003461 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003462 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003463
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003464 read = trace_get_user(&parser, ubuf, cnt, ppos);
3465
Li Zefan4ba79782009-09-22 13:52:20 +08003466 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003467 parser.buffer[parser.idx] = 0;
3468
3469 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003470 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003471 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003472 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003473 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003474 }
3475
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003476 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003477
3478out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003479 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003480out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003481 mutex_unlock(&graph_lock);
3482
3483 return ret;
3484}
3485
3486static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003487 .open = ftrace_graph_open,
3488 .read = seq_read,
3489 .write = ftrace_graph_write,
3490 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003491 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003492};
3493#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3494
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003495static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003496{
Steven Rostedt5072c592008-05-12 21:20:43 +02003497
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003498 trace_create_file("available_filter_functions", 0444,
3499 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003500
Steven Rostedt647bcd02011-05-03 14:39:21 -04003501 trace_create_file("enabled_functions", 0444,
3502 d_tracer, NULL, &ftrace_enabled_fops);
3503
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003504 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3505 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003506
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003507 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003508 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003509
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003510#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003511 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003512 NULL,
3513 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003514#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3515
Steven Rostedt5072c592008-05-12 21:20:43 +02003516 return 0;
3517}
3518
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003519static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003520 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003521 unsigned long *end)
3522{
3523 unsigned long *p;
3524 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003525 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003526
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003527 mutex_lock(&ftrace_lock);
Steven Rostedt32082302011-12-16 14:42:37 -05003528 /*
3529 * Core and each module needs their own pages, as
3530 * modules will free them when they are removed.
3531 * Force a new page to be allocated for modules.
3532 */
3533 if (mod) {
3534 if (!ftrace_pages)
3535 return -ENOMEM;
3536
3537 /*
3538 * If the last page was full, it will be
3539 * allocated anyway.
3540 */
3541 if (ftrace_pages->index != ENTRIES_PER_PAGE) {
3542 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
3543 if (!ftrace_pages->next)
3544 return -ENOMEM;
3545 ftrace_pages = ftrace_pages->next;
3546 }
3547 }
3548
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003549 p = start;
3550 while (p < end) {
3551 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003552 /*
3553 * Some architecture linkers will pad between
3554 * the different mcount_loc sections of different
3555 * object files to satisfy alignments.
3556 * Skip any NULL pointers.
3557 */
3558 if (!addr)
3559 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003560 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003561 }
3562
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003563 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003564 * We only need to disable interrupts on start up
3565 * because we are modifying code that an interrupt
3566 * may execute, and the modification is not atomic.
3567 * But for modules, nothing runs the code we modify
3568 * until we are finished with it, and there's no
3569 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003570 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003571 if (!mod)
3572 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003573 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003574 if (!mod)
3575 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003576 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003577
3578 return 0;
3579}
3580
Steven Rostedt93eb6772009-04-15 13:24:06 -04003581#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003582
3583#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3584
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003585void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003586{
3587 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003588 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003589 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003590
Steven Rostedt93eb6772009-04-15 13:24:06 -04003591 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003592
3593 if (ftrace_disabled)
3594 goto out_unlock;
3595
Steven Rostedt32082302011-12-16 14:42:37 -05003596 /*
3597 * Each module has its own ftrace_pages, remove
3598 * them from the list.
3599 */
3600 last_pg = &ftrace_pages_start;
3601 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3602 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003603 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003604 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003605 * As core pages are first, the first
3606 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003607 */
Steven Rostedt32082302011-12-16 14:42:37 -05003608 if (WARN_ON(pg == ftrace_pages_start))
3609 goto out_unlock;
3610
3611 /* Check if we are deleting the last page */
3612 if (pg == ftrace_pages)
3613 ftrace_pages = next_to_ftrace_page(last_pg);
3614
3615 *last_pg = pg->next;
3616 free_page((unsigned long)pg);
3617 } else
3618 last_pg = &pg->next;
3619 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003620 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003621 mutex_unlock(&ftrace_lock);
3622}
3623
3624static void ftrace_init_module(struct module *mod,
3625 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003626{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003627 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003628 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003629 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003630}
3631
Steven Rostedt93eb6772009-04-15 13:24:06 -04003632static int ftrace_module_notify(struct notifier_block *self,
3633 unsigned long val, void *data)
3634{
3635 struct module *mod = data;
3636
3637 switch (val) {
3638 case MODULE_STATE_COMING:
3639 ftrace_init_module(mod, mod->ftrace_callsites,
3640 mod->ftrace_callsites +
3641 mod->num_ftrace_callsites);
3642 break;
3643 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003644 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003645 break;
3646 }
3647
3648 return 0;
3649}
3650#else
3651static int ftrace_module_notify(struct notifier_block *self,
3652 unsigned long val, void *data)
3653{
3654 return 0;
3655}
3656#endif /* CONFIG_MODULES */
3657
3658struct notifier_block ftrace_module_nb = {
3659 .notifier_call = ftrace_module_notify,
3660 .priority = 0,
3661};
3662
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003663extern unsigned long __start_mcount_loc[];
3664extern unsigned long __stop_mcount_loc[];
3665
3666void __init ftrace_init(void)
3667{
3668 unsigned long count, addr, flags;
3669 int ret;
3670
3671 /* Keep the ftrace pointer to the stub */
3672 addr = (unsigned long)ftrace_stub;
3673
3674 local_irq_save(flags);
3675 ftrace_dyn_arch_init(&addr);
3676 local_irq_restore(flags);
3677
3678 /* ftrace_dyn_arch_init places the return code in addr */
3679 if (addr)
3680 goto failed;
3681
3682 count = __stop_mcount_loc - __start_mcount_loc;
3683
3684 ret = ftrace_dyn_table_alloc(count);
3685 if (ret)
3686 goto failed;
3687
3688 last_ftrace_enabled = ftrace_enabled = 1;
3689
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003690 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003691 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003692 __stop_mcount_loc);
3693
Steven Rostedt93eb6772009-04-15 13:24:06 -04003694 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003695 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003696 pr_warning("Failed to register trace ftrace module notifier\n");
3697
Steven Rostedt2af15d62009-05-28 13:37:24 -04003698 set_ftrace_early_filters();
3699
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003700 return;
3701 failed:
3702 ftrace_disabled = 1;
3703}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003704
Steven Rostedt3d083392008-05-12 21:20:42 +02003705#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003706
Steven Rostedt2b499382011-05-03 22:49:52 -04003707static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003708 .func = ftrace_stub,
3709};
3710
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003711static int __init ftrace_nodyn_init(void)
3712{
3713 ftrace_enabled = 1;
3714 return 0;
3715}
3716device_initcall(ftrace_nodyn_init);
3717
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003718static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3719static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003720/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003721# define ftrace_startup(ops, command) \
3722 ({ \
3723 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3724 0; \
3725 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04003726# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003727# define ftrace_startup_sysctl() do { } while (0)
3728# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003729
3730static inline int
3731ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3732{
3733 return 1;
3734}
3735
Steven Rostedt3d083392008-05-12 21:20:42 +02003736#endif /* CONFIG_DYNAMIC_FTRACE */
3737
Steven Rostedtb8489142011-05-04 09:27:52 -04003738static void
3739ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3740{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003741 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04003742
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003743 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3744 return;
3745
3746 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003747 /*
3748 * Some of the ops may be dynamically allocated,
3749 * they must be freed after a synchronize_sched().
3750 */
3751 preempt_disable_notrace();
3752 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04003753 while (op != &ftrace_list_end) {
3754 if (ftrace_ops_test(op, ip))
3755 op->func(ip, parent_ip);
3756 op = rcu_dereference_raw(op->next);
3757 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003758 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003759 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04003760}
3761
Steven Rostedte32d8952008-12-04 00:26:41 -05003762static void clear_ftrace_swapper(void)
3763{
3764 struct task_struct *p;
3765 int cpu;
3766
3767 get_online_cpus();
3768 for_each_online_cpu(cpu) {
3769 p = idle_task(cpu);
3770 clear_tsk_trace_trace(p);
3771 }
3772 put_online_cpus();
3773}
3774
3775static void set_ftrace_swapper(void)
3776{
3777 struct task_struct *p;
3778 int cpu;
3779
3780 get_online_cpus();
3781 for_each_online_cpu(cpu) {
3782 p = idle_task(cpu);
3783 set_tsk_trace_trace(p);
3784 }
3785 put_online_cpus();
3786}
3787
3788static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003789{
3790 struct task_struct *p;
3791
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003792 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05003793 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05003794 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05003795 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003796 rcu_read_unlock();
3797
Steven Rostedte32d8952008-12-04 00:26:41 -05003798 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003799}
3800
Steven Rostedte32d8952008-12-04 00:26:41 -05003801static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003802{
3803 struct task_struct *p;
3804
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003805 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003806 do_each_pid_task(pid, PIDTYPE_PID, p) {
3807 set_tsk_trace_trace(p);
3808 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003809 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003810}
3811
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003812static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003813{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003814 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003815 clear_ftrace_swapper();
3816 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003817 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05003818}
3819
3820static void set_ftrace_pid_task(struct pid *pid)
3821{
3822 if (pid == ftrace_swapper_pid)
3823 set_ftrace_swapper();
3824 else
3825 set_ftrace_pid(pid);
3826}
3827
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003828static int ftrace_pid_add(int p)
3829{
3830 struct pid *pid;
3831 struct ftrace_pid *fpid;
3832 int ret = -EINVAL;
3833
3834 mutex_lock(&ftrace_lock);
3835
3836 if (!p)
3837 pid = ftrace_swapper_pid;
3838 else
3839 pid = find_get_pid(p);
3840
3841 if (!pid)
3842 goto out;
3843
3844 ret = 0;
3845
3846 list_for_each_entry(fpid, &ftrace_pids, list)
3847 if (fpid->pid == pid)
3848 goto out_put;
3849
3850 ret = -ENOMEM;
3851
3852 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3853 if (!fpid)
3854 goto out_put;
3855
3856 list_add(&fpid->list, &ftrace_pids);
3857 fpid->pid = pid;
3858
3859 set_ftrace_pid_task(pid);
3860
3861 ftrace_update_pid_func();
3862 ftrace_startup_enable(0);
3863
3864 mutex_unlock(&ftrace_lock);
3865 return 0;
3866
3867out_put:
3868 if (pid != ftrace_swapper_pid)
3869 put_pid(pid);
3870
3871out:
3872 mutex_unlock(&ftrace_lock);
3873 return ret;
3874}
3875
3876static void ftrace_pid_reset(void)
3877{
3878 struct ftrace_pid *fpid, *safe;
3879
3880 mutex_lock(&ftrace_lock);
3881 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3882 struct pid *pid = fpid->pid;
3883
3884 clear_ftrace_pid_task(pid);
3885
3886 list_del(&fpid->list);
3887 kfree(fpid);
3888 }
3889
3890 ftrace_update_pid_func();
3891 ftrace_startup_enable(0);
3892
3893 mutex_unlock(&ftrace_lock);
3894}
3895
3896static void *fpid_start(struct seq_file *m, loff_t *pos)
3897{
3898 mutex_lock(&ftrace_lock);
3899
3900 if (list_empty(&ftrace_pids) && (!*pos))
3901 return (void *) 1;
3902
3903 return seq_list_start(&ftrace_pids, *pos);
3904}
3905
3906static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3907{
3908 if (v == (void *)1)
3909 return NULL;
3910
3911 return seq_list_next(v, &ftrace_pids, pos);
3912}
3913
3914static void fpid_stop(struct seq_file *m, void *p)
3915{
3916 mutex_unlock(&ftrace_lock);
3917}
3918
3919static int fpid_show(struct seq_file *m, void *v)
3920{
3921 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3922
3923 if (v == (void *)1) {
3924 seq_printf(m, "no pid\n");
3925 return 0;
3926 }
3927
3928 if (fpid->pid == ftrace_swapper_pid)
3929 seq_printf(m, "swapper tasks\n");
3930 else
3931 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3932
3933 return 0;
3934}
3935
3936static const struct seq_operations ftrace_pid_sops = {
3937 .start = fpid_start,
3938 .next = fpid_next,
3939 .stop = fpid_stop,
3940 .show = fpid_show,
3941};
3942
3943static int
3944ftrace_pid_open(struct inode *inode, struct file *file)
3945{
3946 int ret = 0;
3947
3948 if ((file->f_mode & FMODE_WRITE) &&
3949 (file->f_flags & O_TRUNC))
3950 ftrace_pid_reset();
3951
3952 if (file->f_mode & FMODE_READ)
3953 ret = seq_open(file, &ftrace_pid_sops);
3954
3955 return ret;
3956}
3957
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003958static ssize_t
3959ftrace_pid_write(struct file *filp, const char __user *ubuf,
3960 size_t cnt, loff_t *ppos)
3961{
Ingo Molnar457dc922009-11-23 11:03:28 +01003962 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003963 long val;
3964 int ret;
3965
3966 if (cnt >= sizeof(buf))
3967 return -EINVAL;
3968
3969 if (copy_from_user(&buf, ubuf, cnt))
3970 return -EFAULT;
3971
3972 buf[cnt] = 0;
3973
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003974 /*
3975 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3976 * to clean the filter quietly.
3977 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003978 tmp = strstrip(buf);
3979 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003980 return 1;
3981
Ingo Molnar457dc922009-11-23 11:03:28 +01003982 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003983 if (ret < 0)
3984 return ret;
3985
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003986 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003987
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003988 return ret ? ret : cnt;
3989}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003990
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003991static int
3992ftrace_pid_release(struct inode *inode, struct file *file)
3993{
3994 if (file->f_mode & FMODE_READ)
3995 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003996
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003997 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003998}
3999
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004000static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004001 .open = ftrace_pid_open,
4002 .write = ftrace_pid_write,
4003 .read = seq_read,
4004 .llseek = seq_lseek,
4005 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004006};
4007
4008static __init int ftrace_init_debugfs(void)
4009{
4010 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004011
4012 d_tracer = tracing_init_dentry();
4013 if (!d_tracer)
4014 return 0;
4015
4016 ftrace_init_dyn_debugfs(d_tracer);
4017
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004018 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4019 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004020
4021 ftrace_profile_debugfs(d_tracer);
4022
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004023 return 0;
4024}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004025fs_initcall(ftrace_init_debugfs);
4026
Steven Rostedt3d083392008-05-12 21:20:42 +02004027/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004028 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004029 *
4030 * This function should be used by panic code. It stops ftrace
4031 * but in a not so nice way. If you need to simply kill ftrace
4032 * from a non-atomic section, use ftrace_kill.
4033 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004034void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004035{
4036 ftrace_disabled = 1;
4037 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004038 clear_ftrace_function();
4039}
4040
4041/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004042 * Test if ftrace is dead or not.
4043 */
4044int ftrace_is_dead(void)
4045{
4046 return ftrace_disabled;
4047}
4048
4049/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004050 * register_ftrace_function - register a function for profiling
4051 * @ops - ops structure that holds the function for profiling.
4052 *
4053 * Register a function to be called by all functions in the
4054 * kernel.
4055 *
4056 * Note: @ops->func and all the functions it calls must be labeled
4057 * with "notrace", otherwise it will go into a
4058 * recursive loop.
4059 */
4060int register_ftrace_function(struct ftrace_ops *ops)
4061{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004062 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004063
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004064 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004065
Steven Rostedt45a4a232011-04-21 23:16:46 -04004066 if (unlikely(ftrace_disabled))
4067 goto out_unlock;
4068
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004069 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004070 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004071 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004072
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004073
Steven Rostedt45a4a232011-04-21 23:16:46 -04004074 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004075 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004076 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004077}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004078EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004079
4080/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004081 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004082 * @ops - ops structure that holds the function to unregister
4083 *
4084 * Unregister a function that was added to be called by ftrace profiling.
4085 */
4086int unregister_ftrace_function(struct ftrace_ops *ops)
4087{
4088 int ret;
4089
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004090 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004091 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004092 if (!ret)
4093 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004094 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004095
4096 return ret;
4097}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004098EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004099
Ingo Molnare309b412008-05-12 21:20:51 +02004100int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004101ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004102 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004103 loff_t *ppos)
4104{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004105 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004106
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004107 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004108
Steven Rostedt45a4a232011-04-21 23:16:46 -04004109 if (unlikely(ftrace_disabled))
4110 goto out;
4111
4112 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004113
Li Zefana32c7762009-06-26 16:55:51 +08004114 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004115 goto out;
4116
Li Zefana32c7762009-06-26 16:55:51 +08004117 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004118
4119 if (ftrace_enabled) {
4120
4121 ftrace_startup_sysctl();
4122
4123 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004124 if (ftrace_ops_list != &ftrace_list_end) {
4125 if (ftrace_ops_list->next == &ftrace_list_end)
4126 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004127 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004128 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004129 }
4130
4131 } else {
4132 /* stopping ftrace calls (just send to ftrace_stub) */
4133 ftrace_trace_function = ftrace_stub;
4134
4135 ftrace_shutdown_sysctl();
4136 }
4137
4138 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004139 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004140 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004141}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004142
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004143#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004144
Steven Rostedt597af812009-04-03 15:24:12 -04004145static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004146static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004147
Steven Rostedte49dc192008-12-02 23:50:05 -05004148int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4149{
4150 return 0;
4151}
4152
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004153/* The callbacks that hook a function */
4154trace_func_graph_ret_t ftrace_graph_return =
4155 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004156trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004157
4158/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4159static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4160{
4161 int i;
4162 int ret = 0;
4163 unsigned long flags;
4164 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4165 struct task_struct *g, *t;
4166
4167 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4168 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4169 * sizeof(struct ftrace_ret_stack),
4170 GFP_KERNEL);
4171 if (!ret_stack_list[i]) {
4172 start = 0;
4173 end = i;
4174 ret = -ENOMEM;
4175 goto free;
4176 }
4177 }
4178
4179 read_lock_irqsave(&tasklist_lock, flags);
4180 do_each_thread(g, t) {
4181 if (start == end) {
4182 ret = -EAGAIN;
4183 goto unlock;
4184 }
4185
4186 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004187 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004188 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004189 t->curr_ret_stack = -1;
4190 /* Make sure the tasks see the -1 first: */
4191 smp_wmb();
4192 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004193 }
4194 } while_each_thread(g, t);
4195
4196unlock:
4197 read_unlock_irqrestore(&tasklist_lock, flags);
4198free:
4199 for (i = start; i < end; i++)
4200 kfree(ret_stack_list[i]);
4201 return ret;
4202}
4203
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004204static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004205ftrace_graph_probe_sched_switch(void *ignore,
4206 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004207{
4208 unsigned long long timestamp;
4209 int index;
4210
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004211 /*
4212 * Does the user want to count the time a function was asleep.
4213 * If so, do not update the time stamps.
4214 */
4215 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4216 return;
4217
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004218 timestamp = trace_clock_local();
4219
4220 prev->ftrace_timestamp = timestamp;
4221
4222 /* only process tasks that we timestamped */
4223 if (!next->ftrace_timestamp)
4224 return;
4225
4226 /*
4227 * Update all the counters in next to make up for the
4228 * time next was sleeping.
4229 */
4230 timestamp -= next->ftrace_timestamp;
4231
4232 for (index = next->curr_ret_stack; index >= 0; index--)
4233 next->ret_stack[index].calltime += timestamp;
4234}
4235
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004236/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004237static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004238{
4239 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004240 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004241
4242 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4243 sizeof(struct ftrace_ret_stack *),
4244 GFP_KERNEL);
4245
4246 if (!ret_stack_list)
4247 return -ENOMEM;
4248
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004249 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004250 for_each_online_cpu(cpu) {
4251 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004252 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004253 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004254
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004255 do {
4256 ret = alloc_retstack_tasklist(ret_stack_list);
4257 } while (ret == -EAGAIN);
4258
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004259 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004260 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004261 if (ret)
4262 pr_info("ftrace_graph: Couldn't activate tracepoint"
4263 " probe to kernel_sched_switch\n");
4264 }
4265
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004266 kfree(ret_stack_list);
4267 return ret;
4268}
4269
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004270/*
4271 * Hibernation protection.
4272 * The state of the current task is too much unstable during
4273 * suspend/restore to disk. We want to protect against that.
4274 */
4275static int
4276ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4277 void *unused)
4278{
4279 switch (state) {
4280 case PM_HIBERNATION_PREPARE:
4281 pause_graph_tracing();
4282 break;
4283
4284 case PM_POST_HIBERNATION:
4285 unpause_graph_tracing();
4286 break;
4287 }
4288 return NOTIFY_DONE;
4289}
4290
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004291int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4292 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004293{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004294 int ret = 0;
4295
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004296 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004297
Steven Rostedt05ce5812009-03-24 00:18:31 -04004298 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004299 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004300 ret = -EBUSY;
4301 goto out;
4302 }
4303
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004304 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4305 register_pm_notifier(&ftrace_suspend_notifier);
4306
Steven Rostedt597af812009-04-03 15:24:12 -04004307 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004308 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004309 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004310 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004311 goto out;
4312 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004313
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004314 ftrace_graph_return = retfunc;
4315 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004316
Steven Rostedta1cd6172011-05-23 15:24:25 -04004317 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004318
4319out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004320 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004321 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004322}
4323
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004324void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004325{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004326 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004327
Steven Rostedt597af812009-04-03 15:24:12 -04004328 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004329 goto out;
4330
Steven Rostedt597af812009-04-03 15:24:12 -04004331 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004332 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004333 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004334 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004335 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004336 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004337
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004338 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004339 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004340}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004341
Steven Rostedt868baf02011-02-10 21:26:13 -05004342static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4343
4344static void
4345graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4346{
4347 atomic_set(&t->tracing_graph_pause, 0);
4348 atomic_set(&t->trace_overrun, 0);
4349 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004350 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004351 smp_wmb();
4352 t->ret_stack = ret_stack;
4353}
4354
4355/*
4356 * Allocate a return stack for the idle task. May be the first
4357 * time through, or it may be done by CPU hotplug online.
4358 */
4359void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4360{
4361 t->curr_ret_stack = -1;
4362 /*
4363 * The idle task has no parent, it either has its own
4364 * stack or no stack at all.
4365 */
4366 if (t->ret_stack)
4367 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4368
4369 if (ftrace_graph_active) {
4370 struct ftrace_ret_stack *ret_stack;
4371
4372 ret_stack = per_cpu(idle_ret_stack, cpu);
4373 if (!ret_stack) {
4374 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4375 * sizeof(struct ftrace_ret_stack),
4376 GFP_KERNEL);
4377 if (!ret_stack)
4378 return;
4379 per_cpu(idle_ret_stack, cpu) = ret_stack;
4380 }
4381 graph_init_task(t, ret_stack);
4382 }
4383}
4384
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004385/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004386void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004387{
Steven Rostedt84047e32009-06-02 16:51:55 -04004388 /* Make sure we do not use the parent ret_stack */
4389 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004390 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004391
Steven Rostedt597af812009-04-03 15:24:12 -04004392 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004393 struct ftrace_ret_stack *ret_stack;
4394
4395 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004396 * sizeof(struct ftrace_ret_stack),
4397 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004398 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004399 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004400 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004401 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004402}
4403
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004404void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004405{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004406 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4407
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004408 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004409 /* NULL must become visible to IRQs before we free it: */
4410 barrier();
4411
4412 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004413}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004414
4415void ftrace_graph_stop(void)
4416{
4417 ftrace_stop();
4418}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004419#endif