blob: 458018a1ac9ab24c55046e335147e6e0ce5d50bc [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
1173static int
1174ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src)
1175{
1176 struct ftrace_func_entry *entry;
1177 struct hlist_node *tp, *tn;
1178 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001179 struct ftrace_hash *old_hash;
1180 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001181 unsigned long key;
1182 int size = src->count;
1183 int bits = 0;
1184 int i;
1185
1186 /*
1187 * If the new source is empty, just free dst and assign it
1188 * the empty_hash.
1189 */
1190 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001191 free_ftrace_hash_rcu(*dst);
1192 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001193 return 0;
1194 }
1195
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001196 /*
1197 * Make the hash size about 1/2 the # found
1198 */
1199 for (size /= 2; size; size >>= 1)
1200 bits++;
1201
1202 /* Don't allocate too much */
1203 if (bits > FTRACE_HASH_MAX_BITS)
1204 bits = FTRACE_HASH_MAX_BITS;
1205
Steven Rostedt07fd5512011-05-05 18:03:47 -04001206 new_hash = alloc_ftrace_hash(bits);
1207 if (!new_hash)
1208 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001209
1210 size = 1 << src->size_bits;
1211 for (i = 0; i < size; i++) {
1212 hhd = &src->buckets[i];
1213 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1214 if (bits > 0)
1215 key = hash_long(entry->ip, bits);
1216 else
1217 key = 0;
1218 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001219 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001220 }
1221 }
1222
Steven Rostedt07fd5512011-05-05 18:03:47 -04001223 old_hash = *dst;
1224 rcu_assign_pointer(*dst, new_hash);
1225 free_ftrace_hash_rcu(old_hash);
1226
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001227 return 0;
1228}
1229
Steven Rostedt265c8312009-02-13 12:43:56 -05001230/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001231 * Test the hashes for this ops to see if we want to call
1232 * the ops->func or not.
1233 *
1234 * It's a match if the ip is in the ops->filter_hash or
1235 * the filter_hash does not exist or is empty,
1236 * AND
1237 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001238 *
1239 * This needs to be called with preemption disabled as
1240 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001241 */
1242static int
1243ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1244{
1245 struct ftrace_hash *filter_hash;
1246 struct ftrace_hash *notrace_hash;
1247 int ret;
1248
Steven Rostedtb8489142011-05-04 09:27:52 -04001249 filter_hash = rcu_dereference_raw(ops->filter_hash);
1250 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1251
1252 if ((!filter_hash || !filter_hash->count ||
1253 ftrace_lookup_ip(filter_hash, ip)) &&
1254 (!notrace_hash || !notrace_hash->count ||
1255 !ftrace_lookup_ip(notrace_hash, ip)))
1256 ret = 1;
1257 else
1258 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001259
1260 return ret;
1261}
1262
1263/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001264 * This is a double for. Do not use 'break' to break out of the loop,
1265 * you must use a goto.
1266 */
1267#define do_for_each_ftrace_rec(pg, rec) \
1268 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1269 int _____i; \
1270 for (_____i = 0; _____i < pg->index; _____i++) { \
1271 rec = &pg->records[_____i];
1272
1273#define while_for_each_ftrace_rec() \
1274 } \
1275 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301276
Steven Rostedted926f92011-05-03 13:25:24 -04001277static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1278 int filter_hash,
1279 bool inc)
1280{
1281 struct ftrace_hash *hash;
1282 struct ftrace_hash *other_hash;
1283 struct ftrace_page *pg;
1284 struct dyn_ftrace *rec;
1285 int count = 0;
1286 int all = 0;
1287
1288 /* Only update if the ops has been registered */
1289 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1290 return;
1291
1292 /*
1293 * In the filter_hash case:
1294 * If the count is zero, we update all records.
1295 * Otherwise we just update the items in the hash.
1296 *
1297 * In the notrace_hash case:
1298 * We enable the update in the hash.
1299 * As disabling notrace means enabling the tracing,
1300 * and enabling notrace means disabling, the inc variable
1301 * gets inversed.
1302 */
1303 if (filter_hash) {
1304 hash = ops->filter_hash;
1305 other_hash = ops->notrace_hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001306 if (!hash || !hash->count)
Steven Rostedted926f92011-05-03 13:25:24 -04001307 all = 1;
1308 } else {
1309 inc = !inc;
1310 hash = ops->notrace_hash;
1311 other_hash = ops->filter_hash;
1312 /*
1313 * If the notrace hash has no items,
1314 * then there's nothing to do.
1315 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001316 if (hash && !hash->count)
Steven Rostedted926f92011-05-03 13:25:24 -04001317 return;
1318 }
1319
1320 do_for_each_ftrace_rec(pg, rec) {
1321 int in_other_hash = 0;
1322 int in_hash = 0;
1323 int match = 0;
1324
1325 if (all) {
1326 /*
1327 * Only the filter_hash affects all records.
1328 * Update if the record is not in the notrace hash.
1329 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001330 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001331 match = 1;
1332 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -04001333 in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip);
1334 in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001335
1336 /*
1337 *
1338 */
1339 if (filter_hash && in_hash && !in_other_hash)
1340 match = 1;
1341 else if (!filter_hash && in_hash &&
1342 (in_other_hash || !other_hash->count))
1343 match = 1;
1344 }
1345 if (!match)
1346 continue;
1347
1348 if (inc) {
1349 rec->flags++;
1350 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1351 return;
1352 } else {
1353 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1354 return;
1355 rec->flags--;
1356 }
1357 count++;
1358 /* Shortcut, if we handled all records, we are done. */
1359 if (!all && count == hash->count)
1360 return;
1361 } while_for_each_ftrace_rec();
1362}
1363
1364static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1365 int filter_hash)
1366{
1367 __ftrace_hash_rec_update(ops, filter_hash, 0);
1368}
1369
1370static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1371 int filter_hash)
1372{
1373 __ftrace_hash_rec_update(ops, filter_hash, 1);
1374}
1375
Ingo Molnare309b412008-05-12 21:20:51 +02001376static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad50842008-05-12 21:20:48 +02001377{
Lai Jiangshanee000b72009-03-24 13:38:06 +08001378 rec->freelist = ftrace_free_records;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001379 ftrace_free_records = rec;
1380 rec->flags |= FTRACE_FL_FREE;
1381}
1382
Ingo Molnare309b412008-05-12 21:20:51 +02001383static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001384{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001385 struct dyn_ftrace *rec;
1386
1387 /* First check for freed records */
1388 if (ftrace_free_records) {
1389 rec = ftrace_free_records;
1390
Steven Rostedt37ad50842008-05-12 21:20:48 +02001391 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt6912896e2008-10-23 09:33:03 -04001392 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad50842008-05-12 21:20:48 +02001393 ftrace_free_records = NULL;
1394 return NULL;
1395 }
1396
Lai Jiangshanee000b72009-03-24 13:38:06 +08001397 ftrace_free_records = rec->freelist;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001398 memset(rec, 0, sizeof(*rec));
1399 return rec;
1400 }
1401
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001402 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001403 if (!ftrace_pages->next) {
1404 /* allocate another page */
1405 ftrace_pages->next =
1406 (void *)get_zeroed_page(GFP_KERNEL);
1407 if (!ftrace_pages->next)
1408 return NULL;
1409 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001410 ftrace_pages = ftrace_pages->next;
1411 }
1412
1413 return &ftrace_pages->records[ftrace_pages->index++];
1414}
1415
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001416static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001417ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001418{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001419 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001420
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001421 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001422 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001423
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001424 rec = ftrace_alloc_dyn_node(ip);
1425 if (!rec)
1426 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001427
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001428 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001429 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001430 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001431
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001432 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001433}
1434
Steven Rostedt05736a42008-09-22 14:55:47 -07001435static void print_ip_ins(const char *fmt, unsigned char *p)
1436{
1437 int i;
1438
1439 printk(KERN_CONT "%s", fmt);
1440
1441 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1442 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1443}
1444
Steven Rostedt31e88902008-11-14 16:21:19 -08001445static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001446{
1447 switch (failed) {
1448 case -EFAULT:
1449 FTRACE_WARN_ON_ONCE(1);
1450 pr_info("ftrace faulted on modifying ");
1451 print_ip_sym(ip);
1452 break;
1453 case -EINVAL:
1454 FTRACE_WARN_ON_ONCE(1);
1455 pr_info("ftrace failed to modify ");
1456 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001457 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001458 printk(KERN_CONT "\n");
1459 break;
1460 case -EPERM:
1461 FTRACE_WARN_ON_ONCE(1);
1462 pr_info("ftrace faulted on writing ");
1463 print_ip_sym(ip);
1464 break;
1465 default:
1466 FTRACE_WARN_ON_ONCE(1);
1467 pr_info("ftrace faulted on unknown error ");
1468 print_ip_sym(ip);
1469 }
1470}
1471
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001472
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001473/* Return 1 if the address range is reserved for ftrace */
1474int ftrace_text_reserved(void *start, void *end)
1475{
1476 struct dyn_ftrace *rec;
1477 struct ftrace_page *pg;
1478
1479 do_for_each_ftrace_rec(pg, rec) {
1480 if (rec->ip <= (unsigned long)end &&
1481 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1482 return 1;
1483 } while_for_each_ftrace_rec();
1484 return 0;
1485}
1486
1487
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301488static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001489__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001490{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001491 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001492 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001493
Shaohua Lif0001202009-01-09 11:29:42 +08001494 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001495
Steven Rostedt982c3502008-11-15 16:31:41 -05001496 /*
Steven Rostedted926f92011-05-03 13:25:24 -04001497 * If we are enabling tracing:
Steven Rostedt982c3502008-11-15 16:31:41 -05001498 *
Steven Rostedted926f92011-05-03 13:25:24 -04001499 * If the record has a ref count, then we need to enable it
1500 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001501 *
Steven Rostedted926f92011-05-03 13:25:24 -04001502 * Otherwise we make sure its disabled.
1503 *
1504 * If we are disabling tracing, then disable all records that
1505 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001506 */
Steven Rostedted926f92011-05-03 13:25:24 -04001507 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1508 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001509
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001510 /* If the state of this record hasn't changed, then do nothing */
1511 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1512 return 0;
1513
1514 if (flag) {
1515 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001516 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001517 }
1518
1519 rec->flags &= ~FTRACE_FL_ENABLED;
1520 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001521}
1522
1523static void ftrace_replace_code(int enable)
1524{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001525 struct dyn_ftrace *rec;
1526 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001527 int failed;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001528
Steven Rostedt45a4a232011-04-21 23:16:46 -04001529 if (unlikely(ftrace_disabled))
1530 return;
1531
Steven Rostedt265c8312009-02-13 12:43:56 -05001532 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001533 /* Skip over free records */
1534 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05001535 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001536
Steven Rostedt265c8312009-02-13 12:43:56 -05001537 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001538 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001539 ftrace_bug(failed, rec->ip);
1540 /* Stop processing */
1541 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001542 }
1543 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001544}
1545
Ingo Molnare309b412008-05-12 21:20:51 +02001546static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001547ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001548{
1549 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001550 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001551
1552 ip = rec->ip;
1553
Steven Rostedt45a4a232011-04-21 23:16:46 -04001554 if (unlikely(ftrace_disabled))
1555 return 0;
1556
Shaohua Li25aac9d2009-01-09 11:29:40 +08001557 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001558 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001559 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301560 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001561 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301562 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001563}
1564
Steven Rostedt000ab692009-02-17 13:35:06 -05001565/*
1566 * archs can override this function if they must do something
1567 * before the modifying code is performed.
1568 */
1569int __weak ftrace_arch_code_modify_prepare(void)
1570{
1571 return 0;
1572}
1573
1574/*
1575 * archs can override this function if they must do something
1576 * after the modifying code is performed.
1577 */
1578int __weak ftrace_arch_code_modify_post_process(void)
1579{
1580 return 0;
1581}
1582
Ingo Molnare309b412008-05-12 21:20:51 +02001583static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001584{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001585 int *command = data;
1586
Steven Rostedta3583242008-11-11 15:01:42 -05001587 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001588 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001589 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001590 ftrace_replace_code(0);
1591
1592 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1593 ftrace_update_ftrace_func(ftrace_trace_function);
1594
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001595 if (*command & FTRACE_START_FUNC_RET)
1596 ftrace_enable_ftrace_graph_caller();
1597 else if (*command & FTRACE_STOP_FUNC_RET)
1598 ftrace_disable_ftrace_graph_caller();
1599
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001600 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001601}
1602
Ingo Molnare309b412008-05-12 21:20:51 +02001603static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001604{
Steven Rostedt000ab692009-02-17 13:35:06 -05001605 int ret;
1606
1607 ret = ftrace_arch_code_modify_prepare();
1608 FTRACE_WARN_ON(ret);
1609 if (ret)
1610 return;
1611
Rusty Russell784e2d72008-07-28 12:16:31 -05001612 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001613
1614 ret = ftrace_arch_code_modify_post_process();
1615 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001616}
1617
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001618static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001619static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001620static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001621
1622static void ftrace_startup_enable(int command)
1623{
1624 if (saved_ftrace_func != ftrace_trace_function) {
1625 saved_ftrace_func = ftrace_trace_function;
1626 command |= FTRACE_UPDATE_TRACE_FUNC;
1627 }
1628
1629 if (!command || !ftrace_enabled)
1630 return;
1631
1632 ftrace_run_update_code(command);
1633}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001634
Steven Rostedta1cd6172011-05-23 15:24:25 -04001635static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001636{
Steven Rostedtb8489142011-05-04 09:27:52 -04001637 bool hash_enable = true;
1638
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001639 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001640 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001641
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001642 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001643 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001644
Steven Rostedtb8489142011-05-04 09:27:52 -04001645 /* ops marked global share the filter hashes */
1646 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1647 ops = &global_ops;
1648 /* Don't update hash if global is already set */
1649 if (global_start_up)
1650 hash_enable = false;
1651 global_start_up++;
1652 }
1653
Steven Rostedted926f92011-05-03 13:25:24 -04001654 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001655 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001656 ftrace_hash_rec_enable(ops, 1);
1657
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001658 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001659
1660 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001661}
1662
Steven Rostedtbd69c302011-05-03 21:55:54 -04001663static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001664{
Steven Rostedtb8489142011-05-04 09:27:52 -04001665 bool hash_disable = true;
1666
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001667 if (unlikely(ftrace_disabled))
1668 return;
1669
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001670 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001671 /*
1672 * Just warn in case of unbalance, no need to kill ftrace, it's not
1673 * critical but the ftrace_call callers may be never nopped again after
1674 * further ftrace uses.
1675 */
1676 WARN_ON_ONCE(ftrace_start_up < 0);
1677
Steven Rostedtb8489142011-05-04 09:27:52 -04001678 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1679 ops = &global_ops;
1680 global_start_up--;
1681 WARN_ON_ONCE(global_start_up < 0);
1682 /* Don't update hash if global still has users */
1683 if (global_start_up) {
1684 WARN_ON_ONCE(!ftrace_start_up);
1685 hash_disable = false;
1686 }
1687 }
1688
1689 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04001690 ftrace_hash_rec_disable(ops, 1);
1691
Steven Rostedtb8489142011-05-04 09:27:52 -04001692 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04001693 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001694
1695 if (!ftrace_start_up)
1696 command |= FTRACE_DISABLE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001697
1698 if (saved_ftrace_func != ftrace_trace_function) {
1699 saved_ftrace_func = ftrace_trace_function;
1700 command |= FTRACE_UPDATE_TRACE_FUNC;
1701 }
1702
1703 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001704 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001705
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001706 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001707}
1708
Ingo Molnare309b412008-05-12 21:20:51 +02001709static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001710{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001711 if (unlikely(ftrace_disabled))
1712 return;
1713
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001714 /* Force update next time */
1715 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001716 /* ftrace_start_up is true if we want ftrace running */
1717 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001718 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001719}
1720
Ingo Molnare309b412008-05-12 21:20:51 +02001721static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001722{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001723 if (unlikely(ftrace_disabled))
1724 return;
1725
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001726 /* ftrace_start_up is true if ftrace is running */
1727 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001728 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001729}
1730
Steven Rostedt3d083392008-05-12 21:20:42 +02001731static cycle_t ftrace_update_time;
1732static unsigned long ftrace_update_cnt;
1733unsigned long ftrace_update_tot_cnt;
1734
Steven Rostedt31e88902008-11-14 16:21:19 -08001735static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001736{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001737 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301738 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001739
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001740 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001741 ftrace_update_cnt = 0;
1742
Lai Jiangshane94142a2009-03-13 17:51:27 +08001743 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301744
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001745 /* If something went wrong, bail without enabling anything */
1746 if (unlikely(ftrace_disabled))
1747 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001748
Lai Jiangshane94142a2009-03-13 17:51:27 +08001749 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001750 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001751 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301752
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001753 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001754 * Do the initial record conversion from mcount jump
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001755 * to the NOP instructions.
1756 */
1757 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001758 ftrace_free_rec(p);
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001759 /* Game over */
1760 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001761 }
1762
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001763 ftrace_update_cnt++;
1764
1765 /*
1766 * If the tracing is enabled, go ahead and enable the record.
1767 *
1768 * The reason not to enable the record immediatelly is the
1769 * inherent check of ftrace_make_nop/ftrace_make_call for
1770 * correct previous instructions. Making first the NOP
1771 * conversion puts the module to the correct state, thus
1772 * passing the ftrace_make_call check.
1773 */
1774 if (ftrace_start_up) {
1775 int failed = __ftrace_replace_code(p, 1);
1776 if (failed) {
1777 ftrace_bug(failed, p->ip);
1778 ftrace_free_rec(p);
1779 }
1780 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001781 }
1782
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001783 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001784 ftrace_update_time = stop - start;
1785 ftrace_update_tot_cnt += ftrace_update_cnt;
1786
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001787 return 0;
1788}
1789
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001790static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001791{
1792 struct ftrace_page *pg;
1793 int cnt;
1794 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001795
1796 /* allocate a few pages */
1797 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1798 if (!ftrace_pages_start)
1799 return -1;
1800
1801 /*
1802 * Allocate a few more pages.
1803 *
1804 * TODO: have some parser search vmlinux before
1805 * final linking to find all calls to ftrace.
1806 * Then we can:
1807 * a) know how many pages to allocate.
1808 * and/or
1809 * b) set up the table then.
1810 *
1811 * The dynamic code is still necessary for
1812 * modules.
1813 */
1814
1815 pg = ftrace_pages = ftrace_pages_start;
1816
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001817 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001818 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001819 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001820
1821 for (i = 0; i < cnt; i++) {
1822 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1823
1824 /* If we fail, we'll try later anyway */
1825 if (!pg->next)
1826 break;
1827
1828 pg = pg->next;
1829 }
1830
1831 return 0;
1832}
1833
Steven Rostedt5072c592008-05-12 21:20:43 +02001834enum {
1835 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001836 FTRACE_ITER_NOTRACE = (1 << 1),
Steven Rostedt3499e462011-04-21 22:59:12 -04001837 FTRACE_ITER_PRINTALL = (1 << 2),
1838 FTRACE_ITER_HASH = (1 << 3),
Steven Rostedt647bcd02011-05-03 14:39:21 -04001839 FTRACE_ITER_ENABLED = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02001840};
1841
1842#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1843
1844struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001845 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001846 loff_t func_pos;
1847 struct ftrace_page *pg;
1848 struct dyn_ftrace *func;
1849 struct ftrace_func_probe *probe;
1850 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001851 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001852 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001853 int hidx;
1854 int idx;
1855 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02001856};
1857
Ingo Molnare309b412008-05-12 21:20:51 +02001858static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001859t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001860{
1861 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001862 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001863 struct hlist_head *hhd;
1864
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001865 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001866 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001867
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001868 if (iter->probe)
1869 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001870 retry:
1871 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1872 return NULL;
1873
1874 hhd = &ftrace_func_hash[iter->hidx];
1875
1876 if (hlist_empty(hhd)) {
1877 iter->hidx++;
1878 hnd = NULL;
1879 goto retry;
1880 }
1881
1882 if (!hnd)
1883 hnd = hhd->first;
1884 else {
1885 hnd = hnd->next;
1886 if (!hnd) {
1887 iter->hidx++;
1888 goto retry;
1889 }
1890 }
1891
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001892 if (WARN_ON_ONCE(!hnd))
1893 return NULL;
1894
1895 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1896
1897 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001898}
1899
1900static void *t_hash_start(struct seq_file *m, loff_t *pos)
1901{
1902 struct ftrace_iterator *iter = m->private;
1903 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001904 loff_t l;
1905
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001906 if (iter->func_pos > *pos)
1907 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001908
Li Zefand82d6242009-06-24 09:54:54 +08001909 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001910 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001911 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08001912 if (!p)
1913 break;
1914 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001915 if (!p)
1916 return NULL;
1917
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001918 /* Only set this if we have an item */
1919 iter->flags |= FTRACE_ITER_HASH;
1920
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001921 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001922}
1923
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001924static int
1925t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001926{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001927 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001928
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001929 rec = iter->probe;
1930 if (WARN_ON_ONCE(!rec))
1931 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001932
Steven Rostedt809dcf22009-02-16 23:06:01 -05001933 if (rec->ops->print)
1934 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1935
Steven Rostedtb375a112009-09-17 00:05:58 -04001936 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001937
1938 if (rec->data)
1939 seq_printf(m, ":%p", rec->data);
1940 seq_putc(m, '\n');
1941
1942 return 0;
1943}
1944
1945static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001946t_next(struct seq_file *m, void *v, loff_t *pos)
1947{
1948 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04001949 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02001950 struct dyn_ftrace *rec = NULL;
1951
Steven Rostedt45a4a232011-04-21 23:16:46 -04001952 if (unlikely(ftrace_disabled))
1953 return NULL;
1954
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001955 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001956 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001957
Steven Rostedt5072c592008-05-12 21:20:43 +02001958 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01001959 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02001960
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001961 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001962 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001963
Steven Rostedt5072c592008-05-12 21:20:43 +02001964 retry:
1965 if (iter->idx >= iter->pg->index) {
1966 if (iter->pg->next) {
1967 iter->pg = iter->pg->next;
1968 iter->idx = 0;
1969 goto retry;
1970 }
1971 } else {
1972 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001973 if ((rec->flags & FTRACE_FL_FREE) ||
1974
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001975 ((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04001976 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001977
Steven Rostedt41c52c02008-05-22 11:46:33 -04001978 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04001979 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
1980
1981 ((iter->flags & FTRACE_ITER_ENABLED) &&
1982 !(rec->flags & ~FTRACE_FL_MASK))) {
1983
Steven Rostedt5072c592008-05-12 21:20:43 +02001984 rec = NULL;
1985 goto retry;
1986 }
1987 }
1988
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001989 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001990 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001991
1992 iter->func = rec;
1993
1994 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02001995}
1996
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001997static void reset_iter_read(struct ftrace_iterator *iter)
1998{
1999 iter->pos = 0;
2000 iter->func_pos = 0;
2001 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002002}
2003
2004static void *t_start(struct seq_file *m, loff_t *pos)
2005{
2006 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002007 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002008 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002009 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002010
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002011 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002012
2013 if (unlikely(ftrace_disabled))
2014 return NULL;
2015
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002016 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002017 * If an lseek was done, then reset and start from beginning.
2018 */
2019 if (*pos < iter->pos)
2020 reset_iter_read(iter);
2021
2022 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002023 * For set_ftrace_filter reading, if we have the filter
2024 * off, we can short cut and just print out that all
2025 * functions are enabled.
2026 */
Steven Rostedtf45948e2011-05-02 12:29:25 -04002027 if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002028 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002029 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002030 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002031 /* reset in case of seek/pread */
2032 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002033 return iter;
2034 }
2035
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002036 if (iter->flags & FTRACE_ITER_HASH)
2037 return t_hash_start(m, pos);
2038
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002039 /*
2040 * Unfortunately, we need to restart at ftrace_pages_start
2041 * every time we let go of the ftrace_mutex. This is because
2042 * those pointers can change without the lock.
2043 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002044 iter->pg = ftrace_pages_start;
2045 iter->idx = 0;
2046 for (l = 0; l <= *pos; ) {
2047 p = t_next(m, p, &l);
2048 if (!p)
2049 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002050 }
walimis5821e1b2008-11-15 15:19:06 +08002051
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002052 if (!p) {
2053 if (iter->flags & FTRACE_ITER_FILTER)
2054 return t_hash_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002055
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002056 return NULL;
2057 }
2058
2059 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002060}
2061
2062static void t_stop(struct seq_file *m, void *p)
2063{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002064 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002065}
2066
2067static int t_show(struct seq_file *m, void *v)
2068{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002069 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002070 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002071
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002072 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002073 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002074
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002075 if (iter->flags & FTRACE_ITER_PRINTALL) {
2076 seq_printf(m, "#### all functions enabled ####\n");
2077 return 0;
2078 }
2079
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002080 rec = iter->func;
2081
Steven Rostedt5072c592008-05-12 21:20:43 +02002082 if (!rec)
2083 return 0;
2084
Steven Rostedt647bcd02011-05-03 14:39:21 -04002085 seq_printf(m, "%ps", (void *)rec->ip);
2086 if (iter->flags & FTRACE_ITER_ENABLED)
2087 seq_printf(m, " (%ld)",
2088 rec->flags & ~FTRACE_FL_MASK);
2089 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002090
2091 return 0;
2092}
2093
James Morris88e9d342009-09-22 16:43:43 -07002094static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002095 .start = t_start,
2096 .next = t_next,
2097 .stop = t_stop,
2098 .show = t_show,
2099};
2100
Ingo Molnare309b412008-05-12 21:20:51 +02002101static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002102ftrace_avail_open(struct inode *inode, struct file *file)
2103{
2104 struct ftrace_iterator *iter;
2105 int ret;
2106
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002107 if (unlikely(ftrace_disabled))
2108 return -ENODEV;
2109
Steven Rostedt5072c592008-05-12 21:20:43 +02002110 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2111 if (!iter)
2112 return -ENOMEM;
2113
2114 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002115
2116 ret = seq_open(file, &show_ftrace_seq_ops);
2117 if (!ret) {
2118 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002119
Steven Rostedt5072c592008-05-12 21:20:43 +02002120 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002121 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002122 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002123 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002124
2125 return ret;
2126}
2127
Steven Rostedt647bcd02011-05-03 14:39:21 -04002128static int
2129ftrace_enabled_open(struct inode *inode, struct file *file)
2130{
2131 struct ftrace_iterator *iter;
2132 int ret;
2133
2134 if (unlikely(ftrace_disabled))
2135 return -ENODEV;
2136
2137 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2138 if (!iter)
2139 return -ENOMEM;
2140
2141 iter->pg = ftrace_pages_start;
2142 iter->flags = FTRACE_ITER_ENABLED;
2143
2144 ret = seq_open(file, &show_ftrace_seq_ops);
2145 if (!ret) {
2146 struct seq_file *m = file->private_data;
2147
2148 m->private = iter;
2149 } else {
2150 kfree(iter);
2151 }
2152
2153 return ret;
2154}
2155
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002156static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002157{
Steven Rostedt52baf112009-02-14 01:15:39 -05002158 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002159 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002160 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002161}
2162
Ingo Molnare309b412008-05-12 21:20:51 +02002163static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002164ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002165 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002166{
2167 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002168 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002169 int ret = 0;
2170
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002171 if (unlikely(ftrace_disabled))
2172 return -ENODEV;
2173
Steven Rostedt5072c592008-05-12 21:20:43 +02002174 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2175 if (!iter)
2176 return -ENOMEM;
2177
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002178 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2179 kfree(iter);
2180 return -ENOMEM;
2181 }
2182
Steven Rostedtf45948e2011-05-02 12:29:25 -04002183 if (flag & FTRACE_ITER_NOTRACE)
2184 hash = ops->notrace_hash;
2185 else
2186 hash = ops->filter_hash;
2187
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002188 iter->ops = ops;
2189 iter->flags = flag;
2190
2191 if (file->f_mode & FMODE_WRITE) {
2192 mutex_lock(&ftrace_lock);
2193 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2194 mutex_unlock(&ftrace_lock);
2195
2196 if (!iter->hash) {
2197 trace_parser_put(&iter->parser);
2198 kfree(iter);
2199 return -ENOMEM;
2200 }
2201 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002202
Steven Rostedt41c52c02008-05-22 11:46:33 -04002203 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002204
Steven Rostedt5072c592008-05-12 21:20:43 +02002205 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002206 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002207 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002208
2209 if (file->f_mode & FMODE_READ) {
2210 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002211
2212 ret = seq_open(file, &show_ftrace_seq_ops);
2213 if (!ret) {
2214 struct seq_file *m = file->private_data;
2215 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002216 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002217 /* Failed */
2218 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002219 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002220 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002221 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002222 } else
2223 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002224 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002225
2226 return ret;
2227}
2228
Steven Rostedt41c52c02008-05-22 11:46:33 -04002229static int
2230ftrace_filter_open(struct inode *inode, struct file *file)
2231{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002232 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002233 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002234}
2235
2236static int
2237ftrace_notrace_open(struct inode *inode, struct file *file)
2238{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002239 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002240 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002241}
2242
Ingo Molnare309b412008-05-12 21:20:51 +02002243static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002244ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02002245{
2246 loff_t ret;
2247
2248 if (file->f_mode & FMODE_READ)
2249 ret = seq_lseek(file, offset, origin);
2250 else
2251 file->f_pos = ret = 1;
2252
2253 return ret;
2254}
2255
Steven Rostedt64e7c442009-02-13 17:08:48 -05002256static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002257{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002258 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002259 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002260
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002261 switch (type) {
2262 case MATCH_FULL:
2263 if (strcmp(str, regex) == 0)
2264 matched = 1;
2265 break;
2266 case MATCH_FRONT_ONLY:
2267 if (strncmp(str, regex, len) == 0)
2268 matched = 1;
2269 break;
2270 case MATCH_MIDDLE_ONLY:
2271 if (strstr(str, regex))
2272 matched = 1;
2273 break;
2274 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002275 slen = strlen(str);
2276 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002277 matched = 1;
2278 break;
2279 }
2280
2281 return matched;
2282}
2283
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002284static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002285enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002286{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002287 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002288 int ret = 0;
2289
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002290 entry = ftrace_lookup_ip(hash, rec->ip);
2291 if (not) {
2292 /* Do nothing if it doesn't exist */
2293 if (!entry)
2294 return 0;
2295
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002296 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002297 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002298 /* Do nothing if it exists */
2299 if (entry)
2300 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002301
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002302 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002303 }
2304 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002305}
2306
Steven Rostedt64e7c442009-02-13 17:08:48 -05002307static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002308ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2309 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002310{
2311 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002312 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002313
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002314 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2315
2316 if (mod) {
2317 /* module lookup requires matching the module */
2318 if (!modname || strcmp(modname, mod))
2319 return 0;
2320
2321 /* blank search means to match all funcs in the mod */
2322 if (!len)
2323 return 1;
2324 }
2325
Steven Rostedt64e7c442009-02-13 17:08:48 -05002326 return ftrace_match(str, regex, len, type);
2327}
2328
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002329static int
2330match_records(struct ftrace_hash *hash, char *buff,
2331 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002332{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002333 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002334 struct ftrace_page *pg;
2335 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002336 int type = MATCH_FULL;
2337 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002338 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002339 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002340
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002341 if (len) {
2342 type = filter_parse_regex(buff, len, &search, &not);
2343 search_len = strlen(search);
2344 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002345
Steven Rostedt52baf112009-02-14 01:15:39 -05002346 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002347
2348 if (unlikely(ftrace_disabled))
2349 goto out_unlock;
2350
Steven Rostedt265c8312009-02-13 12:43:56 -05002351 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02002352
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002353 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002354 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002355 if (ret < 0) {
2356 found = ret;
2357 goto out_unlock;
2358 }
Li Zefan311d16d2009-12-08 11:15:11 +08002359 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002360 }
2361 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002362 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002363 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002364
2365 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002366}
2367
Steven Rostedt64e7c442009-02-13 17:08:48 -05002368static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002369ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002370{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002371 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002372}
2373
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002374static int
2375ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002376{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002377 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002378
Steven Rostedt64e7c442009-02-13 17:08:48 -05002379 /* blank or '*' mean the same */
2380 if (strcmp(buff, "*") == 0)
2381 buff[0] = 0;
2382
2383 /* handle the case of 'dont filter this module' */
2384 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2385 buff[0] = 0;
2386 not = 1;
2387 }
2388
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002389 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002390}
2391
Steven Rostedtf6180772009-02-14 00:40:25 -05002392/*
2393 * We register the module command as a template to show others how
2394 * to register the a command as well.
2395 */
2396
2397static int
2398ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
2399{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002400 struct ftrace_ops *ops = &global_ops;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002401 struct ftrace_hash *hash;
Steven Rostedtf6180772009-02-14 00:40:25 -05002402 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002403 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002404
2405 /*
2406 * cmd == 'mod' because we only registered this func
2407 * for the 'mod' ftrace_func_command.
2408 * But if you register one func with multiple commands,
2409 * you can tell which command was used by the cmd
2410 * parameter.
2411 */
2412
2413 /* we must have a module name */
2414 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002415 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002416
2417 mod = strsep(&param, ":");
2418 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002419 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002420
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002421 if (enable)
Steven Rostedtf45948e2011-05-02 12:29:25 -04002422 hash = ops->filter_hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002423 else
Steven Rostedtf45948e2011-05-02 12:29:25 -04002424 hash = ops->notrace_hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002425
2426 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002427 if (!ret)
2428 ret = -EINVAL;
2429 if (ret < 0)
2430 return ret;
2431
2432 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002433}
2434
2435static struct ftrace_func_command ftrace_mod_cmd = {
2436 .name = "mod",
2437 .func = ftrace_mod_callback,
2438};
2439
2440static int __init ftrace_mod_cmd_init(void)
2441{
2442 return register_ftrace_command(&ftrace_mod_cmd);
2443}
2444device_initcall(ftrace_mod_cmd_init);
2445
Steven Rostedt59df055f2009-02-14 15:29:06 -05002446static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002447function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002448{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002449 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002450 struct hlist_head *hhd;
2451 struct hlist_node *n;
2452 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002453
2454 key = hash_long(ip, FTRACE_HASH_BITS);
2455
2456 hhd = &ftrace_func_hash[key];
2457
2458 if (hlist_empty(hhd))
2459 return;
2460
2461 /*
2462 * Disable preemption for these calls to prevent a RCU grace
2463 * period. This syncs the hash iteration and freeing of items
2464 * on the hash. rcu_read_lock is too dangerous here.
2465 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002466 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002467 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2468 if (entry->ip == ip)
2469 entry->ops->func(ip, parent_ip, &entry->data);
2470 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002471 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002472}
2473
Steven Rostedtb6887d72009-02-17 12:32:04 -05002474static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002475{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002476 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002477};
2478
Steven Rostedtb6887d72009-02-17 12:32:04 -05002479static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002480
Steven Rostedtb6887d72009-02-17 12:32:04 -05002481static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002482{
Steven Rostedtb8489142011-05-04 09:27:52 -04002483 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002484 int i;
2485
Steven Rostedtb6887d72009-02-17 12:32:04 -05002486 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002487 return;
2488
2489 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2490 struct hlist_head *hhd = &ftrace_func_hash[i];
2491 if (hhd->first)
2492 break;
2493 }
2494 /* Nothing registered? */
2495 if (i == FTRACE_FUNC_HASHSIZE)
2496 return;
2497
Steven Rostedtb8489142011-05-04 09:27:52 -04002498 ret = __register_ftrace_function(&trace_probe_ops);
2499 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002500 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002501
Steven Rostedtb6887d72009-02-17 12:32:04 -05002502 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002503}
2504
Steven Rostedtb6887d72009-02-17 12:32:04 -05002505static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002506{
Steven Rostedtb8489142011-05-04 09:27:52 -04002507 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002508 int i;
2509
Steven Rostedtb6887d72009-02-17 12:32:04 -05002510 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002511 return;
2512
2513 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2514 struct hlist_head *hhd = &ftrace_func_hash[i];
2515 if (hhd->first)
2516 return;
2517 }
2518
2519 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002520 ret = __unregister_ftrace_function(&trace_probe_ops);
2521 if (!ret)
2522 ftrace_shutdown(&trace_probe_ops, 0);
2523
Steven Rostedtb6887d72009-02-17 12:32:04 -05002524 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002525}
2526
2527
2528static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2529{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002530 struct ftrace_func_probe *entry =
2531 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002532
2533 if (entry->ops->free)
2534 entry->ops->free(&entry->data);
2535 kfree(entry);
2536}
2537
2538
2539int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002540register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002541 void *data)
2542{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002543 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002544 struct ftrace_page *pg;
2545 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002546 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002547 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002548 int count = 0;
2549 char *search;
2550
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002551 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002552 len = strlen(search);
2553
Steven Rostedtb6887d72009-02-17 12:32:04 -05002554 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002555 if (WARN_ON(not))
2556 return -EINVAL;
2557
2558 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002559
Steven Rostedt45a4a232011-04-21 23:16:46 -04002560 if (unlikely(ftrace_disabled))
2561 goto out_unlock;
2562
Steven Rostedt59df055f2009-02-14 15:29:06 -05002563 do_for_each_ftrace_rec(pg, rec) {
2564
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002565 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002566 continue;
2567
2568 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2569 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002570 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002571 if (!count)
2572 count = -ENOMEM;
2573 goto out_unlock;
2574 }
2575
2576 count++;
2577
2578 entry->data = data;
2579
2580 /*
2581 * The caller might want to do something special
2582 * for each function we find. We call the callback
2583 * to give the caller an opportunity to do so.
2584 */
2585 if (ops->callback) {
2586 if (ops->callback(rec->ip, &entry->data) < 0) {
2587 /* caller does not like this func */
2588 kfree(entry);
2589 continue;
2590 }
2591 }
2592
2593 entry->ops = ops;
2594 entry->ip = rec->ip;
2595
2596 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2597 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2598
2599 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002600 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002601
2602 out_unlock:
2603 mutex_unlock(&ftrace_lock);
2604
2605 return count;
2606}
2607
2608enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002609 PROBE_TEST_FUNC = 1,
2610 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002611};
2612
2613static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002614__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002615 void *data, int flags)
2616{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002617 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002618 struct hlist_node *n, *tmp;
2619 char str[KSYM_SYMBOL_LEN];
2620 int type = MATCH_FULL;
2621 int i, len = 0;
2622 char *search;
2623
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002624 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002625 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002626 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002627 int not;
2628
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002629 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002630 len = strlen(search);
2631
Steven Rostedtb6887d72009-02-17 12:32:04 -05002632 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002633 if (WARN_ON(not))
2634 return;
2635 }
2636
2637 mutex_lock(&ftrace_lock);
2638 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2639 struct hlist_head *hhd = &ftrace_func_hash[i];
2640
2641 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2642
2643 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002644 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002645 continue;
2646
Steven Rostedtb6887d72009-02-17 12:32:04 -05002647 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002648 continue;
2649
2650 /* do this last, since it is the most expensive */
2651 if (glob) {
2652 kallsyms_lookup(entry->ip, NULL, NULL,
2653 NULL, str);
2654 if (!ftrace_match(str, glob, len, type))
2655 continue;
2656 }
2657
2658 hlist_del(&entry->node);
2659 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2660 }
2661 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002662 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002663 mutex_unlock(&ftrace_lock);
2664}
2665
2666void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002667unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002668 void *data)
2669{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002670 __unregister_ftrace_function_probe(glob, ops, data,
2671 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002672}
2673
2674void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002675unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002676{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002677 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002678}
2679
Steven Rostedtb6887d72009-02-17 12:32:04 -05002680void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002681{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002682 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002683}
2684
Steven Rostedtf6180772009-02-14 00:40:25 -05002685static LIST_HEAD(ftrace_commands);
2686static DEFINE_MUTEX(ftrace_cmd_mutex);
2687
2688int register_ftrace_command(struct ftrace_func_command *cmd)
2689{
2690 struct ftrace_func_command *p;
2691 int ret = 0;
2692
2693 mutex_lock(&ftrace_cmd_mutex);
2694 list_for_each_entry(p, &ftrace_commands, list) {
2695 if (strcmp(cmd->name, p->name) == 0) {
2696 ret = -EBUSY;
2697 goto out_unlock;
2698 }
2699 }
2700 list_add(&cmd->list, &ftrace_commands);
2701 out_unlock:
2702 mutex_unlock(&ftrace_cmd_mutex);
2703
2704 return ret;
2705}
2706
2707int unregister_ftrace_command(struct ftrace_func_command *cmd)
2708{
2709 struct ftrace_func_command *p, *n;
2710 int ret = -ENODEV;
2711
2712 mutex_lock(&ftrace_cmd_mutex);
2713 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2714 if (strcmp(cmd->name, p->name) == 0) {
2715 ret = 0;
2716 list_del_init(&p->list);
2717 goto out_unlock;
2718 }
2719 }
2720 out_unlock:
2721 mutex_unlock(&ftrace_cmd_mutex);
2722
2723 return ret;
2724}
2725
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002726static int ftrace_process_regex(struct ftrace_hash *hash,
2727 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002728{
Steven Rostedtf6180772009-02-14 00:40:25 -05002729 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002730 struct ftrace_func_command *p;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002731 int ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002732
2733 func = strsep(&next, ":");
2734
2735 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002736 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002737 if (!ret)
2738 ret = -EINVAL;
2739 if (ret < 0)
2740 return ret;
2741 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002742 }
2743
Steven Rostedtf6180772009-02-14 00:40:25 -05002744 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002745
2746 command = strsep(&next, ":");
2747
Steven Rostedtf6180772009-02-14 00:40:25 -05002748 mutex_lock(&ftrace_cmd_mutex);
2749 list_for_each_entry(p, &ftrace_commands, list) {
2750 if (strcmp(p->name, command) == 0) {
2751 ret = p->func(func, command, next, enable);
2752 goto out_unlock;
2753 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002754 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002755 out_unlock:
2756 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002757
Steven Rostedtf6180772009-02-14 00:40:25 -05002758 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002759}
2760
Ingo Molnare309b412008-05-12 21:20:51 +02002761static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002762ftrace_regex_write(struct file *file, const char __user *ubuf,
2763 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002764{
2765 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002766 struct trace_parser *parser;
2767 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002768
Li Zefan4ba79782009-09-22 13:52:20 +08002769 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002770 return 0;
2771
Steven Rostedt41c52c02008-05-22 11:46:33 -04002772 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002773
Steven Rostedt45a4a232011-04-21 23:16:46 -04002774 ret = -ENODEV;
2775 if (unlikely(ftrace_disabled))
2776 goto out_unlock;
2777
Steven Rostedt5072c592008-05-12 21:20:43 +02002778 if (file->f_mode & FMODE_READ) {
2779 struct seq_file *m = file->private_data;
2780 iter = m->private;
2781 } else
2782 iter = file->private_data;
2783
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002784 parser = &iter->parser;
2785 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002786
Li Zefan4ba79782009-09-22 13:52:20 +08002787 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002788 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002789 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002790 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002791 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002792 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002793 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002794 }
2795
Steven Rostedt5072c592008-05-12 21:20:43 +02002796 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002797out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002798 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002799
Steven Rostedt5072c592008-05-12 21:20:43 +02002800 return ret;
2801}
2802
Steven Rostedt41c52c02008-05-22 11:46:33 -04002803static ssize_t
2804ftrace_filter_write(struct file *file, const char __user *ubuf,
2805 size_t cnt, loff_t *ppos)
2806{
2807 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2808}
2809
2810static ssize_t
2811ftrace_notrace_write(struct file *file, const char __user *ubuf,
2812 size_t cnt, loff_t *ppos)
2813{
2814 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2815}
2816
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002817static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002818ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2819 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04002820{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002821 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002822 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002823 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002824
Steven Rostedt936e0742011-05-05 22:54:01 -04002825 /* All global ops uses the global ops filters */
2826 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
2827 ops = &global_ops;
2828
Steven Rostedt41c52c02008-05-22 11:46:33 -04002829 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002830 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002831
Steven Rostedtf45948e2011-05-02 12:29:25 -04002832 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002833 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002834 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002835 orig_hash = &ops->notrace_hash;
2836
2837 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
2838 if (!hash)
2839 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002840
Steven Rostedt41c52c02008-05-22 11:46:33 -04002841 mutex_lock(&ftrace_regex_lock);
2842 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002843 ftrace_filter_reset(hash);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002844 if (buf)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002845 ftrace_match_records(hash, buf, len);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002846
2847 mutex_lock(&ftrace_lock);
2848 ret = ftrace_hash_move(orig_hash, hash);
2849 mutex_unlock(&ftrace_lock);
2850
Steven Rostedt41c52c02008-05-22 11:46:33 -04002851 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002852
2853 free_ftrace_hash(hash);
2854 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002855}
2856
Steven Rostedt77a2b372008-05-12 21:20:45 +02002857/**
2858 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04002859 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02002860 * @buf - the string that holds the function filter text.
2861 * @len - the length of the string.
2862 * @reset - non zero to reset all filters before applying this filter.
2863 *
2864 * Filters denote which functions should be enabled when tracing is enabled.
2865 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2866 */
Steven Rostedt936e0742011-05-05 22:54:01 -04002867void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
2868 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002869{
Steven Rostedt936e0742011-05-05 22:54:01 -04002870 ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002871}
Steven Rostedt936e0742011-05-05 22:54:01 -04002872EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002873
Steven Rostedt41c52c02008-05-22 11:46:33 -04002874/**
2875 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04002876 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04002877 * @buf - the string that holds the function notrace text.
2878 * @len - the length of the string.
2879 * @reset - non zero to reset all filters before applying this filter.
2880 *
2881 * Notrace Filters denote which functions should not be enabled when tracing
2882 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2883 * for tracing.
2884 */
Steven Rostedt936e0742011-05-05 22:54:01 -04002885void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
2886 int len, int reset)
2887{
2888 ftrace_set_regex(ops, buf, len, reset, 0);
2889}
2890EXPORT_SYMBOL_GPL(ftrace_set_notrace);
2891/**
2892 * ftrace_set_filter - set a function to filter on in ftrace
2893 * @ops - the ops to set the filter with
2894 * @buf - the string that holds the function filter text.
2895 * @len - the length of the string.
2896 * @reset - non zero to reset all filters before applying this filter.
2897 *
2898 * Filters denote which functions should be enabled when tracing is enabled.
2899 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2900 */
2901void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
2902{
2903 ftrace_set_regex(&global_ops, buf, len, reset, 1);
2904}
2905EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
2906
2907/**
2908 * ftrace_set_notrace - set a function to not trace in ftrace
2909 * @ops - the ops to set the notrace filter with
2910 * @buf - the string that holds the function notrace text.
2911 * @len - the length of the string.
2912 * @reset - non zero to reset all filters before applying this filter.
2913 *
2914 * Notrace Filters denote which functions should not be enabled when tracing
2915 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2916 * for tracing.
2917 */
2918void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04002919{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002920 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002921}
Steven Rostedt936e0742011-05-05 22:54:01 -04002922EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002923
Steven Rostedt2af15d62009-05-28 13:37:24 -04002924/*
2925 * command line interface to allow users to set filters on boot up.
2926 */
2927#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2928static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2929static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2930
2931static int __init set_ftrace_notrace(char *str)
2932{
2933 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2934 return 1;
2935}
2936__setup("ftrace_notrace=", set_ftrace_notrace);
2937
2938static int __init set_ftrace_filter(char *str)
2939{
2940 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2941 return 1;
2942}
2943__setup("ftrace_filter=", set_ftrace_filter);
2944
Stefan Assmann369bc182009-10-12 22:17:21 +02002945#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002946static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002947static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2948
Stefan Assmann369bc182009-10-12 22:17:21 +02002949static int __init set_graph_function(char *str)
2950{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002951 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002952 return 1;
2953}
2954__setup("ftrace_graph_filter=", set_graph_function);
2955
2956static void __init set_ftrace_early_graph(char *buf)
2957{
2958 int ret;
2959 char *func;
2960
2961 while (buf) {
2962 func = strsep(&buf, ",");
2963 /* we allow only one expression at a time */
2964 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2965 func);
2966 if (ret)
2967 printk(KERN_DEBUG "ftrace: function %s not "
2968 "traceable\n", func);
2969 }
2970}
2971#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2972
Steven Rostedtf45948e2011-05-02 12:29:25 -04002973static void __init
2974set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04002975{
2976 char *func;
2977
2978 while (buf) {
2979 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04002980 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04002981 }
2982}
2983
2984static void __init set_ftrace_early_filters(void)
2985{
2986 if (ftrace_filter_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04002987 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04002988 if (ftrace_notrace_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04002989 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002990#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2991 if (ftrace_graph_buf[0])
2992 set_ftrace_early_graph(ftrace_graph_buf);
2993#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002994}
2995
Ingo Molnare309b412008-05-12 21:20:51 +02002996static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002997ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002998{
2999 struct seq_file *m = (struct seq_file *)file->private_data;
3000 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003001 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003002 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003003 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003004 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003005
Steven Rostedt41c52c02008-05-22 11:46:33 -04003006 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003007 if (file->f_mode & FMODE_READ) {
3008 iter = m->private;
3009
3010 seq_release(inode, file);
3011 } else
3012 iter = file->private_data;
3013
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003014 parser = &iter->parser;
3015 if (trace_parser_loaded(parser)) {
3016 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003017 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003018 }
3019
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003020 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003021
Steven Rostedt058e2972011-04-29 22:35:33 -04003022 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003023 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3024
3025 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003026 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003027 else
3028 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003029
Steven Rostedt058e2972011-04-29 22:35:33 -04003030 mutex_lock(&ftrace_lock);
Steven Rostedted926f92011-05-03 13:25:24 -04003031 /*
3032 * Remove the current set, update the hash and add
3033 * them back.
3034 */
3035 ftrace_hash_rec_disable(iter->ops, filter_hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003036 ret = ftrace_hash_move(orig_hash, iter->hash);
Steven Rostedted926f92011-05-03 13:25:24 -04003037 if (!ret) {
3038 ftrace_hash_rec_enable(iter->ops, filter_hash);
3039 if (iter->ops->flags & FTRACE_OPS_FL_ENABLED
3040 && ftrace_enabled)
3041 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
3042 }
Steven Rostedt058e2972011-04-29 22:35:33 -04003043 mutex_unlock(&ftrace_lock);
3044 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003045 free_ftrace_hash(iter->hash);
3046 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003047
Steven Rostedt41c52c02008-05-22 11:46:33 -04003048 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003049 return 0;
3050}
3051
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003052static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003053 .open = ftrace_avail_open,
3054 .read = seq_read,
3055 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003056 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003057};
3058
Steven Rostedt647bcd02011-05-03 14:39:21 -04003059static const struct file_operations ftrace_enabled_fops = {
3060 .open = ftrace_enabled_open,
3061 .read = seq_read,
3062 .llseek = seq_lseek,
3063 .release = seq_release_private,
3064};
3065
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003066static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003067 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003068 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003069 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003070 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003071 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003072};
3073
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003074static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003075 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003076 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003077 .write = ftrace_notrace_write,
3078 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003079 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003080};
3081
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003082#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3083
3084static DEFINE_MUTEX(graph_lock);
3085
3086int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003087int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003088unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3089
3090static void *
Li Zefan85951842009-06-24 09:54:00 +08003091__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003092{
Li Zefan85951842009-06-24 09:54:00 +08003093 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003094 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003095 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003096}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003097
Li Zefan85951842009-06-24 09:54:00 +08003098static void *
3099g_next(struct seq_file *m, void *v, loff_t *pos)
3100{
3101 (*pos)++;
3102 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003103}
3104
3105static void *g_start(struct seq_file *m, loff_t *pos)
3106{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003107 mutex_lock(&graph_lock);
3108
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003109 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003110 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003111 return (void *)1;
3112
Li Zefan85951842009-06-24 09:54:00 +08003113 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003114}
3115
3116static void g_stop(struct seq_file *m, void *p)
3117{
3118 mutex_unlock(&graph_lock);
3119}
3120
3121static int g_show(struct seq_file *m, void *v)
3122{
3123 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003124
3125 if (!ptr)
3126 return 0;
3127
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003128 if (ptr == (unsigned long *)1) {
3129 seq_printf(m, "#### all functions enabled ####\n");
3130 return 0;
3131 }
3132
Steven Rostedtb375a112009-09-17 00:05:58 -04003133 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003134
3135 return 0;
3136}
3137
James Morris88e9d342009-09-22 16:43:43 -07003138static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003139 .start = g_start,
3140 .next = g_next,
3141 .stop = g_stop,
3142 .show = g_show,
3143};
3144
3145static int
3146ftrace_graph_open(struct inode *inode, struct file *file)
3147{
3148 int ret = 0;
3149
3150 if (unlikely(ftrace_disabled))
3151 return -ENODEV;
3152
3153 mutex_lock(&graph_lock);
3154 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003155 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003156 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003157 ftrace_graph_count = 0;
3158 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3159 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003160 mutex_unlock(&graph_lock);
3161
Li Zefana4ec5e02009-09-18 14:06:28 +08003162 if (file->f_mode & FMODE_READ)
3163 ret = seq_open(file, &ftrace_graph_seq_ops);
3164
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003165 return ret;
3166}
3167
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003168static int
Li Zefan87827112009-07-23 11:29:11 +08003169ftrace_graph_release(struct inode *inode, struct file *file)
3170{
3171 if (file->f_mode & FMODE_READ)
3172 seq_release(inode, file);
3173 return 0;
3174}
3175
3176static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003177ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003178{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003179 struct dyn_ftrace *rec;
3180 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003181 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003182 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003183 int type, not;
3184 char *search;
3185 bool exists;
3186 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003187
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003188 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003189 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003190 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3191 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003192
3193 search_len = strlen(search);
3194
Steven Rostedt52baf112009-02-14 01:15:39 -05003195 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003196
3197 if (unlikely(ftrace_disabled)) {
3198 mutex_unlock(&ftrace_lock);
3199 return -ENODEV;
3200 }
3201
Steven Rostedt265c8312009-02-13 12:43:56 -05003202 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003203
Steven Rostedt45a4a232011-04-21 23:16:46 -04003204 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05003205 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003206
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003207 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003208 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003209 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003210 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003211 if (array[i] == rec->ip) {
3212 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003213 break;
3214 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003215 }
3216
3217 if (!not) {
3218 fail = 0;
3219 if (!exists) {
3220 array[(*idx)++] = rec->ip;
3221 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3222 goto out;
3223 }
3224 } else {
3225 if (exists) {
3226 array[i] = array[--(*idx)];
3227 array[*idx] = 0;
3228 fail = 0;
3229 }
3230 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003231 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003232 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003233out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003234 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003235
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003236 if (fail)
3237 return -EINVAL;
3238
3239 ftrace_graph_filter_enabled = 1;
3240 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003241}
3242
3243static ssize_t
3244ftrace_graph_write(struct file *file, const char __user *ubuf,
3245 size_t cnt, loff_t *ppos)
3246{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003247 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003248 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003249
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003250 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003251 return 0;
3252
3253 mutex_lock(&graph_lock);
3254
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003255 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3256 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003257 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003258 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003259
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003260 read = trace_get_user(&parser, ubuf, cnt, ppos);
3261
Li Zefan4ba79782009-09-22 13:52:20 +08003262 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003263 parser.buffer[parser.idx] = 0;
3264
3265 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003266 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003267 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003268 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003269 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003270 }
3271
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003272 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003273
3274out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003275 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003276out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003277 mutex_unlock(&graph_lock);
3278
3279 return ret;
3280}
3281
3282static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003283 .open = ftrace_graph_open,
3284 .read = seq_read,
3285 .write = ftrace_graph_write,
3286 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003287 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003288};
3289#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3290
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003291static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003292{
Steven Rostedt5072c592008-05-12 21:20:43 +02003293
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003294 trace_create_file("available_filter_functions", 0444,
3295 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003296
Steven Rostedt647bcd02011-05-03 14:39:21 -04003297 trace_create_file("enabled_functions", 0444,
3298 d_tracer, NULL, &ftrace_enabled_fops);
3299
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003300 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3301 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003302
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003303 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003304 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003305
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003306#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003307 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003308 NULL,
3309 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003310#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3311
Steven Rostedt5072c592008-05-12 21:20:43 +02003312 return 0;
3313}
3314
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003315static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003316 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003317 unsigned long *end)
3318{
3319 unsigned long *p;
3320 unsigned long addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003321
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003322 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003323 p = start;
3324 while (p < end) {
3325 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003326 /*
3327 * Some architecture linkers will pad between
3328 * the different mcount_loc sections of different
3329 * object files to satisfy alignments.
3330 * Skip any NULL pointers.
3331 */
3332 if (!addr)
3333 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003334 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003335 }
3336
Steven Rostedt31e88902008-11-14 16:21:19 -08003337 ftrace_update_code(mod);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003338 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003339
3340 return 0;
3341}
3342
Steven Rostedt93eb6772009-04-15 13:24:06 -04003343#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003344void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003345{
3346 struct dyn_ftrace *rec;
3347 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003348
Steven Rostedt93eb6772009-04-15 13:24:06 -04003349 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003350
3351 if (ftrace_disabled)
3352 goto out_unlock;
3353
Steven Rostedt93eb6772009-04-15 13:24:06 -04003354 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003355 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003356 /*
3357 * rec->ip is changed in ftrace_free_rec()
3358 * It should not between s and e if record was freed.
3359 */
3360 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
3361 ftrace_free_rec(rec);
3362 }
3363 } while_for_each_ftrace_rec();
Steven Rostedt45a4a232011-04-21 23:16:46 -04003364 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003365 mutex_unlock(&ftrace_lock);
3366}
3367
3368static void ftrace_init_module(struct module *mod,
3369 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003370{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003371 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003372 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003373 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003374}
3375
Steven Rostedt93eb6772009-04-15 13:24:06 -04003376static int ftrace_module_notify(struct notifier_block *self,
3377 unsigned long val, void *data)
3378{
3379 struct module *mod = data;
3380
3381 switch (val) {
3382 case MODULE_STATE_COMING:
3383 ftrace_init_module(mod, mod->ftrace_callsites,
3384 mod->ftrace_callsites +
3385 mod->num_ftrace_callsites);
3386 break;
3387 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003388 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003389 break;
3390 }
3391
3392 return 0;
3393}
3394#else
3395static int ftrace_module_notify(struct notifier_block *self,
3396 unsigned long val, void *data)
3397{
3398 return 0;
3399}
3400#endif /* CONFIG_MODULES */
3401
3402struct notifier_block ftrace_module_nb = {
3403 .notifier_call = ftrace_module_notify,
3404 .priority = 0,
3405};
3406
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003407extern unsigned long __start_mcount_loc[];
3408extern unsigned long __stop_mcount_loc[];
3409
3410void __init ftrace_init(void)
3411{
3412 unsigned long count, addr, flags;
3413 int ret;
3414
3415 /* Keep the ftrace pointer to the stub */
3416 addr = (unsigned long)ftrace_stub;
3417
3418 local_irq_save(flags);
3419 ftrace_dyn_arch_init(&addr);
3420 local_irq_restore(flags);
3421
3422 /* ftrace_dyn_arch_init places the return code in addr */
3423 if (addr)
3424 goto failed;
3425
3426 count = __stop_mcount_loc - __start_mcount_loc;
3427
3428 ret = ftrace_dyn_table_alloc(count);
3429 if (ret)
3430 goto failed;
3431
3432 last_ftrace_enabled = ftrace_enabled = 1;
3433
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003434 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003435 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003436 __stop_mcount_loc);
3437
Steven Rostedt93eb6772009-04-15 13:24:06 -04003438 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003439 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003440 pr_warning("Failed to register trace ftrace module notifier\n");
3441
Steven Rostedt2af15d62009-05-28 13:37:24 -04003442 set_ftrace_early_filters();
3443
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003444 return;
3445 failed:
3446 ftrace_disabled = 1;
3447}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003448
Steven Rostedt3d083392008-05-12 21:20:42 +02003449#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003450
Steven Rostedt2b499382011-05-03 22:49:52 -04003451static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003452 .func = ftrace_stub,
3453};
3454
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003455static int __init ftrace_nodyn_init(void)
3456{
3457 ftrace_enabled = 1;
3458 return 0;
3459}
3460device_initcall(ftrace_nodyn_init);
3461
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003462static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3463static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003464/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003465# define ftrace_startup(ops, command) \
3466 ({ \
3467 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3468 0; \
3469 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04003470# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003471# define ftrace_startup_sysctl() do { } while (0)
3472# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003473
3474static inline int
3475ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3476{
3477 return 1;
3478}
3479
Steven Rostedt3d083392008-05-12 21:20:42 +02003480#endif /* CONFIG_DYNAMIC_FTRACE */
3481
Steven Rostedtb8489142011-05-04 09:27:52 -04003482static void
3483ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3484{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003485 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04003486
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003487 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3488 return;
3489
3490 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003491 /*
3492 * Some of the ops may be dynamically allocated,
3493 * they must be freed after a synchronize_sched().
3494 */
3495 preempt_disable_notrace();
3496 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04003497 while (op != &ftrace_list_end) {
3498 if (ftrace_ops_test(op, ip))
3499 op->func(ip, parent_ip);
3500 op = rcu_dereference_raw(op->next);
3501 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003502 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003503 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04003504}
3505
Steven Rostedte32d8952008-12-04 00:26:41 -05003506static void clear_ftrace_swapper(void)
3507{
3508 struct task_struct *p;
3509 int cpu;
3510
3511 get_online_cpus();
3512 for_each_online_cpu(cpu) {
3513 p = idle_task(cpu);
3514 clear_tsk_trace_trace(p);
3515 }
3516 put_online_cpus();
3517}
3518
3519static void set_ftrace_swapper(void)
3520{
3521 struct task_struct *p;
3522 int cpu;
3523
3524 get_online_cpus();
3525 for_each_online_cpu(cpu) {
3526 p = idle_task(cpu);
3527 set_tsk_trace_trace(p);
3528 }
3529 put_online_cpus();
3530}
3531
3532static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003533{
3534 struct task_struct *p;
3535
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003536 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05003537 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05003538 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05003539 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003540 rcu_read_unlock();
3541
Steven Rostedte32d8952008-12-04 00:26:41 -05003542 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003543}
3544
Steven Rostedte32d8952008-12-04 00:26:41 -05003545static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003546{
3547 struct task_struct *p;
3548
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003549 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003550 do_each_pid_task(pid, PIDTYPE_PID, p) {
3551 set_tsk_trace_trace(p);
3552 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003553 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003554}
3555
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003556static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003557{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003558 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003559 clear_ftrace_swapper();
3560 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003561 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05003562}
3563
3564static void set_ftrace_pid_task(struct pid *pid)
3565{
3566 if (pid == ftrace_swapper_pid)
3567 set_ftrace_swapper();
3568 else
3569 set_ftrace_pid(pid);
3570}
3571
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003572static int ftrace_pid_add(int p)
3573{
3574 struct pid *pid;
3575 struct ftrace_pid *fpid;
3576 int ret = -EINVAL;
3577
3578 mutex_lock(&ftrace_lock);
3579
3580 if (!p)
3581 pid = ftrace_swapper_pid;
3582 else
3583 pid = find_get_pid(p);
3584
3585 if (!pid)
3586 goto out;
3587
3588 ret = 0;
3589
3590 list_for_each_entry(fpid, &ftrace_pids, list)
3591 if (fpid->pid == pid)
3592 goto out_put;
3593
3594 ret = -ENOMEM;
3595
3596 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3597 if (!fpid)
3598 goto out_put;
3599
3600 list_add(&fpid->list, &ftrace_pids);
3601 fpid->pid = pid;
3602
3603 set_ftrace_pid_task(pid);
3604
3605 ftrace_update_pid_func();
3606 ftrace_startup_enable(0);
3607
3608 mutex_unlock(&ftrace_lock);
3609 return 0;
3610
3611out_put:
3612 if (pid != ftrace_swapper_pid)
3613 put_pid(pid);
3614
3615out:
3616 mutex_unlock(&ftrace_lock);
3617 return ret;
3618}
3619
3620static void ftrace_pid_reset(void)
3621{
3622 struct ftrace_pid *fpid, *safe;
3623
3624 mutex_lock(&ftrace_lock);
3625 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3626 struct pid *pid = fpid->pid;
3627
3628 clear_ftrace_pid_task(pid);
3629
3630 list_del(&fpid->list);
3631 kfree(fpid);
3632 }
3633
3634 ftrace_update_pid_func();
3635 ftrace_startup_enable(0);
3636
3637 mutex_unlock(&ftrace_lock);
3638}
3639
3640static void *fpid_start(struct seq_file *m, loff_t *pos)
3641{
3642 mutex_lock(&ftrace_lock);
3643
3644 if (list_empty(&ftrace_pids) && (!*pos))
3645 return (void *) 1;
3646
3647 return seq_list_start(&ftrace_pids, *pos);
3648}
3649
3650static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3651{
3652 if (v == (void *)1)
3653 return NULL;
3654
3655 return seq_list_next(v, &ftrace_pids, pos);
3656}
3657
3658static void fpid_stop(struct seq_file *m, void *p)
3659{
3660 mutex_unlock(&ftrace_lock);
3661}
3662
3663static int fpid_show(struct seq_file *m, void *v)
3664{
3665 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3666
3667 if (v == (void *)1) {
3668 seq_printf(m, "no pid\n");
3669 return 0;
3670 }
3671
3672 if (fpid->pid == ftrace_swapper_pid)
3673 seq_printf(m, "swapper tasks\n");
3674 else
3675 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3676
3677 return 0;
3678}
3679
3680static const struct seq_operations ftrace_pid_sops = {
3681 .start = fpid_start,
3682 .next = fpid_next,
3683 .stop = fpid_stop,
3684 .show = fpid_show,
3685};
3686
3687static int
3688ftrace_pid_open(struct inode *inode, struct file *file)
3689{
3690 int ret = 0;
3691
3692 if ((file->f_mode & FMODE_WRITE) &&
3693 (file->f_flags & O_TRUNC))
3694 ftrace_pid_reset();
3695
3696 if (file->f_mode & FMODE_READ)
3697 ret = seq_open(file, &ftrace_pid_sops);
3698
3699 return ret;
3700}
3701
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003702static ssize_t
3703ftrace_pid_write(struct file *filp, const char __user *ubuf,
3704 size_t cnt, loff_t *ppos)
3705{
Ingo Molnar457dc922009-11-23 11:03:28 +01003706 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003707 long val;
3708 int ret;
3709
3710 if (cnt >= sizeof(buf))
3711 return -EINVAL;
3712
3713 if (copy_from_user(&buf, ubuf, cnt))
3714 return -EFAULT;
3715
3716 buf[cnt] = 0;
3717
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003718 /*
3719 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3720 * to clean the filter quietly.
3721 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003722 tmp = strstrip(buf);
3723 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003724 return 1;
3725
Ingo Molnar457dc922009-11-23 11:03:28 +01003726 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003727 if (ret < 0)
3728 return ret;
3729
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003730 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003731
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003732 return ret ? ret : cnt;
3733}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003734
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003735static int
3736ftrace_pid_release(struct inode *inode, struct file *file)
3737{
3738 if (file->f_mode & FMODE_READ)
3739 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003740
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003741 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003742}
3743
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003744static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003745 .open = ftrace_pid_open,
3746 .write = ftrace_pid_write,
3747 .read = seq_read,
3748 .llseek = seq_lseek,
3749 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003750};
3751
3752static __init int ftrace_init_debugfs(void)
3753{
3754 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003755
3756 d_tracer = tracing_init_dentry();
3757 if (!d_tracer)
3758 return 0;
3759
3760 ftrace_init_dyn_debugfs(d_tracer);
3761
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003762 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3763 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003764
3765 ftrace_profile_debugfs(d_tracer);
3766
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003767 return 0;
3768}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003769fs_initcall(ftrace_init_debugfs);
3770
Steven Rostedt3d083392008-05-12 21:20:42 +02003771/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003772 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003773 *
3774 * This function should be used by panic code. It stops ftrace
3775 * but in a not so nice way. If you need to simply kill ftrace
3776 * from a non-atomic section, use ftrace_kill.
3777 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003778void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003779{
3780 ftrace_disabled = 1;
3781 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003782 clear_ftrace_function();
3783}
3784
3785/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003786 * register_ftrace_function - register a function for profiling
3787 * @ops - ops structure that holds the function for profiling.
3788 *
3789 * Register a function to be called by all functions in the
3790 * kernel.
3791 *
3792 * Note: @ops->func and all the functions it calls must be labeled
3793 * with "notrace", otherwise it will go into a
3794 * recursive loop.
3795 */
3796int register_ftrace_function(struct ftrace_ops *ops)
3797{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003798 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003799
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003800 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003801
Steven Rostedt45a4a232011-04-21 23:16:46 -04003802 if (unlikely(ftrace_disabled))
3803 goto out_unlock;
3804
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003805 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04003806 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04003807 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003808
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003809
Steven Rostedt45a4a232011-04-21 23:16:46 -04003810 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003811 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003812 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003813}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003814EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02003815
3816/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003817 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003818 * @ops - ops structure that holds the function to unregister
3819 *
3820 * Unregister a function that was added to be called by ftrace profiling.
3821 */
3822int unregister_ftrace_function(struct ftrace_ops *ops)
3823{
3824 int ret;
3825
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003826 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003827 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04003828 if (!ret)
3829 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003830 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003831
3832 return ret;
3833}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003834EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003835
Ingo Molnare309b412008-05-12 21:20:51 +02003836int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003837ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003838 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003839 loff_t *ppos)
3840{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003841 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003842
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003843 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003844
Steven Rostedt45a4a232011-04-21 23:16:46 -04003845 if (unlikely(ftrace_disabled))
3846 goto out;
3847
3848 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003849
Li Zefana32c7762009-06-26 16:55:51 +08003850 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003851 goto out;
3852
Li Zefana32c7762009-06-26 16:55:51 +08003853 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003854
3855 if (ftrace_enabled) {
3856
3857 ftrace_startup_sysctl();
3858
3859 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04003860 if (ftrace_ops_list != &ftrace_list_end) {
3861 if (ftrace_ops_list->next == &ftrace_list_end)
3862 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003863 else
Steven Rostedtb8489142011-05-04 09:27:52 -04003864 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003865 }
3866
3867 } else {
3868 /* stopping ftrace calls (just send to ftrace_stub) */
3869 ftrace_trace_function = ftrace_stub;
3870
3871 ftrace_shutdown_sysctl();
3872 }
3873
3874 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003875 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003876 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003877}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003878
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003879#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003880
Steven Rostedt597af812009-04-03 15:24:12 -04003881static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003882static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003883
Steven Rostedte49dc192008-12-02 23:50:05 -05003884int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3885{
3886 return 0;
3887}
3888
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003889/* The callbacks that hook a function */
3890trace_func_graph_ret_t ftrace_graph_return =
3891 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003892trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003893
3894/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3895static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3896{
3897 int i;
3898 int ret = 0;
3899 unsigned long flags;
3900 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3901 struct task_struct *g, *t;
3902
3903 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3904 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3905 * sizeof(struct ftrace_ret_stack),
3906 GFP_KERNEL);
3907 if (!ret_stack_list[i]) {
3908 start = 0;
3909 end = i;
3910 ret = -ENOMEM;
3911 goto free;
3912 }
3913 }
3914
3915 read_lock_irqsave(&tasklist_lock, flags);
3916 do_each_thread(g, t) {
3917 if (start == end) {
3918 ret = -EAGAIN;
3919 goto unlock;
3920 }
3921
3922 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003923 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003924 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003925 t->curr_ret_stack = -1;
3926 /* Make sure the tasks see the -1 first: */
3927 smp_wmb();
3928 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003929 }
3930 } while_each_thread(g, t);
3931
3932unlock:
3933 read_unlock_irqrestore(&tasklist_lock, flags);
3934free:
3935 for (i = start; i < end; i++)
3936 kfree(ret_stack_list[i]);
3937 return ret;
3938}
3939
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003940static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04003941ftrace_graph_probe_sched_switch(void *ignore,
3942 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003943{
3944 unsigned long long timestamp;
3945 int index;
3946
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003947 /*
3948 * Does the user want to count the time a function was asleep.
3949 * If so, do not update the time stamps.
3950 */
3951 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3952 return;
3953
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003954 timestamp = trace_clock_local();
3955
3956 prev->ftrace_timestamp = timestamp;
3957
3958 /* only process tasks that we timestamped */
3959 if (!next->ftrace_timestamp)
3960 return;
3961
3962 /*
3963 * Update all the counters in next to make up for the
3964 * time next was sleeping.
3965 */
3966 timestamp -= next->ftrace_timestamp;
3967
3968 for (index = next->curr_ret_stack; index >= 0; index--)
3969 next->ret_stack[index].calltime += timestamp;
3970}
3971
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003972/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003973static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003974{
3975 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003976 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003977
3978 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3979 sizeof(struct ftrace_ret_stack *),
3980 GFP_KERNEL);
3981
3982 if (!ret_stack_list)
3983 return -ENOMEM;
3984
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003985 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003986 for_each_online_cpu(cpu) {
3987 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05003988 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04003989 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003990
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003991 do {
3992 ret = alloc_retstack_tasklist(ret_stack_list);
3993 } while (ret == -EAGAIN);
3994
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003995 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04003996 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003997 if (ret)
3998 pr_info("ftrace_graph: Couldn't activate tracepoint"
3999 " probe to kernel_sched_switch\n");
4000 }
4001
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004002 kfree(ret_stack_list);
4003 return ret;
4004}
4005
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004006/*
4007 * Hibernation protection.
4008 * The state of the current task is too much unstable during
4009 * suspend/restore to disk. We want to protect against that.
4010 */
4011static int
4012ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4013 void *unused)
4014{
4015 switch (state) {
4016 case PM_HIBERNATION_PREPARE:
4017 pause_graph_tracing();
4018 break;
4019
4020 case PM_POST_HIBERNATION:
4021 unpause_graph_tracing();
4022 break;
4023 }
4024 return NOTIFY_DONE;
4025}
4026
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004027int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4028 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004029{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004030 int ret = 0;
4031
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004032 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004033
Steven Rostedt05ce5812009-03-24 00:18:31 -04004034 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004035 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004036 ret = -EBUSY;
4037 goto out;
4038 }
4039
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004040 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4041 register_pm_notifier(&ftrace_suspend_notifier);
4042
Steven Rostedt597af812009-04-03 15:24:12 -04004043 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004044 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004045 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004046 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004047 goto out;
4048 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004049
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004050 ftrace_graph_return = retfunc;
4051 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004052
Steven Rostedta1cd6172011-05-23 15:24:25 -04004053 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004054
4055out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004056 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004057 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004058}
4059
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004060void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004061{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004062 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004063
Steven Rostedt597af812009-04-03 15:24:12 -04004064 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004065 goto out;
4066
Steven Rostedt597af812009-04-03 15:24:12 -04004067 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004068 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004069 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004070 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004071 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004072 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004073
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004074 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004075 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004076}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004077
Steven Rostedt868baf02011-02-10 21:26:13 -05004078static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4079
4080static void
4081graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4082{
4083 atomic_set(&t->tracing_graph_pause, 0);
4084 atomic_set(&t->trace_overrun, 0);
4085 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004086 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004087 smp_wmb();
4088 t->ret_stack = ret_stack;
4089}
4090
4091/*
4092 * Allocate a return stack for the idle task. May be the first
4093 * time through, or it may be done by CPU hotplug online.
4094 */
4095void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4096{
4097 t->curr_ret_stack = -1;
4098 /*
4099 * The idle task has no parent, it either has its own
4100 * stack or no stack at all.
4101 */
4102 if (t->ret_stack)
4103 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4104
4105 if (ftrace_graph_active) {
4106 struct ftrace_ret_stack *ret_stack;
4107
4108 ret_stack = per_cpu(idle_ret_stack, cpu);
4109 if (!ret_stack) {
4110 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4111 * sizeof(struct ftrace_ret_stack),
4112 GFP_KERNEL);
4113 if (!ret_stack)
4114 return;
4115 per_cpu(idle_ret_stack, cpu) = ret_stack;
4116 }
4117 graph_init_task(t, ret_stack);
4118 }
4119}
4120
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004121/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004122void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004123{
Steven Rostedt84047e32009-06-02 16:51:55 -04004124 /* Make sure we do not use the parent ret_stack */
4125 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004126 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004127
Steven Rostedt597af812009-04-03 15:24:12 -04004128 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004129 struct ftrace_ret_stack *ret_stack;
4130
4131 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004132 * sizeof(struct ftrace_ret_stack),
4133 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004134 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004135 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004136 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004137 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004138}
4139
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004140void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004141{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004142 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4143
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004144 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004145 /* NULL must become visible to IRQs before we free it: */
4146 barrier();
4147
4148 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004149}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004150
4151void ftrace_graph_stop(void)
4152{
4153 ftrace_stop();
4154}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004155#endif