blob: bebbc959ee8c1388321aca9d8249457edff83d58 [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
Abhishek Sagar395a59d2008-06-21 23:47:27 +053035#include <asm/ftrace.h>
Steven Rostedt2af15d62009-05-28 13:37:24 -040036#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053037
Steven Rostedt0706f1c2009-03-23 23:12:58 -040038#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040039#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020040
Steven Rostedt6912896e2008-10-23 09:33:03 -040041#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040042 ({ \
43 int ___r = cond; \
44 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040045 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040046 ___r; \
47 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040048
49#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040050 ({ \
51 int ___r = cond; \
52 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040053 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040054 ___r; \
55 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040056
Steven Rostedt8fc0c702009-02-16 15:28:00 -050057/* hash bits for specific function selection */
58#define FTRACE_HASH_BITS 7
59#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040060#define FTRACE_HASH_DEFAULT_BITS 10
61#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050062
Steven Rostedt4eebcc82008-05-12 21:20:48 +020063/* ftrace_enabled is a method to turn ftrace on or off */
64int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020065static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020066
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050067/* Quick disabling of function tracer. */
68int function_trace_stop;
69
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040070/* List for set_ftrace_pid's pids. */
71LIST_HEAD(ftrace_pids);
72struct ftrace_pid {
73 struct list_head list;
74 struct pid *pid;
75};
76
Steven Rostedt4eebcc82008-05-12 21:20:48 +020077/*
78 * ftrace_disabled is set when an anomaly is discovered.
79 * ftrace_disabled is much stronger than ftrace_enabled.
80 */
81static int ftrace_disabled __read_mostly;
82
Steven Rostedt52baf112009-02-14 01:15:39 -050083static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020084
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020085static struct ftrace_ops ftrace_list_end __read_mostly =
86{
Steven Rostedtfb9fb012009-03-25 13:26:41 -040087 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020088};
89
Steven Rostedtb8489142011-05-04 09:27:52 -040090static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
91static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020092ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050093ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050094ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -040095static struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020096
Steven Rostedtb8489142011-05-04 09:27:52 -040097static void
98ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
99
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800100/*
Steven Rostedtb8489142011-05-04 09:27:52 -0400101 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800102 * can use rcu_dereference_raw() is that elements removed from this list
103 * are simply leaked, so there is no need to interact with a grace-period
104 * mechanism. The rcu_dereference_raw() calls are needed to handle
Steven Rostedtb8489142011-05-04 09:27:52 -0400105 * concurrent insertions into the ftrace_global_list.
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800106 *
107 * Silly Alpha and silly pointer-speculation compiler optimizations!
108 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400109static void ftrace_global_list_func(unsigned long ip,
110 unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200111{
Steven Rostedtb8489142011-05-04 09:27:52 -0400112 struct ftrace_ops *op = rcu_dereference_raw(ftrace_global_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200113
114 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200115 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800116 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200117 };
118}
119
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500120static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
121{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500122 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500123 return;
124
125 ftrace_pid_function(ip, parent_ip);
126}
127
128static void set_ftrace_pid_function(ftrace_func_t func)
129{
130 /* do not set ftrace_pid_function to itself! */
131 if (func != ftrace_pid_func)
132 ftrace_pid_function = func;
133}
134
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200135/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200136 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200137 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200138 * This NULLs the ftrace function and in essence stops
139 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200140 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200141void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200142{
Steven Rostedt3d083392008-05-12 21:20:42 +0200143 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500144 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500145 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200146}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200147
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500148#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
149/*
150 * For those archs that do not test ftrace_trace_stop in their
151 * mcount call site, we need to do it from C.
152 */
153static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
154{
155 if (function_trace_stop)
156 return;
157
158 __ftrace_trace_function(ip, parent_ip);
159}
160#endif
161
Steven Rostedt2b499382011-05-03 22:49:52 -0400162static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400163{
164 ftrace_func_t func;
165
166 /*
167 * If there's only one function registered, then call that
168 * function directly. Otherwise, we need to iterate over the
169 * registered callers.
170 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400171 if (ftrace_global_list == &ftrace_list_end ||
172 ftrace_global_list->next == &ftrace_list_end)
173 func = ftrace_global_list->func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400174 else
Steven Rostedtb8489142011-05-04 09:27:52 -0400175 func = ftrace_global_list_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400176
177 /* If we filter on pids, update to use the pid function */
178 if (!list_empty(&ftrace_pids)) {
179 set_ftrace_pid_function(func);
180 func = ftrace_pid_func;
181 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400182
183 global_ops.func = func;
184}
185
186static void update_ftrace_function(void)
187{
188 ftrace_func_t func;
189
190 update_global_ops();
191
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400192 /*
193 * If we are at the end of the list and this ops is
194 * not dynamic, then have the mcount trampoline call
195 * the function directly
196 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400197 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400198 (ftrace_ops_list->next == &ftrace_list_end &&
199 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
Steven Rostedtb8489142011-05-04 09:27:52 -0400200 func = ftrace_ops_list->func;
201 else
202 func = ftrace_ops_list_func;
Steven Rostedt2b499382011-05-03 22:49:52 -0400203
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400204#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
205 ftrace_trace_function = func;
206#else
207 __ftrace_trace_function = func;
208 ftrace_trace_function = ftrace_test_stop_func;
209#endif
210}
211
Steven Rostedt2b499382011-05-03 22:49:52 -0400212static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200213{
Steven Rostedt2b499382011-05-03 22:49:52 -0400214 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200215 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400216 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200217 * CPU might be walking that list. We need to make sure
218 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400219 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200220 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400221 rcu_assign_pointer(*list, ops);
222}
Steven Rostedt3d083392008-05-12 21:20:42 +0200223
Steven Rostedt2b499382011-05-03 22:49:52 -0400224static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
225{
226 struct ftrace_ops **p;
227
228 /*
229 * If we are removing the last function, then simply point
230 * to the ftrace_stub.
231 */
232 if (*list == ops && ops->next == &ftrace_list_end) {
233 *list = &ftrace_list_end;
234 return 0;
235 }
236
237 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
238 if (*p == ops)
239 break;
240
241 if (*p != ops)
242 return -1;
243
244 *p = (*p)->next;
245 return 0;
246}
247
248static int __register_ftrace_function(struct ftrace_ops *ops)
249{
250 if (ftrace_disabled)
251 return -ENODEV;
252
253 if (FTRACE_WARN_ON(ops == &global_ops))
254 return -EINVAL;
255
Steven Rostedtb8489142011-05-04 09:27:52 -0400256 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
257 return -EBUSY;
258
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400259 if (!core_kernel_data((unsigned long)ops))
260 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
261
Steven Rostedtb8489142011-05-04 09:27:52 -0400262 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
263 int first = ftrace_global_list == &ftrace_list_end;
264 add_ftrace_ops(&ftrace_global_list, ops);
265 ops->flags |= FTRACE_OPS_FL_ENABLED;
266 if (first)
267 add_ftrace_ops(&ftrace_ops_list, &global_ops);
268 } else
269 add_ftrace_ops(&ftrace_ops_list, ops);
270
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400271 if (ftrace_enabled)
272 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200273
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200274 return 0;
275}
276
Ingo Molnare309b412008-05-12 21:20:51 +0200277static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200278{
Steven Rostedt2b499382011-05-03 22:49:52 -0400279 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200280
Steven Rostedt2b499382011-05-03 22:49:52 -0400281 if (ftrace_disabled)
282 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200283
Steven Rostedtb8489142011-05-04 09:27:52 -0400284 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
285 return -EBUSY;
286
Steven Rostedt2b499382011-05-03 22:49:52 -0400287 if (FTRACE_WARN_ON(ops == &global_ops))
288 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200289
Steven Rostedtb8489142011-05-04 09:27:52 -0400290 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
291 ret = remove_ftrace_ops(&ftrace_global_list, ops);
292 if (!ret && ftrace_global_list == &ftrace_list_end)
293 ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops);
294 if (!ret)
295 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
296 } else
297 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
298
Steven Rostedt2b499382011-05-03 22:49:52 -0400299 if (ret < 0)
300 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400301
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400302 if (ftrace_enabled)
303 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200304
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400305 /*
306 * Dynamic ops may be freed, we must make sure that all
307 * callers are done before leaving this function.
308 */
309 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
310 synchronize_sched();
311
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500312 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200313}
314
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500315static void ftrace_update_pid_func(void)
316{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400317 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500318 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900319 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500320
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400321 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500322}
323
Steven Rostedt493762f2009-03-23 17:12:36 -0400324#ifdef CONFIG_FUNCTION_PROFILER
325struct ftrace_profile {
326 struct hlist_node node;
327 unsigned long ip;
328 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400329#ifdef CONFIG_FUNCTION_GRAPH_TRACER
330 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400331 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400332#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400333};
334
335struct ftrace_profile_page {
336 struct ftrace_profile_page *next;
337 unsigned long index;
338 struct ftrace_profile records[];
339};
340
Steven Rostedtcafb1682009-03-24 20:50:39 -0400341struct ftrace_profile_stat {
342 atomic_t disabled;
343 struct hlist_head *hash;
344 struct ftrace_profile_page *pages;
345 struct ftrace_profile_page *start;
346 struct tracer_stat stat;
347};
348
Steven Rostedt493762f2009-03-23 17:12:36 -0400349#define PROFILE_RECORDS_SIZE \
350 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
351
352#define PROFILES_PER_PAGE \
353 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
354
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400355static int ftrace_profile_bits __read_mostly;
356static int ftrace_profile_enabled __read_mostly;
357
358/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400359static DEFINE_MUTEX(ftrace_profile_lock);
360
Steven Rostedtcafb1682009-03-24 20:50:39 -0400361static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400362
363#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
364
Steven Rostedt493762f2009-03-23 17:12:36 -0400365static void *
366function_stat_next(void *v, int idx)
367{
368 struct ftrace_profile *rec = v;
369 struct ftrace_profile_page *pg;
370
371 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
372
373 again:
Li Zefan0296e422009-06-26 11:15:37 +0800374 if (idx != 0)
375 rec++;
376
Steven Rostedt493762f2009-03-23 17:12:36 -0400377 if ((void *)rec >= (void *)&pg->records[pg->index]) {
378 pg = pg->next;
379 if (!pg)
380 return NULL;
381 rec = &pg->records[0];
382 if (!rec->counter)
383 goto again;
384 }
385
386 return rec;
387}
388
389static void *function_stat_start(struct tracer_stat *trace)
390{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400391 struct ftrace_profile_stat *stat =
392 container_of(trace, struct ftrace_profile_stat, stat);
393
394 if (!stat || !stat->start)
395 return NULL;
396
397 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400398}
399
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400400#ifdef CONFIG_FUNCTION_GRAPH_TRACER
401/* function graph compares on total time */
402static int function_stat_cmp(void *p1, void *p2)
403{
404 struct ftrace_profile *a = p1;
405 struct ftrace_profile *b = p2;
406
407 if (a->time < b->time)
408 return -1;
409 if (a->time > b->time)
410 return 1;
411 else
412 return 0;
413}
414#else
415/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400416static int function_stat_cmp(void *p1, void *p2)
417{
418 struct ftrace_profile *a = p1;
419 struct ftrace_profile *b = p2;
420
421 if (a->counter < b->counter)
422 return -1;
423 if (a->counter > b->counter)
424 return 1;
425 else
426 return 0;
427}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400428#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400429
430static int function_stat_headers(struct seq_file *m)
431{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400432#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400433 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400434 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400435 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400436 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400437#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400438 seq_printf(m, " Function Hit\n"
439 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400440#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400441 return 0;
442}
443
444static int function_stat_show(struct seq_file *m, void *v)
445{
446 struct ftrace_profile *rec = v;
447 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800448 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400449#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400450 static struct trace_seq s;
451 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400452 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400453#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800454 mutex_lock(&ftrace_profile_lock);
455
456 /* we raced with function_profile_reset() */
457 if (unlikely(rec->counter == 0)) {
458 ret = -EBUSY;
459 goto out;
460 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400461
462 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400463 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400464
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400465#ifdef CONFIG_FUNCTION_GRAPH_TRACER
466 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400467 avg = rec->time;
468 do_div(avg, rec->counter);
469
Chase Douglase330b3b2010-04-26 14:02:05 -0400470 /* Sample standard deviation (s^2) */
471 if (rec->counter <= 1)
472 stddev = 0;
473 else {
474 stddev = rec->time_squared - rec->counter * avg * avg;
475 /*
476 * Divide only 1000 for ns^2 -> us^2 conversion.
477 * trace_print_graph_duration will divide 1000 again.
478 */
479 do_div(stddev, (rec->counter - 1) * 1000);
480 }
481
Steven Rostedt34886c82009-03-25 21:00:47 -0400482 trace_seq_init(&s);
483 trace_print_graph_duration(rec->time, &s);
484 trace_seq_puts(&s, " ");
485 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400486 trace_seq_puts(&s, " ");
487 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400488 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400489#endif
490 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800491out:
492 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400493
Li Zefan3aaba202010-08-23 16:50:12 +0800494 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400495}
496
Steven Rostedtcafb1682009-03-24 20:50:39 -0400497static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400498{
499 struct ftrace_profile_page *pg;
500
Steven Rostedtcafb1682009-03-24 20:50:39 -0400501 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400502
503 while (pg) {
504 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
505 pg->index = 0;
506 pg = pg->next;
507 }
508
Steven Rostedtcafb1682009-03-24 20:50:39 -0400509 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400510 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
511}
512
Steven Rostedtcafb1682009-03-24 20:50:39 -0400513int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400514{
515 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400516 int functions;
517 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400518 int i;
519
520 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400521 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400522 return 0;
523
Steven Rostedtcafb1682009-03-24 20:50:39 -0400524 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
525 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400526 return -ENOMEM;
527
Steven Rostedt318e0a72009-03-25 20:06:34 -0400528#ifdef CONFIG_DYNAMIC_FTRACE
529 functions = ftrace_update_tot_cnt;
530#else
531 /*
532 * We do not know the number of functions that exist because
533 * dynamic tracing is what counts them. With past experience
534 * we have around 20K functions. That should be more than enough.
535 * It is highly unlikely we will execute every function in
536 * the kernel.
537 */
538 functions = 20000;
539#endif
540
Steven Rostedtcafb1682009-03-24 20:50:39 -0400541 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400542
Steven Rostedt318e0a72009-03-25 20:06:34 -0400543 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
544
545 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400546 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400547 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400548 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400549 pg = pg->next;
550 }
551
552 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400553
554 out_free:
555 pg = stat->start;
556 while (pg) {
557 unsigned long tmp = (unsigned long)pg;
558
559 pg = pg->next;
560 free_page(tmp);
561 }
562
563 free_page((unsigned long)stat->pages);
564 stat->pages = NULL;
565 stat->start = NULL;
566
567 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400568}
569
Steven Rostedtcafb1682009-03-24 20:50:39 -0400570static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400571{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400572 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400573 int size;
574
Steven Rostedtcafb1682009-03-24 20:50:39 -0400575 stat = &per_cpu(ftrace_profile_stats, cpu);
576
577 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400578 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400579 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400580 return 0;
581 }
582
583 /*
584 * We are profiling all functions, but usually only a few thousand
585 * functions are hit. We'll make a hash of 1024 items.
586 */
587 size = FTRACE_PROFILE_HASH_SIZE;
588
Steven Rostedtcafb1682009-03-24 20:50:39 -0400589 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400590
Steven Rostedtcafb1682009-03-24 20:50:39 -0400591 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400592 return -ENOMEM;
593
Steven Rostedtcafb1682009-03-24 20:50:39 -0400594 if (!ftrace_profile_bits) {
595 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400596
Steven Rostedtcafb1682009-03-24 20:50:39 -0400597 for (; size; size >>= 1)
598 ftrace_profile_bits++;
599 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400600
Steven Rostedt318e0a72009-03-25 20:06:34 -0400601 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400602 if (ftrace_profile_pages_init(stat) < 0) {
603 kfree(stat->hash);
604 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400605 return -ENOMEM;
606 }
607
608 return 0;
609}
610
Steven Rostedtcafb1682009-03-24 20:50:39 -0400611static int ftrace_profile_init(void)
612{
613 int cpu;
614 int ret = 0;
615
616 for_each_online_cpu(cpu) {
617 ret = ftrace_profile_init_cpu(cpu);
618 if (ret)
619 break;
620 }
621
622 return ret;
623}
624
Steven Rostedt493762f2009-03-23 17:12:36 -0400625/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400626static struct ftrace_profile *
627ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400628{
629 struct ftrace_profile *rec;
630 struct hlist_head *hhd;
631 struct hlist_node *n;
632 unsigned long key;
633
634 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400635 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400636
637 if (hlist_empty(hhd))
638 return NULL;
639
640 hlist_for_each_entry_rcu(rec, n, hhd, node) {
641 if (rec->ip == ip)
642 return rec;
643 }
644
645 return NULL;
646}
647
Steven Rostedtcafb1682009-03-24 20:50:39 -0400648static void ftrace_add_profile(struct ftrace_profile_stat *stat,
649 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400650{
651 unsigned long key;
652
653 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400654 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400655}
656
Steven Rostedt318e0a72009-03-25 20:06:34 -0400657/*
658 * The memory is already allocated, this simply finds a new record to use.
659 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400660static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400661ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400662{
663 struct ftrace_profile *rec = NULL;
664
Steven Rostedt318e0a72009-03-25 20:06:34 -0400665 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400666 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400667 goto out;
668
Steven Rostedt493762f2009-03-23 17:12:36 -0400669 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400670 * Try to find the function again since an NMI
671 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400672 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400673 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400674 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400675 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400676
Steven Rostedtcafb1682009-03-24 20:50:39 -0400677 if (stat->pages->index == PROFILES_PER_PAGE) {
678 if (!stat->pages->next)
679 goto out;
680 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400681 }
682
Steven Rostedtcafb1682009-03-24 20:50:39 -0400683 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400684 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400685 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400686
Steven Rostedt493762f2009-03-23 17:12:36 -0400687 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400688 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400689
690 return rec;
691}
692
Steven Rostedt493762f2009-03-23 17:12:36 -0400693static void
694function_profile_call(unsigned long ip, unsigned long parent_ip)
695{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400696 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400697 struct ftrace_profile *rec;
698 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400699
700 if (!ftrace_profile_enabled)
701 return;
702
Steven Rostedt493762f2009-03-23 17:12:36 -0400703 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400704
705 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400706 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400707 goto out;
708
709 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400710 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400711 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400712 if (!rec)
713 goto out;
714 }
715
716 rec->counter++;
717 out:
718 local_irq_restore(flags);
719}
720
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400721#ifdef CONFIG_FUNCTION_GRAPH_TRACER
722static int profile_graph_entry(struct ftrace_graph_ent *trace)
723{
724 function_profile_call(trace->func, 0);
725 return 1;
726}
727
728static void profile_graph_return(struct ftrace_graph_ret *trace)
729{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400730 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400731 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400732 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400733 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400734
735 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400736 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400737 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400738 goto out;
739
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400740 /* If the calltime was zero'd ignore it */
741 if (!trace->calltime)
742 goto out;
743
Steven Rostedta2a16d62009-03-24 23:17:58 -0400744 calltime = trace->rettime - trace->calltime;
745
746 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
747 int index;
748
749 index = trace->depth;
750
751 /* Append this call time to the parent time to subtract */
752 if (index)
753 current->ret_stack[index - 1].subtime += calltime;
754
755 if (current->ret_stack[index].subtime < calltime)
756 calltime -= current->ret_stack[index].subtime;
757 else
758 calltime = 0;
759 }
760
Steven Rostedtcafb1682009-03-24 20:50:39 -0400761 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400762 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400763 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400764 rec->time_squared += calltime * calltime;
765 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400766
Steven Rostedtcafb1682009-03-24 20:50:39 -0400767 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400768 local_irq_restore(flags);
769}
770
771static int register_ftrace_profiler(void)
772{
773 return register_ftrace_graph(&profile_graph_return,
774 &profile_graph_entry);
775}
776
777static void unregister_ftrace_profiler(void)
778{
779 unregister_ftrace_graph();
780}
781#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400782static struct ftrace_ops ftrace_profile_ops __read_mostly =
783{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400784 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400785};
786
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400787static int register_ftrace_profiler(void)
788{
789 return register_ftrace_function(&ftrace_profile_ops);
790}
791
792static void unregister_ftrace_profiler(void)
793{
794 unregister_ftrace_function(&ftrace_profile_ops);
795}
796#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
797
Steven Rostedt493762f2009-03-23 17:12:36 -0400798static ssize_t
799ftrace_profile_write(struct file *filp, const char __user *ubuf,
800 size_t cnt, loff_t *ppos)
801{
802 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400803 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400804 int ret;
805
806 if (cnt >= sizeof(buf))
807 return -EINVAL;
808
809 if (copy_from_user(&buf, ubuf, cnt))
810 return -EFAULT;
811
812 buf[cnt] = 0;
813
814 ret = strict_strtoul(buf, 10, &val);
815 if (ret < 0)
816 return ret;
817
818 val = !!val;
819
820 mutex_lock(&ftrace_profile_lock);
821 if (ftrace_profile_enabled ^ val) {
822 if (val) {
823 ret = ftrace_profile_init();
824 if (ret < 0) {
825 cnt = ret;
826 goto out;
827 }
828
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400829 ret = register_ftrace_profiler();
830 if (ret < 0) {
831 cnt = ret;
832 goto out;
833 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400834 ftrace_profile_enabled = 1;
835 } else {
836 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400837 /*
838 * unregister_ftrace_profiler calls stop_machine
839 * so this acts like an synchronize_sched.
840 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400841 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400842 }
843 }
844 out:
845 mutex_unlock(&ftrace_profile_lock);
846
Jiri Olsacf8517c2009-10-23 19:36:16 -0400847 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400848
849 return cnt;
850}
851
852static ssize_t
853ftrace_profile_read(struct file *filp, char __user *ubuf,
854 size_t cnt, loff_t *ppos)
855{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400856 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400857 int r;
858
859 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
860 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
861}
862
863static const struct file_operations ftrace_profile_fops = {
864 .open = tracing_open_generic,
865 .read = ftrace_profile_read,
866 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200867 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400868};
869
Steven Rostedtcafb1682009-03-24 20:50:39 -0400870/* used to initialize the real stat files */
871static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400872 .name = "functions",
873 .stat_start = function_stat_start,
874 .stat_next = function_stat_next,
875 .stat_cmp = function_stat_cmp,
876 .stat_headers = function_stat_headers,
877 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400878};
879
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400880static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400881{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400882 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400883 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400884 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400885 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400886 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400887
Steven Rostedtcafb1682009-03-24 20:50:39 -0400888 for_each_possible_cpu(cpu) {
889 stat = &per_cpu(ftrace_profile_stats, cpu);
890
891 /* allocate enough for function name + cpu number */
892 name = kmalloc(32, GFP_KERNEL);
893 if (!name) {
894 /*
895 * The files created are permanent, if something happens
896 * we still do not free memory.
897 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400898 WARN(1,
899 "Could not allocate stat file for cpu %d\n",
900 cpu);
901 return;
902 }
903 stat->stat = function_stats;
904 snprintf(name, 32, "function%d", cpu);
905 stat->stat.name = name;
906 ret = register_stat_tracer(&stat->stat);
907 if (ret) {
908 WARN(1,
909 "Could not register function stat for cpu %d\n",
910 cpu);
911 kfree(name);
912 return;
913 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400914 }
915
916 entry = debugfs_create_file("function_profile_enabled", 0644,
917 d_tracer, NULL, &ftrace_profile_fops);
918 if (!entry)
919 pr_warning("Could not create debugfs "
920 "'function_profile_enabled' entry\n");
921}
922
923#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400924static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400925{
926}
927#endif /* CONFIG_FUNCTION_PROFILER */
928
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100929static struct pid * const ftrace_swapper_pid = &init_struct_pid;
930
Steven Rostedt3d083392008-05-12 21:20:42 +0200931#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100932
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400933#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400934# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400935#endif
936
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500937static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
938
Steven Rostedtb6887d72009-02-17 12:32:04 -0500939struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500940 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500941 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500942 unsigned long flags;
943 unsigned long ip;
944 void *data;
945 struct rcu_head rcu;
946};
947
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200948enum {
949 FTRACE_ENABLE_CALLS = (1 << 0),
950 FTRACE_DISABLE_CALLS = (1 << 1),
951 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
Steven Rostedt79e406d2010-09-14 22:19:46 -0400952 FTRACE_START_FUNC_RET = (1 << 3),
953 FTRACE_STOP_FUNC_RET = (1 << 4),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200954};
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400955struct ftrace_func_entry {
956 struct hlist_node hlist;
957 unsigned long ip;
958};
959
960struct ftrace_hash {
961 unsigned long size_bits;
962 struct hlist_head *buckets;
963 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -0400964 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400965};
966
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400967/*
968 * We make these constant because no one should touch them,
969 * but they are used as the default "empty hash", to avoid allocating
970 * it all the time. These are in a read only section such that if
971 * anyone does try to modify it, it will cause an exception.
972 */
973static const struct hlist_head empty_buckets[1];
974static const struct ftrace_hash empty_hash = {
975 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400976};
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400977#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +0200978
Steven Rostedt2b499382011-05-03 22:49:52 -0400979static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -0400980 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400981 .notrace_hash = EMPTY_HASH,
982 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -0400983};
984
Lai Jiangshane94142a2009-03-13 17:51:27 +0800985static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200986
Steven Rostedt41c52c02008-05-22 11:46:33 -0400987static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200988
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200989struct ftrace_page {
990 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500991 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200992 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700993};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200994
995#define ENTRIES_PER_PAGE \
996 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
997
998/* estimate from running different kernels */
999#define NR_TO_INIT 10000
1000
1001static struct ftrace_page *ftrace_pages_start;
1002static struct ftrace_page *ftrace_pages;
1003
Steven Rostedt37ad50842008-05-12 21:20:48 +02001004static struct dyn_ftrace *ftrace_free_records;
1005
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001006static struct ftrace_func_entry *
1007ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1008{
1009 unsigned long key;
1010 struct ftrace_func_entry *entry;
1011 struct hlist_head *hhd;
1012 struct hlist_node *n;
1013
1014 if (!hash->count)
1015 return NULL;
1016
1017 if (hash->size_bits > 0)
1018 key = hash_long(ip, hash->size_bits);
1019 else
1020 key = 0;
1021
1022 hhd = &hash->buckets[key];
1023
1024 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1025 if (entry->ip == ip)
1026 return entry;
1027 }
1028 return NULL;
1029}
1030
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001031static void __add_hash_entry(struct ftrace_hash *hash,
1032 struct ftrace_func_entry *entry)
1033{
1034 struct hlist_head *hhd;
1035 unsigned long key;
1036
1037 if (hash->size_bits)
1038 key = hash_long(entry->ip, hash->size_bits);
1039 else
1040 key = 0;
1041
1042 hhd = &hash->buckets[key];
1043 hlist_add_head(&entry->hlist, hhd);
1044 hash->count++;
1045}
1046
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001047static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1048{
1049 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001050
1051 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1052 if (!entry)
1053 return -ENOMEM;
1054
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001055 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001056 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001057
1058 return 0;
1059}
1060
1061static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001062free_hash_entry(struct ftrace_hash *hash,
1063 struct ftrace_func_entry *entry)
1064{
1065 hlist_del(&entry->hlist);
1066 kfree(entry);
1067 hash->count--;
1068}
1069
1070static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001071remove_hash_entry(struct ftrace_hash *hash,
1072 struct ftrace_func_entry *entry)
1073{
1074 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001075 hash->count--;
1076}
1077
1078static void ftrace_hash_clear(struct ftrace_hash *hash)
1079{
1080 struct hlist_head *hhd;
1081 struct hlist_node *tp, *tn;
1082 struct ftrace_func_entry *entry;
1083 int size = 1 << hash->size_bits;
1084 int i;
1085
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001086 if (!hash->count)
1087 return;
1088
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001089 for (i = 0; i < size; i++) {
1090 hhd = &hash->buckets[i];
1091 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001092 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001093 }
1094 FTRACE_WARN_ON(hash->count);
1095}
1096
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001097static void free_ftrace_hash(struct ftrace_hash *hash)
1098{
1099 if (!hash || hash == EMPTY_HASH)
1100 return;
1101 ftrace_hash_clear(hash);
1102 kfree(hash->buckets);
1103 kfree(hash);
1104}
1105
Steven Rostedt07fd5512011-05-05 18:03:47 -04001106static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1107{
1108 struct ftrace_hash *hash;
1109
1110 hash = container_of(rcu, struct ftrace_hash, rcu);
1111 free_ftrace_hash(hash);
1112}
1113
1114static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1115{
1116 if (!hash || hash == EMPTY_HASH)
1117 return;
1118 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1119}
1120
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001121static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1122{
1123 struct ftrace_hash *hash;
1124 int size;
1125
1126 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1127 if (!hash)
1128 return NULL;
1129
1130 size = 1 << size_bits;
1131 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1132
1133 if (!hash->buckets) {
1134 kfree(hash);
1135 return NULL;
1136 }
1137
1138 hash->size_bits = size_bits;
1139
1140 return hash;
1141}
1142
1143static struct ftrace_hash *
1144alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1145{
1146 struct ftrace_func_entry *entry;
1147 struct ftrace_hash *new_hash;
1148 struct hlist_node *tp;
1149 int size;
1150 int ret;
1151 int i;
1152
1153 new_hash = alloc_ftrace_hash(size_bits);
1154 if (!new_hash)
1155 return NULL;
1156
1157 /* Empty hash? */
1158 if (!hash || !hash->count)
1159 return new_hash;
1160
1161 size = 1 << hash->size_bits;
1162 for (i = 0; i < size; i++) {
1163 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1164 ret = add_hash_entry(new_hash, entry->ip);
1165 if (ret < 0)
1166 goto free_hash;
1167 }
1168 }
1169
1170 FTRACE_WARN_ON(new_hash->count != hash->count);
1171
1172 return new_hash;
1173
1174 free_hash:
1175 free_ftrace_hash(new_hash);
1176 return NULL;
1177}
1178
1179static int
1180ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src)
1181{
1182 struct ftrace_func_entry *entry;
1183 struct hlist_node *tp, *tn;
1184 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001185 struct ftrace_hash *old_hash;
1186 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001187 unsigned long key;
1188 int size = src->count;
1189 int bits = 0;
1190 int i;
1191
1192 /*
1193 * If the new source is empty, just free dst and assign it
1194 * the empty_hash.
1195 */
1196 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001197 free_ftrace_hash_rcu(*dst);
1198 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001199 return 0;
1200 }
1201
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001202 /*
1203 * Make the hash size about 1/2 the # found
1204 */
1205 for (size /= 2; size; size >>= 1)
1206 bits++;
1207
1208 /* Don't allocate too much */
1209 if (bits > FTRACE_HASH_MAX_BITS)
1210 bits = FTRACE_HASH_MAX_BITS;
1211
Steven Rostedt07fd5512011-05-05 18:03:47 -04001212 new_hash = alloc_ftrace_hash(bits);
1213 if (!new_hash)
1214 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001215
1216 size = 1 << src->size_bits;
1217 for (i = 0; i < size; i++) {
1218 hhd = &src->buckets[i];
1219 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1220 if (bits > 0)
1221 key = hash_long(entry->ip, bits);
1222 else
1223 key = 0;
1224 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001225 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001226 }
1227 }
1228
Steven Rostedt07fd5512011-05-05 18:03:47 -04001229 old_hash = *dst;
1230 rcu_assign_pointer(*dst, new_hash);
1231 free_ftrace_hash_rcu(old_hash);
1232
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001233 return 0;
1234}
1235
Steven Rostedt265c8312009-02-13 12:43:56 -05001236/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001237 * Test the hashes for this ops to see if we want to call
1238 * the ops->func or not.
1239 *
1240 * It's a match if the ip is in the ops->filter_hash or
1241 * the filter_hash does not exist or is empty,
1242 * AND
1243 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001244 *
1245 * This needs to be called with preemption disabled as
1246 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001247 */
1248static int
1249ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1250{
1251 struct ftrace_hash *filter_hash;
1252 struct ftrace_hash *notrace_hash;
1253 int ret;
1254
Steven Rostedtb8489142011-05-04 09:27:52 -04001255 filter_hash = rcu_dereference_raw(ops->filter_hash);
1256 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1257
1258 if ((!filter_hash || !filter_hash->count ||
1259 ftrace_lookup_ip(filter_hash, ip)) &&
1260 (!notrace_hash || !notrace_hash->count ||
1261 !ftrace_lookup_ip(notrace_hash, ip)))
1262 ret = 1;
1263 else
1264 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001265
1266 return ret;
1267}
1268
1269/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001270 * This is a double for. Do not use 'break' to break out of the loop,
1271 * you must use a goto.
1272 */
1273#define do_for_each_ftrace_rec(pg, rec) \
1274 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1275 int _____i; \
1276 for (_____i = 0; _____i < pg->index; _____i++) { \
1277 rec = &pg->records[_____i];
1278
1279#define while_for_each_ftrace_rec() \
1280 } \
1281 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301282
Steven Rostedted926f92011-05-03 13:25:24 -04001283static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1284 int filter_hash,
1285 bool inc)
1286{
1287 struct ftrace_hash *hash;
1288 struct ftrace_hash *other_hash;
1289 struct ftrace_page *pg;
1290 struct dyn_ftrace *rec;
1291 int count = 0;
1292 int all = 0;
1293
1294 /* Only update if the ops has been registered */
1295 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1296 return;
1297
1298 /*
1299 * In the filter_hash case:
1300 * If the count is zero, we update all records.
1301 * Otherwise we just update the items in the hash.
1302 *
1303 * In the notrace_hash case:
1304 * We enable the update in the hash.
1305 * As disabling notrace means enabling the tracing,
1306 * and enabling notrace means disabling, the inc variable
1307 * gets inversed.
1308 */
1309 if (filter_hash) {
1310 hash = ops->filter_hash;
1311 other_hash = ops->notrace_hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001312 if (!hash || !hash->count)
Steven Rostedted926f92011-05-03 13:25:24 -04001313 all = 1;
1314 } else {
1315 inc = !inc;
1316 hash = ops->notrace_hash;
1317 other_hash = ops->filter_hash;
1318 /*
1319 * If the notrace hash has no items,
1320 * then there's nothing to do.
1321 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001322 if (hash && !hash->count)
Steven Rostedted926f92011-05-03 13:25:24 -04001323 return;
1324 }
1325
1326 do_for_each_ftrace_rec(pg, rec) {
1327 int in_other_hash = 0;
1328 int in_hash = 0;
1329 int match = 0;
1330
1331 if (all) {
1332 /*
1333 * Only the filter_hash affects all records.
1334 * Update if the record is not in the notrace hash.
1335 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001336 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001337 match = 1;
1338 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -04001339 in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip);
1340 in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001341
1342 /*
1343 *
1344 */
1345 if (filter_hash && in_hash && !in_other_hash)
1346 match = 1;
1347 else if (!filter_hash && in_hash &&
1348 (in_other_hash || !other_hash->count))
1349 match = 1;
1350 }
1351 if (!match)
1352 continue;
1353
1354 if (inc) {
1355 rec->flags++;
1356 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1357 return;
1358 } else {
1359 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1360 return;
1361 rec->flags--;
1362 }
1363 count++;
1364 /* Shortcut, if we handled all records, we are done. */
1365 if (!all && count == hash->count)
1366 return;
1367 } while_for_each_ftrace_rec();
1368}
1369
1370static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1371 int filter_hash)
1372{
1373 __ftrace_hash_rec_update(ops, filter_hash, 0);
1374}
1375
1376static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1377 int filter_hash)
1378{
1379 __ftrace_hash_rec_update(ops, filter_hash, 1);
1380}
1381
Ingo Molnare309b412008-05-12 21:20:51 +02001382static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad50842008-05-12 21:20:48 +02001383{
Lai Jiangshanee000b72009-03-24 13:38:06 +08001384 rec->freelist = ftrace_free_records;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001385 ftrace_free_records = rec;
1386 rec->flags |= FTRACE_FL_FREE;
1387}
1388
Ingo Molnare309b412008-05-12 21:20:51 +02001389static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001390{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001391 struct dyn_ftrace *rec;
1392
1393 /* First check for freed records */
1394 if (ftrace_free_records) {
1395 rec = ftrace_free_records;
1396
Steven Rostedt37ad50842008-05-12 21:20:48 +02001397 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt6912896e2008-10-23 09:33:03 -04001398 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad50842008-05-12 21:20:48 +02001399 ftrace_free_records = NULL;
1400 return NULL;
1401 }
1402
Lai Jiangshanee000b72009-03-24 13:38:06 +08001403 ftrace_free_records = rec->freelist;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001404 memset(rec, 0, sizeof(*rec));
1405 return rec;
1406 }
1407
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001408 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001409 if (!ftrace_pages->next) {
1410 /* allocate another page */
1411 ftrace_pages->next =
1412 (void *)get_zeroed_page(GFP_KERNEL);
1413 if (!ftrace_pages->next)
1414 return NULL;
1415 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001416 ftrace_pages = ftrace_pages->next;
1417 }
1418
1419 return &ftrace_pages->records[ftrace_pages->index++];
1420}
1421
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001422static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001423ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001424{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001425 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001426
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001427 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001428 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001429
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001430 rec = ftrace_alloc_dyn_node(ip);
1431 if (!rec)
1432 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001433
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001434 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001435 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001436 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001437
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001438 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001439}
1440
Steven Rostedt05736a42008-09-22 14:55:47 -07001441static void print_ip_ins(const char *fmt, unsigned char *p)
1442{
1443 int i;
1444
1445 printk(KERN_CONT "%s", fmt);
1446
1447 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1448 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1449}
1450
Steven Rostedt31e88902008-11-14 16:21:19 -08001451static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001452{
1453 switch (failed) {
1454 case -EFAULT:
1455 FTRACE_WARN_ON_ONCE(1);
1456 pr_info("ftrace faulted on modifying ");
1457 print_ip_sym(ip);
1458 break;
1459 case -EINVAL:
1460 FTRACE_WARN_ON_ONCE(1);
1461 pr_info("ftrace failed to modify ");
1462 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001463 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001464 printk(KERN_CONT "\n");
1465 break;
1466 case -EPERM:
1467 FTRACE_WARN_ON_ONCE(1);
1468 pr_info("ftrace faulted on writing ");
1469 print_ip_sym(ip);
1470 break;
1471 default:
1472 FTRACE_WARN_ON_ONCE(1);
1473 pr_info("ftrace faulted on unknown error ");
1474 print_ip_sym(ip);
1475 }
1476}
1477
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001478
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001479/* Return 1 if the address range is reserved for ftrace */
1480int ftrace_text_reserved(void *start, void *end)
1481{
1482 struct dyn_ftrace *rec;
1483 struct ftrace_page *pg;
1484
1485 do_for_each_ftrace_rec(pg, rec) {
1486 if (rec->ip <= (unsigned long)end &&
1487 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1488 return 1;
1489 } while_for_each_ftrace_rec();
1490 return 0;
1491}
1492
1493
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301494static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001495__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001496{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001497 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001498 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001499
Shaohua Lif0001202009-01-09 11:29:42 +08001500 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001501
Steven Rostedt982c3502008-11-15 16:31:41 -05001502 /*
Steven Rostedted926f92011-05-03 13:25:24 -04001503 * If we are enabling tracing:
Steven Rostedt982c3502008-11-15 16:31:41 -05001504 *
Steven Rostedted926f92011-05-03 13:25:24 -04001505 * If the record has a ref count, then we need to enable it
1506 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001507 *
Steven Rostedted926f92011-05-03 13:25:24 -04001508 * Otherwise we make sure its disabled.
1509 *
1510 * If we are disabling tracing, then disable all records that
1511 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001512 */
Steven Rostedted926f92011-05-03 13:25:24 -04001513 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1514 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001515
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001516 /* If the state of this record hasn't changed, then do nothing */
1517 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1518 return 0;
1519
1520 if (flag) {
1521 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001522 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001523 }
1524
1525 rec->flags &= ~FTRACE_FL_ENABLED;
1526 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001527}
1528
1529static void ftrace_replace_code(int enable)
1530{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001531 struct dyn_ftrace *rec;
1532 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001533 int failed;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001534
Steven Rostedt45a4a232011-04-21 23:16:46 -04001535 if (unlikely(ftrace_disabled))
1536 return;
1537
Steven Rostedt265c8312009-02-13 12:43:56 -05001538 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001539 /* Skip over free records */
1540 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05001541 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001542
Steven Rostedt265c8312009-02-13 12:43:56 -05001543 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001544 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001545 ftrace_bug(failed, rec->ip);
1546 /* Stop processing */
1547 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001548 }
1549 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001550}
1551
Ingo Molnare309b412008-05-12 21:20:51 +02001552static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001553ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001554{
1555 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001556 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001557
1558 ip = rec->ip;
1559
Steven Rostedt45a4a232011-04-21 23:16:46 -04001560 if (unlikely(ftrace_disabled))
1561 return 0;
1562
Shaohua Li25aac9d2009-01-09 11:29:40 +08001563 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001564 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001565 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301566 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001567 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301568 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001569}
1570
Steven Rostedt000ab692009-02-17 13:35:06 -05001571/*
1572 * archs can override this function if they must do something
1573 * before the modifying code is performed.
1574 */
1575int __weak ftrace_arch_code_modify_prepare(void)
1576{
1577 return 0;
1578}
1579
1580/*
1581 * archs can override this function if they must do something
1582 * after the modifying code is performed.
1583 */
1584int __weak ftrace_arch_code_modify_post_process(void)
1585{
1586 return 0;
1587}
1588
Ingo Molnare309b412008-05-12 21:20:51 +02001589static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001590{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001591 int *command = data;
1592
Steven Rostedta3583242008-11-11 15:01:42 -05001593 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001594 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001595 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001596 ftrace_replace_code(0);
1597
1598 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1599 ftrace_update_ftrace_func(ftrace_trace_function);
1600
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001601 if (*command & FTRACE_START_FUNC_RET)
1602 ftrace_enable_ftrace_graph_caller();
1603 else if (*command & FTRACE_STOP_FUNC_RET)
1604 ftrace_disable_ftrace_graph_caller();
1605
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001606 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001607}
1608
Ingo Molnare309b412008-05-12 21:20:51 +02001609static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001610{
Steven Rostedt000ab692009-02-17 13:35:06 -05001611 int ret;
1612
1613 ret = ftrace_arch_code_modify_prepare();
1614 FTRACE_WARN_ON(ret);
1615 if (ret)
1616 return;
1617
Rusty Russell784e2d72008-07-28 12:16:31 -05001618 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001619
1620 ret = ftrace_arch_code_modify_post_process();
1621 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001622}
1623
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001624static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001625static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001626static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001627
1628static void ftrace_startup_enable(int command)
1629{
1630 if (saved_ftrace_func != ftrace_trace_function) {
1631 saved_ftrace_func = ftrace_trace_function;
1632 command |= FTRACE_UPDATE_TRACE_FUNC;
1633 }
1634
1635 if (!command || !ftrace_enabled)
1636 return;
1637
1638 ftrace_run_update_code(command);
1639}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001640
Steven Rostedta1cd6172011-05-23 15:24:25 -04001641static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001642{
Steven Rostedtb8489142011-05-04 09:27:52 -04001643 bool hash_enable = true;
1644
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001645 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001646 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001647
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001648 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001649 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001650
Steven Rostedtb8489142011-05-04 09:27:52 -04001651 /* ops marked global share the filter hashes */
1652 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1653 ops = &global_ops;
1654 /* Don't update hash if global is already set */
1655 if (global_start_up)
1656 hash_enable = false;
1657 global_start_up++;
1658 }
1659
Steven Rostedted926f92011-05-03 13:25:24 -04001660 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001661 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001662 ftrace_hash_rec_enable(ops, 1);
1663
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001664 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001665
1666 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001667}
1668
Steven Rostedtbd69c302011-05-03 21:55:54 -04001669static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001670{
Steven Rostedtb8489142011-05-04 09:27:52 -04001671 bool hash_disable = true;
1672
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001673 if (unlikely(ftrace_disabled))
1674 return;
1675
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001676 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001677 /*
1678 * Just warn in case of unbalance, no need to kill ftrace, it's not
1679 * critical but the ftrace_call callers may be never nopped again after
1680 * further ftrace uses.
1681 */
1682 WARN_ON_ONCE(ftrace_start_up < 0);
1683
Steven Rostedtb8489142011-05-04 09:27:52 -04001684 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1685 ops = &global_ops;
1686 global_start_up--;
1687 WARN_ON_ONCE(global_start_up < 0);
1688 /* Don't update hash if global still has users */
1689 if (global_start_up) {
1690 WARN_ON_ONCE(!ftrace_start_up);
1691 hash_disable = false;
1692 }
1693 }
1694
1695 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04001696 ftrace_hash_rec_disable(ops, 1);
1697
Steven Rostedtb8489142011-05-04 09:27:52 -04001698 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04001699 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001700
1701 if (!ftrace_start_up)
1702 command |= FTRACE_DISABLE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001703
1704 if (saved_ftrace_func != ftrace_trace_function) {
1705 saved_ftrace_func = ftrace_trace_function;
1706 command |= FTRACE_UPDATE_TRACE_FUNC;
1707 }
1708
1709 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001710 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001711
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001712 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001713}
1714
Ingo Molnare309b412008-05-12 21:20:51 +02001715static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001716{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001717 if (unlikely(ftrace_disabled))
1718 return;
1719
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001720 /* Force update next time */
1721 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001722 /* ftrace_start_up is true if we want ftrace running */
1723 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001724 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001725}
1726
Ingo Molnare309b412008-05-12 21:20:51 +02001727static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001728{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001729 if (unlikely(ftrace_disabled))
1730 return;
1731
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001732 /* ftrace_start_up is true if ftrace is running */
1733 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001734 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001735}
1736
Steven Rostedt3d083392008-05-12 21:20:42 +02001737static cycle_t ftrace_update_time;
1738static unsigned long ftrace_update_cnt;
1739unsigned long ftrace_update_tot_cnt;
1740
Steven Rostedt31e88902008-11-14 16:21:19 -08001741static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001742{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001743 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301744 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001745
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001746 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001747 ftrace_update_cnt = 0;
1748
Lai Jiangshane94142a2009-03-13 17:51:27 +08001749 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301750
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001751 /* If something went wrong, bail without enabling anything */
1752 if (unlikely(ftrace_disabled))
1753 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001754
Lai Jiangshane94142a2009-03-13 17:51:27 +08001755 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001756 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001757 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301758
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001759 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001760 * Do the initial record conversion from mcount jump
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001761 * to the NOP instructions.
1762 */
1763 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001764 ftrace_free_rec(p);
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001765 /* Game over */
1766 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001767 }
1768
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001769 ftrace_update_cnt++;
1770
1771 /*
1772 * If the tracing is enabled, go ahead and enable the record.
1773 *
1774 * The reason not to enable the record immediatelly is the
1775 * inherent check of ftrace_make_nop/ftrace_make_call for
1776 * correct previous instructions. Making first the NOP
1777 * conversion puts the module to the correct state, thus
1778 * passing the ftrace_make_call check.
1779 */
1780 if (ftrace_start_up) {
1781 int failed = __ftrace_replace_code(p, 1);
1782 if (failed) {
1783 ftrace_bug(failed, p->ip);
1784 ftrace_free_rec(p);
1785 }
1786 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001787 }
1788
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001789 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001790 ftrace_update_time = stop - start;
1791 ftrace_update_tot_cnt += ftrace_update_cnt;
1792
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001793 return 0;
1794}
1795
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001796static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001797{
1798 struct ftrace_page *pg;
1799 int cnt;
1800 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001801
1802 /* allocate a few pages */
1803 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1804 if (!ftrace_pages_start)
1805 return -1;
1806
1807 /*
1808 * Allocate a few more pages.
1809 *
1810 * TODO: have some parser search vmlinux before
1811 * final linking to find all calls to ftrace.
1812 * Then we can:
1813 * a) know how many pages to allocate.
1814 * and/or
1815 * b) set up the table then.
1816 *
1817 * The dynamic code is still necessary for
1818 * modules.
1819 */
1820
1821 pg = ftrace_pages = ftrace_pages_start;
1822
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001823 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001824 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001825 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001826
1827 for (i = 0; i < cnt; i++) {
1828 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1829
1830 /* If we fail, we'll try later anyway */
1831 if (!pg->next)
1832 break;
1833
1834 pg = pg->next;
1835 }
1836
1837 return 0;
1838}
1839
Steven Rostedt5072c592008-05-12 21:20:43 +02001840enum {
1841 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001842 FTRACE_ITER_NOTRACE = (1 << 1),
Steven Rostedt3499e462011-04-21 22:59:12 -04001843 FTRACE_ITER_PRINTALL = (1 << 2),
1844 FTRACE_ITER_HASH = (1 << 3),
Steven Rostedt647bcd02011-05-03 14:39:21 -04001845 FTRACE_ITER_ENABLED = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02001846};
1847
1848#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1849
1850struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001851 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001852 loff_t func_pos;
1853 struct ftrace_page *pg;
1854 struct dyn_ftrace *func;
1855 struct ftrace_func_probe *probe;
1856 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001857 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001858 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001859 int hidx;
1860 int idx;
1861 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02001862};
1863
Ingo Molnare309b412008-05-12 21:20:51 +02001864static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001865t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001866{
1867 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001868 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001869 struct hlist_head *hhd;
1870
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001871 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001872 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001873
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001874 if (iter->probe)
1875 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001876 retry:
1877 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1878 return NULL;
1879
1880 hhd = &ftrace_func_hash[iter->hidx];
1881
1882 if (hlist_empty(hhd)) {
1883 iter->hidx++;
1884 hnd = NULL;
1885 goto retry;
1886 }
1887
1888 if (!hnd)
1889 hnd = hhd->first;
1890 else {
1891 hnd = hnd->next;
1892 if (!hnd) {
1893 iter->hidx++;
1894 goto retry;
1895 }
1896 }
1897
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001898 if (WARN_ON_ONCE(!hnd))
1899 return NULL;
1900
1901 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1902
1903 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001904}
1905
1906static void *t_hash_start(struct seq_file *m, loff_t *pos)
1907{
1908 struct ftrace_iterator *iter = m->private;
1909 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001910 loff_t l;
1911
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001912 if (iter->func_pos > *pos)
1913 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001914
Li Zefand82d6242009-06-24 09:54:54 +08001915 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001916 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001917 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08001918 if (!p)
1919 break;
1920 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001921 if (!p)
1922 return NULL;
1923
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001924 /* Only set this if we have an item */
1925 iter->flags |= FTRACE_ITER_HASH;
1926
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001927 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001928}
1929
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001930static int
1931t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001932{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001933 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001934
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001935 rec = iter->probe;
1936 if (WARN_ON_ONCE(!rec))
1937 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001938
Steven Rostedt809dcf22009-02-16 23:06:01 -05001939 if (rec->ops->print)
1940 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1941
Steven Rostedtb375a112009-09-17 00:05:58 -04001942 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001943
1944 if (rec->data)
1945 seq_printf(m, ":%p", rec->data);
1946 seq_putc(m, '\n');
1947
1948 return 0;
1949}
1950
1951static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001952t_next(struct seq_file *m, void *v, loff_t *pos)
1953{
1954 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04001955 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02001956 struct dyn_ftrace *rec = NULL;
1957
Steven Rostedt45a4a232011-04-21 23:16:46 -04001958 if (unlikely(ftrace_disabled))
1959 return NULL;
1960
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001961 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001962 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001963
Steven Rostedt5072c592008-05-12 21:20:43 +02001964 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01001965 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02001966
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001967 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001968 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001969
Steven Rostedt5072c592008-05-12 21:20:43 +02001970 retry:
1971 if (iter->idx >= iter->pg->index) {
1972 if (iter->pg->next) {
1973 iter->pg = iter->pg->next;
1974 iter->idx = 0;
1975 goto retry;
1976 }
1977 } else {
1978 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001979 if ((rec->flags & FTRACE_FL_FREE) ||
1980
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001981 ((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04001982 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001983
Steven Rostedt41c52c02008-05-22 11:46:33 -04001984 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04001985 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
1986
1987 ((iter->flags & FTRACE_ITER_ENABLED) &&
1988 !(rec->flags & ~FTRACE_FL_MASK))) {
1989
Steven Rostedt5072c592008-05-12 21:20:43 +02001990 rec = NULL;
1991 goto retry;
1992 }
1993 }
1994
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001995 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001996 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001997
1998 iter->func = rec;
1999
2000 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002001}
2002
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002003static void reset_iter_read(struct ftrace_iterator *iter)
2004{
2005 iter->pos = 0;
2006 iter->func_pos = 0;
2007 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002008}
2009
2010static void *t_start(struct seq_file *m, loff_t *pos)
2011{
2012 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002013 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002014 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002015 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002016
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002017 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002018
2019 if (unlikely(ftrace_disabled))
2020 return NULL;
2021
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002022 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002023 * If an lseek was done, then reset and start from beginning.
2024 */
2025 if (*pos < iter->pos)
2026 reset_iter_read(iter);
2027
2028 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002029 * For set_ftrace_filter reading, if we have the filter
2030 * off, we can short cut and just print out that all
2031 * functions are enabled.
2032 */
Steven Rostedtf45948e2011-05-02 12:29:25 -04002033 if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002034 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002035 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002036 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002037 /* reset in case of seek/pread */
2038 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002039 return iter;
2040 }
2041
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002042 if (iter->flags & FTRACE_ITER_HASH)
2043 return t_hash_start(m, pos);
2044
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002045 /*
2046 * Unfortunately, we need to restart at ftrace_pages_start
2047 * every time we let go of the ftrace_mutex. This is because
2048 * those pointers can change without the lock.
2049 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002050 iter->pg = ftrace_pages_start;
2051 iter->idx = 0;
2052 for (l = 0; l <= *pos; ) {
2053 p = t_next(m, p, &l);
2054 if (!p)
2055 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002056 }
walimis5821e1b2008-11-15 15:19:06 +08002057
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002058 if (!p) {
2059 if (iter->flags & FTRACE_ITER_FILTER)
2060 return t_hash_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002061
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002062 return NULL;
2063 }
2064
2065 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002066}
2067
2068static void t_stop(struct seq_file *m, void *p)
2069{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002070 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002071}
2072
2073static int t_show(struct seq_file *m, void *v)
2074{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002075 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002076 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002077
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002078 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002079 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002080
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002081 if (iter->flags & FTRACE_ITER_PRINTALL) {
2082 seq_printf(m, "#### all functions enabled ####\n");
2083 return 0;
2084 }
2085
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002086 rec = iter->func;
2087
Steven Rostedt5072c592008-05-12 21:20:43 +02002088 if (!rec)
2089 return 0;
2090
Steven Rostedt647bcd02011-05-03 14:39:21 -04002091 seq_printf(m, "%ps", (void *)rec->ip);
2092 if (iter->flags & FTRACE_ITER_ENABLED)
2093 seq_printf(m, " (%ld)",
2094 rec->flags & ~FTRACE_FL_MASK);
2095 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002096
2097 return 0;
2098}
2099
James Morris88e9d342009-09-22 16:43:43 -07002100static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002101 .start = t_start,
2102 .next = t_next,
2103 .stop = t_stop,
2104 .show = t_show,
2105};
2106
Ingo Molnare309b412008-05-12 21:20:51 +02002107static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002108ftrace_avail_open(struct inode *inode, struct file *file)
2109{
2110 struct ftrace_iterator *iter;
2111 int ret;
2112
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002113 if (unlikely(ftrace_disabled))
2114 return -ENODEV;
2115
Steven Rostedt5072c592008-05-12 21:20:43 +02002116 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2117 if (!iter)
2118 return -ENOMEM;
2119
2120 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002121
2122 ret = seq_open(file, &show_ftrace_seq_ops);
2123 if (!ret) {
2124 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002125
Steven Rostedt5072c592008-05-12 21:20:43 +02002126 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002127 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002128 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002129 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002130
2131 return ret;
2132}
2133
Steven Rostedt647bcd02011-05-03 14:39:21 -04002134static int
2135ftrace_enabled_open(struct inode *inode, struct file *file)
2136{
2137 struct ftrace_iterator *iter;
2138 int ret;
2139
2140 if (unlikely(ftrace_disabled))
2141 return -ENODEV;
2142
2143 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2144 if (!iter)
2145 return -ENOMEM;
2146
2147 iter->pg = ftrace_pages_start;
2148 iter->flags = FTRACE_ITER_ENABLED;
2149
2150 ret = seq_open(file, &show_ftrace_seq_ops);
2151 if (!ret) {
2152 struct seq_file *m = file->private_data;
2153
2154 m->private = iter;
2155 } else {
2156 kfree(iter);
2157 }
2158
2159 return ret;
2160}
2161
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002162static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002163{
Steven Rostedt52baf112009-02-14 01:15:39 -05002164 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002165 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002166 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002167}
2168
Ingo Molnare309b412008-05-12 21:20:51 +02002169static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002170ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002171 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002172{
2173 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002174 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002175 int ret = 0;
2176
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002177 if (unlikely(ftrace_disabled))
2178 return -ENODEV;
2179
Steven Rostedt5072c592008-05-12 21:20:43 +02002180 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2181 if (!iter)
2182 return -ENOMEM;
2183
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002184 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2185 kfree(iter);
2186 return -ENOMEM;
2187 }
2188
Steven Rostedtf45948e2011-05-02 12:29:25 -04002189 if (flag & FTRACE_ITER_NOTRACE)
2190 hash = ops->notrace_hash;
2191 else
2192 hash = ops->filter_hash;
2193
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002194 iter->ops = ops;
2195 iter->flags = flag;
2196
2197 if (file->f_mode & FMODE_WRITE) {
2198 mutex_lock(&ftrace_lock);
2199 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2200 mutex_unlock(&ftrace_lock);
2201
2202 if (!iter->hash) {
2203 trace_parser_put(&iter->parser);
2204 kfree(iter);
2205 return -ENOMEM;
2206 }
2207 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002208
Steven Rostedt41c52c02008-05-22 11:46:33 -04002209 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002210
Steven Rostedt5072c592008-05-12 21:20:43 +02002211 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002212 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002213 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002214
2215 if (file->f_mode & FMODE_READ) {
2216 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002217
2218 ret = seq_open(file, &show_ftrace_seq_ops);
2219 if (!ret) {
2220 struct seq_file *m = file->private_data;
2221 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002222 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002223 /* Failed */
2224 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002225 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002226 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002227 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002228 } else
2229 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002230 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002231
2232 return ret;
2233}
2234
Steven Rostedt41c52c02008-05-22 11:46:33 -04002235static int
2236ftrace_filter_open(struct inode *inode, struct file *file)
2237{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002238 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002239 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002240}
2241
2242static int
2243ftrace_notrace_open(struct inode *inode, struct file *file)
2244{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002245 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002246 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002247}
2248
Ingo Molnare309b412008-05-12 21:20:51 +02002249static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002250ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02002251{
2252 loff_t ret;
2253
2254 if (file->f_mode & FMODE_READ)
2255 ret = seq_lseek(file, offset, origin);
2256 else
2257 file->f_pos = ret = 1;
2258
2259 return ret;
2260}
2261
Steven Rostedt64e7c442009-02-13 17:08:48 -05002262static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002263{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002264 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002265 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002266
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002267 switch (type) {
2268 case MATCH_FULL:
2269 if (strcmp(str, regex) == 0)
2270 matched = 1;
2271 break;
2272 case MATCH_FRONT_ONLY:
2273 if (strncmp(str, regex, len) == 0)
2274 matched = 1;
2275 break;
2276 case MATCH_MIDDLE_ONLY:
2277 if (strstr(str, regex))
2278 matched = 1;
2279 break;
2280 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002281 slen = strlen(str);
2282 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002283 matched = 1;
2284 break;
2285 }
2286
2287 return matched;
2288}
2289
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002290static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002291enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002292{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002293 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002294 int ret = 0;
2295
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002296 entry = ftrace_lookup_ip(hash, rec->ip);
2297 if (not) {
2298 /* Do nothing if it doesn't exist */
2299 if (!entry)
2300 return 0;
2301
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002302 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002303 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002304 /* Do nothing if it exists */
2305 if (entry)
2306 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002307
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002308 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002309 }
2310 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002311}
2312
Steven Rostedt64e7c442009-02-13 17:08:48 -05002313static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002314ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2315 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002316{
2317 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002318 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002319
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002320 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2321
2322 if (mod) {
2323 /* module lookup requires matching the module */
2324 if (!modname || strcmp(modname, mod))
2325 return 0;
2326
2327 /* blank search means to match all funcs in the mod */
2328 if (!len)
2329 return 1;
2330 }
2331
Steven Rostedt64e7c442009-02-13 17:08:48 -05002332 return ftrace_match(str, regex, len, type);
2333}
2334
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002335static int
2336match_records(struct ftrace_hash *hash, char *buff,
2337 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002338{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002339 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002340 struct ftrace_page *pg;
2341 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002342 int type = MATCH_FULL;
2343 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002344 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002345 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002346
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002347 if (len) {
2348 type = filter_parse_regex(buff, len, &search, &not);
2349 search_len = strlen(search);
2350 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002351
Steven Rostedt52baf112009-02-14 01:15:39 -05002352 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002353
2354 if (unlikely(ftrace_disabled))
2355 goto out_unlock;
2356
Steven Rostedt265c8312009-02-13 12:43:56 -05002357 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02002358
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002359 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002360 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002361 if (ret < 0) {
2362 found = ret;
2363 goto out_unlock;
2364 }
Li Zefan311d16d2009-12-08 11:15:11 +08002365 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002366 }
2367 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002368 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002369 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002370
2371 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002372}
2373
Steven Rostedt64e7c442009-02-13 17:08:48 -05002374static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002375ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002376{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002377 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002378}
2379
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002380static int
2381ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002382{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002383 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002384
Steven Rostedt64e7c442009-02-13 17:08:48 -05002385 /* blank or '*' mean the same */
2386 if (strcmp(buff, "*") == 0)
2387 buff[0] = 0;
2388
2389 /* handle the case of 'dont filter this module' */
2390 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2391 buff[0] = 0;
2392 not = 1;
2393 }
2394
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002395 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002396}
2397
Steven Rostedtf6180772009-02-14 00:40:25 -05002398/*
2399 * We register the module command as a template to show others how
2400 * to register the a command as well.
2401 */
2402
2403static int
2404ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
2405{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002406 struct ftrace_ops *ops = &global_ops;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002407 struct ftrace_hash *hash;
Steven Rostedtf6180772009-02-14 00:40:25 -05002408 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002409 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002410
2411 /*
2412 * cmd == 'mod' because we only registered this func
2413 * for the 'mod' ftrace_func_command.
2414 * But if you register one func with multiple commands,
2415 * you can tell which command was used by the cmd
2416 * parameter.
2417 */
2418
2419 /* we must have a module name */
2420 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002421 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002422
2423 mod = strsep(&param, ":");
2424 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002425 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002426
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002427 if (enable)
Steven Rostedtf45948e2011-05-02 12:29:25 -04002428 hash = ops->filter_hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002429 else
Steven Rostedtf45948e2011-05-02 12:29:25 -04002430 hash = ops->notrace_hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002431
2432 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002433 if (!ret)
2434 ret = -EINVAL;
2435 if (ret < 0)
2436 return ret;
2437
2438 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002439}
2440
2441static struct ftrace_func_command ftrace_mod_cmd = {
2442 .name = "mod",
2443 .func = ftrace_mod_callback,
2444};
2445
2446static int __init ftrace_mod_cmd_init(void)
2447{
2448 return register_ftrace_command(&ftrace_mod_cmd);
2449}
2450device_initcall(ftrace_mod_cmd_init);
2451
Steven Rostedt59df055f2009-02-14 15:29:06 -05002452static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002453function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002454{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002455 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002456 struct hlist_head *hhd;
2457 struct hlist_node *n;
2458 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002459
2460 key = hash_long(ip, FTRACE_HASH_BITS);
2461
2462 hhd = &ftrace_func_hash[key];
2463
2464 if (hlist_empty(hhd))
2465 return;
2466
2467 /*
2468 * Disable preemption for these calls to prevent a RCU grace
2469 * period. This syncs the hash iteration and freeing of items
2470 * on the hash. rcu_read_lock is too dangerous here.
2471 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002472 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002473 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2474 if (entry->ip == ip)
2475 entry->ops->func(ip, parent_ip, &entry->data);
2476 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002477 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002478}
2479
Steven Rostedtb6887d72009-02-17 12:32:04 -05002480static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002481{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002482 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002483};
2484
Steven Rostedtb6887d72009-02-17 12:32:04 -05002485static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002486
Steven Rostedtb6887d72009-02-17 12:32:04 -05002487static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002488{
Steven Rostedtb8489142011-05-04 09:27:52 -04002489 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002490 int i;
2491
Steven Rostedtb6887d72009-02-17 12:32:04 -05002492 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002493 return;
2494
2495 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2496 struct hlist_head *hhd = &ftrace_func_hash[i];
2497 if (hhd->first)
2498 break;
2499 }
2500 /* Nothing registered? */
2501 if (i == FTRACE_FUNC_HASHSIZE)
2502 return;
2503
Steven Rostedtb8489142011-05-04 09:27:52 -04002504 ret = __register_ftrace_function(&trace_probe_ops);
2505 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002506 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002507
Steven Rostedtb6887d72009-02-17 12:32:04 -05002508 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002509}
2510
Steven Rostedtb6887d72009-02-17 12:32:04 -05002511static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002512{
Steven Rostedtb8489142011-05-04 09:27:52 -04002513 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002514 int i;
2515
Steven Rostedtb6887d72009-02-17 12:32:04 -05002516 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002517 return;
2518
2519 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2520 struct hlist_head *hhd = &ftrace_func_hash[i];
2521 if (hhd->first)
2522 return;
2523 }
2524
2525 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002526 ret = __unregister_ftrace_function(&trace_probe_ops);
2527 if (!ret)
2528 ftrace_shutdown(&trace_probe_ops, 0);
2529
Steven Rostedtb6887d72009-02-17 12:32:04 -05002530 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002531}
2532
2533
2534static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2535{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002536 struct ftrace_func_probe *entry =
2537 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002538
2539 if (entry->ops->free)
2540 entry->ops->free(&entry->data);
2541 kfree(entry);
2542}
2543
2544
2545int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002546register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002547 void *data)
2548{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002549 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002550 struct ftrace_page *pg;
2551 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002552 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002553 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002554 int count = 0;
2555 char *search;
2556
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002557 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002558 len = strlen(search);
2559
Steven Rostedtb6887d72009-02-17 12:32:04 -05002560 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002561 if (WARN_ON(not))
2562 return -EINVAL;
2563
2564 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002565
Steven Rostedt45a4a232011-04-21 23:16:46 -04002566 if (unlikely(ftrace_disabled))
2567 goto out_unlock;
2568
Steven Rostedt59df055f2009-02-14 15:29:06 -05002569 do_for_each_ftrace_rec(pg, rec) {
2570
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002571 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002572 continue;
2573
2574 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2575 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002576 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002577 if (!count)
2578 count = -ENOMEM;
2579 goto out_unlock;
2580 }
2581
2582 count++;
2583
2584 entry->data = data;
2585
2586 /*
2587 * The caller might want to do something special
2588 * for each function we find. We call the callback
2589 * to give the caller an opportunity to do so.
2590 */
2591 if (ops->callback) {
2592 if (ops->callback(rec->ip, &entry->data) < 0) {
2593 /* caller does not like this func */
2594 kfree(entry);
2595 continue;
2596 }
2597 }
2598
2599 entry->ops = ops;
2600 entry->ip = rec->ip;
2601
2602 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2603 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2604
2605 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002606 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002607
2608 out_unlock:
2609 mutex_unlock(&ftrace_lock);
2610
2611 return count;
2612}
2613
2614enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002615 PROBE_TEST_FUNC = 1,
2616 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002617};
2618
2619static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002620__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002621 void *data, int flags)
2622{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002623 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002624 struct hlist_node *n, *tmp;
2625 char str[KSYM_SYMBOL_LEN];
2626 int type = MATCH_FULL;
2627 int i, len = 0;
2628 char *search;
2629
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002630 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002631 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002632 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002633 int not;
2634
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002635 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002636 len = strlen(search);
2637
Steven Rostedtb6887d72009-02-17 12:32:04 -05002638 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002639 if (WARN_ON(not))
2640 return;
2641 }
2642
2643 mutex_lock(&ftrace_lock);
2644 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2645 struct hlist_head *hhd = &ftrace_func_hash[i];
2646
2647 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2648
2649 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002650 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002651 continue;
2652
Steven Rostedtb6887d72009-02-17 12:32:04 -05002653 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002654 continue;
2655
2656 /* do this last, since it is the most expensive */
2657 if (glob) {
2658 kallsyms_lookup(entry->ip, NULL, NULL,
2659 NULL, str);
2660 if (!ftrace_match(str, glob, len, type))
2661 continue;
2662 }
2663
2664 hlist_del(&entry->node);
2665 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2666 }
2667 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002668 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002669 mutex_unlock(&ftrace_lock);
2670}
2671
2672void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002673unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002674 void *data)
2675{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002676 __unregister_ftrace_function_probe(glob, ops, data,
2677 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002678}
2679
2680void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002681unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002682{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002683 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002684}
2685
Steven Rostedtb6887d72009-02-17 12:32:04 -05002686void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002687{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002688 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002689}
2690
Steven Rostedtf6180772009-02-14 00:40:25 -05002691static LIST_HEAD(ftrace_commands);
2692static DEFINE_MUTEX(ftrace_cmd_mutex);
2693
2694int register_ftrace_command(struct ftrace_func_command *cmd)
2695{
2696 struct ftrace_func_command *p;
2697 int ret = 0;
2698
2699 mutex_lock(&ftrace_cmd_mutex);
2700 list_for_each_entry(p, &ftrace_commands, list) {
2701 if (strcmp(cmd->name, p->name) == 0) {
2702 ret = -EBUSY;
2703 goto out_unlock;
2704 }
2705 }
2706 list_add(&cmd->list, &ftrace_commands);
2707 out_unlock:
2708 mutex_unlock(&ftrace_cmd_mutex);
2709
2710 return ret;
2711}
2712
2713int unregister_ftrace_command(struct ftrace_func_command *cmd)
2714{
2715 struct ftrace_func_command *p, *n;
2716 int ret = -ENODEV;
2717
2718 mutex_lock(&ftrace_cmd_mutex);
2719 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2720 if (strcmp(cmd->name, p->name) == 0) {
2721 ret = 0;
2722 list_del_init(&p->list);
2723 goto out_unlock;
2724 }
2725 }
2726 out_unlock:
2727 mutex_unlock(&ftrace_cmd_mutex);
2728
2729 return ret;
2730}
2731
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002732static int ftrace_process_regex(struct ftrace_hash *hash,
2733 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002734{
Steven Rostedtf6180772009-02-14 00:40:25 -05002735 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002736 struct ftrace_func_command *p;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002737 int ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002738
2739 func = strsep(&next, ":");
2740
2741 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002742 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002743 if (!ret)
2744 ret = -EINVAL;
2745 if (ret < 0)
2746 return ret;
2747 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002748 }
2749
Steven Rostedtf6180772009-02-14 00:40:25 -05002750 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002751
2752 command = strsep(&next, ":");
2753
Steven Rostedtf6180772009-02-14 00:40:25 -05002754 mutex_lock(&ftrace_cmd_mutex);
2755 list_for_each_entry(p, &ftrace_commands, list) {
2756 if (strcmp(p->name, command) == 0) {
2757 ret = p->func(func, command, next, enable);
2758 goto out_unlock;
2759 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002760 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002761 out_unlock:
2762 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002763
Steven Rostedtf6180772009-02-14 00:40:25 -05002764 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002765}
2766
Ingo Molnare309b412008-05-12 21:20:51 +02002767static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002768ftrace_regex_write(struct file *file, const char __user *ubuf,
2769 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002770{
2771 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002772 struct trace_parser *parser;
2773 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002774
Li Zefan4ba79782009-09-22 13:52:20 +08002775 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002776 return 0;
2777
Steven Rostedt41c52c02008-05-22 11:46:33 -04002778 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002779
Steven Rostedt45a4a232011-04-21 23:16:46 -04002780 ret = -ENODEV;
2781 if (unlikely(ftrace_disabled))
2782 goto out_unlock;
2783
Steven Rostedt5072c592008-05-12 21:20:43 +02002784 if (file->f_mode & FMODE_READ) {
2785 struct seq_file *m = file->private_data;
2786 iter = m->private;
2787 } else
2788 iter = file->private_data;
2789
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002790 parser = &iter->parser;
2791 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002792
Li Zefan4ba79782009-09-22 13:52:20 +08002793 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002794 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002795 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002796 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002797 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002798 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002799 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002800 }
2801
Steven Rostedt5072c592008-05-12 21:20:43 +02002802 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002803out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002804 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002805
Steven Rostedt5072c592008-05-12 21:20:43 +02002806 return ret;
2807}
2808
Steven Rostedt41c52c02008-05-22 11:46:33 -04002809static ssize_t
2810ftrace_filter_write(struct file *file, const char __user *ubuf,
2811 size_t cnt, loff_t *ppos)
2812{
2813 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2814}
2815
2816static ssize_t
2817ftrace_notrace_write(struct file *file, const char __user *ubuf,
2818 size_t cnt, loff_t *ppos)
2819{
2820 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2821}
2822
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002823static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002824ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2825 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04002826{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002827 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002828 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002829 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002830
Steven Rostedt936e0742011-05-05 22:54:01 -04002831 /* All global ops uses the global ops filters */
2832 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
2833 ops = &global_ops;
2834
Steven Rostedt41c52c02008-05-22 11:46:33 -04002835 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002836 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002837
Steven Rostedtf45948e2011-05-02 12:29:25 -04002838 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002839 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002840 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002841 orig_hash = &ops->notrace_hash;
2842
2843 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
2844 if (!hash)
2845 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002846
Steven Rostedt41c52c02008-05-22 11:46:33 -04002847 mutex_lock(&ftrace_regex_lock);
2848 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002849 ftrace_filter_reset(hash);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002850 if (buf)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002851 ftrace_match_records(hash, buf, len);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002852
2853 mutex_lock(&ftrace_lock);
2854 ret = ftrace_hash_move(orig_hash, hash);
2855 mutex_unlock(&ftrace_lock);
2856
Steven Rostedt41c52c02008-05-22 11:46:33 -04002857 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002858
2859 free_ftrace_hash(hash);
2860 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002861}
2862
Steven Rostedt77a2b372008-05-12 21:20:45 +02002863/**
2864 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04002865 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02002866 * @buf - the string that holds the function filter text.
2867 * @len - the length of the string.
2868 * @reset - non zero to reset all filters before applying this filter.
2869 *
2870 * Filters denote which functions should be enabled when tracing is enabled.
2871 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2872 */
Steven Rostedt936e0742011-05-05 22:54:01 -04002873void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
2874 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002875{
Steven Rostedt936e0742011-05-05 22:54:01 -04002876 ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002877}
Steven Rostedt936e0742011-05-05 22:54:01 -04002878EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002879
Steven Rostedt41c52c02008-05-22 11:46:33 -04002880/**
2881 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04002882 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04002883 * @buf - the string that holds the function notrace text.
2884 * @len - the length of the string.
2885 * @reset - non zero to reset all filters before applying this filter.
2886 *
2887 * Notrace Filters denote which functions should not be enabled when tracing
2888 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2889 * for tracing.
2890 */
Steven Rostedt936e0742011-05-05 22:54:01 -04002891void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
2892 int len, int reset)
2893{
2894 ftrace_set_regex(ops, buf, len, reset, 0);
2895}
2896EXPORT_SYMBOL_GPL(ftrace_set_notrace);
2897/**
2898 * ftrace_set_filter - set a function to filter on in ftrace
2899 * @ops - the ops to set the filter with
2900 * @buf - the string that holds the function filter text.
2901 * @len - the length of the string.
2902 * @reset - non zero to reset all filters before applying this filter.
2903 *
2904 * Filters denote which functions should be enabled when tracing is enabled.
2905 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2906 */
2907void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
2908{
2909 ftrace_set_regex(&global_ops, buf, len, reset, 1);
2910}
2911EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
2912
2913/**
2914 * ftrace_set_notrace - set a function to not trace in ftrace
2915 * @ops - the ops to set the notrace filter with
2916 * @buf - the string that holds the function notrace text.
2917 * @len - the length of the string.
2918 * @reset - non zero to reset all filters before applying this filter.
2919 *
2920 * Notrace Filters denote which functions should not be enabled when tracing
2921 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2922 * for tracing.
2923 */
2924void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04002925{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002926 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002927}
Steven Rostedt936e0742011-05-05 22:54:01 -04002928EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002929
Steven Rostedt2af15d62009-05-28 13:37:24 -04002930/*
2931 * command line interface to allow users to set filters on boot up.
2932 */
2933#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2934static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2935static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2936
2937static int __init set_ftrace_notrace(char *str)
2938{
2939 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2940 return 1;
2941}
2942__setup("ftrace_notrace=", set_ftrace_notrace);
2943
2944static int __init set_ftrace_filter(char *str)
2945{
2946 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2947 return 1;
2948}
2949__setup("ftrace_filter=", set_ftrace_filter);
2950
Stefan Assmann369bc182009-10-12 22:17:21 +02002951#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002952static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002953static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2954
Stefan Assmann369bc182009-10-12 22:17:21 +02002955static int __init set_graph_function(char *str)
2956{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002957 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002958 return 1;
2959}
2960__setup("ftrace_graph_filter=", set_graph_function);
2961
2962static void __init set_ftrace_early_graph(char *buf)
2963{
2964 int ret;
2965 char *func;
2966
2967 while (buf) {
2968 func = strsep(&buf, ",");
2969 /* we allow only one expression at a time */
2970 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2971 func);
2972 if (ret)
2973 printk(KERN_DEBUG "ftrace: function %s not "
2974 "traceable\n", func);
2975 }
2976}
2977#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2978
Steven Rostedtf45948e2011-05-02 12:29:25 -04002979static void __init
2980set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04002981{
2982 char *func;
2983
2984 while (buf) {
2985 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04002986 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04002987 }
2988}
2989
2990static void __init set_ftrace_early_filters(void)
2991{
2992 if (ftrace_filter_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04002993 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04002994 if (ftrace_notrace_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04002995 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002996#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2997 if (ftrace_graph_buf[0])
2998 set_ftrace_early_graph(ftrace_graph_buf);
2999#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003000}
3001
Ingo Molnare309b412008-05-12 21:20:51 +02003002static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003003ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003004{
3005 struct seq_file *m = (struct seq_file *)file->private_data;
3006 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003007 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003008 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003009 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003010 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003011
Steven Rostedt41c52c02008-05-22 11:46:33 -04003012 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003013 if (file->f_mode & FMODE_READ) {
3014 iter = m->private;
3015
3016 seq_release(inode, file);
3017 } else
3018 iter = file->private_data;
3019
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003020 parser = &iter->parser;
3021 if (trace_parser_loaded(parser)) {
3022 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003023 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003024 }
3025
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003026 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003027
Steven Rostedt058e2972011-04-29 22:35:33 -04003028 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003029 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3030
3031 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003032 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003033 else
3034 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003035
Steven Rostedt058e2972011-04-29 22:35:33 -04003036 mutex_lock(&ftrace_lock);
Steven Rostedted926f92011-05-03 13:25:24 -04003037 /*
3038 * Remove the current set, update the hash and add
3039 * them back.
3040 */
3041 ftrace_hash_rec_disable(iter->ops, filter_hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003042 ret = ftrace_hash_move(orig_hash, iter->hash);
Steven Rostedted926f92011-05-03 13:25:24 -04003043 if (!ret) {
3044 ftrace_hash_rec_enable(iter->ops, filter_hash);
3045 if (iter->ops->flags & FTRACE_OPS_FL_ENABLED
3046 && ftrace_enabled)
3047 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
3048 }
Steven Rostedt058e2972011-04-29 22:35:33 -04003049 mutex_unlock(&ftrace_lock);
3050 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003051 free_ftrace_hash(iter->hash);
3052 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003053
Steven Rostedt41c52c02008-05-22 11:46:33 -04003054 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003055 return 0;
3056}
3057
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003058static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003059 .open = ftrace_avail_open,
3060 .read = seq_read,
3061 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003062 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003063};
3064
Steven Rostedt647bcd02011-05-03 14:39:21 -04003065static const struct file_operations ftrace_enabled_fops = {
3066 .open = ftrace_enabled_open,
3067 .read = seq_read,
3068 .llseek = seq_lseek,
3069 .release = seq_release_private,
3070};
3071
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003072static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003073 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003074 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003075 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003076 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003077 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003078};
3079
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003080static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003081 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003082 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003083 .write = ftrace_notrace_write,
3084 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003085 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003086};
3087
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003088#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3089
3090static DEFINE_MUTEX(graph_lock);
3091
3092int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003093int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003094unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3095
3096static void *
Li Zefan85951842009-06-24 09:54:00 +08003097__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003098{
Li Zefan85951842009-06-24 09:54:00 +08003099 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003100 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003101 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003102}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003103
Li Zefan85951842009-06-24 09:54:00 +08003104static void *
3105g_next(struct seq_file *m, void *v, loff_t *pos)
3106{
3107 (*pos)++;
3108 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003109}
3110
3111static void *g_start(struct seq_file *m, loff_t *pos)
3112{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003113 mutex_lock(&graph_lock);
3114
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003115 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003116 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003117 return (void *)1;
3118
Li Zefan85951842009-06-24 09:54:00 +08003119 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003120}
3121
3122static void g_stop(struct seq_file *m, void *p)
3123{
3124 mutex_unlock(&graph_lock);
3125}
3126
3127static int g_show(struct seq_file *m, void *v)
3128{
3129 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003130
3131 if (!ptr)
3132 return 0;
3133
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003134 if (ptr == (unsigned long *)1) {
3135 seq_printf(m, "#### all functions enabled ####\n");
3136 return 0;
3137 }
3138
Steven Rostedtb375a112009-09-17 00:05:58 -04003139 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003140
3141 return 0;
3142}
3143
James Morris88e9d342009-09-22 16:43:43 -07003144static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003145 .start = g_start,
3146 .next = g_next,
3147 .stop = g_stop,
3148 .show = g_show,
3149};
3150
3151static int
3152ftrace_graph_open(struct inode *inode, struct file *file)
3153{
3154 int ret = 0;
3155
3156 if (unlikely(ftrace_disabled))
3157 return -ENODEV;
3158
3159 mutex_lock(&graph_lock);
3160 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003161 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003162 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003163 ftrace_graph_count = 0;
3164 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3165 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003166 mutex_unlock(&graph_lock);
3167
Li Zefana4ec5e02009-09-18 14:06:28 +08003168 if (file->f_mode & FMODE_READ)
3169 ret = seq_open(file, &ftrace_graph_seq_ops);
3170
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003171 return ret;
3172}
3173
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003174static int
Li Zefan87827112009-07-23 11:29:11 +08003175ftrace_graph_release(struct inode *inode, struct file *file)
3176{
3177 if (file->f_mode & FMODE_READ)
3178 seq_release(inode, file);
3179 return 0;
3180}
3181
3182static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003183ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003184{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003185 struct dyn_ftrace *rec;
3186 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003187 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003188 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003189 int type, not;
3190 char *search;
3191 bool exists;
3192 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003193
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003194 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003195 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003196 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3197 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003198
3199 search_len = strlen(search);
3200
Steven Rostedt52baf112009-02-14 01:15:39 -05003201 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003202
3203 if (unlikely(ftrace_disabled)) {
3204 mutex_unlock(&ftrace_lock);
3205 return -ENODEV;
3206 }
3207
Steven Rostedt265c8312009-02-13 12:43:56 -05003208 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003209
Steven Rostedt45a4a232011-04-21 23:16:46 -04003210 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05003211 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003212
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003213 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003214 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003215 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003216 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003217 if (array[i] == rec->ip) {
3218 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003219 break;
3220 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003221 }
3222
3223 if (!not) {
3224 fail = 0;
3225 if (!exists) {
3226 array[(*idx)++] = rec->ip;
3227 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3228 goto out;
3229 }
3230 } else {
3231 if (exists) {
3232 array[i] = array[--(*idx)];
3233 array[*idx] = 0;
3234 fail = 0;
3235 }
3236 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003237 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003238 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003239out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003240 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003241
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003242 if (fail)
3243 return -EINVAL;
3244
3245 ftrace_graph_filter_enabled = 1;
3246 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003247}
3248
3249static ssize_t
3250ftrace_graph_write(struct file *file, const char __user *ubuf,
3251 size_t cnt, loff_t *ppos)
3252{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003253 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003254 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003255
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003256 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003257 return 0;
3258
3259 mutex_lock(&graph_lock);
3260
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003261 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3262 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003263 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003264 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003265
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003266 read = trace_get_user(&parser, ubuf, cnt, ppos);
3267
Li Zefan4ba79782009-09-22 13:52:20 +08003268 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003269 parser.buffer[parser.idx] = 0;
3270
3271 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003272 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003273 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003274 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003275 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003276 }
3277
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003278 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003279
3280out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003281 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003282out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003283 mutex_unlock(&graph_lock);
3284
3285 return ret;
3286}
3287
3288static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003289 .open = ftrace_graph_open,
3290 .read = seq_read,
3291 .write = ftrace_graph_write,
3292 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003293 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003294};
3295#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3296
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003297static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003298{
Steven Rostedt5072c592008-05-12 21:20:43 +02003299
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003300 trace_create_file("available_filter_functions", 0444,
3301 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003302
Steven Rostedt647bcd02011-05-03 14:39:21 -04003303 trace_create_file("enabled_functions", 0444,
3304 d_tracer, NULL, &ftrace_enabled_fops);
3305
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003306 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3307 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003308
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003309 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003310 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003311
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003312#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003313 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003314 NULL,
3315 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003316#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3317
Steven Rostedt5072c592008-05-12 21:20:43 +02003318 return 0;
3319}
3320
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003321static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003322 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003323 unsigned long *end)
3324{
3325 unsigned long *p;
3326 unsigned long addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003327
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003328 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003329 p = start;
3330 while (p < end) {
3331 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003332 /*
3333 * Some architecture linkers will pad between
3334 * the different mcount_loc sections of different
3335 * object files to satisfy alignments.
3336 * Skip any NULL pointers.
3337 */
3338 if (!addr)
3339 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003340 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003341 }
3342
Steven Rostedt31e88902008-11-14 16:21:19 -08003343 ftrace_update_code(mod);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003344 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003345
3346 return 0;
3347}
3348
Steven Rostedt93eb6772009-04-15 13:24:06 -04003349#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003350void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003351{
3352 struct dyn_ftrace *rec;
3353 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003354
Steven Rostedt93eb6772009-04-15 13:24:06 -04003355 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003356
3357 if (ftrace_disabled)
3358 goto out_unlock;
3359
Steven Rostedt93eb6772009-04-15 13:24:06 -04003360 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003361 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003362 /*
3363 * rec->ip is changed in ftrace_free_rec()
3364 * It should not between s and e if record was freed.
3365 */
3366 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
3367 ftrace_free_rec(rec);
3368 }
3369 } while_for_each_ftrace_rec();
Steven Rostedt45a4a232011-04-21 23:16:46 -04003370 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003371 mutex_unlock(&ftrace_lock);
3372}
3373
3374static void ftrace_init_module(struct module *mod,
3375 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003376{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003377 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003378 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003379 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003380}
3381
Steven Rostedt93eb6772009-04-15 13:24:06 -04003382static int ftrace_module_notify(struct notifier_block *self,
3383 unsigned long val, void *data)
3384{
3385 struct module *mod = data;
3386
3387 switch (val) {
3388 case MODULE_STATE_COMING:
3389 ftrace_init_module(mod, mod->ftrace_callsites,
3390 mod->ftrace_callsites +
3391 mod->num_ftrace_callsites);
3392 break;
3393 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003394 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003395 break;
3396 }
3397
3398 return 0;
3399}
3400#else
3401static int ftrace_module_notify(struct notifier_block *self,
3402 unsigned long val, void *data)
3403{
3404 return 0;
3405}
3406#endif /* CONFIG_MODULES */
3407
3408struct notifier_block ftrace_module_nb = {
3409 .notifier_call = ftrace_module_notify,
3410 .priority = 0,
3411};
3412
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003413extern unsigned long __start_mcount_loc[];
3414extern unsigned long __stop_mcount_loc[];
3415
3416void __init ftrace_init(void)
3417{
3418 unsigned long count, addr, flags;
3419 int ret;
3420
3421 /* Keep the ftrace pointer to the stub */
3422 addr = (unsigned long)ftrace_stub;
3423
3424 local_irq_save(flags);
3425 ftrace_dyn_arch_init(&addr);
3426 local_irq_restore(flags);
3427
3428 /* ftrace_dyn_arch_init places the return code in addr */
3429 if (addr)
3430 goto failed;
3431
3432 count = __stop_mcount_loc - __start_mcount_loc;
3433
3434 ret = ftrace_dyn_table_alloc(count);
3435 if (ret)
3436 goto failed;
3437
3438 last_ftrace_enabled = ftrace_enabled = 1;
3439
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003440 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003441 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003442 __stop_mcount_loc);
3443
Steven Rostedt93eb6772009-04-15 13:24:06 -04003444 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003445 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003446 pr_warning("Failed to register trace ftrace module notifier\n");
3447
Steven Rostedt2af15d62009-05-28 13:37:24 -04003448 set_ftrace_early_filters();
3449
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003450 return;
3451 failed:
3452 ftrace_disabled = 1;
3453}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003454
Steven Rostedt3d083392008-05-12 21:20:42 +02003455#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003456
Steven Rostedt2b499382011-05-03 22:49:52 -04003457static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003458 .func = ftrace_stub,
3459};
3460
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003461static int __init ftrace_nodyn_init(void)
3462{
3463 ftrace_enabled = 1;
3464 return 0;
3465}
3466device_initcall(ftrace_nodyn_init);
3467
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003468static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3469static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003470/* Keep as macros so we do not need to define the commands */
Steven Rostedta1cd6172011-05-23 15:24:25 -04003471# define ftrace_startup(ops, command) ({0;})
Steven Rostedtbd69c302011-05-03 21:55:54 -04003472# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003473# define ftrace_startup_sysctl() do { } while (0)
3474# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003475
3476static inline int
3477ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3478{
3479 return 1;
3480}
3481
Steven Rostedt3d083392008-05-12 21:20:42 +02003482#endif /* CONFIG_DYNAMIC_FTRACE */
3483
Steven Rostedtb8489142011-05-04 09:27:52 -04003484static void
3485ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3486{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003487 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04003488
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003489 /*
3490 * Some of the ops may be dynamically allocated,
3491 * they must be freed after a synchronize_sched().
3492 */
3493 preempt_disable_notrace();
3494 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04003495 while (op != &ftrace_list_end) {
3496 if (ftrace_ops_test(op, ip))
3497 op->func(ip, parent_ip);
3498 op = rcu_dereference_raw(op->next);
3499 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003500 preempt_enable_notrace();
Steven Rostedtb8489142011-05-04 09:27:52 -04003501}
3502
Steven Rostedte32d8952008-12-04 00:26:41 -05003503static void clear_ftrace_swapper(void)
3504{
3505 struct task_struct *p;
3506 int cpu;
3507
3508 get_online_cpus();
3509 for_each_online_cpu(cpu) {
3510 p = idle_task(cpu);
3511 clear_tsk_trace_trace(p);
3512 }
3513 put_online_cpus();
3514}
3515
3516static void set_ftrace_swapper(void)
3517{
3518 struct task_struct *p;
3519 int cpu;
3520
3521 get_online_cpus();
3522 for_each_online_cpu(cpu) {
3523 p = idle_task(cpu);
3524 set_tsk_trace_trace(p);
3525 }
3526 put_online_cpus();
3527}
3528
3529static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003530{
3531 struct task_struct *p;
3532
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003533 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05003534 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05003535 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05003536 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003537 rcu_read_unlock();
3538
Steven Rostedte32d8952008-12-04 00:26:41 -05003539 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003540}
3541
Steven Rostedte32d8952008-12-04 00:26:41 -05003542static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003543{
3544 struct task_struct *p;
3545
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003546 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003547 do_each_pid_task(pid, PIDTYPE_PID, p) {
3548 set_tsk_trace_trace(p);
3549 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003550 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003551}
3552
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003553static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003554{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003555 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003556 clear_ftrace_swapper();
3557 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003558 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05003559}
3560
3561static void set_ftrace_pid_task(struct pid *pid)
3562{
3563 if (pid == ftrace_swapper_pid)
3564 set_ftrace_swapper();
3565 else
3566 set_ftrace_pid(pid);
3567}
3568
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003569static int ftrace_pid_add(int p)
3570{
3571 struct pid *pid;
3572 struct ftrace_pid *fpid;
3573 int ret = -EINVAL;
3574
3575 mutex_lock(&ftrace_lock);
3576
3577 if (!p)
3578 pid = ftrace_swapper_pid;
3579 else
3580 pid = find_get_pid(p);
3581
3582 if (!pid)
3583 goto out;
3584
3585 ret = 0;
3586
3587 list_for_each_entry(fpid, &ftrace_pids, list)
3588 if (fpid->pid == pid)
3589 goto out_put;
3590
3591 ret = -ENOMEM;
3592
3593 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3594 if (!fpid)
3595 goto out_put;
3596
3597 list_add(&fpid->list, &ftrace_pids);
3598 fpid->pid = pid;
3599
3600 set_ftrace_pid_task(pid);
3601
3602 ftrace_update_pid_func();
3603 ftrace_startup_enable(0);
3604
3605 mutex_unlock(&ftrace_lock);
3606 return 0;
3607
3608out_put:
3609 if (pid != ftrace_swapper_pid)
3610 put_pid(pid);
3611
3612out:
3613 mutex_unlock(&ftrace_lock);
3614 return ret;
3615}
3616
3617static void ftrace_pid_reset(void)
3618{
3619 struct ftrace_pid *fpid, *safe;
3620
3621 mutex_lock(&ftrace_lock);
3622 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3623 struct pid *pid = fpid->pid;
3624
3625 clear_ftrace_pid_task(pid);
3626
3627 list_del(&fpid->list);
3628 kfree(fpid);
3629 }
3630
3631 ftrace_update_pid_func();
3632 ftrace_startup_enable(0);
3633
3634 mutex_unlock(&ftrace_lock);
3635}
3636
3637static void *fpid_start(struct seq_file *m, loff_t *pos)
3638{
3639 mutex_lock(&ftrace_lock);
3640
3641 if (list_empty(&ftrace_pids) && (!*pos))
3642 return (void *) 1;
3643
3644 return seq_list_start(&ftrace_pids, *pos);
3645}
3646
3647static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3648{
3649 if (v == (void *)1)
3650 return NULL;
3651
3652 return seq_list_next(v, &ftrace_pids, pos);
3653}
3654
3655static void fpid_stop(struct seq_file *m, void *p)
3656{
3657 mutex_unlock(&ftrace_lock);
3658}
3659
3660static int fpid_show(struct seq_file *m, void *v)
3661{
3662 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3663
3664 if (v == (void *)1) {
3665 seq_printf(m, "no pid\n");
3666 return 0;
3667 }
3668
3669 if (fpid->pid == ftrace_swapper_pid)
3670 seq_printf(m, "swapper tasks\n");
3671 else
3672 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3673
3674 return 0;
3675}
3676
3677static const struct seq_operations ftrace_pid_sops = {
3678 .start = fpid_start,
3679 .next = fpid_next,
3680 .stop = fpid_stop,
3681 .show = fpid_show,
3682};
3683
3684static int
3685ftrace_pid_open(struct inode *inode, struct file *file)
3686{
3687 int ret = 0;
3688
3689 if ((file->f_mode & FMODE_WRITE) &&
3690 (file->f_flags & O_TRUNC))
3691 ftrace_pid_reset();
3692
3693 if (file->f_mode & FMODE_READ)
3694 ret = seq_open(file, &ftrace_pid_sops);
3695
3696 return ret;
3697}
3698
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003699static ssize_t
3700ftrace_pid_write(struct file *filp, const char __user *ubuf,
3701 size_t cnt, loff_t *ppos)
3702{
Ingo Molnar457dc922009-11-23 11:03:28 +01003703 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003704 long val;
3705 int ret;
3706
3707 if (cnt >= sizeof(buf))
3708 return -EINVAL;
3709
3710 if (copy_from_user(&buf, ubuf, cnt))
3711 return -EFAULT;
3712
3713 buf[cnt] = 0;
3714
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003715 /*
3716 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3717 * to clean the filter quietly.
3718 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003719 tmp = strstrip(buf);
3720 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003721 return 1;
3722
Ingo Molnar457dc922009-11-23 11:03:28 +01003723 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003724 if (ret < 0)
3725 return ret;
3726
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003727 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003728
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003729 return ret ? ret : cnt;
3730}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003731
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003732static int
3733ftrace_pid_release(struct inode *inode, struct file *file)
3734{
3735 if (file->f_mode & FMODE_READ)
3736 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003737
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003738 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003739}
3740
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003741static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003742 .open = ftrace_pid_open,
3743 .write = ftrace_pid_write,
3744 .read = seq_read,
3745 .llseek = seq_lseek,
3746 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003747};
3748
3749static __init int ftrace_init_debugfs(void)
3750{
3751 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003752
3753 d_tracer = tracing_init_dentry();
3754 if (!d_tracer)
3755 return 0;
3756
3757 ftrace_init_dyn_debugfs(d_tracer);
3758
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003759 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3760 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003761
3762 ftrace_profile_debugfs(d_tracer);
3763
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003764 return 0;
3765}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003766fs_initcall(ftrace_init_debugfs);
3767
Steven Rostedt3d083392008-05-12 21:20:42 +02003768/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003769 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003770 *
3771 * This function should be used by panic code. It stops ftrace
3772 * but in a not so nice way. If you need to simply kill ftrace
3773 * from a non-atomic section, use ftrace_kill.
3774 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003775void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003776{
3777 ftrace_disabled = 1;
3778 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003779 clear_ftrace_function();
3780}
3781
3782/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003783 * register_ftrace_function - register a function for profiling
3784 * @ops - ops structure that holds the function for profiling.
3785 *
3786 * Register a function to be called by all functions in the
3787 * kernel.
3788 *
3789 * Note: @ops->func and all the functions it calls must be labeled
3790 * with "notrace", otherwise it will go into a
3791 * recursive loop.
3792 */
3793int register_ftrace_function(struct ftrace_ops *ops)
3794{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003795 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003796
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003797 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003798
Steven Rostedt45a4a232011-04-21 23:16:46 -04003799 if (unlikely(ftrace_disabled))
3800 goto out_unlock;
3801
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003802 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04003803 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04003804 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003805
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003806
Steven Rostedt45a4a232011-04-21 23:16:46 -04003807 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003808 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003809 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003810}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003811EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02003812
3813/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003814 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003815 * @ops - ops structure that holds the function to unregister
3816 *
3817 * Unregister a function that was added to be called by ftrace profiling.
3818 */
3819int unregister_ftrace_function(struct ftrace_ops *ops)
3820{
3821 int ret;
3822
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003823 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003824 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04003825 if (!ret)
3826 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003827 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003828
3829 return ret;
3830}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003831EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003832
Ingo Molnare309b412008-05-12 21:20:51 +02003833int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003834ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003835 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003836 loff_t *ppos)
3837{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003838 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003839
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003840 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003841
Steven Rostedt45a4a232011-04-21 23:16:46 -04003842 if (unlikely(ftrace_disabled))
3843 goto out;
3844
3845 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003846
Li Zefana32c7762009-06-26 16:55:51 +08003847 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003848 goto out;
3849
Li Zefana32c7762009-06-26 16:55:51 +08003850 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003851
3852 if (ftrace_enabled) {
3853
3854 ftrace_startup_sysctl();
3855
3856 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04003857 if (ftrace_ops_list != &ftrace_list_end) {
3858 if (ftrace_ops_list->next == &ftrace_list_end)
3859 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003860 else
Steven Rostedtb8489142011-05-04 09:27:52 -04003861 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003862 }
3863
3864 } else {
3865 /* stopping ftrace calls (just send to ftrace_stub) */
3866 ftrace_trace_function = ftrace_stub;
3867
3868 ftrace_shutdown_sysctl();
3869 }
3870
3871 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003872 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003873 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003874}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003875
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003876#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003877
Steven Rostedt597af812009-04-03 15:24:12 -04003878static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003879static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003880
Steven Rostedte49dc192008-12-02 23:50:05 -05003881int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3882{
3883 return 0;
3884}
3885
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003886/* The callbacks that hook a function */
3887trace_func_graph_ret_t ftrace_graph_return =
3888 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003889trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003890
3891/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3892static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3893{
3894 int i;
3895 int ret = 0;
3896 unsigned long flags;
3897 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3898 struct task_struct *g, *t;
3899
3900 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3901 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3902 * sizeof(struct ftrace_ret_stack),
3903 GFP_KERNEL);
3904 if (!ret_stack_list[i]) {
3905 start = 0;
3906 end = i;
3907 ret = -ENOMEM;
3908 goto free;
3909 }
3910 }
3911
3912 read_lock_irqsave(&tasklist_lock, flags);
3913 do_each_thread(g, t) {
3914 if (start == end) {
3915 ret = -EAGAIN;
3916 goto unlock;
3917 }
3918
3919 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003920 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003921 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003922 t->curr_ret_stack = -1;
3923 /* Make sure the tasks see the -1 first: */
3924 smp_wmb();
3925 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003926 }
3927 } while_each_thread(g, t);
3928
3929unlock:
3930 read_unlock_irqrestore(&tasklist_lock, flags);
3931free:
3932 for (i = start; i < end; i++)
3933 kfree(ret_stack_list[i]);
3934 return ret;
3935}
3936
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003937static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04003938ftrace_graph_probe_sched_switch(void *ignore,
3939 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003940{
3941 unsigned long long timestamp;
3942 int index;
3943
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003944 /*
3945 * Does the user want to count the time a function was asleep.
3946 * If so, do not update the time stamps.
3947 */
3948 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3949 return;
3950
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003951 timestamp = trace_clock_local();
3952
3953 prev->ftrace_timestamp = timestamp;
3954
3955 /* only process tasks that we timestamped */
3956 if (!next->ftrace_timestamp)
3957 return;
3958
3959 /*
3960 * Update all the counters in next to make up for the
3961 * time next was sleeping.
3962 */
3963 timestamp -= next->ftrace_timestamp;
3964
3965 for (index = next->curr_ret_stack; index >= 0; index--)
3966 next->ret_stack[index].calltime += timestamp;
3967}
3968
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003969/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003970static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003971{
3972 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003973 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003974
3975 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3976 sizeof(struct ftrace_ret_stack *),
3977 GFP_KERNEL);
3978
3979 if (!ret_stack_list)
3980 return -ENOMEM;
3981
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003982 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003983 for_each_online_cpu(cpu) {
3984 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05003985 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04003986 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003987
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003988 do {
3989 ret = alloc_retstack_tasklist(ret_stack_list);
3990 } while (ret == -EAGAIN);
3991
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003992 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04003993 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003994 if (ret)
3995 pr_info("ftrace_graph: Couldn't activate tracepoint"
3996 " probe to kernel_sched_switch\n");
3997 }
3998
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003999 kfree(ret_stack_list);
4000 return ret;
4001}
4002
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004003/*
4004 * Hibernation protection.
4005 * The state of the current task is too much unstable during
4006 * suspend/restore to disk. We want to protect against that.
4007 */
4008static int
4009ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4010 void *unused)
4011{
4012 switch (state) {
4013 case PM_HIBERNATION_PREPARE:
4014 pause_graph_tracing();
4015 break;
4016
4017 case PM_POST_HIBERNATION:
4018 unpause_graph_tracing();
4019 break;
4020 }
4021 return NOTIFY_DONE;
4022}
4023
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004024int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4025 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004026{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004027 int ret = 0;
4028
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004029 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004030
Steven Rostedt05ce5812009-03-24 00:18:31 -04004031 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004032 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004033 ret = -EBUSY;
4034 goto out;
4035 }
4036
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004037 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4038 register_pm_notifier(&ftrace_suspend_notifier);
4039
Steven Rostedt597af812009-04-03 15:24:12 -04004040 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004041 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004042 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004043 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004044 goto out;
4045 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004046
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004047 ftrace_graph_return = retfunc;
4048 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004049
Steven Rostedta1cd6172011-05-23 15:24:25 -04004050 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004051
4052out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004053 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004054 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004055}
4056
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004057void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004058{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004059 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004060
Steven Rostedt597af812009-04-03 15:24:12 -04004061 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004062 goto out;
4063
Steven Rostedt597af812009-04-03 15:24:12 -04004064 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004065 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004066 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004067 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004068 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004069 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004070
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004071 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004072 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004073}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004074
Steven Rostedt868baf02011-02-10 21:26:13 -05004075static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4076
4077static void
4078graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4079{
4080 atomic_set(&t->tracing_graph_pause, 0);
4081 atomic_set(&t->trace_overrun, 0);
4082 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004083 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004084 smp_wmb();
4085 t->ret_stack = ret_stack;
4086}
4087
4088/*
4089 * Allocate a return stack for the idle task. May be the first
4090 * time through, or it may be done by CPU hotplug online.
4091 */
4092void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4093{
4094 t->curr_ret_stack = -1;
4095 /*
4096 * The idle task has no parent, it either has its own
4097 * stack or no stack at all.
4098 */
4099 if (t->ret_stack)
4100 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4101
4102 if (ftrace_graph_active) {
4103 struct ftrace_ret_stack *ret_stack;
4104
4105 ret_stack = per_cpu(idle_ret_stack, cpu);
4106 if (!ret_stack) {
4107 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4108 * sizeof(struct ftrace_ret_stack),
4109 GFP_KERNEL);
4110 if (!ret_stack)
4111 return;
4112 per_cpu(idle_ret_stack, cpu) = ret_stack;
4113 }
4114 graph_init_task(t, ret_stack);
4115 }
4116}
4117
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004118/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004119void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004120{
Steven Rostedt84047e32009-06-02 16:51:55 -04004121 /* Make sure we do not use the parent ret_stack */
4122 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004123 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004124
Steven Rostedt597af812009-04-03 15:24:12 -04004125 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004126 struct ftrace_ret_stack *ret_stack;
4127
4128 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004129 * sizeof(struct ftrace_ret_stack),
4130 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004131 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004132 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004133 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004134 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004135}
4136
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004137void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004138{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004139 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4140
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004141 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004142 /* NULL must become visible to IRQs before we free it: */
4143 barrier();
4144
4145 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004146}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004147
4148void ftrace_graph_stop(void)
4149{
4150 ftrace_stop();
4151}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004152#endif