blob: a0dc0de8d6491b017c62e9f65e3bfa37074ec604 [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>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010025#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020026#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020028#include <linux/ctype.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020029#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050030#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080031#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020032
Steven Rostedtad8d75f2009-04-14 19:39:12 -040033#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040034
Steven Rostedt2af15d62009-05-28 13:37:24 -040035#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053036
Steven Rostedt0706f1c2009-03-23 23:12:58 -040037#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040038#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020039
Steven Rostedt6912896e2008-10-23 09:33:03 -040040#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040041 ({ \
42 int ___r = cond; \
43 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040044 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040045 ___r; \
46 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040047
48#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040049 ({ \
50 int ___r = cond; \
51 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040052 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040053 ___r; \
54 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040055
Steven Rostedt8fc0c702009-02-16 15:28:00 -050056/* hash bits for specific function selection */
57#define FTRACE_HASH_BITS 7
58#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040059#define FTRACE_HASH_DEFAULT_BITS 10
60#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050061
Steven Rostedt4eebcc82008-05-12 21:20:48 +020062/* ftrace_enabled is a method to turn ftrace on or off */
63int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020064static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020065
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050066/* Quick disabling of function tracer. */
67int function_trace_stop;
68
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040069/* List for set_ftrace_pid's pids. */
70LIST_HEAD(ftrace_pids);
71struct ftrace_pid {
72 struct list_head list;
73 struct pid *pid;
74};
75
Steven Rostedt4eebcc82008-05-12 21:20:48 +020076/*
77 * ftrace_disabled is set when an anomaly is discovered.
78 * ftrace_disabled is much stronger than ftrace_enabled.
79 */
80static int ftrace_disabled __read_mostly;
81
Steven Rostedt52baf112009-02-14 01:15:39 -050082static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020083
Paul McQuadebd38c0e2011-05-31 20:51:55 +010084static struct ftrace_ops ftrace_list_end __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -040085 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020086};
87
Steven Rostedtb8489142011-05-04 09:27:52 -040088static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
89static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020090ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050091ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050092ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -040093static struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020094
Steven Rostedtb8489142011-05-04 09:27:52 -040095static void
96ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
97
Paul E. McKenney3f379b02010-03-05 15:03:25 -080098/*
Steven Rostedtb8489142011-05-04 09:27:52 -040099 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800100 * can use rcu_dereference_raw() is that elements removed from this list
101 * are simply leaked, so there is no need to interact with a grace-period
102 * mechanism. The rcu_dereference_raw() calls are needed to handle
Steven Rostedtb8489142011-05-04 09:27:52 -0400103 * concurrent insertions into the ftrace_global_list.
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800104 *
105 * Silly Alpha and silly pointer-speculation compiler optimizations!
106 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400107static void ftrace_global_list_func(unsigned long ip,
108 unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200109{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400110 struct ftrace_ops *op;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200111
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400112 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
113 return;
114
115 trace_recursion_set(TRACE_GLOBAL_BIT);
116 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200117 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200118 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800119 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200120 };
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400121 trace_recursion_clear(TRACE_GLOBAL_BIT);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200122}
123
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500124static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
125{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500126 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500127 return;
128
129 ftrace_pid_function(ip, parent_ip);
130}
131
132static void set_ftrace_pid_function(ftrace_func_t func)
133{
134 /* do not set ftrace_pid_function to itself! */
135 if (func != ftrace_pid_func)
136 ftrace_pid_function = func;
137}
138
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200139/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200140 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200141 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200142 * This NULLs the ftrace function and in essence stops
143 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200144 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200145void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200146{
Steven Rostedt3d083392008-05-12 21:20:42 +0200147 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500148 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500149 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200150}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200151
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500152#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
153/*
154 * For those archs that do not test ftrace_trace_stop in their
155 * mcount call site, we need to do it from C.
156 */
157static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
158{
159 if (function_trace_stop)
160 return;
161
162 __ftrace_trace_function(ip, parent_ip);
163}
164#endif
165
Steven Rostedt2b499382011-05-03 22:49:52 -0400166static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400167{
168 ftrace_func_t func;
169
170 /*
171 * If there's only one function registered, then call that
172 * function directly. Otherwise, we need to iterate over the
173 * registered callers.
174 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400175 if (ftrace_global_list == &ftrace_list_end ||
176 ftrace_global_list->next == &ftrace_list_end)
177 func = ftrace_global_list->func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400178 else
Steven Rostedtb8489142011-05-04 09:27:52 -0400179 func = ftrace_global_list_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400180
181 /* If we filter on pids, update to use the pid function */
182 if (!list_empty(&ftrace_pids)) {
183 set_ftrace_pid_function(func);
184 func = ftrace_pid_func;
185 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400186
187 global_ops.func = func;
188}
189
190static void update_ftrace_function(void)
191{
192 ftrace_func_t func;
193
194 update_global_ops();
195
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400196 /*
197 * If we are at the end of the list and this ops is
198 * not dynamic, then have the mcount trampoline call
199 * the function directly
200 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400201 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400202 (ftrace_ops_list->next == &ftrace_list_end &&
203 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
Steven Rostedtb8489142011-05-04 09:27:52 -0400204 func = ftrace_ops_list->func;
205 else
206 func = ftrace_ops_list_func;
Steven Rostedt2b499382011-05-03 22:49:52 -0400207
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400208#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
209 ftrace_trace_function = func;
210#else
211 __ftrace_trace_function = func;
212 ftrace_trace_function = ftrace_test_stop_func;
213#endif
214}
215
Steven Rostedt2b499382011-05-03 22:49:52 -0400216static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200217{
Steven Rostedt2b499382011-05-03 22:49:52 -0400218 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200219 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400220 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200221 * CPU might be walking that list. We need to make sure
222 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400223 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200224 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400225 rcu_assign_pointer(*list, ops);
226}
Steven Rostedt3d083392008-05-12 21:20:42 +0200227
Steven Rostedt2b499382011-05-03 22:49:52 -0400228static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
229{
230 struct ftrace_ops **p;
231
232 /*
233 * If we are removing the last function, then simply point
234 * to the ftrace_stub.
235 */
236 if (*list == ops && ops->next == &ftrace_list_end) {
237 *list = &ftrace_list_end;
238 return 0;
239 }
240
241 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
242 if (*p == ops)
243 break;
244
245 if (*p != ops)
246 return -1;
247
248 *p = (*p)->next;
249 return 0;
250}
251
252static int __register_ftrace_function(struct ftrace_ops *ops)
253{
254 if (ftrace_disabled)
255 return -ENODEV;
256
257 if (FTRACE_WARN_ON(ops == &global_ops))
258 return -EINVAL;
259
Steven Rostedtb8489142011-05-04 09:27:52 -0400260 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
261 return -EBUSY;
262
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400263 if (!core_kernel_data((unsigned long)ops))
264 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
265
Steven Rostedtb8489142011-05-04 09:27:52 -0400266 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
267 int first = ftrace_global_list == &ftrace_list_end;
268 add_ftrace_ops(&ftrace_global_list, ops);
269 ops->flags |= FTRACE_OPS_FL_ENABLED;
270 if (first)
271 add_ftrace_ops(&ftrace_ops_list, &global_ops);
272 } else
273 add_ftrace_ops(&ftrace_ops_list, ops);
274
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400275 if (ftrace_enabled)
276 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200277
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200278 return 0;
279}
280
Ingo Molnare309b412008-05-12 21:20:51 +0200281static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200282{
Steven Rostedt2b499382011-05-03 22:49:52 -0400283 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200284
Steven Rostedt2b499382011-05-03 22:49:52 -0400285 if (ftrace_disabled)
286 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200287
Steven Rostedtb8489142011-05-04 09:27:52 -0400288 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
289 return -EBUSY;
290
Steven Rostedt2b499382011-05-03 22:49:52 -0400291 if (FTRACE_WARN_ON(ops == &global_ops))
292 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200293
Steven Rostedtb8489142011-05-04 09:27:52 -0400294 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
295 ret = remove_ftrace_ops(&ftrace_global_list, ops);
296 if (!ret && ftrace_global_list == &ftrace_list_end)
297 ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops);
298 if (!ret)
299 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
300 } else
301 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
302
Steven Rostedt2b499382011-05-03 22:49:52 -0400303 if (ret < 0)
304 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400305
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400306 if (ftrace_enabled)
307 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200308
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400309 /*
310 * Dynamic ops may be freed, we must make sure that all
311 * callers are done before leaving this function.
312 */
313 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
314 synchronize_sched();
315
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500316 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200317}
318
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500319static void ftrace_update_pid_func(void)
320{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400321 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500322 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900323 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500324
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400325 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500326}
327
Steven Rostedt493762f2009-03-23 17:12:36 -0400328#ifdef CONFIG_FUNCTION_PROFILER
329struct ftrace_profile {
330 struct hlist_node node;
331 unsigned long ip;
332 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400333#ifdef CONFIG_FUNCTION_GRAPH_TRACER
334 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400335 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400336#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400337};
338
339struct ftrace_profile_page {
340 struct ftrace_profile_page *next;
341 unsigned long index;
342 struct ftrace_profile records[];
343};
344
Steven Rostedtcafb1682009-03-24 20:50:39 -0400345struct ftrace_profile_stat {
346 atomic_t disabled;
347 struct hlist_head *hash;
348 struct ftrace_profile_page *pages;
349 struct ftrace_profile_page *start;
350 struct tracer_stat stat;
351};
352
Steven Rostedt493762f2009-03-23 17:12:36 -0400353#define PROFILE_RECORDS_SIZE \
354 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
355
356#define PROFILES_PER_PAGE \
357 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
358
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400359static int ftrace_profile_bits __read_mostly;
360static int ftrace_profile_enabled __read_mostly;
361
362/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400363static DEFINE_MUTEX(ftrace_profile_lock);
364
Steven Rostedtcafb1682009-03-24 20:50:39 -0400365static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400366
367#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
368
Steven Rostedt493762f2009-03-23 17:12:36 -0400369static void *
370function_stat_next(void *v, int idx)
371{
372 struct ftrace_profile *rec = v;
373 struct ftrace_profile_page *pg;
374
375 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
376
377 again:
Li Zefan0296e422009-06-26 11:15:37 +0800378 if (idx != 0)
379 rec++;
380
Steven Rostedt493762f2009-03-23 17:12:36 -0400381 if ((void *)rec >= (void *)&pg->records[pg->index]) {
382 pg = pg->next;
383 if (!pg)
384 return NULL;
385 rec = &pg->records[0];
386 if (!rec->counter)
387 goto again;
388 }
389
390 return rec;
391}
392
393static void *function_stat_start(struct tracer_stat *trace)
394{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400395 struct ftrace_profile_stat *stat =
396 container_of(trace, struct ftrace_profile_stat, stat);
397
398 if (!stat || !stat->start)
399 return NULL;
400
401 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400402}
403
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400404#ifdef CONFIG_FUNCTION_GRAPH_TRACER
405/* function graph compares on total time */
406static int function_stat_cmp(void *p1, void *p2)
407{
408 struct ftrace_profile *a = p1;
409 struct ftrace_profile *b = p2;
410
411 if (a->time < b->time)
412 return -1;
413 if (a->time > b->time)
414 return 1;
415 else
416 return 0;
417}
418#else
419/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400420static int function_stat_cmp(void *p1, void *p2)
421{
422 struct ftrace_profile *a = p1;
423 struct ftrace_profile *b = p2;
424
425 if (a->counter < b->counter)
426 return -1;
427 if (a->counter > b->counter)
428 return 1;
429 else
430 return 0;
431}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400432#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400433
434static int function_stat_headers(struct seq_file *m)
435{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400436#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400437 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400438 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400439 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400440 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400441#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400442 seq_printf(m, " Function Hit\n"
443 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400444#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400445 return 0;
446}
447
448static int function_stat_show(struct seq_file *m, void *v)
449{
450 struct ftrace_profile *rec = v;
451 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800452 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400453#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400454 static struct trace_seq s;
455 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400456 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400457#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800458 mutex_lock(&ftrace_profile_lock);
459
460 /* we raced with function_profile_reset() */
461 if (unlikely(rec->counter == 0)) {
462 ret = -EBUSY;
463 goto out;
464 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400465
466 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400467 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400468
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400469#ifdef CONFIG_FUNCTION_GRAPH_TRACER
470 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400471 avg = rec->time;
472 do_div(avg, rec->counter);
473
Chase Douglase330b3b2010-04-26 14:02:05 -0400474 /* Sample standard deviation (s^2) */
475 if (rec->counter <= 1)
476 stddev = 0;
477 else {
478 stddev = rec->time_squared - rec->counter * avg * avg;
479 /*
480 * Divide only 1000 for ns^2 -> us^2 conversion.
481 * trace_print_graph_duration will divide 1000 again.
482 */
483 do_div(stddev, (rec->counter - 1) * 1000);
484 }
485
Steven Rostedt34886c82009-03-25 21:00:47 -0400486 trace_seq_init(&s);
487 trace_print_graph_duration(rec->time, &s);
488 trace_seq_puts(&s, " ");
489 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400490 trace_seq_puts(&s, " ");
491 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400492 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400493#endif
494 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800495out:
496 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400497
Li Zefan3aaba202010-08-23 16:50:12 +0800498 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400499}
500
Steven Rostedtcafb1682009-03-24 20:50:39 -0400501static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400502{
503 struct ftrace_profile_page *pg;
504
Steven Rostedtcafb1682009-03-24 20:50:39 -0400505 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400506
507 while (pg) {
508 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
509 pg->index = 0;
510 pg = pg->next;
511 }
512
Steven Rostedtcafb1682009-03-24 20:50:39 -0400513 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400514 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
515}
516
Steven Rostedtcafb1682009-03-24 20:50:39 -0400517int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400518{
519 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400520 int functions;
521 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400522 int i;
523
524 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400525 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400526 return 0;
527
Steven Rostedtcafb1682009-03-24 20:50:39 -0400528 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
529 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400530 return -ENOMEM;
531
Steven Rostedt318e0a72009-03-25 20:06:34 -0400532#ifdef CONFIG_DYNAMIC_FTRACE
533 functions = ftrace_update_tot_cnt;
534#else
535 /*
536 * We do not know the number of functions that exist because
537 * dynamic tracing is what counts them. With past experience
538 * we have around 20K functions. That should be more than enough.
539 * It is highly unlikely we will execute every function in
540 * the kernel.
541 */
542 functions = 20000;
543#endif
544
Steven Rostedtcafb1682009-03-24 20:50:39 -0400545 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400546
Steven Rostedt318e0a72009-03-25 20:06:34 -0400547 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
548
549 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400550 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400551 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400552 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400553 pg = pg->next;
554 }
555
556 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400557
558 out_free:
559 pg = stat->start;
560 while (pg) {
561 unsigned long tmp = (unsigned long)pg;
562
563 pg = pg->next;
564 free_page(tmp);
565 }
566
567 free_page((unsigned long)stat->pages);
568 stat->pages = NULL;
569 stat->start = NULL;
570
571 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400572}
573
Steven Rostedtcafb1682009-03-24 20:50:39 -0400574static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400575{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400576 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400577 int size;
578
Steven Rostedtcafb1682009-03-24 20:50:39 -0400579 stat = &per_cpu(ftrace_profile_stats, cpu);
580
581 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400582 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400583 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400584 return 0;
585 }
586
587 /*
588 * We are profiling all functions, but usually only a few thousand
589 * functions are hit. We'll make a hash of 1024 items.
590 */
591 size = FTRACE_PROFILE_HASH_SIZE;
592
Steven Rostedtcafb1682009-03-24 20:50:39 -0400593 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400594
Steven Rostedtcafb1682009-03-24 20:50:39 -0400595 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400596 return -ENOMEM;
597
Steven Rostedtcafb1682009-03-24 20:50:39 -0400598 if (!ftrace_profile_bits) {
599 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400600
Steven Rostedtcafb1682009-03-24 20:50:39 -0400601 for (; size; size >>= 1)
602 ftrace_profile_bits++;
603 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400604
Steven Rostedt318e0a72009-03-25 20:06:34 -0400605 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400606 if (ftrace_profile_pages_init(stat) < 0) {
607 kfree(stat->hash);
608 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400609 return -ENOMEM;
610 }
611
612 return 0;
613}
614
Steven Rostedtcafb1682009-03-24 20:50:39 -0400615static int ftrace_profile_init(void)
616{
617 int cpu;
618 int ret = 0;
619
620 for_each_online_cpu(cpu) {
621 ret = ftrace_profile_init_cpu(cpu);
622 if (ret)
623 break;
624 }
625
626 return ret;
627}
628
Steven Rostedt493762f2009-03-23 17:12:36 -0400629/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400630static struct ftrace_profile *
631ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400632{
633 struct ftrace_profile *rec;
634 struct hlist_head *hhd;
635 struct hlist_node *n;
636 unsigned long key;
637
638 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400639 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400640
641 if (hlist_empty(hhd))
642 return NULL;
643
644 hlist_for_each_entry_rcu(rec, n, hhd, node) {
645 if (rec->ip == ip)
646 return rec;
647 }
648
649 return NULL;
650}
651
Steven Rostedtcafb1682009-03-24 20:50:39 -0400652static void ftrace_add_profile(struct ftrace_profile_stat *stat,
653 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400654{
655 unsigned long key;
656
657 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400658 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400659}
660
Steven Rostedt318e0a72009-03-25 20:06:34 -0400661/*
662 * The memory is already allocated, this simply finds a new record to use.
663 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400664static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400665ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400666{
667 struct ftrace_profile *rec = NULL;
668
Steven Rostedt318e0a72009-03-25 20:06:34 -0400669 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400670 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400671 goto out;
672
Steven Rostedt493762f2009-03-23 17:12:36 -0400673 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400674 * Try to find the function again since an NMI
675 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400676 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400677 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400678 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400679 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400680
Steven Rostedtcafb1682009-03-24 20:50:39 -0400681 if (stat->pages->index == PROFILES_PER_PAGE) {
682 if (!stat->pages->next)
683 goto out;
684 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400685 }
686
Steven Rostedtcafb1682009-03-24 20:50:39 -0400687 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400688 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400689 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400690
Steven Rostedt493762f2009-03-23 17:12:36 -0400691 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400692 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400693
694 return rec;
695}
696
Steven Rostedt493762f2009-03-23 17:12:36 -0400697static void
698function_profile_call(unsigned long ip, unsigned long parent_ip)
699{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400700 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400701 struct ftrace_profile *rec;
702 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400703
704 if (!ftrace_profile_enabled)
705 return;
706
Steven Rostedt493762f2009-03-23 17:12:36 -0400707 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400708
709 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400710 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400711 goto out;
712
713 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400714 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400715 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400716 if (!rec)
717 goto out;
718 }
719
720 rec->counter++;
721 out:
722 local_irq_restore(flags);
723}
724
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400725#ifdef CONFIG_FUNCTION_GRAPH_TRACER
726static int profile_graph_entry(struct ftrace_graph_ent *trace)
727{
728 function_profile_call(trace->func, 0);
729 return 1;
730}
731
732static void profile_graph_return(struct ftrace_graph_ret *trace)
733{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400734 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400735 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400736 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400737 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400738
739 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400740 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400741 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400742 goto out;
743
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400744 /* If the calltime was zero'd ignore it */
745 if (!trace->calltime)
746 goto out;
747
Steven Rostedta2a16d62009-03-24 23:17:58 -0400748 calltime = trace->rettime - trace->calltime;
749
750 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
751 int index;
752
753 index = trace->depth;
754
755 /* Append this call time to the parent time to subtract */
756 if (index)
757 current->ret_stack[index - 1].subtime += calltime;
758
759 if (current->ret_stack[index].subtime < calltime)
760 calltime -= current->ret_stack[index].subtime;
761 else
762 calltime = 0;
763 }
764
Steven Rostedtcafb1682009-03-24 20:50:39 -0400765 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400766 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400767 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400768 rec->time_squared += calltime * calltime;
769 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400770
Steven Rostedtcafb1682009-03-24 20:50:39 -0400771 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400772 local_irq_restore(flags);
773}
774
775static int register_ftrace_profiler(void)
776{
777 return register_ftrace_graph(&profile_graph_return,
778 &profile_graph_entry);
779}
780
781static void unregister_ftrace_profiler(void)
782{
783 unregister_ftrace_graph();
784}
785#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100786static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400787 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400788};
789
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400790static int register_ftrace_profiler(void)
791{
792 return register_ftrace_function(&ftrace_profile_ops);
793}
794
795static void unregister_ftrace_profiler(void)
796{
797 unregister_ftrace_function(&ftrace_profile_ops);
798}
799#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
800
Steven Rostedt493762f2009-03-23 17:12:36 -0400801static ssize_t
802ftrace_profile_write(struct file *filp, const char __user *ubuf,
803 size_t cnt, loff_t *ppos)
804{
805 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400806 int ret;
807
Peter Huewe22fe9b52011-06-07 21:58:27 +0200808 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
809 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400810 return ret;
811
812 val = !!val;
813
814 mutex_lock(&ftrace_profile_lock);
815 if (ftrace_profile_enabled ^ val) {
816 if (val) {
817 ret = ftrace_profile_init();
818 if (ret < 0) {
819 cnt = ret;
820 goto out;
821 }
822
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400823 ret = register_ftrace_profiler();
824 if (ret < 0) {
825 cnt = ret;
826 goto out;
827 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400828 ftrace_profile_enabled = 1;
829 } else {
830 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400831 /*
832 * unregister_ftrace_profiler calls stop_machine
833 * so this acts like an synchronize_sched.
834 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400835 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400836 }
837 }
838 out:
839 mutex_unlock(&ftrace_profile_lock);
840
Jiri Olsacf8517c2009-10-23 19:36:16 -0400841 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400842
843 return cnt;
844}
845
846static ssize_t
847ftrace_profile_read(struct file *filp, char __user *ubuf,
848 size_t cnt, loff_t *ppos)
849{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400850 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400851 int r;
852
853 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
854 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
855}
856
857static const struct file_operations ftrace_profile_fops = {
858 .open = tracing_open_generic,
859 .read = ftrace_profile_read,
860 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200861 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400862};
863
Steven Rostedtcafb1682009-03-24 20:50:39 -0400864/* used to initialize the real stat files */
865static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400866 .name = "functions",
867 .stat_start = function_stat_start,
868 .stat_next = function_stat_next,
869 .stat_cmp = function_stat_cmp,
870 .stat_headers = function_stat_headers,
871 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400872};
873
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400874static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400875{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400876 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400877 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400878 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400879 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400880 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400881
Steven Rostedtcafb1682009-03-24 20:50:39 -0400882 for_each_possible_cpu(cpu) {
883 stat = &per_cpu(ftrace_profile_stats, cpu);
884
885 /* allocate enough for function name + cpu number */
886 name = kmalloc(32, GFP_KERNEL);
887 if (!name) {
888 /*
889 * The files created are permanent, if something happens
890 * we still do not free memory.
891 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400892 WARN(1,
893 "Could not allocate stat file for cpu %d\n",
894 cpu);
895 return;
896 }
897 stat->stat = function_stats;
898 snprintf(name, 32, "function%d", cpu);
899 stat->stat.name = name;
900 ret = register_stat_tracer(&stat->stat);
901 if (ret) {
902 WARN(1,
903 "Could not register function stat for cpu %d\n",
904 cpu);
905 kfree(name);
906 return;
907 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400908 }
909
910 entry = debugfs_create_file("function_profile_enabled", 0644,
911 d_tracer, NULL, &ftrace_profile_fops);
912 if (!entry)
913 pr_warning("Could not create debugfs "
914 "'function_profile_enabled' entry\n");
915}
916
917#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400918static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400919{
920}
921#endif /* CONFIG_FUNCTION_PROFILER */
922
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100923static struct pid * const ftrace_swapper_pid = &init_struct_pid;
924
Steven Rostedt3d083392008-05-12 21:20:42 +0200925#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100926
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400927#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400928# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400929#endif
930
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500931static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
932
Steven Rostedtb6887d72009-02-17 12:32:04 -0500933struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500934 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500935 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500936 unsigned long flags;
937 unsigned long ip;
938 void *data;
939 struct rcu_head rcu;
940};
941
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200942enum {
943 FTRACE_ENABLE_CALLS = (1 << 0),
944 FTRACE_DISABLE_CALLS = (1 << 1),
945 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
Steven Rostedt79e406d2010-09-14 22:19:46 -0400946 FTRACE_START_FUNC_RET = (1 << 3),
947 FTRACE_STOP_FUNC_RET = (1 << 4),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200948};
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400949struct ftrace_func_entry {
950 struct hlist_node hlist;
951 unsigned long ip;
952};
953
954struct ftrace_hash {
955 unsigned long size_bits;
956 struct hlist_head *buckets;
957 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -0400958 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400959};
960
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400961/*
962 * We make these constant because no one should touch them,
963 * but they are used as the default "empty hash", to avoid allocating
964 * it all the time. These are in a read only section such that if
965 * anyone does try to modify it, it will cause an exception.
966 */
967static const struct hlist_head empty_buckets[1];
968static const struct ftrace_hash empty_hash = {
969 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400970};
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400971#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +0200972
Steven Rostedt2b499382011-05-03 22:49:52 -0400973static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -0400974 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400975 .notrace_hash = EMPTY_HASH,
976 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -0400977};
978
Lai Jiangshane94142a2009-03-13 17:51:27 +0800979static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200980
Steven Rostedt41c52c02008-05-22 11:46:33 -0400981static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200982
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200983struct ftrace_page {
984 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500985 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200986 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700987};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200988
989#define ENTRIES_PER_PAGE \
990 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
991
992/* estimate from running different kernels */
993#define NR_TO_INIT 10000
994
995static struct ftrace_page *ftrace_pages_start;
996static struct ftrace_page *ftrace_pages;
997
Steven Rostedt37ad50842008-05-12 21:20:48 +0200998static struct dyn_ftrace *ftrace_free_records;
999
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001000static struct ftrace_func_entry *
1001ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1002{
1003 unsigned long key;
1004 struct ftrace_func_entry *entry;
1005 struct hlist_head *hhd;
1006 struct hlist_node *n;
1007
1008 if (!hash->count)
1009 return NULL;
1010
1011 if (hash->size_bits > 0)
1012 key = hash_long(ip, hash->size_bits);
1013 else
1014 key = 0;
1015
1016 hhd = &hash->buckets[key];
1017
1018 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1019 if (entry->ip == ip)
1020 return entry;
1021 }
1022 return NULL;
1023}
1024
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001025static void __add_hash_entry(struct ftrace_hash *hash,
1026 struct ftrace_func_entry *entry)
1027{
1028 struct hlist_head *hhd;
1029 unsigned long key;
1030
1031 if (hash->size_bits)
1032 key = hash_long(entry->ip, hash->size_bits);
1033 else
1034 key = 0;
1035
1036 hhd = &hash->buckets[key];
1037 hlist_add_head(&entry->hlist, hhd);
1038 hash->count++;
1039}
1040
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001041static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1042{
1043 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001044
1045 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1046 if (!entry)
1047 return -ENOMEM;
1048
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001049 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001050 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001051
1052 return 0;
1053}
1054
1055static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001056free_hash_entry(struct ftrace_hash *hash,
1057 struct ftrace_func_entry *entry)
1058{
1059 hlist_del(&entry->hlist);
1060 kfree(entry);
1061 hash->count--;
1062}
1063
1064static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001065remove_hash_entry(struct ftrace_hash *hash,
1066 struct ftrace_func_entry *entry)
1067{
1068 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001069 hash->count--;
1070}
1071
1072static void ftrace_hash_clear(struct ftrace_hash *hash)
1073{
1074 struct hlist_head *hhd;
1075 struct hlist_node *tp, *tn;
1076 struct ftrace_func_entry *entry;
1077 int size = 1 << hash->size_bits;
1078 int i;
1079
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001080 if (!hash->count)
1081 return;
1082
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001083 for (i = 0; i < size; i++) {
1084 hhd = &hash->buckets[i];
1085 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001086 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001087 }
1088 FTRACE_WARN_ON(hash->count);
1089}
1090
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001091static void free_ftrace_hash(struct ftrace_hash *hash)
1092{
1093 if (!hash || hash == EMPTY_HASH)
1094 return;
1095 ftrace_hash_clear(hash);
1096 kfree(hash->buckets);
1097 kfree(hash);
1098}
1099
Steven Rostedt07fd5512011-05-05 18:03:47 -04001100static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1101{
1102 struct ftrace_hash *hash;
1103
1104 hash = container_of(rcu, struct ftrace_hash, rcu);
1105 free_ftrace_hash(hash);
1106}
1107
1108static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1109{
1110 if (!hash || hash == EMPTY_HASH)
1111 return;
1112 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1113}
1114
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001115static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1116{
1117 struct ftrace_hash *hash;
1118 int size;
1119
1120 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1121 if (!hash)
1122 return NULL;
1123
1124 size = 1 << size_bits;
1125 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1126
1127 if (!hash->buckets) {
1128 kfree(hash);
1129 return NULL;
1130 }
1131
1132 hash->size_bits = size_bits;
1133
1134 return hash;
1135}
1136
1137static struct ftrace_hash *
1138alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1139{
1140 struct ftrace_func_entry *entry;
1141 struct ftrace_hash *new_hash;
1142 struct hlist_node *tp;
1143 int size;
1144 int ret;
1145 int i;
1146
1147 new_hash = alloc_ftrace_hash(size_bits);
1148 if (!new_hash)
1149 return NULL;
1150
1151 /* Empty hash? */
1152 if (!hash || !hash->count)
1153 return new_hash;
1154
1155 size = 1 << hash->size_bits;
1156 for (i = 0; i < size; i++) {
1157 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1158 ret = add_hash_entry(new_hash, entry->ip);
1159 if (ret < 0)
1160 goto free_hash;
1161 }
1162 }
1163
1164 FTRACE_WARN_ON(new_hash->count != hash->count);
1165
1166 return new_hash;
1167
1168 free_hash:
1169 free_ftrace_hash(new_hash);
1170 return NULL;
1171}
1172
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001173static void
1174ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1175static void
1176ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1177
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001178static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001179ftrace_hash_move(struct ftrace_ops *ops, int enable,
1180 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001181{
1182 struct ftrace_func_entry *entry;
1183 struct hlist_node *tp, *tn;
1184 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001185 struct ftrace_hash *old_hash;
1186 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001187 unsigned long key;
1188 int size = src->count;
1189 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001190 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001191 int i;
1192
1193 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001194 * Remove the current set, update the hash and add
1195 * them back.
1196 */
1197 ftrace_hash_rec_disable(ops, enable);
1198
1199 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001200 * If the new source is empty, just free dst and assign it
1201 * the empty_hash.
1202 */
1203 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001204 free_ftrace_hash_rcu(*dst);
1205 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001206 return 0;
1207 }
1208
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001209 /*
1210 * Make the hash size about 1/2 the # found
1211 */
1212 for (size /= 2; size; size >>= 1)
1213 bits++;
1214
1215 /* Don't allocate too much */
1216 if (bits > FTRACE_HASH_MAX_BITS)
1217 bits = FTRACE_HASH_MAX_BITS;
1218
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001219 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001220 new_hash = alloc_ftrace_hash(bits);
1221 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001222 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001223
1224 size = 1 << src->size_bits;
1225 for (i = 0; i < size; i++) {
1226 hhd = &src->buckets[i];
1227 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1228 if (bits > 0)
1229 key = hash_long(entry->ip, bits);
1230 else
1231 key = 0;
1232 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001233 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001234 }
1235 }
1236
Steven Rostedt07fd5512011-05-05 18:03:47 -04001237 old_hash = *dst;
1238 rcu_assign_pointer(*dst, new_hash);
1239 free_ftrace_hash_rcu(old_hash);
1240
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001241 ret = 0;
1242 out:
1243 /*
1244 * Enable regardless of ret:
1245 * On success, we enable the new hash.
1246 * On failure, we re-enable the original hash.
1247 */
1248 ftrace_hash_rec_enable(ops, enable);
1249
1250 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001251}
1252
Steven Rostedt265c8312009-02-13 12:43:56 -05001253/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001254 * Test the hashes for this ops to see if we want to call
1255 * the ops->func or not.
1256 *
1257 * It's a match if the ip is in the ops->filter_hash or
1258 * the filter_hash does not exist or is empty,
1259 * AND
1260 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001261 *
1262 * This needs to be called with preemption disabled as
1263 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001264 */
1265static int
1266ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1267{
1268 struct ftrace_hash *filter_hash;
1269 struct ftrace_hash *notrace_hash;
1270 int ret;
1271
Steven Rostedtb8489142011-05-04 09:27:52 -04001272 filter_hash = rcu_dereference_raw(ops->filter_hash);
1273 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1274
1275 if ((!filter_hash || !filter_hash->count ||
1276 ftrace_lookup_ip(filter_hash, ip)) &&
1277 (!notrace_hash || !notrace_hash->count ||
1278 !ftrace_lookup_ip(notrace_hash, ip)))
1279 ret = 1;
1280 else
1281 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001282
1283 return ret;
1284}
1285
1286/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001287 * This is a double for. Do not use 'break' to break out of the loop,
1288 * you must use a goto.
1289 */
1290#define do_for_each_ftrace_rec(pg, rec) \
1291 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1292 int _____i; \
1293 for (_____i = 0; _____i < pg->index; _____i++) { \
1294 rec = &pg->records[_____i];
1295
1296#define while_for_each_ftrace_rec() \
1297 } \
1298 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301299
Steven Rostedted926f92011-05-03 13:25:24 -04001300static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1301 int filter_hash,
1302 bool inc)
1303{
1304 struct ftrace_hash *hash;
1305 struct ftrace_hash *other_hash;
1306 struct ftrace_page *pg;
1307 struct dyn_ftrace *rec;
1308 int count = 0;
1309 int all = 0;
1310
1311 /* Only update if the ops has been registered */
1312 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1313 return;
1314
1315 /*
1316 * In the filter_hash case:
1317 * If the count is zero, we update all records.
1318 * Otherwise we just update the items in the hash.
1319 *
1320 * In the notrace_hash case:
1321 * We enable the update in the hash.
1322 * As disabling notrace means enabling the tracing,
1323 * and enabling notrace means disabling, the inc variable
1324 * gets inversed.
1325 */
1326 if (filter_hash) {
1327 hash = ops->filter_hash;
1328 other_hash = ops->notrace_hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001329 if (!hash || !hash->count)
Steven Rostedted926f92011-05-03 13:25:24 -04001330 all = 1;
1331 } else {
1332 inc = !inc;
1333 hash = ops->notrace_hash;
1334 other_hash = ops->filter_hash;
1335 /*
1336 * If the notrace hash has no items,
1337 * then there's nothing to do.
1338 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001339 if (hash && !hash->count)
Steven Rostedted926f92011-05-03 13:25:24 -04001340 return;
1341 }
1342
1343 do_for_each_ftrace_rec(pg, rec) {
1344 int in_other_hash = 0;
1345 int in_hash = 0;
1346 int match = 0;
1347
1348 if (all) {
1349 /*
1350 * Only the filter_hash affects all records.
1351 * Update if the record is not in the notrace hash.
1352 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001353 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001354 match = 1;
1355 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -04001356 in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip);
1357 in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001358
1359 /*
1360 *
1361 */
1362 if (filter_hash && in_hash && !in_other_hash)
1363 match = 1;
1364 else if (!filter_hash && in_hash &&
1365 (in_other_hash || !other_hash->count))
1366 match = 1;
1367 }
1368 if (!match)
1369 continue;
1370
1371 if (inc) {
1372 rec->flags++;
1373 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1374 return;
1375 } else {
1376 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1377 return;
1378 rec->flags--;
1379 }
1380 count++;
1381 /* Shortcut, if we handled all records, we are done. */
1382 if (!all && count == hash->count)
1383 return;
1384 } while_for_each_ftrace_rec();
1385}
1386
1387static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1388 int filter_hash)
1389{
1390 __ftrace_hash_rec_update(ops, filter_hash, 0);
1391}
1392
1393static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1394 int filter_hash)
1395{
1396 __ftrace_hash_rec_update(ops, filter_hash, 1);
1397}
1398
Ingo Molnare309b412008-05-12 21:20:51 +02001399static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad50842008-05-12 21:20:48 +02001400{
Lai Jiangshanee000b72009-03-24 13:38:06 +08001401 rec->freelist = ftrace_free_records;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001402 ftrace_free_records = rec;
1403 rec->flags |= FTRACE_FL_FREE;
1404}
1405
Ingo Molnare309b412008-05-12 21:20:51 +02001406static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001407{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001408 struct dyn_ftrace *rec;
1409
1410 /* First check for freed records */
1411 if (ftrace_free_records) {
1412 rec = ftrace_free_records;
1413
Steven Rostedt37ad50842008-05-12 21:20:48 +02001414 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt6912896e2008-10-23 09:33:03 -04001415 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad50842008-05-12 21:20:48 +02001416 ftrace_free_records = NULL;
1417 return NULL;
1418 }
1419
Lai Jiangshanee000b72009-03-24 13:38:06 +08001420 ftrace_free_records = rec->freelist;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001421 memset(rec, 0, sizeof(*rec));
1422 return rec;
1423 }
1424
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001425 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001426 if (!ftrace_pages->next) {
1427 /* allocate another page */
1428 ftrace_pages->next =
1429 (void *)get_zeroed_page(GFP_KERNEL);
1430 if (!ftrace_pages->next)
1431 return NULL;
1432 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001433 ftrace_pages = ftrace_pages->next;
1434 }
1435
1436 return &ftrace_pages->records[ftrace_pages->index++];
1437}
1438
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001439static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001440ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001441{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001442 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001443
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001444 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001445 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001446
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001447 rec = ftrace_alloc_dyn_node(ip);
1448 if (!rec)
1449 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001450
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001451 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001452 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001453 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001454
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001455 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001456}
1457
Steven Rostedt05736a42008-09-22 14:55:47 -07001458static void print_ip_ins(const char *fmt, unsigned char *p)
1459{
1460 int i;
1461
1462 printk(KERN_CONT "%s", fmt);
1463
1464 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1465 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1466}
1467
Steven Rostedt31e88902008-11-14 16:21:19 -08001468static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001469{
1470 switch (failed) {
1471 case -EFAULT:
1472 FTRACE_WARN_ON_ONCE(1);
1473 pr_info("ftrace faulted on modifying ");
1474 print_ip_sym(ip);
1475 break;
1476 case -EINVAL:
1477 FTRACE_WARN_ON_ONCE(1);
1478 pr_info("ftrace failed to modify ");
1479 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001480 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001481 printk(KERN_CONT "\n");
1482 break;
1483 case -EPERM:
1484 FTRACE_WARN_ON_ONCE(1);
1485 pr_info("ftrace faulted on writing ");
1486 print_ip_sym(ip);
1487 break;
1488 default:
1489 FTRACE_WARN_ON_ONCE(1);
1490 pr_info("ftrace faulted on unknown error ");
1491 print_ip_sym(ip);
1492 }
1493}
1494
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001495
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001496/* Return 1 if the address range is reserved for ftrace */
1497int ftrace_text_reserved(void *start, void *end)
1498{
1499 struct dyn_ftrace *rec;
1500 struct ftrace_page *pg;
1501
1502 do_for_each_ftrace_rec(pg, rec) {
1503 if (rec->ip <= (unsigned long)end &&
1504 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1505 return 1;
1506 } while_for_each_ftrace_rec();
1507 return 0;
1508}
1509
1510
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301511static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001512__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001513{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001514 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001515 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001516
Shaohua Lif0001202009-01-09 11:29:42 +08001517 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001518
Steven Rostedt982c3502008-11-15 16:31:41 -05001519 /*
Steven Rostedted926f92011-05-03 13:25:24 -04001520 * If we are enabling tracing:
Steven Rostedt982c3502008-11-15 16:31:41 -05001521 *
Steven Rostedted926f92011-05-03 13:25:24 -04001522 * If the record has a ref count, then we need to enable it
1523 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001524 *
Steven Rostedted926f92011-05-03 13:25:24 -04001525 * Otherwise we make sure its disabled.
1526 *
1527 * If we are disabling tracing, then disable all records that
1528 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001529 */
Steven Rostedted926f92011-05-03 13:25:24 -04001530 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1531 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001532
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001533 /* If the state of this record hasn't changed, then do nothing */
1534 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1535 return 0;
1536
1537 if (flag) {
1538 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001539 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001540 }
1541
1542 rec->flags &= ~FTRACE_FL_ENABLED;
1543 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001544}
1545
1546static void ftrace_replace_code(int enable)
1547{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001548 struct dyn_ftrace *rec;
1549 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001550 int failed;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001551
Steven Rostedt45a4a232011-04-21 23:16:46 -04001552 if (unlikely(ftrace_disabled))
1553 return;
1554
Steven Rostedt265c8312009-02-13 12:43:56 -05001555 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001556 /* Skip over free records */
1557 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05001558 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001559
Steven Rostedt265c8312009-02-13 12:43:56 -05001560 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001561 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001562 ftrace_bug(failed, rec->ip);
1563 /* Stop processing */
1564 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001565 }
1566 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001567}
1568
Ingo Molnare309b412008-05-12 21:20:51 +02001569static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001570ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001571{
1572 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001573 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001574
1575 ip = rec->ip;
1576
Steven Rostedt45a4a232011-04-21 23:16:46 -04001577 if (unlikely(ftrace_disabled))
1578 return 0;
1579
Shaohua Li25aac9d2009-01-09 11:29:40 +08001580 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001581 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001582 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301583 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001584 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301585 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001586}
1587
Steven Rostedt000ab692009-02-17 13:35:06 -05001588/*
1589 * archs can override this function if they must do something
1590 * before the modifying code is performed.
1591 */
1592int __weak ftrace_arch_code_modify_prepare(void)
1593{
1594 return 0;
1595}
1596
1597/*
1598 * archs can override this function if they must do something
1599 * after the modifying code is performed.
1600 */
1601int __weak ftrace_arch_code_modify_post_process(void)
1602{
1603 return 0;
1604}
1605
Ingo Molnare309b412008-05-12 21:20:51 +02001606static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001607{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001608 int *command = data;
1609
Steven Rostedta3583242008-11-11 15:01:42 -05001610 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001611 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001612 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001613 ftrace_replace_code(0);
1614
1615 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1616 ftrace_update_ftrace_func(ftrace_trace_function);
1617
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001618 if (*command & FTRACE_START_FUNC_RET)
1619 ftrace_enable_ftrace_graph_caller();
1620 else if (*command & FTRACE_STOP_FUNC_RET)
1621 ftrace_disable_ftrace_graph_caller();
1622
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001623 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001624}
1625
Ingo Molnare309b412008-05-12 21:20:51 +02001626static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001627{
Steven Rostedt000ab692009-02-17 13:35:06 -05001628 int ret;
1629
1630 ret = ftrace_arch_code_modify_prepare();
1631 FTRACE_WARN_ON(ret);
1632 if (ret)
1633 return;
1634
Rusty Russell784e2d72008-07-28 12:16:31 -05001635 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001636
1637 ret = ftrace_arch_code_modify_post_process();
1638 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001639}
1640
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001641static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001642static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001643static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001644
1645static void ftrace_startup_enable(int command)
1646{
1647 if (saved_ftrace_func != ftrace_trace_function) {
1648 saved_ftrace_func = ftrace_trace_function;
1649 command |= FTRACE_UPDATE_TRACE_FUNC;
1650 }
1651
1652 if (!command || !ftrace_enabled)
1653 return;
1654
1655 ftrace_run_update_code(command);
1656}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001657
Steven Rostedta1cd6172011-05-23 15:24:25 -04001658static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001659{
Steven Rostedtb8489142011-05-04 09:27:52 -04001660 bool hash_enable = true;
1661
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001662 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001663 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001664
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001665 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001666 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001667
Steven Rostedtb8489142011-05-04 09:27:52 -04001668 /* ops marked global share the filter hashes */
1669 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1670 ops = &global_ops;
1671 /* Don't update hash if global is already set */
1672 if (global_start_up)
1673 hash_enable = false;
1674 global_start_up++;
1675 }
1676
Steven Rostedted926f92011-05-03 13:25:24 -04001677 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001678 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001679 ftrace_hash_rec_enable(ops, 1);
1680
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001681 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001682
1683 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001684}
1685
Steven Rostedtbd69c302011-05-03 21:55:54 -04001686static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001687{
Steven Rostedtb8489142011-05-04 09:27:52 -04001688 bool hash_disable = true;
1689
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001690 if (unlikely(ftrace_disabled))
1691 return;
1692
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001693 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001694 /*
1695 * Just warn in case of unbalance, no need to kill ftrace, it's not
1696 * critical but the ftrace_call callers may be never nopped again after
1697 * further ftrace uses.
1698 */
1699 WARN_ON_ONCE(ftrace_start_up < 0);
1700
Steven Rostedtb8489142011-05-04 09:27:52 -04001701 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1702 ops = &global_ops;
1703 global_start_up--;
1704 WARN_ON_ONCE(global_start_up < 0);
1705 /* Don't update hash if global still has users */
1706 if (global_start_up) {
1707 WARN_ON_ONCE(!ftrace_start_up);
1708 hash_disable = false;
1709 }
1710 }
1711
1712 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04001713 ftrace_hash_rec_disable(ops, 1);
1714
Steven Rostedtb8489142011-05-04 09:27:52 -04001715 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04001716 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001717
1718 if (!ftrace_start_up)
1719 command |= FTRACE_DISABLE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001720
1721 if (saved_ftrace_func != ftrace_trace_function) {
1722 saved_ftrace_func = ftrace_trace_function;
1723 command |= FTRACE_UPDATE_TRACE_FUNC;
1724 }
1725
1726 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001727 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001728
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001729 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001730}
1731
Ingo Molnare309b412008-05-12 21:20:51 +02001732static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001733{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001734 if (unlikely(ftrace_disabled))
1735 return;
1736
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001737 /* Force update next time */
1738 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001739 /* ftrace_start_up is true if we want ftrace running */
1740 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001741 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001742}
1743
Ingo Molnare309b412008-05-12 21:20:51 +02001744static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001745{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001746 if (unlikely(ftrace_disabled))
1747 return;
1748
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001749 /* ftrace_start_up is true if ftrace is running */
1750 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001751 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001752}
1753
Steven Rostedt3d083392008-05-12 21:20:42 +02001754static cycle_t ftrace_update_time;
1755static unsigned long ftrace_update_cnt;
1756unsigned long ftrace_update_tot_cnt;
1757
Steven Rostedt31e88902008-11-14 16:21:19 -08001758static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001759{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001760 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301761 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001762
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001763 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001764 ftrace_update_cnt = 0;
1765
Lai Jiangshane94142a2009-03-13 17:51:27 +08001766 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301767
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001768 /* If something went wrong, bail without enabling anything */
1769 if (unlikely(ftrace_disabled))
1770 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001771
Lai Jiangshane94142a2009-03-13 17:51:27 +08001772 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001773 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001774 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301775
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001776 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001777 * Do the initial record conversion from mcount jump
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001778 * to the NOP instructions.
1779 */
1780 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001781 ftrace_free_rec(p);
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001782 /* Game over */
1783 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001784 }
1785
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001786 ftrace_update_cnt++;
1787
1788 /*
1789 * If the tracing is enabled, go ahead and enable the record.
1790 *
1791 * The reason not to enable the record immediatelly is the
1792 * inherent check of ftrace_make_nop/ftrace_make_call for
1793 * correct previous instructions. Making first the NOP
1794 * conversion puts the module to the correct state, thus
1795 * passing the ftrace_make_call check.
1796 */
1797 if (ftrace_start_up) {
1798 int failed = __ftrace_replace_code(p, 1);
1799 if (failed) {
1800 ftrace_bug(failed, p->ip);
1801 ftrace_free_rec(p);
1802 }
1803 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001804 }
1805
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001806 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001807 ftrace_update_time = stop - start;
1808 ftrace_update_tot_cnt += ftrace_update_cnt;
1809
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001810 return 0;
1811}
1812
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001813static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001814{
1815 struct ftrace_page *pg;
1816 int cnt;
1817 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001818
1819 /* allocate a few pages */
1820 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1821 if (!ftrace_pages_start)
1822 return -1;
1823
1824 /*
1825 * Allocate a few more pages.
1826 *
1827 * TODO: have some parser search vmlinux before
1828 * final linking to find all calls to ftrace.
1829 * Then we can:
1830 * a) know how many pages to allocate.
1831 * and/or
1832 * b) set up the table then.
1833 *
1834 * The dynamic code is still necessary for
1835 * modules.
1836 */
1837
1838 pg = ftrace_pages = ftrace_pages_start;
1839
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001840 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001841 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001842 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001843
1844 for (i = 0; i < cnt; i++) {
1845 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1846
1847 /* If we fail, we'll try later anyway */
1848 if (!pg->next)
1849 break;
1850
1851 pg = pg->next;
1852 }
1853
1854 return 0;
1855}
1856
Steven Rostedt5072c592008-05-12 21:20:43 +02001857enum {
1858 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001859 FTRACE_ITER_NOTRACE = (1 << 1),
Steven Rostedt3499e462011-04-21 22:59:12 -04001860 FTRACE_ITER_PRINTALL = (1 << 2),
1861 FTRACE_ITER_HASH = (1 << 3),
Steven Rostedt647bcd02011-05-03 14:39:21 -04001862 FTRACE_ITER_ENABLED = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02001863};
1864
1865#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1866
1867struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001868 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001869 loff_t func_pos;
1870 struct ftrace_page *pg;
1871 struct dyn_ftrace *func;
1872 struct ftrace_func_probe *probe;
1873 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001874 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001875 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001876 int hidx;
1877 int idx;
1878 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02001879};
1880
Ingo Molnare309b412008-05-12 21:20:51 +02001881static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001882t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001883{
1884 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001885 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001886 struct hlist_head *hhd;
1887
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001888 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001889 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001890
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001891 if (iter->probe)
1892 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001893 retry:
1894 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1895 return NULL;
1896
1897 hhd = &ftrace_func_hash[iter->hidx];
1898
1899 if (hlist_empty(hhd)) {
1900 iter->hidx++;
1901 hnd = NULL;
1902 goto retry;
1903 }
1904
1905 if (!hnd)
1906 hnd = hhd->first;
1907 else {
1908 hnd = hnd->next;
1909 if (!hnd) {
1910 iter->hidx++;
1911 goto retry;
1912 }
1913 }
1914
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001915 if (WARN_ON_ONCE(!hnd))
1916 return NULL;
1917
1918 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1919
1920 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001921}
1922
1923static void *t_hash_start(struct seq_file *m, loff_t *pos)
1924{
1925 struct ftrace_iterator *iter = m->private;
1926 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001927 loff_t l;
1928
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001929 if (iter->func_pos > *pos)
1930 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001931
Li Zefand82d6242009-06-24 09:54:54 +08001932 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001933 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001934 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08001935 if (!p)
1936 break;
1937 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001938 if (!p)
1939 return NULL;
1940
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001941 /* Only set this if we have an item */
1942 iter->flags |= FTRACE_ITER_HASH;
1943
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001944 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001945}
1946
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001947static int
1948t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001949{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001950 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001951
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001952 rec = iter->probe;
1953 if (WARN_ON_ONCE(!rec))
1954 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001955
Steven Rostedt809dcf22009-02-16 23:06:01 -05001956 if (rec->ops->print)
1957 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1958
Steven Rostedtb375a112009-09-17 00:05:58 -04001959 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001960
1961 if (rec->data)
1962 seq_printf(m, ":%p", rec->data);
1963 seq_putc(m, '\n');
1964
1965 return 0;
1966}
1967
1968static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001969t_next(struct seq_file *m, void *v, loff_t *pos)
1970{
1971 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04001972 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02001973 struct dyn_ftrace *rec = NULL;
1974
Steven Rostedt45a4a232011-04-21 23:16:46 -04001975 if (unlikely(ftrace_disabled))
1976 return NULL;
1977
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001978 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001979 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001980
Steven Rostedt5072c592008-05-12 21:20:43 +02001981 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01001982 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02001983
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001984 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001985 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001986
Steven Rostedt5072c592008-05-12 21:20:43 +02001987 retry:
1988 if (iter->idx >= iter->pg->index) {
1989 if (iter->pg->next) {
1990 iter->pg = iter->pg->next;
1991 iter->idx = 0;
1992 goto retry;
1993 }
1994 } else {
1995 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001996 if ((rec->flags & FTRACE_FL_FREE) ||
1997
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001998 ((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04001999 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05002000
Steven Rostedt41c52c02008-05-22 11:46:33 -04002001 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002002 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2003
2004 ((iter->flags & FTRACE_ITER_ENABLED) &&
2005 !(rec->flags & ~FTRACE_FL_MASK))) {
2006
Steven Rostedt5072c592008-05-12 21:20:43 +02002007 rec = NULL;
2008 goto retry;
2009 }
2010 }
2011
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002012 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002013 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002014
2015 iter->func = rec;
2016
2017 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002018}
2019
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002020static void reset_iter_read(struct ftrace_iterator *iter)
2021{
2022 iter->pos = 0;
2023 iter->func_pos = 0;
2024 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002025}
2026
2027static void *t_start(struct seq_file *m, loff_t *pos)
2028{
2029 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002030 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002031 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002032 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002033
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002034 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002035
2036 if (unlikely(ftrace_disabled))
2037 return NULL;
2038
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002039 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002040 * If an lseek was done, then reset and start from beginning.
2041 */
2042 if (*pos < iter->pos)
2043 reset_iter_read(iter);
2044
2045 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002046 * For set_ftrace_filter reading, if we have the filter
2047 * off, we can short cut and just print out that all
2048 * functions are enabled.
2049 */
Steven Rostedtf45948e2011-05-02 12:29:25 -04002050 if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002051 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002052 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002053 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002054 /* reset in case of seek/pread */
2055 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002056 return iter;
2057 }
2058
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002059 if (iter->flags & FTRACE_ITER_HASH)
2060 return t_hash_start(m, pos);
2061
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002062 /*
2063 * Unfortunately, we need to restart at ftrace_pages_start
2064 * every time we let go of the ftrace_mutex. This is because
2065 * those pointers can change without the lock.
2066 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002067 iter->pg = ftrace_pages_start;
2068 iter->idx = 0;
2069 for (l = 0; l <= *pos; ) {
2070 p = t_next(m, p, &l);
2071 if (!p)
2072 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002073 }
walimis5821e1b2008-11-15 15:19:06 +08002074
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002075 if (!p) {
2076 if (iter->flags & FTRACE_ITER_FILTER)
2077 return t_hash_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002078
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002079 return NULL;
2080 }
2081
2082 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002083}
2084
2085static void t_stop(struct seq_file *m, void *p)
2086{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002087 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002088}
2089
2090static int t_show(struct seq_file *m, void *v)
2091{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002092 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002093 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002094
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002095 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002096 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002097
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002098 if (iter->flags & FTRACE_ITER_PRINTALL) {
2099 seq_printf(m, "#### all functions enabled ####\n");
2100 return 0;
2101 }
2102
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002103 rec = iter->func;
2104
Steven Rostedt5072c592008-05-12 21:20:43 +02002105 if (!rec)
2106 return 0;
2107
Steven Rostedt647bcd02011-05-03 14:39:21 -04002108 seq_printf(m, "%ps", (void *)rec->ip);
2109 if (iter->flags & FTRACE_ITER_ENABLED)
2110 seq_printf(m, " (%ld)",
2111 rec->flags & ~FTRACE_FL_MASK);
2112 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002113
2114 return 0;
2115}
2116
James Morris88e9d342009-09-22 16:43:43 -07002117static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002118 .start = t_start,
2119 .next = t_next,
2120 .stop = t_stop,
2121 .show = t_show,
2122};
2123
Ingo Molnare309b412008-05-12 21:20:51 +02002124static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002125ftrace_avail_open(struct inode *inode, struct file *file)
2126{
2127 struct ftrace_iterator *iter;
2128 int ret;
2129
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002130 if (unlikely(ftrace_disabled))
2131 return -ENODEV;
2132
Steven Rostedt5072c592008-05-12 21:20:43 +02002133 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2134 if (!iter)
2135 return -ENOMEM;
2136
2137 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002138
2139 ret = seq_open(file, &show_ftrace_seq_ops);
2140 if (!ret) {
2141 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002142
Steven Rostedt5072c592008-05-12 21:20:43 +02002143 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002144 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002145 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002146 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002147
2148 return ret;
2149}
2150
Steven Rostedt647bcd02011-05-03 14:39:21 -04002151static int
2152ftrace_enabled_open(struct inode *inode, struct file *file)
2153{
2154 struct ftrace_iterator *iter;
2155 int ret;
2156
2157 if (unlikely(ftrace_disabled))
2158 return -ENODEV;
2159
2160 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2161 if (!iter)
2162 return -ENOMEM;
2163
2164 iter->pg = ftrace_pages_start;
2165 iter->flags = FTRACE_ITER_ENABLED;
2166
2167 ret = seq_open(file, &show_ftrace_seq_ops);
2168 if (!ret) {
2169 struct seq_file *m = file->private_data;
2170
2171 m->private = iter;
2172 } else {
2173 kfree(iter);
2174 }
2175
2176 return ret;
2177}
2178
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002179static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002180{
Steven Rostedt52baf112009-02-14 01:15:39 -05002181 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002182 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002183 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002184}
2185
Ingo Molnare309b412008-05-12 21:20:51 +02002186static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002187ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002188 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002189{
2190 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002191 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002192 int ret = 0;
2193
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002194 if (unlikely(ftrace_disabled))
2195 return -ENODEV;
2196
Steven Rostedt5072c592008-05-12 21:20:43 +02002197 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2198 if (!iter)
2199 return -ENOMEM;
2200
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002201 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2202 kfree(iter);
2203 return -ENOMEM;
2204 }
2205
Steven Rostedtf45948e2011-05-02 12:29:25 -04002206 if (flag & FTRACE_ITER_NOTRACE)
2207 hash = ops->notrace_hash;
2208 else
2209 hash = ops->filter_hash;
2210
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002211 iter->ops = ops;
2212 iter->flags = flag;
2213
2214 if (file->f_mode & FMODE_WRITE) {
2215 mutex_lock(&ftrace_lock);
2216 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2217 mutex_unlock(&ftrace_lock);
2218
2219 if (!iter->hash) {
2220 trace_parser_put(&iter->parser);
2221 kfree(iter);
2222 return -ENOMEM;
2223 }
2224 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002225
Steven Rostedt41c52c02008-05-22 11:46:33 -04002226 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002227
Steven Rostedt5072c592008-05-12 21:20:43 +02002228 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002229 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002230 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002231
2232 if (file->f_mode & FMODE_READ) {
2233 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002234
2235 ret = seq_open(file, &show_ftrace_seq_ops);
2236 if (!ret) {
2237 struct seq_file *m = file->private_data;
2238 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002239 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002240 /* Failed */
2241 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002242 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002243 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002244 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002245 } else
2246 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002247 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002248
2249 return ret;
2250}
2251
Steven Rostedt41c52c02008-05-22 11:46:33 -04002252static int
2253ftrace_filter_open(struct inode *inode, struct file *file)
2254{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002255 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002256 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002257}
2258
2259static int
2260ftrace_notrace_open(struct inode *inode, struct file *file)
2261{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002262 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002263 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002264}
2265
Ingo Molnare309b412008-05-12 21:20:51 +02002266static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002267ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02002268{
2269 loff_t ret;
2270
2271 if (file->f_mode & FMODE_READ)
2272 ret = seq_lseek(file, offset, origin);
2273 else
2274 file->f_pos = ret = 1;
2275
2276 return ret;
2277}
2278
Steven Rostedt64e7c442009-02-13 17:08:48 -05002279static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002280{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002281 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002282 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002283
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002284 switch (type) {
2285 case MATCH_FULL:
2286 if (strcmp(str, regex) == 0)
2287 matched = 1;
2288 break;
2289 case MATCH_FRONT_ONLY:
2290 if (strncmp(str, regex, len) == 0)
2291 matched = 1;
2292 break;
2293 case MATCH_MIDDLE_ONLY:
2294 if (strstr(str, regex))
2295 matched = 1;
2296 break;
2297 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002298 slen = strlen(str);
2299 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002300 matched = 1;
2301 break;
2302 }
2303
2304 return matched;
2305}
2306
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002307static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002308enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002309{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002310 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002311 int ret = 0;
2312
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002313 entry = ftrace_lookup_ip(hash, rec->ip);
2314 if (not) {
2315 /* Do nothing if it doesn't exist */
2316 if (!entry)
2317 return 0;
2318
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002319 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002320 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002321 /* Do nothing if it exists */
2322 if (entry)
2323 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002324
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002325 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002326 }
2327 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002328}
2329
Steven Rostedt64e7c442009-02-13 17:08:48 -05002330static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002331ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2332 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002333{
2334 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002335 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002336
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002337 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2338
2339 if (mod) {
2340 /* module lookup requires matching the module */
2341 if (!modname || strcmp(modname, mod))
2342 return 0;
2343
2344 /* blank search means to match all funcs in the mod */
2345 if (!len)
2346 return 1;
2347 }
2348
Steven Rostedt64e7c442009-02-13 17:08:48 -05002349 return ftrace_match(str, regex, len, type);
2350}
2351
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002352static int
2353match_records(struct ftrace_hash *hash, char *buff,
2354 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002355{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002356 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002357 struct ftrace_page *pg;
2358 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002359 int type = MATCH_FULL;
2360 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002361 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002362 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002363
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002364 if (len) {
2365 type = filter_parse_regex(buff, len, &search, &not);
2366 search_len = strlen(search);
2367 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002368
Steven Rostedt52baf112009-02-14 01:15:39 -05002369 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002370
2371 if (unlikely(ftrace_disabled))
2372 goto out_unlock;
2373
Steven Rostedt265c8312009-02-13 12:43:56 -05002374 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02002375
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002376 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002377 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002378 if (ret < 0) {
2379 found = ret;
2380 goto out_unlock;
2381 }
Li Zefan311d16d2009-12-08 11:15:11 +08002382 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002383 }
2384 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002385 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002386 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002387
2388 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002389}
2390
Steven Rostedt64e7c442009-02-13 17:08:48 -05002391static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002392ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002393{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002394 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002395}
2396
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002397static int
2398ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002399{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002400 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002401
Steven Rostedt64e7c442009-02-13 17:08:48 -05002402 /* blank or '*' mean the same */
2403 if (strcmp(buff, "*") == 0)
2404 buff[0] = 0;
2405
2406 /* handle the case of 'dont filter this module' */
2407 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2408 buff[0] = 0;
2409 not = 1;
2410 }
2411
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002412 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002413}
2414
Steven Rostedtf6180772009-02-14 00:40:25 -05002415/*
2416 * We register the module command as a template to show others how
2417 * to register the a command as well.
2418 */
2419
2420static int
2421ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
2422{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002423 struct ftrace_ops *ops = &global_ops;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002424 struct ftrace_hash *hash;
Steven Rostedtf6180772009-02-14 00:40:25 -05002425 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002426 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002427
2428 /*
2429 * cmd == 'mod' because we only registered this func
2430 * for the 'mod' ftrace_func_command.
2431 * But if you register one func with multiple commands,
2432 * you can tell which command was used by the cmd
2433 * parameter.
2434 */
2435
2436 /* we must have a module name */
2437 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002438 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002439
2440 mod = strsep(&param, ":");
2441 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002442 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002443
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002444 if (enable)
Steven Rostedtf45948e2011-05-02 12:29:25 -04002445 hash = ops->filter_hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002446 else
Steven Rostedtf45948e2011-05-02 12:29:25 -04002447 hash = ops->notrace_hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002448
2449 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002450 if (!ret)
2451 ret = -EINVAL;
2452 if (ret < 0)
2453 return ret;
2454
2455 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002456}
2457
2458static struct ftrace_func_command ftrace_mod_cmd = {
2459 .name = "mod",
2460 .func = ftrace_mod_callback,
2461};
2462
2463static int __init ftrace_mod_cmd_init(void)
2464{
2465 return register_ftrace_command(&ftrace_mod_cmd);
2466}
2467device_initcall(ftrace_mod_cmd_init);
2468
Steven Rostedt59df055f2009-02-14 15:29:06 -05002469static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002470function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002471{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002472 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002473 struct hlist_head *hhd;
2474 struct hlist_node *n;
2475 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002476
2477 key = hash_long(ip, FTRACE_HASH_BITS);
2478
2479 hhd = &ftrace_func_hash[key];
2480
2481 if (hlist_empty(hhd))
2482 return;
2483
2484 /*
2485 * Disable preemption for these calls to prevent a RCU grace
2486 * period. This syncs the hash iteration and freeing of items
2487 * on the hash. rcu_read_lock is too dangerous here.
2488 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002489 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002490 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2491 if (entry->ip == ip)
2492 entry->ops->func(ip, parent_ip, &entry->data);
2493 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002494 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002495}
2496
Steven Rostedtb6887d72009-02-17 12:32:04 -05002497static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002498{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002499 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002500};
2501
Steven Rostedtb6887d72009-02-17 12:32:04 -05002502static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002503
Steven Rostedtb6887d72009-02-17 12:32:04 -05002504static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002505{
Steven Rostedtb8489142011-05-04 09:27:52 -04002506 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002507 int i;
2508
Steven Rostedtb6887d72009-02-17 12:32:04 -05002509 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002510 return;
2511
2512 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2513 struct hlist_head *hhd = &ftrace_func_hash[i];
2514 if (hhd->first)
2515 break;
2516 }
2517 /* Nothing registered? */
2518 if (i == FTRACE_FUNC_HASHSIZE)
2519 return;
2520
Steven Rostedtb8489142011-05-04 09:27:52 -04002521 ret = __register_ftrace_function(&trace_probe_ops);
2522 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002523 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002524
Steven Rostedtb6887d72009-02-17 12:32:04 -05002525 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002526}
2527
Steven Rostedtb6887d72009-02-17 12:32:04 -05002528static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002529{
Steven Rostedtb8489142011-05-04 09:27:52 -04002530 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002531 int i;
2532
Steven Rostedtb6887d72009-02-17 12:32:04 -05002533 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002534 return;
2535
2536 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2537 struct hlist_head *hhd = &ftrace_func_hash[i];
2538 if (hhd->first)
2539 return;
2540 }
2541
2542 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002543 ret = __unregister_ftrace_function(&trace_probe_ops);
2544 if (!ret)
2545 ftrace_shutdown(&trace_probe_ops, 0);
2546
Steven Rostedtb6887d72009-02-17 12:32:04 -05002547 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002548}
2549
2550
2551static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2552{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002553 struct ftrace_func_probe *entry =
2554 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002555
2556 if (entry->ops->free)
2557 entry->ops->free(&entry->data);
2558 kfree(entry);
2559}
2560
2561
2562int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002563register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002564 void *data)
2565{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002566 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002567 struct ftrace_page *pg;
2568 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002569 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002570 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002571 int count = 0;
2572 char *search;
2573
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002574 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002575 len = strlen(search);
2576
Steven Rostedtb6887d72009-02-17 12:32:04 -05002577 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002578 if (WARN_ON(not))
2579 return -EINVAL;
2580
2581 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002582
Steven Rostedt45a4a232011-04-21 23:16:46 -04002583 if (unlikely(ftrace_disabled))
2584 goto out_unlock;
2585
Steven Rostedt59df055f2009-02-14 15:29:06 -05002586 do_for_each_ftrace_rec(pg, rec) {
2587
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002588 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002589 continue;
2590
2591 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2592 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002593 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002594 if (!count)
2595 count = -ENOMEM;
2596 goto out_unlock;
2597 }
2598
2599 count++;
2600
2601 entry->data = data;
2602
2603 /*
2604 * The caller might want to do something special
2605 * for each function we find. We call the callback
2606 * to give the caller an opportunity to do so.
2607 */
2608 if (ops->callback) {
2609 if (ops->callback(rec->ip, &entry->data) < 0) {
2610 /* caller does not like this func */
2611 kfree(entry);
2612 continue;
2613 }
2614 }
2615
2616 entry->ops = ops;
2617 entry->ip = rec->ip;
2618
2619 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2620 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2621
2622 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002623 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002624
2625 out_unlock:
2626 mutex_unlock(&ftrace_lock);
2627
2628 return count;
2629}
2630
2631enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002632 PROBE_TEST_FUNC = 1,
2633 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002634};
2635
2636static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002637__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002638 void *data, int flags)
2639{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002640 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002641 struct hlist_node *n, *tmp;
2642 char str[KSYM_SYMBOL_LEN];
2643 int type = MATCH_FULL;
2644 int i, len = 0;
2645 char *search;
2646
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002647 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002648 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002649 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002650 int not;
2651
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002652 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002653 len = strlen(search);
2654
Steven Rostedtb6887d72009-02-17 12:32:04 -05002655 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002656 if (WARN_ON(not))
2657 return;
2658 }
2659
2660 mutex_lock(&ftrace_lock);
2661 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2662 struct hlist_head *hhd = &ftrace_func_hash[i];
2663
2664 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2665
2666 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002667 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002668 continue;
2669
Steven Rostedtb6887d72009-02-17 12:32:04 -05002670 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002671 continue;
2672
2673 /* do this last, since it is the most expensive */
2674 if (glob) {
2675 kallsyms_lookup(entry->ip, NULL, NULL,
2676 NULL, str);
2677 if (!ftrace_match(str, glob, len, type))
2678 continue;
2679 }
2680
2681 hlist_del(&entry->node);
2682 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2683 }
2684 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002685 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002686 mutex_unlock(&ftrace_lock);
2687}
2688
2689void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002690unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002691 void *data)
2692{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002693 __unregister_ftrace_function_probe(glob, ops, data,
2694 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002695}
2696
2697void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002698unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002699{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002700 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002701}
2702
Steven Rostedtb6887d72009-02-17 12:32:04 -05002703void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002704{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002705 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002706}
2707
Steven Rostedtf6180772009-02-14 00:40:25 -05002708static LIST_HEAD(ftrace_commands);
2709static DEFINE_MUTEX(ftrace_cmd_mutex);
2710
2711int register_ftrace_command(struct ftrace_func_command *cmd)
2712{
2713 struct ftrace_func_command *p;
2714 int ret = 0;
2715
2716 mutex_lock(&ftrace_cmd_mutex);
2717 list_for_each_entry(p, &ftrace_commands, list) {
2718 if (strcmp(cmd->name, p->name) == 0) {
2719 ret = -EBUSY;
2720 goto out_unlock;
2721 }
2722 }
2723 list_add(&cmd->list, &ftrace_commands);
2724 out_unlock:
2725 mutex_unlock(&ftrace_cmd_mutex);
2726
2727 return ret;
2728}
2729
2730int unregister_ftrace_command(struct ftrace_func_command *cmd)
2731{
2732 struct ftrace_func_command *p, *n;
2733 int ret = -ENODEV;
2734
2735 mutex_lock(&ftrace_cmd_mutex);
2736 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2737 if (strcmp(cmd->name, p->name) == 0) {
2738 ret = 0;
2739 list_del_init(&p->list);
2740 goto out_unlock;
2741 }
2742 }
2743 out_unlock:
2744 mutex_unlock(&ftrace_cmd_mutex);
2745
2746 return ret;
2747}
2748
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002749static int ftrace_process_regex(struct ftrace_hash *hash,
2750 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002751{
Steven Rostedtf6180772009-02-14 00:40:25 -05002752 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002753 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08002754 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002755
2756 func = strsep(&next, ":");
2757
2758 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002759 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002760 if (!ret)
2761 ret = -EINVAL;
2762 if (ret < 0)
2763 return ret;
2764 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002765 }
2766
Steven Rostedtf6180772009-02-14 00:40:25 -05002767 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002768
2769 command = strsep(&next, ":");
2770
Steven Rostedtf6180772009-02-14 00:40:25 -05002771 mutex_lock(&ftrace_cmd_mutex);
2772 list_for_each_entry(p, &ftrace_commands, list) {
2773 if (strcmp(p->name, command) == 0) {
2774 ret = p->func(func, command, next, enable);
2775 goto out_unlock;
2776 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002777 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002778 out_unlock:
2779 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002780
Steven Rostedtf6180772009-02-14 00:40:25 -05002781 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002782}
2783
Ingo Molnare309b412008-05-12 21:20:51 +02002784static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002785ftrace_regex_write(struct file *file, const char __user *ubuf,
2786 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002787{
2788 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002789 struct trace_parser *parser;
2790 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002791
Li Zefan4ba79782009-09-22 13:52:20 +08002792 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002793 return 0;
2794
Steven Rostedt41c52c02008-05-22 11:46:33 -04002795 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002796
Steven Rostedt45a4a232011-04-21 23:16:46 -04002797 ret = -ENODEV;
2798 if (unlikely(ftrace_disabled))
2799 goto out_unlock;
2800
Steven Rostedt5072c592008-05-12 21:20:43 +02002801 if (file->f_mode & FMODE_READ) {
2802 struct seq_file *m = file->private_data;
2803 iter = m->private;
2804 } else
2805 iter = file->private_data;
2806
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002807 parser = &iter->parser;
2808 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002809
Li Zefan4ba79782009-09-22 13:52:20 +08002810 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002811 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002812 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002813 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002814 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002815 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002816 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002817 }
2818
Steven Rostedt5072c592008-05-12 21:20:43 +02002819 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002820out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002821 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002822
Steven Rostedt5072c592008-05-12 21:20:43 +02002823 return ret;
2824}
2825
Steven Rostedt41c52c02008-05-22 11:46:33 -04002826static ssize_t
2827ftrace_filter_write(struct file *file, const char __user *ubuf,
2828 size_t cnt, loff_t *ppos)
2829{
2830 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2831}
2832
2833static ssize_t
2834ftrace_notrace_write(struct file *file, const char __user *ubuf,
2835 size_t cnt, loff_t *ppos)
2836{
2837 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2838}
2839
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002840static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002841ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2842 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04002843{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002844 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002845 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002846 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002847
Steven Rostedt936e0742011-05-05 22:54:01 -04002848 /* All global ops uses the global ops filters */
2849 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
2850 ops = &global_ops;
2851
Steven Rostedt41c52c02008-05-22 11:46:33 -04002852 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002853 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002854
Steven Rostedtf45948e2011-05-02 12:29:25 -04002855 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002856 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002857 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002858 orig_hash = &ops->notrace_hash;
2859
2860 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
2861 if (!hash)
2862 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002863
Steven Rostedt41c52c02008-05-22 11:46:33 -04002864 mutex_lock(&ftrace_regex_lock);
2865 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002866 ftrace_filter_reset(hash);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002867 if (buf)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002868 ftrace_match_records(hash, buf, len);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002869
2870 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04002871 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04002872 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
2873 && ftrace_enabled)
2874 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2875
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002876 mutex_unlock(&ftrace_lock);
2877
Steven Rostedt41c52c02008-05-22 11:46:33 -04002878 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002879
2880 free_ftrace_hash(hash);
2881 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002882}
2883
Steven Rostedt77a2b372008-05-12 21:20:45 +02002884/**
2885 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04002886 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02002887 * @buf - the string that holds the function filter text.
2888 * @len - the length of the string.
2889 * @reset - non zero to reset all filters before applying this filter.
2890 *
2891 * Filters denote which functions should be enabled when tracing is enabled.
2892 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2893 */
Steven Rostedt936e0742011-05-05 22:54:01 -04002894void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
2895 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002896{
Steven Rostedt936e0742011-05-05 22:54:01 -04002897 ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002898}
Steven Rostedt936e0742011-05-05 22:54:01 -04002899EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002900
Steven Rostedt41c52c02008-05-22 11:46:33 -04002901/**
2902 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04002903 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04002904 * @buf - the string that holds the function notrace text.
2905 * @len - the length of the string.
2906 * @reset - non zero to reset all filters before applying this filter.
2907 *
2908 * Notrace Filters denote which functions should not be enabled when tracing
2909 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2910 * for tracing.
2911 */
Steven Rostedt936e0742011-05-05 22:54:01 -04002912void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
2913 int len, int reset)
2914{
2915 ftrace_set_regex(ops, buf, len, reset, 0);
2916}
2917EXPORT_SYMBOL_GPL(ftrace_set_notrace);
2918/**
2919 * ftrace_set_filter - set a function to filter on in ftrace
2920 * @ops - the ops to set the filter with
2921 * @buf - the string that holds the function filter text.
2922 * @len - the length of the string.
2923 * @reset - non zero to reset all filters before applying this filter.
2924 *
2925 * Filters denote which functions should be enabled when tracing is enabled.
2926 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2927 */
2928void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
2929{
2930 ftrace_set_regex(&global_ops, buf, len, reset, 1);
2931}
2932EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
2933
2934/**
2935 * ftrace_set_notrace - set a function to not trace in ftrace
2936 * @ops - the ops to set the notrace filter with
2937 * @buf - the string that holds the function notrace text.
2938 * @len - the length of the string.
2939 * @reset - non zero to reset all filters before applying this filter.
2940 *
2941 * Notrace Filters denote which functions should not be enabled when tracing
2942 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2943 * for tracing.
2944 */
2945void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04002946{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002947 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002948}
Steven Rostedt936e0742011-05-05 22:54:01 -04002949EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002950
Steven Rostedt2af15d62009-05-28 13:37:24 -04002951/*
2952 * command line interface to allow users to set filters on boot up.
2953 */
2954#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2955static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2956static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2957
2958static int __init set_ftrace_notrace(char *str)
2959{
2960 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2961 return 1;
2962}
2963__setup("ftrace_notrace=", set_ftrace_notrace);
2964
2965static int __init set_ftrace_filter(char *str)
2966{
2967 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2968 return 1;
2969}
2970__setup("ftrace_filter=", set_ftrace_filter);
2971
Stefan Assmann369bc182009-10-12 22:17:21 +02002972#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002973static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002974static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2975
Stefan Assmann369bc182009-10-12 22:17:21 +02002976static int __init set_graph_function(char *str)
2977{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002978 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002979 return 1;
2980}
2981__setup("ftrace_graph_filter=", set_graph_function);
2982
2983static void __init set_ftrace_early_graph(char *buf)
2984{
2985 int ret;
2986 char *func;
2987
2988 while (buf) {
2989 func = strsep(&buf, ",");
2990 /* we allow only one expression at a time */
2991 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2992 func);
2993 if (ret)
2994 printk(KERN_DEBUG "ftrace: function %s not "
2995 "traceable\n", func);
2996 }
2997}
2998#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2999
Steven Rostedtf45948e2011-05-02 12:29:25 -04003000static void __init
3001set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003002{
3003 char *func;
3004
3005 while (buf) {
3006 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003007 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003008 }
3009}
3010
3011static void __init set_ftrace_early_filters(void)
3012{
3013 if (ftrace_filter_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04003014 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003015 if (ftrace_notrace_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04003016 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003017#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3018 if (ftrace_graph_buf[0])
3019 set_ftrace_early_graph(ftrace_graph_buf);
3020#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003021}
3022
Ingo Molnare309b412008-05-12 21:20:51 +02003023static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003024ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003025{
3026 struct seq_file *m = (struct seq_file *)file->private_data;
3027 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003028 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003029 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003030 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003031 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003032
Steven Rostedt41c52c02008-05-22 11:46:33 -04003033 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003034 if (file->f_mode & FMODE_READ) {
3035 iter = m->private;
3036
3037 seq_release(inode, file);
3038 } else
3039 iter = file->private_data;
3040
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003041 parser = &iter->parser;
3042 if (trace_parser_loaded(parser)) {
3043 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003044 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003045 }
3046
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003047 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003048
Steven Rostedt058e2972011-04-29 22:35:33 -04003049 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003050 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3051
3052 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003053 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003054 else
3055 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003056
Steven Rostedt058e2972011-04-29 22:35:33 -04003057 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003058 ret = ftrace_hash_move(iter->ops, filter_hash,
3059 orig_hash, iter->hash);
3060 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3061 && ftrace_enabled)
3062 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
3063
Steven Rostedt058e2972011-04-29 22:35:33 -04003064 mutex_unlock(&ftrace_lock);
3065 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003066 free_ftrace_hash(iter->hash);
3067 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003068
Steven Rostedt41c52c02008-05-22 11:46:33 -04003069 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003070 return 0;
3071}
3072
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003073static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003074 .open = ftrace_avail_open,
3075 .read = seq_read,
3076 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003077 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003078};
3079
Steven Rostedt647bcd02011-05-03 14:39:21 -04003080static const struct file_operations ftrace_enabled_fops = {
3081 .open = ftrace_enabled_open,
3082 .read = seq_read,
3083 .llseek = seq_lseek,
3084 .release = seq_release_private,
3085};
3086
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003087static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003088 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003089 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003090 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003091 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003092 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003093};
3094
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003095static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003096 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003097 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003098 .write = ftrace_notrace_write,
3099 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003100 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003101};
3102
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003103#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3104
3105static DEFINE_MUTEX(graph_lock);
3106
3107int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003108int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003109unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3110
3111static void *
Li Zefan85951842009-06-24 09:54:00 +08003112__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003113{
Li Zefan85951842009-06-24 09:54:00 +08003114 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003115 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003116 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003117}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003118
Li Zefan85951842009-06-24 09:54:00 +08003119static void *
3120g_next(struct seq_file *m, void *v, loff_t *pos)
3121{
3122 (*pos)++;
3123 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003124}
3125
3126static void *g_start(struct seq_file *m, loff_t *pos)
3127{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003128 mutex_lock(&graph_lock);
3129
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003130 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003131 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003132 return (void *)1;
3133
Li Zefan85951842009-06-24 09:54:00 +08003134 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003135}
3136
3137static void g_stop(struct seq_file *m, void *p)
3138{
3139 mutex_unlock(&graph_lock);
3140}
3141
3142static int g_show(struct seq_file *m, void *v)
3143{
3144 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003145
3146 if (!ptr)
3147 return 0;
3148
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003149 if (ptr == (unsigned long *)1) {
3150 seq_printf(m, "#### all functions enabled ####\n");
3151 return 0;
3152 }
3153
Steven Rostedtb375a112009-09-17 00:05:58 -04003154 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003155
3156 return 0;
3157}
3158
James Morris88e9d342009-09-22 16:43:43 -07003159static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003160 .start = g_start,
3161 .next = g_next,
3162 .stop = g_stop,
3163 .show = g_show,
3164};
3165
3166static int
3167ftrace_graph_open(struct inode *inode, struct file *file)
3168{
3169 int ret = 0;
3170
3171 if (unlikely(ftrace_disabled))
3172 return -ENODEV;
3173
3174 mutex_lock(&graph_lock);
3175 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003176 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003177 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003178 ftrace_graph_count = 0;
3179 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3180 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003181 mutex_unlock(&graph_lock);
3182
Li Zefana4ec5e02009-09-18 14:06:28 +08003183 if (file->f_mode & FMODE_READ)
3184 ret = seq_open(file, &ftrace_graph_seq_ops);
3185
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003186 return ret;
3187}
3188
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003189static int
Li Zefan87827112009-07-23 11:29:11 +08003190ftrace_graph_release(struct inode *inode, struct file *file)
3191{
3192 if (file->f_mode & FMODE_READ)
3193 seq_release(inode, file);
3194 return 0;
3195}
3196
3197static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003198ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003199{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003200 struct dyn_ftrace *rec;
3201 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003202 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003203 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003204 int type, not;
3205 char *search;
3206 bool exists;
3207 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003208
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003209 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003210 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003211 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3212 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003213
3214 search_len = strlen(search);
3215
Steven Rostedt52baf112009-02-14 01:15:39 -05003216 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003217
3218 if (unlikely(ftrace_disabled)) {
3219 mutex_unlock(&ftrace_lock);
3220 return -ENODEV;
3221 }
3222
Steven Rostedt265c8312009-02-13 12:43:56 -05003223 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003224
Steven Rostedt45a4a232011-04-21 23:16:46 -04003225 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05003226 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003227
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003228 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003229 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003230 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003231 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003232 if (array[i] == rec->ip) {
3233 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003234 break;
3235 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003236 }
3237
3238 if (!not) {
3239 fail = 0;
3240 if (!exists) {
3241 array[(*idx)++] = rec->ip;
3242 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3243 goto out;
3244 }
3245 } else {
3246 if (exists) {
3247 array[i] = array[--(*idx)];
3248 array[*idx] = 0;
3249 fail = 0;
3250 }
3251 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003252 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003253 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003254out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003255 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003256
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003257 if (fail)
3258 return -EINVAL;
3259
3260 ftrace_graph_filter_enabled = 1;
3261 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003262}
3263
3264static ssize_t
3265ftrace_graph_write(struct file *file, const char __user *ubuf,
3266 size_t cnt, loff_t *ppos)
3267{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003268 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003269 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003270
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003271 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003272 return 0;
3273
3274 mutex_lock(&graph_lock);
3275
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003276 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3277 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003278 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003279 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003280
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003281 read = trace_get_user(&parser, ubuf, cnt, ppos);
3282
Li Zefan4ba79782009-09-22 13:52:20 +08003283 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003284 parser.buffer[parser.idx] = 0;
3285
3286 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003287 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003288 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003289 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003290 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003291 }
3292
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003293 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003294
3295out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003296 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003297out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003298 mutex_unlock(&graph_lock);
3299
3300 return ret;
3301}
3302
3303static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003304 .open = ftrace_graph_open,
3305 .read = seq_read,
3306 .write = ftrace_graph_write,
3307 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003308 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003309};
3310#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3311
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003312static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003313{
Steven Rostedt5072c592008-05-12 21:20:43 +02003314
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003315 trace_create_file("available_filter_functions", 0444,
3316 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003317
Steven Rostedt647bcd02011-05-03 14:39:21 -04003318 trace_create_file("enabled_functions", 0444,
3319 d_tracer, NULL, &ftrace_enabled_fops);
3320
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003321 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3322 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003323
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003324 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003325 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003326
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003327#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003328 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003329 NULL,
3330 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003331#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3332
Steven Rostedt5072c592008-05-12 21:20:43 +02003333 return 0;
3334}
3335
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003336static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003337 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003338 unsigned long *end)
3339{
3340 unsigned long *p;
3341 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003342 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003343
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003344 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003345 p = start;
3346 while (p < end) {
3347 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003348 /*
3349 * Some architecture linkers will pad between
3350 * the different mcount_loc sections of different
3351 * object files to satisfy alignments.
3352 * Skip any NULL pointers.
3353 */
3354 if (!addr)
3355 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003356 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003357 }
3358
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003359 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003360 * We only need to disable interrupts on start up
3361 * because we are modifying code that an interrupt
3362 * may execute, and the modification is not atomic.
3363 * But for modules, nothing runs the code we modify
3364 * until we are finished with it, and there's no
3365 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003366 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003367 if (!mod)
3368 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003369 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003370 if (!mod)
3371 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003372 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003373
3374 return 0;
3375}
3376
Steven Rostedt93eb6772009-04-15 13:24:06 -04003377#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003378void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003379{
3380 struct dyn_ftrace *rec;
3381 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003382
Steven Rostedt93eb6772009-04-15 13:24:06 -04003383 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003384
3385 if (ftrace_disabled)
3386 goto out_unlock;
3387
Steven Rostedt93eb6772009-04-15 13:24:06 -04003388 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003389 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003390 /*
3391 * rec->ip is changed in ftrace_free_rec()
3392 * It should not between s and e if record was freed.
3393 */
3394 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
3395 ftrace_free_rec(rec);
3396 }
3397 } while_for_each_ftrace_rec();
Steven Rostedt45a4a232011-04-21 23:16:46 -04003398 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003399 mutex_unlock(&ftrace_lock);
3400}
3401
3402static void ftrace_init_module(struct module *mod,
3403 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003404{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003405 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003406 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003407 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003408}
3409
Steven Rostedt93eb6772009-04-15 13:24:06 -04003410static int ftrace_module_notify(struct notifier_block *self,
3411 unsigned long val, void *data)
3412{
3413 struct module *mod = data;
3414
3415 switch (val) {
3416 case MODULE_STATE_COMING:
3417 ftrace_init_module(mod, mod->ftrace_callsites,
3418 mod->ftrace_callsites +
3419 mod->num_ftrace_callsites);
3420 break;
3421 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003422 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003423 break;
3424 }
3425
3426 return 0;
3427}
3428#else
3429static int ftrace_module_notify(struct notifier_block *self,
3430 unsigned long val, void *data)
3431{
3432 return 0;
3433}
3434#endif /* CONFIG_MODULES */
3435
3436struct notifier_block ftrace_module_nb = {
3437 .notifier_call = ftrace_module_notify,
3438 .priority = 0,
3439};
3440
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003441extern unsigned long __start_mcount_loc[];
3442extern unsigned long __stop_mcount_loc[];
3443
3444void __init ftrace_init(void)
3445{
3446 unsigned long count, addr, flags;
3447 int ret;
3448
3449 /* Keep the ftrace pointer to the stub */
3450 addr = (unsigned long)ftrace_stub;
3451
3452 local_irq_save(flags);
3453 ftrace_dyn_arch_init(&addr);
3454 local_irq_restore(flags);
3455
3456 /* ftrace_dyn_arch_init places the return code in addr */
3457 if (addr)
3458 goto failed;
3459
3460 count = __stop_mcount_loc - __start_mcount_loc;
3461
3462 ret = ftrace_dyn_table_alloc(count);
3463 if (ret)
3464 goto failed;
3465
3466 last_ftrace_enabled = ftrace_enabled = 1;
3467
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003468 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003469 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003470 __stop_mcount_loc);
3471
Steven Rostedt93eb6772009-04-15 13:24:06 -04003472 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003473 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003474 pr_warning("Failed to register trace ftrace module notifier\n");
3475
Steven Rostedt2af15d62009-05-28 13:37:24 -04003476 set_ftrace_early_filters();
3477
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003478 return;
3479 failed:
3480 ftrace_disabled = 1;
3481}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003482
Steven Rostedt3d083392008-05-12 21:20:42 +02003483#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003484
Steven Rostedt2b499382011-05-03 22:49:52 -04003485static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003486 .func = ftrace_stub,
3487};
3488
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003489static int __init ftrace_nodyn_init(void)
3490{
3491 ftrace_enabled = 1;
3492 return 0;
3493}
3494device_initcall(ftrace_nodyn_init);
3495
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003496static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3497static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003498/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003499# define ftrace_startup(ops, command) \
3500 ({ \
3501 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3502 0; \
3503 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04003504# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003505# define ftrace_startup_sysctl() do { } while (0)
3506# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003507
3508static inline int
3509ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3510{
3511 return 1;
3512}
3513
Steven Rostedt3d083392008-05-12 21:20:42 +02003514#endif /* CONFIG_DYNAMIC_FTRACE */
3515
Steven Rostedtb8489142011-05-04 09:27:52 -04003516static void
3517ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3518{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003519 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04003520
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003521 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3522 return;
3523
3524 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003525 /*
3526 * Some of the ops may be dynamically allocated,
3527 * they must be freed after a synchronize_sched().
3528 */
3529 preempt_disable_notrace();
3530 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04003531 while (op != &ftrace_list_end) {
3532 if (ftrace_ops_test(op, ip))
3533 op->func(ip, parent_ip);
3534 op = rcu_dereference_raw(op->next);
3535 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003536 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003537 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04003538}
3539
Steven Rostedte32d8952008-12-04 00:26:41 -05003540static void clear_ftrace_swapper(void)
3541{
3542 struct task_struct *p;
3543 int cpu;
3544
3545 get_online_cpus();
3546 for_each_online_cpu(cpu) {
3547 p = idle_task(cpu);
3548 clear_tsk_trace_trace(p);
3549 }
3550 put_online_cpus();
3551}
3552
3553static void set_ftrace_swapper(void)
3554{
3555 struct task_struct *p;
3556 int cpu;
3557
3558 get_online_cpus();
3559 for_each_online_cpu(cpu) {
3560 p = idle_task(cpu);
3561 set_tsk_trace_trace(p);
3562 }
3563 put_online_cpus();
3564}
3565
3566static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003567{
3568 struct task_struct *p;
3569
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003570 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05003571 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05003572 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05003573 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003574 rcu_read_unlock();
3575
Steven Rostedte32d8952008-12-04 00:26:41 -05003576 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003577}
3578
Steven Rostedte32d8952008-12-04 00:26:41 -05003579static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003580{
3581 struct task_struct *p;
3582
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003583 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003584 do_each_pid_task(pid, PIDTYPE_PID, p) {
3585 set_tsk_trace_trace(p);
3586 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003587 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003588}
3589
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003590static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003591{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003592 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003593 clear_ftrace_swapper();
3594 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003595 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05003596}
3597
3598static void set_ftrace_pid_task(struct pid *pid)
3599{
3600 if (pid == ftrace_swapper_pid)
3601 set_ftrace_swapper();
3602 else
3603 set_ftrace_pid(pid);
3604}
3605
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003606static int ftrace_pid_add(int p)
3607{
3608 struct pid *pid;
3609 struct ftrace_pid *fpid;
3610 int ret = -EINVAL;
3611
3612 mutex_lock(&ftrace_lock);
3613
3614 if (!p)
3615 pid = ftrace_swapper_pid;
3616 else
3617 pid = find_get_pid(p);
3618
3619 if (!pid)
3620 goto out;
3621
3622 ret = 0;
3623
3624 list_for_each_entry(fpid, &ftrace_pids, list)
3625 if (fpid->pid == pid)
3626 goto out_put;
3627
3628 ret = -ENOMEM;
3629
3630 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3631 if (!fpid)
3632 goto out_put;
3633
3634 list_add(&fpid->list, &ftrace_pids);
3635 fpid->pid = pid;
3636
3637 set_ftrace_pid_task(pid);
3638
3639 ftrace_update_pid_func();
3640 ftrace_startup_enable(0);
3641
3642 mutex_unlock(&ftrace_lock);
3643 return 0;
3644
3645out_put:
3646 if (pid != ftrace_swapper_pid)
3647 put_pid(pid);
3648
3649out:
3650 mutex_unlock(&ftrace_lock);
3651 return ret;
3652}
3653
3654static void ftrace_pid_reset(void)
3655{
3656 struct ftrace_pid *fpid, *safe;
3657
3658 mutex_lock(&ftrace_lock);
3659 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3660 struct pid *pid = fpid->pid;
3661
3662 clear_ftrace_pid_task(pid);
3663
3664 list_del(&fpid->list);
3665 kfree(fpid);
3666 }
3667
3668 ftrace_update_pid_func();
3669 ftrace_startup_enable(0);
3670
3671 mutex_unlock(&ftrace_lock);
3672}
3673
3674static void *fpid_start(struct seq_file *m, loff_t *pos)
3675{
3676 mutex_lock(&ftrace_lock);
3677
3678 if (list_empty(&ftrace_pids) && (!*pos))
3679 return (void *) 1;
3680
3681 return seq_list_start(&ftrace_pids, *pos);
3682}
3683
3684static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3685{
3686 if (v == (void *)1)
3687 return NULL;
3688
3689 return seq_list_next(v, &ftrace_pids, pos);
3690}
3691
3692static void fpid_stop(struct seq_file *m, void *p)
3693{
3694 mutex_unlock(&ftrace_lock);
3695}
3696
3697static int fpid_show(struct seq_file *m, void *v)
3698{
3699 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3700
3701 if (v == (void *)1) {
3702 seq_printf(m, "no pid\n");
3703 return 0;
3704 }
3705
3706 if (fpid->pid == ftrace_swapper_pid)
3707 seq_printf(m, "swapper tasks\n");
3708 else
3709 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3710
3711 return 0;
3712}
3713
3714static const struct seq_operations ftrace_pid_sops = {
3715 .start = fpid_start,
3716 .next = fpid_next,
3717 .stop = fpid_stop,
3718 .show = fpid_show,
3719};
3720
3721static int
3722ftrace_pid_open(struct inode *inode, struct file *file)
3723{
3724 int ret = 0;
3725
3726 if ((file->f_mode & FMODE_WRITE) &&
3727 (file->f_flags & O_TRUNC))
3728 ftrace_pid_reset();
3729
3730 if (file->f_mode & FMODE_READ)
3731 ret = seq_open(file, &ftrace_pid_sops);
3732
3733 return ret;
3734}
3735
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003736static ssize_t
3737ftrace_pid_write(struct file *filp, const char __user *ubuf,
3738 size_t cnt, loff_t *ppos)
3739{
Ingo Molnar457dc922009-11-23 11:03:28 +01003740 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003741 long val;
3742 int ret;
3743
3744 if (cnt >= sizeof(buf))
3745 return -EINVAL;
3746
3747 if (copy_from_user(&buf, ubuf, cnt))
3748 return -EFAULT;
3749
3750 buf[cnt] = 0;
3751
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003752 /*
3753 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3754 * to clean the filter quietly.
3755 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003756 tmp = strstrip(buf);
3757 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003758 return 1;
3759
Ingo Molnar457dc922009-11-23 11:03:28 +01003760 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003761 if (ret < 0)
3762 return ret;
3763
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003764 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003765
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003766 return ret ? ret : cnt;
3767}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003768
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003769static int
3770ftrace_pid_release(struct inode *inode, struct file *file)
3771{
3772 if (file->f_mode & FMODE_READ)
3773 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003774
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003775 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003776}
3777
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003778static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003779 .open = ftrace_pid_open,
3780 .write = ftrace_pid_write,
3781 .read = seq_read,
3782 .llseek = seq_lseek,
3783 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003784};
3785
3786static __init int ftrace_init_debugfs(void)
3787{
3788 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003789
3790 d_tracer = tracing_init_dentry();
3791 if (!d_tracer)
3792 return 0;
3793
3794 ftrace_init_dyn_debugfs(d_tracer);
3795
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003796 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3797 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003798
3799 ftrace_profile_debugfs(d_tracer);
3800
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003801 return 0;
3802}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003803fs_initcall(ftrace_init_debugfs);
3804
Steven Rostedt3d083392008-05-12 21:20:42 +02003805/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003806 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003807 *
3808 * This function should be used by panic code. It stops ftrace
3809 * but in a not so nice way. If you need to simply kill ftrace
3810 * from a non-atomic section, use ftrace_kill.
3811 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003812void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003813{
3814 ftrace_disabled = 1;
3815 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003816 clear_ftrace_function();
3817}
3818
3819/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003820 * register_ftrace_function - register a function for profiling
3821 * @ops - ops structure that holds the function for profiling.
3822 *
3823 * Register a function to be called by all functions in the
3824 * kernel.
3825 *
3826 * Note: @ops->func and all the functions it calls must be labeled
3827 * with "notrace", otherwise it will go into a
3828 * recursive loop.
3829 */
3830int register_ftrace_function(struct ftrace_ops *ops)
3831{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003832 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003833
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003834 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003835
Steven Rostedt45a4a232011-04-21 23:16:46 -04003836 if (unlikely(ftrace_disabled))
3837 goto out_unlock;
3838
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003839 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04003840 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04003841 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003842
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003843
Steven Rostedt45a4a232011-04-21 23:16:46 -04003844 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003845 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003846 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003847}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003848EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02003849
3850/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003851 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003852 * @ops - ops structure that holds the function to unregister
3853 *
3854 * Unregister a function that was added to be called by ftrace profiling.
3855 */
3856int unregister_ftrace_function(struct ftrace_ops *ops)
3857{
3858 int ret;
3859
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003860 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003861 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04003862 if (!ret)
3863 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003864 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003865
3866 return ret;
3867}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003868EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003869
Ingo Molnare309b412008-05-12 21:20:51 +02003870int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003871ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003872 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003873 loff_t *ppos)
3874{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003875 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003876
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003877 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003878
Steven Rostedt45a4a232011-04-21 23:16:46 -04003879 if (unlikely(ftrace_disabled))
3880 goto out;
3881
3882 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003883
Li Zefana32c7762009-06-26 16:55:51 +08003884 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003885 goto out;
3886
Li Zefana32c7762009-06-26 16:55:51 +08003887 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003888
3889 if (ftrace_enabled) {
3890
3891 ftrace_startup_sysctl();
3892
3893 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04003894 if (ftrace_ops_list != &ftrace_list_end) {
3895 if (ftrace_ops_list->next == &ftrace_list_end)
3896 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003897 else
Steven Rostedtb8489142011-05-04 09:27:52 -04003898 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003899 }
3900
3901 } else {
3902 /* stopping ftrace calls (just send to ftrace_stub) */
3903 ftrace_trace_function = ftrace_stub;
3904
3905 ftrace_shutdown_sysctl();
3906 }
3907
3908 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003909 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003910 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003911}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003912
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003913#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003914
Steven Rostedt597af812009-04-03 15:24:12 -04003915static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003916static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003917
Steven Rostedte49dc192008-12-02 23:50:05 -05003918int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3919{
3920 return 0;
3921}
3922
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003923/* The callbacks that hook a function */
3924trace_func_graph_ret_t ftrace_graph_return =
3925 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003926trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003927
3928/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3929static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3930{
3931 int i;
3932 int ret = 0;
3933 unsigned long flags;
3934 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3935 struct task_struct *g, *t;
3936
3937 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3938 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3939 * sizeof(struct ftrace_ret_stack),
3940 GFP_KERNEL);
3941 if (!ret_stack_list[i]) {
3942 start = 0;
3943 end = i;
3944 ret = -ENOMEM;
3945 goto free;
3946 }
3947 }
3948
3949 read_lock_irqsave(&tasklist_lock, flags);
3950 do_each_thread(g, t) {
3951 if (start == end) {
3952 ret = -EAGAIN;
3953 goto unlock;
3954 }
3955
3956 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003957 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003958 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003959 t->curr_ret_stack = -1;
3960 /* Make sure the tasks see the -1 first: */
3961 smp_wmb();
3962 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003963 }
3964 } while_each_thread(g, t);
3965
3966unlock:
3967 read_unlock_irqrestore(&tasklist_lock, flags);
3968free:
3969 for (i = start; i < end; i++)
3970 kfree(ret_stack_list[i]);
3971 return ret;
3972}
3973
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003974static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04003975ftrace_graph_probe_sched_switch(void *ignore,
3976 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003977{
3978 unsigned long long timestamp;
3979 int index;
3980
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003981 /*
3982 * Does the user want to count the time a function was asleep.
3983 * If so, do not update the time stamps.
3984 */
3985 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3986 return;
3987
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003988 timestamp = trace_clock_local();
3989
3990 prev->ftrace_timestamp = timestamp;
3991
3992 /* only process tasks that we timestamped */
3993 if (!next->ftrace_timestamp)
3994 return;
3995
3996 /*
3997 * Update all the counters in next to make up for the
3998 * time next was sleeping.
3999 */
4000 timestamp -= next->ftrace_timestamp;
4001
4002 for (index = next->curr_ret_stack; index >= 0; index--)
4003 next->ret_stack[index].calltime += timestamp;
4004}
4005
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004006/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004007static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004008{
4009 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004010 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004011
4012 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4013 sizeof(struct ftrace_ret_stack *),
4014 GFP_KERNEL);
4015
4016 if (!ret_stack_list)
4017 return -ENOMEM;
4018
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004019 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004020 for_each_online_cpu(cpu) {
4021 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004022 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004023 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004024
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004025 do {
4026 ret = alloc_retstack_tasklist(ret_stack_list);
4027 } while (ret == -EAGAIN);
4028
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004029 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004030 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004031 if (ret)
4032 pr_info("ftrace_graph: Couldn't activate tracepoint"
4033 " probe to kernel_sched_switch\n");
4034 }
4035
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004036 kfree(ret_stack_list);
4037 return ret;
4038}
4039
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004040/*
4041 * Hibernation protection.
4042 * The state of the current task is too much unstable during
4043 * suspend/restore to disk. We want to protect against that.
4044 */
4045static int
4046ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4047 void *unused)
4048{
4049 switch (state) {
4050 case PM_HIBERNATION_PREPARE:
4051 pause_graph_tracing();
4052 break;
4053
4054 case PM_POST_HIBERNATION:
4055 unpause_graph_tracing();
4056 break;
4057 }
4058 return NOTIFY_DONE;
4059}
4060
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004061int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4062 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004063{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004064 int ret = 0;
4065
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004066 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004067
Steven Rostedt05ce5812009-03-24 00:18:31 -04004068 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004069 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004070 ret = -EBUSY;
4071 goto out;
4072 }
4073
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004074 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4075 register_pm_notifier(&ftrace_suspend_notifier);
4076
Steven Rostedt597af812009-04-03 15:24:12 -04004077 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004078 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004079 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004080 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004081 goto out;
4082 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004083
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004084 ftrace_graph_return = retfunc;
4085 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004086
Steven Rostedta1cd6172011-05-23 15:24:25 -04004087 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004088
4089out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004090 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004091 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004092}
4093
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004094void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004095{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004096 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004097
Steven Rostedt597af812009-04-03 15:24:12 -04004098 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004099 goto out;
4100
Steven Rostedt597af812009-04-03 15:24:12 -04004101 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004102 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004103 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004104 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004105 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004106 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004107
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004108 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004109 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004110}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004111
Steven Rostedt868baf02011-02-10 21:26:13 -05004112static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4113
4114static void
4115graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4116{
4117 atomic_set(&t->tracing_graph_pause, 0);
4118 atomic_set(&t->trace_overrun, 0);
4119 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004120 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004121 smp_wmb();
4122 t->ret_stack = ret_stack;
4123}
4124
4125/*
4126 * Allocate a return stack for the idle task. May be the first
4127 * time through, or it may be done by CPU hotplug online.
4128 */
4129void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4130{
4131 t->curr_ret_stack = -1;
4132 /*
4133 * The idle task has no parent, it either has its own
4134 * stack or no stack at all.
4135 */
4136 if (t->ret_stack)
4137 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4138
4139 if (ftrace_graph_active) {
4140 struct ftrace_ret_stack *ret_stack;
4141
4142 ret_stack = per_cpu(idle_ret_stack, cpu);
4143 if (!ret_stack) {
4144 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4145 * sizeof(struct ftrace_ret_stack),
4146 GFP_KERNEL);
4147 if (!ret_stack)
4148 return;
4149 per_cpu(idle_ret_stack, cpu) = ret_stack;
4150 }
4151 graph_init_task(t, ret_stack);
4152 }
4153}
4154
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004155/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004156void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004157{
Steven Rostedt84047e32009-06-02 16:51:55 -04004158 /* Make sure we do not use the parent ret_stack */
4159 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004160 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004161
Steven Rostedt597af812009-04-03 15:24:12 -04004162 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004163 struct ftrace_ret_stack *ret_stack;
4164
4165 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004166 * sizeof(struct ftrace_ret_stack),
4167 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004168 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004169 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004170 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004171 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004172}
4173
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004174void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004175{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004176 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4177
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004178 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004179 /* NULL must become visible to IRQs before we free it: */
4180 barrier();
4181
4182 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004183}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004184
4185void ftrace_graph_stop(void)
4186{
4187 ftrace_stop();
4188}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004189#endif