blob: eb19fae2c54a2473cc20ee1e937b2d71fa631073 [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 Rostedt69128962008-10-23 09:33:03 -040041#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040042 ({ \
43 int ___r = cond; \
44 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040045 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040046 ___r; \
47 })
Steven Rostedt69128962008-10-23 09:33:03 -040048
49#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040050 ({ \
51 int ___r = cond; \
52 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040053 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040054 ___r; \
55 })
Steven Rostedt69128962008-10-23 09:33:03 -040056
Steven Rostedt8fc0c702009-02-16 15:28:00 -050057/* hash bits for specific function selection */
58#define FTRACE_HASH_BITS 7
59#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
60
Steven Rostedt4eebcc82008-05-12 21:20:48 +020061/* ftrace_enabled is a method to turn ftrace on or off */
62int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020063static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020064
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050065/* Quick disabling of function tracer. */
66int function_trace_stop;
67
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040068/* List for set_ftrace_pid's pids. */
69LIST_HEAD(ftrace_pids);
70struct ftrace_pid {
71 struct list_head list;
72 struct pid *pid;
73};
74
Steven Rostedt4eebcc82008-05-12 21:20:48 +020075/*
76 * ftrace_disabled is set when an anomaly is discovered.
77 * ftrace_disabled is much stronger than ftrace_enabled.
78 */
79static int ftrace_disabled __read_mostly;
80
Steven Rostedt52baf112009-02-14 01:15:39 -050081static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020082
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020083static struct ftrace_ops ftrace_list_end __read_mostly =
84{
Steven Rostedtfb9fb012009-03-25 13:26:41 -040085 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020086};
87
88static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
89ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050090ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050091ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020092
Paul E. McKenney3f379b02010-03-05 15:03:25 -080093/*
94 * Traverse the ftrace_list, invoking all entries. The reason that we
95 * can use rcu_dereference_raw() is that elements removed from this list
96 * are simply leaked, so there is no need to interact with a grace-period
97 * mechanism. The rcu_dereference_raw() calls are needed to handle
98 * concurrent insertions into the ftrace_list.
99 *
100 * Silly Alpha and silly pointer-speculation compiler optimizations!
101 */
Ingo Molnarf2252932008-05-22 10:37:48 +0200102static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200103{
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800104 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200105
106 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200107 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800108 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200109 };
110}
111
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500112static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
113{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500114 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500115 return;
116
117 ftrace_pid_function(ip, parent_ip);
118}
119
120static void set_ftrace_pid_function(ftrace_func_t func)
121{
122 /* do not set ftrace_pid_function to itself! */
123 if (func != ftrace_pid_func)
124 ftrace_pid_function = func;
125}
126
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200127/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200128 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200129 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200130 * This NULLs the ftrace function and in essence stops
131 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200132 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200133void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200134{
Steven Rostedt3d083392008-05-12 21:20:42 +0200135 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500136 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500137 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200138}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200139
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500140#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
141/*
142 * For those archs that do not test ftrace_trace_stop in their
143 * mcount call site, we need to do it from C.
144 */
145static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
146{
147 if (function_trace_stop)
148 return;
149
150 __ftrace_trace_function(ip, parent_ip);
151}
152#endif
153
Ingo Molnare309b412008-05-12 21:20:51 +0200154static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200155{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200156 ops->next = ftrace_list;
157 /*
158 * We are entering ops into the ftrace_list but another
159 * CPU might be walking that list. We need to make sure
160 * the ops->next pointer is valid before another CPU sees
161 * the ops pointer included into the ftrace_list.
162 */
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800163 rcu_assign_pointer(ftrace_list, ops);
Steven Rostedt3d083392008-05-12 21:20:42 +0200164
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200165 if (ftrace_enabled) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500166 ftrace_func_t func;
167
168 if (ops->next == &ftrace_list_end)
169 func = ops->func;
170 else
171 func = ftrace_list_func;
172
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400173 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500174 set_ftrace_pid_function(func);
175 func = ftrace_pid_func;
176 }
177
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200178 /*
179 * For one func, simply call it directly.
180 * For more than one func, call the chain.
181 */
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500182#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500183 ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500184#else
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500185 __ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500186 ftrace_trace_function = ftrace_test_stop_func;
187#endif
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200188 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200189
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200190 return 0;
191}
192
Ingo Molnare309b412008-05-12 21:20:51 +0200193static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200194{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200195 struct ftrace_ops **p;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200196
197 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200198 * If we are removing the last function, then simply point
199 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200200 */
201 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
202 ftrace_trace_function = ftrace_stub;
203 ftrace_list = &ftrace_list_end;
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500204 return 0;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200205 }
206
207 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
208 if (*p == ops)
209 break;
210
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500211 if (*p != ops)
212 return -1;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200213
214 *p = (*p)->next;
215
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200216 if (ftrace_enabled) {
217 /* If we only have one func left, then call that directly */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500218 if (ftrace_list->next == &ftrace_list_end) {
219 ftrace_func_t func = ftrace_list->func;
220
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400221 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500222 set_ftrace_pid_function(func);
223 func = ftrace_pid_func;
224 }
225#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
226 ftrace_trace_function = func;
227#else
228 __ftrace_trace_function = func;
229#endif
230 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200231 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200232
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500233 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200234}
235
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500236static void ftrace_update_pid_func(void)
237{
238 ftrace_func_t func;
239
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500240 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900241 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500242
Matt Fleming33974092009-09-28 16:43:01 +0100243#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500244 func = ftrace_trace_function;
Matt Fleming33974092009-09-28 16:43:01 +0100245#else
246 func = __ftrace_trace_function;
247#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500248
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400249 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500250 set_ftrace_pid_function(func);
251 func = ftrace_pid_func;
252 } else {
Liming Wang66eafeb2008-12-02 10:33:08 +0800253 if (func == ftrace_pid_func)
254 func = ftrace_pid_function;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500255 }
256
257#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
258 ftrace_trace_function = func;
259#else
260 __ftrace_trace_function = func;
261#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500262}
263
Steven Rostedt493762f2009-03-23 17:12:36 -0400264#ifdef CONFIG_FUNCTION_PROFILER
265struct ftrace_profile {
266 struct hlist_node node;
267 unsigned long ip;
268 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400269#ifdef CONFIG_FUNCTION_GRAPH_TRACER
270 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400271 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400272#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400273};
274
275struct ftrace_profile_page {
276 struct ftrace_profile_page *next;
277 unsigned long index;
278 struct ftrace_profile records[];
279};
280
Steven Rostedtcafb1682009-03-24 20:50:39 -0400281struct ftrace_profile_stat {
282 atomic_t disabled;
283 struct hlist_head *hash;
284 struct ftrace_profile_page *pages;
285 struct ftrace_profile_page *start;
286 struct tracer_stat stat;
287};
288
Steven Rostedt493762f2009-03-23 17:12:36 -0400289#define PROFILE_RECORDS_SIZE \
290 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
291
292#define PROFILES_PER_PAGE \
293 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
294
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400295static int ftrace_profile_bits __read_mostly;
296static int ftrace_profile_enabled __read_mostly;
297
298/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400299static DEFINE_MUTEX(ftrace_profile_lock);
300
Steven Rostedtcafb1682009-03-24 20:50:39 -0400301static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400302
303#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
304
Steven Rostedt493762f2009-03-23 17:12:36 -0400305static void *
306function_stat_next(void *v, int idx)
307{
308 struct ftrace_profile *rec = v;
309 struct ftrace_profile_page *pg;
310
311 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
312
313 again:
Li Zefan0296e422009-06-26 11:15:37 +0800314 if (idx != 0)
315 rec++;
316
Steven Rostedt493762f2009-03-23 17:12:36 -0400317 if ((void *)rec >= (void *)&pg->records[pg->index]) {
318 pg = pg->next;
319 if (!pg)
320 return NULL;
321 rec = &pg->records[0];
322 if (!rec->counter)
323 goto again;
324 }
325
326 return rec;
327}
328
329static void *function_stat_start(struct tracer_stat *trace)
330{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400331 struct ftrace_profile_stat *stat =
332 container_of(trace, struct ftrace_profile_stat, stat);
333
334 if (!stat || !stat->start)
335 return NULL;
336
337 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400338}
339
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400340#ifdef CONFIG_FUNCTION_GRAPH_TRACER
341/* function graph compares on total time */
342static int function_stat_cmp(void *p1, void *p2)
343{
344 struct ftrace_profile *a = p1;
345 struct ftrace_profile *b = p2;
346
347 if (a->time < b->time)
348 return -1;
349 if (a->time > b->time)
350 return 1;
351 else
352 return 0;
353}
354#else
355/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400356static int function_stat_cmp(void *p1, void *p2)
357{
358 struct ftrace_profile *a = p1;
359 struct ftrace_profile *b = p2;
360
361 if (a->counter < b->counter)
362 return -1;
363 if (a->counter > b->counter)
364 return 1;
365 else
366 return 0;
367}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400368#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400369
370static int function_stat_headers(struct seq_file *m)
371{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400372#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400373 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400374 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400375 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400376 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400377#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400378 seq_printf(m, " Function Hit\n"
379 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400380#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400381 return 0;
382}
383
384static int function_stat_show(struct seq_file *m, void *v)
385{
386 struct ftrace_profile *rec = v;
387 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800388 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400389#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400390 static struct trace_seq s;
391 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400392 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400393#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800394 mutex_lock(&ftrace_profile_lock);
395
396 /* we raced with function_profile_reset() */
397 if (unlikely(rec->counter == 0)) {
398 ret = -EBUSY;
399 goto out;
400 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400401
402 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400403 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400404
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400405#ifdef CONFIG_FUNCTION_GRAPH_TRACER
406 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400407 avg = rec->time;
408 do_div(avg, rec->counter);
409
Chase Douglase330b3b2010-04-26 14:02:05 -0400410 /* Sample standard deviation (s^2) */
411 if (rec->counter <= 1)
412 stddev = 0;
413 else {
414 stddev = rec->time_squared - rec->counter * avg * avg;
415 /*
416 * Divide only 1000 for ns^2 -> us^2 conversion.
417 * trace_print_graph_duration will divide 1000 again.
418 */
419 do_div(stddev, (rec->counter - 1) * 1000);
420 }
421
Steven Rostedt34886c82009-03-25 21:00:47 -0400422 trace_seq_init(&s);
423 trace_print_graph_duration(rec->time, &s);
424 trace_seq_puts(&s, " ");
425 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400426 trace_seq_puts(&s, " ");
427 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400428 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400429#endif
430 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800431out:
432 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400433
Li Zefan3aaba202010-08-23 16:50:12 +0800434 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400435}
436
Steven Rostedtcafb1682009-03-24 20:50:39 -0400437static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400438{
439 struct ftrace_profile_page *pg;
440
Steven Rostedtcafb1682009-03-24 20:50:39 -0400441 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400442
443 while (pg) {
444 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
445 pg->index = 0;
446 pg = pg->next;
447 }
448
Steven Rostedtcafb1682009-03-24 20:50:39 -0400449 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400450 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
451}
452
Steven Rostedtcafb1682009-03-24 20:50:39 -0400453int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400454{
455 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400456 int functions;
457 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400458 int i;
459
460 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400461 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400462 return 0;
463
Steven Rostedtcafb1682009-03-24 20:50:39 -0400464 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
465 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400466 return -ENOMEM;
467
Steven Rostedt318e0a72009-03-25 20:06:34 -0400468#ifdef CONFIG_DYNAMIC_FTRACE
469 functions = ftrace_update_tot_cnt;
470#else
471 /*
472 * We do not know the number of functions that exist because
473 * dynamic tracing is what counts them. With past experience
474 * we have around 20K functions. That should be more than enough.
475 * It is highly unlikely we will execute every function in
476 * the kernel.
477 */
478 functions = 20000;
479#endif
480
Steven Rostedtcafb1682009-03-24 20:50:39 -0400481 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400482
Steven Rostedt318e0a72009-03-25 20:06:34 -0400483 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
484
485 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400486 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400487 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400488 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400489 pg = pg->next;
490 }
491
492 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400493
494 out_free:
495 pg = stat->start;
496 while (pg) {
497 unsigned long tmp = (unsigned long)pg;
498
499 pg = pg->next;
500 free_page(tmp);
501 }
502
503 free_page((unsigned long)stat->pages);
504 stat->pages = NULL;
505 stat->start = NULL;
506
507 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400508}
509
Steven Rostedtcafb1682009-03-24 20:50:39 -0400510static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400511{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400512 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400513 int size;
514
Steven Rostedtcafb1682009-03-24 20:50:39 -0400515 stat = &per_cpu(ftrace_profile_stats, cpu);
516
517 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400518 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400519 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400520 return 0;
521 }
522
523 /*
524 * We are profiling all functions, but usually only a few thousand
525 * functions are hit. We'll make a hash of 1024 items.
526 */
527 size = FTRACE_PROFILE_HASH_SIZE;
528
Steven Rostedtcafb1682009-03-24 20:50:39 -0400529 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400530
Steven Rostedtcafb1682009-03-24 20:50:39 -0400531 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400532 return -ENOMEM;
533
Steven Rostedtcafb1682009-03-24 20:50:39 -0400534 if (!ftrace_profile_bits) {
535 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400536
Steven Rostedtcafb1682009-03-24 20:50:39 -0400537 for (; size; size >>= 1)
538 ftrace_profile_bits++;
539 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400540
Steven Rostedt318e0a72009-03-25 20:06:34 -0400541 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400542 if (ftrace_profile_pages_init(stat) < 0) {
543 kfree(stat->hash);
544 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400545 return -ENOMEM;
546 }
547
548 return 0;
549}
550
Steven Rostedtcafb1682009-03-24 20:50:39 -0400551static int ftrace_profile_init(void)
552{
553 int cpu;
554 int ret = 0;
555
556 for_each_online_cpu(cpu) {
557 ret = ftrace_profile_init_cpu(cpu);
558 if (ret)
559 break;
560 }
561
562 return ret;
563}
564
Steven Rostedt493762f2009-03-23 17:12:36 -0400565/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400566static struct ftrace_profile *
567ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400568{
569 struct ftrace_profile *rec;
570 struct hlist_head *hhd;
571 struct hlist_node *n;
572 unsigned long key;
573
574 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400575 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400576
577 if (hlist_empty(hhd))
578 return NULL;
579
580 hlist_for_each_entry_rcu(rec, n, hhd, node) {
581 if (rec->ip == ip)
582 return rec;
583 }
584
585 return NULL;
586}
587
Steven Rostedtcafb1682009-03-24 20:50:39 -0400588static void ftrace_add_profile(struct ftrace_profile_stat *stat,
589 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400590{
591 unsigned long key;
592
593 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400594 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400595}
596
Steven Rostedt318e0a72009-03-25 20:06:34 -0400597/*
598 * The memory is already allocated, this simply finds a new record to use.
599 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400600static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400601ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400602{
603 struct ftrace_profile *rec = NULL;
604
Steven Rostedt318e0a72009-03-25 20:06:34 -0400605 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400606 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400607 goto out;
608
Steven Rostedt493762f2009-03-23 17:12:36 -0400609 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400610 * Try to find the function again since an NMI
611 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400612 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400613 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400614 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400615 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400616
Steven Rostedtcafb1682009-03-24 20:50:39 -0400617 if (stat->pages->index == PROFILES_PER_PAGE) {
618 if (!stat->pages->next)
619 goto out;
620 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400621 }
622
Steven Rostedtcafb1682009-03-24 20:50:39 -0400623 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400624 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400625 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400626
Steven Rostedt493762f2009-03-23 17:12:36 -0400627 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400628 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400629
630 return rec;
631}
632
Steven Rostedt493762f2009-03-23 17:12:36 -0400633static void
634function_profile_call(unsigned long ip, unsigned long parent_ip)
635{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400636 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400637 struct ftrace_profile *rec;
638 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400639
640 if (!ftrace_profile_enabled)
641 return;
642
Steven Rostedt493762f2009-03-23 17:12:36 -0400643 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400644
645 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400646 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400647 goto out;
648
649 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400650 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400651 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400652 if (!rec)
653 goto out;
654 }
655
656 rec->counter++;
657 out:
658 local_irq_restore(flags);
659}
660
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400661#ifdef CONFIG_FUNCTION_GRAPH_TRACER
662static int profile_graph_entry(struct ftrace_graph_ent *trace)
663{
664 function_profile_call(trace->func, 0);
665 return 1;
666}
667
668static void profile_graph_return(struct ftrace_graph_ret *trace)
669{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400670 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400671 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400672 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400673 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400674
675 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400676 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400677 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400678 goto out;
679
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400680 /* If the calltime was zero'd ignore it */
681 if (!trace->calltime)
682 goto out;
683
Steven Rostedta2a16d62009-03-24 23:17:58 -0400684 calltime = trace->rettime - trace->calltime;
685
686 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
687 int index;
688
689 index = trace->depth;
690
691 /* Append this call time to the parent time to subtract */
692 if (index)
693 current->ret_stack[index - 1].subtime += calltime;
694
695 if (current->ret_stack[index].subtime < calltime)
696 calltime -= current->ret_stack[index].subtime;
697 else
698 calltime = 0;
699 }
700
Steven Rostedtcafb1682009-03-24 20:50:39 -0400701 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400702 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400703 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400704 rec->time_squared += calltime * calltime;
705 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400706
Steven Rostedtcafb1682009-03-24 20:50:39 -0400707 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400708 local_irq_restore(flags);
709}
710
711static int register_ftrace_profiler(void)
712{
713 return register_ftrace_graph(&profile_graph_return,
714 &profile_graph_entry);
715}
716
717static void unregister_ftrace_profiler(void)
718{
719 unregister_ftrace_graph();
720}
721#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400722static struct ftrace_ops ftrace_profile_ops __read_mostly =
723{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400724 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400725};
726
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400727static int register_ftrace_profiler(void)
728{
729 return register_ftrace_function(&ftrace_profile_ops);
730}
731
732static void unregister_ftrace_profiler(void)
733{
734 unregister_ftrace_function(&ftrace_profile_ops);
735}
736#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
737
Steven Rostedt493762f2009-03-23 17:12:36 -0400738static ssize_t
739ftrace_profile_write(struct file *filp, const char __user *ubuf,
740 size_t cnt, loff_t *ppos)
741{
742 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400743 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400744 int ret;
745
746 if (cnt >= sizeof(buf))
747 return -EINVAL;
748
749 if (copy_from_user(&buf, ubuf, cnt))
750 return -EFAULT;
751
752 buf[cnt] = 0;
753
754 ret = strict_strtoul(buf, 10, &val);
755 if (ret < 0)
756 return ret;
757
758 val = !!val;
759
760 mutex_lock(&ftrace_profile_lock);
761 if (ftrace_profile_enabled ^ val) {
762 if (val) {
763 ret = ftrace_profile_init();
764 if (ret < 0) {
765 cnt = ret;
766 goto out;
767 }
768
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400769 ret = register_ftrace_profiler();
770 if (ret < 0) {
771 cnt = ret;
772 goto out;
773 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400774 ftrace_profile_enabled = 1;
775 } else {
776 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400777 /*
778 * unregister_ftrace_profiler calls stop_machine
779 * so this acts like an synchronize_sched.
780 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400781 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400782 }
783 }
784 out:
785 mutex_unlock(&ftrace_profile_lock);
786
Jiri Olsacf8517c2009-10-23 19:36:16 -0400787 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400788
789 return cnt;
790}
791
792static ssize_t
793ftrace_profile_read(struct file *filp, char __user *ubuf,
794 size_t cnt, loff_t *ppos)
795{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400796 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400797 int r;
798
799 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
800 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
801}
802
803static const struct file_operations ftrace_profile_fops = {
804 .open = tracing_open_generic,
805 .read = ftrace_profile_read,
806 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200807 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400808};
809
Steven Rostedtcafb1682009-03-24 20:50:39 -0400810/* used to initialize the real stat files */
811static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400812 .name = "functions",
813 .stat_start = function_stat_start,
814 .stat_next = function_stat_next,
815 .stat_cmp = function_stat_cmp,
816 .stat_headers = function_stat_headers,
817 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400818};
819
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400820static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400821{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400822 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400823 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400825 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400826 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400827
Steven Rostedtcafb1682009-03-24 20:50:39 -0400828 for_each_possible_cpu(cpu) {
829 stat = &per_cpu(ftrace_profile_stats, cpu);
830
831 /* allocate enough for function name + cpu number */
832 name = kmalloc(32, GFP_KERNEL);
833 if (!name) {
834 /*
835 * The files created are permanent, if something happens
836 * we still do not free memory.
837 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400838 WARN(1,
839 "Could not allocate stat file for cpu %d\n",
840 cpu);
841 return;
842 }
843 stat->stat = function_stats;
844 snprintf(name, 32, "function%d", cpu);
845 stat->stat.name = name;
846 ret = register_stat_tracer(&stat->stat);
847 if (ret) {
848 WARN(1,
849 "Could not register function stat for cpu %d\n",
850 cpu);
851 kfree(name);
852 return;
853 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400854 }
855
856 entry = debugfs_create_file("function_profile_enabled", 0644,
857 d_tracer, NULL, &ftrace_profile_fops);
858 if (!entry)
859 pr_warning("Could not create debugfs "
860 "'function_profile_enabled' entry\n");
861}
862
863#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400864static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400865{
866}
867#endif /* CONFIG_FUNCTION_PROFILER */
868
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100869static struct pid * const ftrace_swapper_pid = &init_struct_pid;
870
Steven Rostedt3d083392008-05-12 21:20:42 +0200871#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100872
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400873#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400874# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400875#endif
876
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500877static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
878
Steven Rostedtb6887d72009-02-17 12:32:04 -0500879struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500880 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500881 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500882 unsigned long flags;
883 unsigned long ip;
884 void *data;
885 struct rcu_head rcu;
886};
887
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200888enum {
889 FTRACE_ENABLE_CALLS = (1 << 0),
890 FTRACE_DISABLE_CALLS = (1 << 1),
891 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
Steven Rostedt79e406d2010-09-14 22:19:46 -0400892 FTRACE_START_FUNC_RET = (1 << 3),
893 FTRACE_STOP_FUNC_RET = (1 << 4),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200894};
895
Steven Rostedt5072c592008-05-12 21:20:43 +0200896static int ftrace_filtered;
897
Lai Jiangshane94142a2009-03-13 17:51:27 +0800898static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200899
Steven Rostedt41c52c02008-05-22 11:46:33 -0400900static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200901
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200902struct ftrace_page {
903 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500904 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200905 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700906};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200907
908#define ENTRIES_PER_PAGE \
909 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
910
911/* estimate from running different kernels */
912#define NR_TO_INIT 10000
913
914static struct ftrace_page *ftrace_pages_start;
915static struct ftrace_page *ftrace_pages;
916
Steven Rostedt37ad5082008-05-12 21:20:48 +0200917static struct dyn_ftrace *ftrace_free_records;
918
Steven Rostedt265c8312009-02-13 12:43:56 -0500919/*
920 * This is a double for. Do not use 'break' to break out of the loop,
921 * you must use a goto.
922 */
923#define do_for_each_ftrace_rec(pg, rec) \
924 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
925 int _____i; \
926 for (_____i = 0; _____i < pg->index; _____i++) { \
927 rec = &pg->records[_____i];
928
929#define while_for_each_ftrace_rec() \
930 } \
931 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530932
Ingo Molnare309b412008-05-12 21:20:51 +0200933static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad5082008-05-12 21:20:48 +0200934{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800935 rec->freelist = ftrace_free_records;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200936 ftrace_free_records = rec;
937 rec->flags |= FTRACE_FL_FREE;
938}
939
Ingo Molnare309b412008-05-12 21:20:51 +0200940static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200941{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200942 struct dyn_ftrace *rec;
943
944 /* First check for freed records */
945 if (ftrace_free_records) {
946 rec = ftrace_free_records;
947
Steven Rostedt37ad5082008-05-12 21:20:48 +0200948 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt69128962008-10-23 09:33:03 -0400949 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad5082008-05-12 21:20:48 +0200950 ftrace_free_records = NULL;
951 return NULL;
952 }
953
Lai Jiangshanee000b72009-03-24 13:38:06 +0800954 ftrace_free_records = rec->freelist;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200955 memset(rec, 0, sizeof(*rec));
956 return rec;
957 }
958
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200959 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400960 if (!ftrace_pages->next) {
961 /* allocate another page */
962 ftrace_pages->next =
963 (void *)get_zeroed_page(GFP_KERNEL);
964 if (!ftrace_pages->next)
965 return NULL;
966 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200967 ftrace_pages = ftrace_pages->next;
968 }
969
970 return &ftrace_pages->records[ftrace_pages->index++];
971}
972
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400973static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200974ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200975{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400976 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200977
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800978 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400979 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200980
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400981 rec = ftrace_alloc_dyn_node(ip);
982 if (!rec)
983 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200984
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400985 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +0800986 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +0800987 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200988
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400989 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200990}
991
Steven Rostedt05736a42008-09-22 14:55:47 -0700992static void print_ip_ins(const char *fmt, unsigned char *p)
993{
994 int i;
995
996 printk(KERN_CONT "%s", fmt);
997
998 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
999 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1000}
1001
Steven Rostedt31e88902008-11-14 16:21:19 -08001002static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001003{
1004 switch (failed) {
1005 case -EFAULT:
1006 FTRACE_WARN_ON_ONCE(1);
1007 pr_info("ftrace faulted on modifying ");
1008 print_ip_sym(ip);
1009 break;
1010 case -EINVAL:
1011 FTRACE_WARN_ON_ONCE(1);
1012 pr_info("ftrace failed to modify ");
1013 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001014 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001015 printk(KERN_CONT "\n");
1016 break;
1017 case -EPERM:
1018 FTRACE_WARN_ON_ONCE(1);
1019 pr_info("ftrace faulted on writing ");
1020 print_ip_sym(ip);
1021 break;
1022 default:
1023 FTRACE_WARN_ON_ONCE(1);
1024 pr_info("ftrace faulted on unknown error ");
1025 print_ip_sym(ip);
1026 }
1027}
1028
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001029
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001030/* Return 1 if the address range is reserved for ftrace */
1031int ftrace_text_reserved(void *start, void *end)
1032{
1033 struct dyn_ftrace *rec;
1034 struct ftrace_page *pg;
1035
1036 do_for_each_ftrace_rec(pg, rec) {
1037 if (rec->ip <= (unsigned long)end &&
1038 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1039 return 1;
1040 } while_for_each_ftrace_rec();
1041 return 0;
1042}
1043
1044
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301045static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001046__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001047{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001048 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001049 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001050
Shaohua Lif0001202009-01-09 11:29:42 +08001051 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001052
Steven Rostedt982c3502008-11-15 16:31:41 -05001053 /*
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001054 * If this record is not to be traced or we want to disable it,
1055 * then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001056 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001057 * If we want to enable it and filtering is off, then enable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001058 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001059 * If we want to enable it and filtering is on, enable it only if
1060 * it's filtered
Steven Rostedt982c3502008-11-15 16:31:41 -05001061 */
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001062 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1063 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1064 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001065 }
1066
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001067 /* If the state of this record hasn't changed, then do nothing */
1068 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1069 return 0;
1070
1071 if (flag) {
1072 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001073 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001074 }
1075
1076 rec->flags &= ~FTRACE_FL_ENABLED;
1077 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001078}
1079
1080static void ftrace_replace_code(int enable)
1081{
Steven Rostedt37ad5082008-05-12 21:20:48 +02001082 struct dyn_ftrace *rec;
1083 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001084 int failed;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001085
Steven Rostedt45a4a232011-04-21 23:16:46 -04001086 if (unlikely(ftrace_disabled))
1087 return;
1088
Steven Rostedt265c8312009-02-13 12:43:56 -05001089 do_for_each_ftrace_rec(pg, rec) {
1090 /*
Zhaoleifa9d13c2009-03-13 17:16:34 +08001091 * Skip over free records, records that have
1092 * failed and not converted.
Steven Rostedt265c8312009-02-13 12:43:56 -05001093 */
1094 if (rec->flags & FTRACE_FL_FREE ||
Frederic Weisbecker03303542009-03-16 22:41:00 +01001095 !(rec->flags & FTRACE_FL_CONVERTED))
Steven Rostedt265c8312009-02-13 12:43:56 -05001096 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001097
Steven Rostedt265c8312009-02-13 12:43:56 -05001098 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001099 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001100 ftrace_bug(failed, rec->ip);
1101 /* Stop processing */
1102 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001103 }
1104 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001105}
1106
Ingo Molnare309b412008-05-12 21:20:51 +02001107static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001108ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001109{
1110 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001111 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001112
1113 ip = rec->ip;
1114
Steven Rostedt45a4a232011-04-21 23:16:46 -04001115 if (unlikely(ftrace_disabled))
1116 return 0;
1117
Shaohua Li25aac9d2009-01-09 11:29:40 +08001118 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001119 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001120 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301121 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001122 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301123 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001124}
1125
Steven Rostedt000ab692009-02-17 13:35:06 -05001126/*
1127 * archs can override this function if they must do something
1128 * before the modifying code is performed.
1129 */
1130int __weak ftrace_arch_code_modify_prepare(void)
1131{
1132 return 0;
1133}
1134
1135/*
1136 * archs can override this function if they must do something
1137 * after the modifying code is performed.
1138 */
1139int __weak ftrace_arch_code_modify_post_process(void)
1140{
1141 return 0;
1142}
1143
Ingo Molnare309b412008-05-12 21:20:51 +02001144static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001145{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001146 int *command = data;
1147
Steven Rostedta3583242008-11-11 15:01:42 -05001148 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001149 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001150 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001151 ftrace_replace_code(0);
1152
1153 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1154 ftrace_update_ftrace_func(ftrace_trace_function);
1155
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001156 if (*command & FTRACE_START_FUNC_RET)
1157 ftrace_enable_ftrace_graph_caller();
1158 else if (*command & FTRACE_STOP_FUNC_RET)
1159 ftrace_disable_ftrace_graph_caller();
1160
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001161 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001162}
1163
Ingo Molnare309b412008-05-12 21:20:51 +02001164static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001165{
Steven Rostedt000ab692009-02-17 13:35:06 -05001166 int ret;
1167
1168 ret = ftrace_arch_code_modify_prepare();
1169 FTRACE_WARN_ON(ret);
1170 if (ret)
1171 return;
1172
Rusty Russell784e2d72008-07-28 12:16:31 -05001173 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001174
1175 ret = ftrace_arch_code_modify_post_process();
1176 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001177}
1178
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001179static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001180static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001181
1182static void ftrace_startup_enable(int command)
1183{
1184 if (saved_ftrace_func != ftrace_trace_function) {
1185 saved_ftrace_func = ftrace_trace_function;
1186 command |= FTRACE_UPDATE_TRACE_FUNC;
1187 }
1188
1189 if (!command || !ftrace_enabled)
1190 return;
1191
1192 ftrace_run_update_code(command);
1193}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001194
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001195static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001196{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001197 if (unlikely(ftrace_disabled))
1198 return;
1199
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001200 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001201 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001202
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001203 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001204}
1205
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001206static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001207{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001208 if (unlikely(ftrace_disabled))
1209 return;
1210
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001211 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001212 /*
1213 * Just warn in case of unbalance, no need to kill ftrace, it's not
1214 * critical but the ftrace_call callers may be never nopped again after
1215 * further ftrace uses.
1216 */
1217 WARN_ON_ONCE(ftrace_start_up < 0);
1218
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001219 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001220 command |= FTRACE_DISABLE_CALLS;
1221
1222 if (saved_ftrace_func != ftrace_trace_function) {
1223 saved_ftrace_func = ftrace_trace_function;
1224 command |= FTRACE_UPDATE_TRACE_FUNC;
1225 }
1226
1227 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001228 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001229
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001230 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001231}
1232
Ingo Molnare309b412008-05-12 21:20:51 +02001233static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001234{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001235 if (unlikely(ftrace_disabled))
1236 return;
1237
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001238 /* Force update next time */
1239 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001240 /* ftrace_start_up is true if we want ftrace running */
1241 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001242 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001243}
1244
Ingo Molnare309b412008-05-12 21:20:51 +02001245static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001246{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001247 if (unlikely(ftrace_disabled))
1248 return;
1249
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001250 /* ftrace_start_up is true if ftrace is running */
1251 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001252 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001253}
1254
Steven Rostedt3d083392008-05-12 21:20:42 +02001255static cycle_t ftrace_update_time;
1256static unsigned long ftrace_update_cnt;
1257unsigned long ftrace_update_tot_cnt;
1258
Steven Rostedt31e88902008-11-14 16:21:19 -08001259static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001260{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001261 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301262 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001263
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001264 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001265 ftrace_update_cnt = 0;
1266
Lai Jiangshane94142a2009-03-13 17:51:27 +08001267 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301268
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001269 /* If something went wrong, bail without enabling anything */
1270 if (unlikely(ftrace_disabled))
1271 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001272
Lai Jiangshane94142a2009-03-13 17:51:27 +08001273 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001274 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001275 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301276
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001277 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001278 * Do the initial record conversion from mcount jump
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001279 * to the NOP instructions.
1280 */
1281 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001282 ftrace_free_rec(p);
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001283 continue;
1284 }
1285
1286 p->flags |= FTRACE_FL_CONVERTED;
1287 ftrace_update_cnt++;
1288
1289 /*
1290 * If the tracing is enabled, go ahead and enable the record.
1291 *
1292 * The reason not to enable the record immediatelly is the
1293 * inherent check of ftrace_make_nop/ftrace_make_call for
1294 * correct previous instructions. Making first the NOP
1295 * conversion puts the module to the correct state, thus
1296 * passing the ftrace_make_call check.
1297 */
1298 if (ftrace_start_up) {
1299 int failed = __ftrace_replace_code(p, 1);
1300 if (failed) {
1301 ftrace_bug(failed, p->ip);
1302 ftrace_free_rec(p);
1303 }
1304 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001305 }
1306
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001307 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001308 ftrace_update_time = stop - start;
1309 ftrace_update_tot_cnt += ftrace_update_cnt;
1310
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001311 return 0;
1312}
1313
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001314static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001315{
1316 struct ftrace_page *pg;
1317 int cnt;
1318 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001319
1320 /* allocate a few pages */
1321 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1322 if (!ftrace_pages_start)
1323 return -1;
1324
1325 /*
1326 * Allocate a few more pages.
1327 *
1328 * TODO: have some parser search vmlinux before
1329 * final linking to find all calls to ftrace.
1330 * Then we can:
1331 * a) know how many pages to allocate.
1332 * and/or
1333 * b) set up the table then.
1334 *
1335 * The dynamic code is still necessary for
1336 * modules.
1337 */
1338
1339 pg = ftrace_pages = ftrace_pages_start;
1340
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001341 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001342 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001343 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001344
1345 for (i = 0; i < cnt; i++) {
1346 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1347
1348 /* If we fail, we'll try later anyway */
1349 if (!pg->next)
1350 break;
1351
1352 pg = pg->next;
1353 }
1354
1355 return 0;
1356}
1357
Steven Rostedt5072c592008-05-12 21:20:43 +02001358enum {
1359 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001360 FTRACE_ITER_NOTRACE = (1 << 1),
Steven Rostedt3499e462011-04-21 22:59:12 -04001361 FTRACE_ITER_PRINTALL = (1 << 2),
1362 FTRACE_ITER_HASH = (1 << 3),
Steven Rostedt5072c592008-05-12 21:20:43 +02001363};
1364
1365#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1366
1367struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001368 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001369 loff_t func_pos;
1370 struct ftrace_page *pg;
1371 struct dyn_ftrace *func;
1372 struct ftrace_func_probe *probe;
1373 struct trace_parser parser;
1374 int hidx;
1375 int idx;
1376 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02001377};
1378
Ingo Molnare309b412008-05-12 21:20:51 +02001379static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001380t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001381{
1382 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001383 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001384 struct hlist_head *hhd;
1385
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001386 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001387 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001388
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001389 if (iter->probe)
1390 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001391 retry:
1392 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1393 return NULL;
1394
1395 hhd = &ftrace_func_hash[iter->hidx];
1396
1397 if (hlist_empty(hhd)) {
1398 iter->hidx++;
1399 hnd = NULL;
1400 goto retry;
1401 }
1402
1403 if (!hnd)
1404 hnd = hhd->first;
1405 else {
1406 hnd = hnd->next;
1407 if (!hnd) {
1408 iter->hidx++;
1409 goto retry;
1410 }
1411 }
1412
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001413 if (WARN_ON_ONCE(!hnd))
1414 return NULL;
1415
1416 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1417
1418 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001419}
1420
1421static void *t_hash_start(struct seq_file *m, loff_t *pos)
1422{
1423 struct ftrace_iterator *iter = m->private;
1424 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001425 loff_t l;
1426
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001427 if (iter->func_pos > *pos)
1428 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001429
Li Zefand82d6242009-06-24 09:54:54 +08001430 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001431 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001432 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08001433 if (!p)
1434 break;
1435 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001436 if (!p)
1437 return NULL;
1438
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001439 /* Only set this if we have an item */
1440 iter->flags |= FTRACE_ITER_HASH;
1441
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001442 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001443}
1444
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001445static int
1446t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001447{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001448 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001449
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001450 rec = iter->probe;
1451 if (WARN_ON_ONCE(!rec))
1452 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001453
Steven Rostedt809dcf22009-02-16 23:06:01 -05001454 if (rec->ops->print)
1455 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1456
Steven Rostedtb375a112009-09-17 00:05:58 -04001457 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001458
1459 if (rec->data)
1460 seq_printf(m, ":%p", rec->data);
1461 seq_putc(m, '\n');
1462
1463 return 0;
1464}
1465
1466static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001467t_next(struct seq_file *m, void *v, loff_t *pos)
1468{
1469 struct ftrace_iterator *iter = m->private;
1470 struct dyn_ftrace *rec = NULL;
1471
Steven Rostedt45a4a232011-04-21 23:16:46 -04001472 if (unlikely(ftrace_disabled))
1473 return NULL;
1474
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001475 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001476 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001477
Steven Rostedt5072c592008-05-12 21:20:43 +02001478 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01001479 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02001480
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001481 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001482 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001483
Steven Rostedt5072c592008-05-12 21:20:43 +02001484 retry:
1485 if (iter->idx >= iter->pg->index) {
1486 if (iter->pg->next) {
1487 iter->pg = iter->pg->next;
1488 iter->idx = 0;
1489 goto retry;
1490 }
1491 } else {
1492 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001493 if ((rec->flags & FTRACE_FL_FREE) ||
1494
Steven Rostedt0183fb12008-11-07 22:36:02 -05001495 ((iter->flags & FTRACE_ITER_FILTER) &&
1496 !(rec->flags & FTRACE_FL_FILTER)) ||
1497
Steven Rostedt41c52c02008-05-22 11:46:33 -04001498 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1499 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001500 rec = NULL;
1501 goto retry;
1502 }
1503 }
1504
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001505 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001506 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001507
1508 iter->func = rec;
1509
1510 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02001511}
1512
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001513static void reset_iter_read(struct ftrace_iterator *iter)
1514{
1515 iter->pos = 0;
1516 iter->func_pos = 0;
1517 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02001518}
1519
1520static void *t_start(struct seq_file *m, loff_t *pos)
1521{
1522 struct ftrace_iterator *iter = m->private;
1523 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001524 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001525
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001526 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04001527
1528 if (unlikely(ftrace_disabled))
1529 return NULL;
1530
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001531 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001532 * If an lseek was done, then reset and start from beginning.
1533 */
1534 if (*pos < iter->pos)
1535 reset_iter_read(iter);
1536
1537 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001538 * For set_ftrace_filter reading, if we have the filter
1539 * off, we can short cut and just print out that all
1540 * functions are enabled.
1541 */
1542 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1543 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001544 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001545 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07001546 /* reset in case of seek/pread */
1547 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001548 return iter;
1549 }
1550
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001551 if (iter->flags & FTRACE_ITER_HASH)
1552 return t_hash_start(m, pos);
1553
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001554 /*
1555 * Unfortunately, we need to restart at ftrace_pages_start
1556 * every time we let go of the ftrace_mutex. This is because
1557 * those pointers can change without the lock.
1558 */
Li Zefan694ce0a2009-06-24 09:54:19 +08001559 iter->pg = ftrace_pages_start;
1560 iter->idx = 0;
1561 for (l = 0; l <= *pos; ) {
1562 p = t_next(m, p, &l);
1563 if (!p)
1564 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001565 }
walimis5821e1b2008-11-15 15:19:06 +08001566
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001567 if (!p) {
1568 if (iter->flags & FTRACE_ITER_FILTER)
1569 return t_hash_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001570
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001571 return NULL;
1572 }
1573
1574 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02001575}
1576
1577static void t_stop(struct seq_file *m, void *p)
1578{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001579 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001580}
1581
1582static int t_show(struct seq_file *m, void *v)
1583{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001584 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001585 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02001586
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001587 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001588 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001589
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001590 if (iter->flags & FTRACE_ITER_PRINTALL) {
1591 seq_printf(m, "#### all functions enabled ####\n");
1592 return 0;
1593 }
1594
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001595 rec = iter->func;
1596
Steven Rostedt5072c592008-05-12 21:20:43 +02001597 if (!rec)
1598 return 0;
1599
Steven Rostedtb375a112009-09-17 00:05:58 -04001600 seq_printf(m, "%ps\n", (void *)rec->ip);
Steven Rostedt5072c592008-05-12 21:20:43 +02001601
1602 return 0;
1603}
1604
James Morris88e9d342009-09-22 16:43:43 -07001605static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001606 .start = t_start,
1607 .next = t_next,
1608 .stop = t_stop,
1609 .show = t_show,
1610};
1611
Ingo Molnare309b412008-05-12 21:20:51 +02001612static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001613ftrace_avail_open(struct inode *inode, struct file *file)
1614{
1615 struct ftrace_iterator *iter;
1616 int ret;
1617
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001618 if (unlikely(ftrace_disabled))
1619 return -ENODEV;
1620
Steven Rostedt5072c592008-05-12 21:20:43 +02001621 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1622 if (!iter)
1623 return -ENOMEM;
1624
1625 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001626
1627 ret = seq_open(file, &show_ftrace_seq_ops);
1628 if (!ret) {
1629 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001630
Steven Rostedt5072c592008-05-12 21:20:43 +02001631 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001632 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001633 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001634 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001635
1636 return ret;
1637}
1638
Steven Rostedt41c52c02008-05-22 11:46:33 -04001639static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001640{
1641 struct ftrace_page *pg;
1642 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001643 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001644
Steven Rostedt52baf112009-02-14 01:15:39 -05001645 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001646 if (enable)
1647 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001648 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001649 rec->flags &= ~type;
1650 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001651 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001652}
1653
Ingo Molnare309b412008-05-12 21:20:51 +02001654static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001655ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001656{
1657 struct ftrace_iterator *iter;
1658 int ret = 0;
1659
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001660 if (unlikely(ftrace_disabled))
1661 return -ENODEV;
1662
Steven Rostedt5072c592008-05-12 21:20:43 +02001663 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1664 if (!iter)
1665 return -ENOMEM;
1666
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001667 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1668 kfree(iter);
1669 return -ENOMEM;
1670 }
1671
Steven Rostedt41c52c02008-05-22 11:46:33 -04001672 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001673 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04001674 (file->f_flags & O_TRUNC))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001675 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001676
1677 if (file->f_mode & FMODE_READ) {
1678 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001679 iter->flags = enable ? FTRACE_ITER_FILTER :
1680 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001681
1682 ret = seq_open(file, &show_ftrace_seq_ops);
1683 if (!ret) {
1684 struct seq_file *m = file->private_data;
1685 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08001686 } else {
1687 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02001688 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08001689 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001690 } else
1691 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001692 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001693
1694 return ret;
1695}
1696
Steven Rostedt41c52c02008-05-22 11:46:33 -04001697static int
1698ftrace_filter_open(struct inode *inode, struct file *file)
1699{
1700 return ftrace_regex_open(inode, file, 1);
1701}
1702
1703static int
1704ftrace_notrace_open(struct inode *inode, struct file *file)
1705{
1706 return ftrace_regex_open(inode, file, 0);
1707}
1708
Ingo Molnare309b412008-05-12 21:20:51 +02001709static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001710ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001711{
1712 loff_t ret;
1713
1714 if (file->f_mode & FMODE_READ)
1715 ret = seq_lseek(file, offset, origin);
1716 else
1717 file->f_pos = ret = 1;
1718
1719 return ret;
1720}
1721
Steven Rostedt64e7c442009-02-13 17:08:48 -05001722static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001723{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001724 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08001725 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001726
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001727 switch (type) {
1728 case MATCH_FULL:
1729 if (strcmp(str, regex) == 0)
1730 matched = 1;
1731 break;
1732 case MATCH_FRONT_ONLY:
1733 if (strncmp(str, regex, len) == 0)
1734 matched = 1;
1735 break;
1736 case MATCH_MIDDLE_ONLY:
1737 if (strstr(str, regex))
1738 matched = 1;
1739 break;
1740 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08001741 slen = strlen(str);
1742 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001743 matched = 1;
1744 break;
1745 }
1746
1747 return matched;
1748}
1749
Steven Rostedt64e7c442009-02-13 17:08:48 -05001750static int
1751ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1752{
1753 char str[KSYM_SYMBOL_LEN];
1754
1755 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1756 return ftrace_match(str, regex, len, type);
1757}
1758
Li Zefan311d16d2009-12-08 11:15:11 +08001759static int ftrace_match_records(char *buff, int len, int enable)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001760{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001761 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001762 struct ftrace_page *pg;
1763 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001764 unsigned long flag;
1765 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001766 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001767 int not;
Li Zefan311d16d2009-12-08 11:15:11 +08001768 int found = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001769
Steven Rostedt6a24a242009-02-17 11:20:26 -05001770 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001771 type = filter_parse_regex(buff, len, &search, &not);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001772
1773 search_len = strlen(search);
1774
Steven Rostedt52baf112009-02-14 01:15:39 -05001775 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001776 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001777
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001778 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001779 if (not)
1780 rec->flags &= ~flag;
1781 else
1782 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001783 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001784 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001785 /*
1786 * Only enable filtering if we have a function that
1787 * is filtered on.
1788 */
1789 if (enable && (rec->flags & FTRACE_FL_FILTER))
1790 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001791 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001792 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001793
1794 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02001795}
1796
Steven Rostedt64e7c442009-02-13 17:08:48 -05001797static int
1798ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1799 char *regex, int len, int type)
1800{
1801 char str[KSYM_SYMBOL_LEN];
1802 char *modname;
1803
1804 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1805
1806 if (!modname || strcmp(modname, mod))
1807 return 0;
1808
1809 /* blank search means to match all funcs in the mod */
1810 if (len)
1811 return ftrace_match(str, regex, len, type);
1812 else
1813 return 1;
1814}
1815
Li Zefan311d16d2009-12-08 11:15:11 +08001816static int ftrace_match_module_records(char *buff, char *mod, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001817{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001818 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001819 struct ftrace_page *pg;
1820 struct dyn_ftrace *rec;
1821 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001822 char *search = buff;
1823 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001824 int not = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08001825 int found = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001826
Steven Rostedt6a24a242009-02-17 11:20:26 -05001827 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1828
Steven Rostedt64e7c442009-02-13 17:08:48 -05001829 /* blank or '*' mean the same */
1830 if (strcmp(buff, "*") == 0)
1831 buff[0] = 0;
1832
1833 /* handle the case of 'dont filter this module' */
1834 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1835 buff[0] = 0;
1836 not = 1;
1837 }
1838
1839 if (strlen(buff)) {
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001840 type = filter_parse_regex(buff, strlen(buff), &search, &not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001841 search_len = strlen(search);
1842 }
1843
Steven Rostedt52baf112009-02-14 01:15:39 -05001844 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001845
Steven Rostedt45a4a232011-04-21 23:16:46 -04001846 if (unlikely(ftrace_disabled))
1847 goto out_unlock;
1848
1849 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt64e7c442009-02-13 17:08:48 -05001850
1851 if (ftrace_match_module_record(rec, mod,
1852 search, search_len, type)) {
1853 if (not)
1854 rec->flags &= ~flag;
1855 else
1856 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001857 found = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001858 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001859 if (enable && (rec->flags & FTRACE_FL_FILTER))
1860 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001861
1862 } while_for_each_ftrace_rec();
Steven Rostedt45a4a232011-04-21 23:16:46 -04001863 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05001864 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001865
1866 return found;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001867}
1868
Steven Rostedtf6180772009-02-14 00:40:25 -05001869/*
1870 * We register the module command as a template to show others how
1871 * to register the a command as well.
1872 */
1873
1874static int
1875ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1876{
1877 char *mod;
1878
1879 /*
1880 * cmd == 'mod' because we only registered this func
1881 * for the 'mod' ftrace_func_command.
1882 * But if you register one func with multiple commands,
1883 * you can tell which command was used by the cmd
1884 * parameter.
1885 */
1886
1887 /* we must have a module name */
1888 if (!param)
1889 return -EINVAL;
1890
1891 mod = strsep(&param, ":");
1892 if (!strlen(mod))
1893 return -EINVAL;
1894
Li Zefan311d16d2009-12-08 11:15:11 +08001895 if (ftrace_match_module_records(func, mod, enable))
1896 return 0;
1897 return -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05001898}
1899
1900static struct ftrace_func_command ftrace_mod_cmd = {
1901 .name = "mod",
1902 .func = ftrace_mod_callback,
1903};
1904
1905static int __init ftrace_mod_cmd_init(void)
1906{
1907 return register_ftrace_command(&ftrace_mod_cmd);
1908}
1909device_initcall(ftrace_mod_cmd_init);
1910
Steven Rostedt59df055f2009-02-14 15:29:06 -05001911static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001912function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001913{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001914 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001915 struct hlist_head *hhd;
1916 struct hlist_node *n;
1917 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001918
1919 key = hash_long(ip, FTRACE_HASH_BITS);
1920
1921 hhd = &ftrace_func_hash[key];
1922
1923 if (hlist_empty(hhd))
1924 return;
1925
1926 /*
1927 * Disable preemption for these calls to prevent a RCU grace
1928 * period. This syncs the hash iteration and freeing of items
1929 * on the hash. rcu_read_lock is too dangerous here.
1930 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04001931 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05001932 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1933 if (entry->ip == ip)
1934 entry->ops->func(ip, parent_ip, &entry->data);
1935 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04001936 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05001937}
1938
Steven Rostedtb6887d72009-02-17 12:32:04 -05001939static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001940{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001941 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001942};
1943
Steven Rostedtb6887d72009-02-17 12:32:04 -05001944static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001945
Steven Rostedtb6887d72009-02-17 12:32:04 -05001946static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001947{
1948 int i;
1949
Steven Rostedtb6887d72009-02-17 12:32:04 -05001950 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001951 return;
1952
1953 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1954 struct hlist_head *hhd = &ftrace_func_hash[i];
1955 if (hhd->first)
1956 break;
1957 }
1958 /* Nothing registered? */
1959 if (i == FTRACE_FUNC_HASHSIZE)
1960 return;
1961
Steven Rostedtb6887d72009-02-17 12:32:04 -05001962 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001963 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001964 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001965}
1966
Steven Rostedtb6887d72009-02-17 12:32:04 -05001967static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001968{
1969 int i;
1970
Steven Rostedtb6887d72009-02-17 12:32:04 -05001971 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001972 return;
1973
1974 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1975 struct hlist_head *hhd = &ftrace_func_hash[i];
1976 if (hhd->first)
1977 return;
1978 }
1979
1980 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05001981 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001982 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001983 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001984}
1985
1986
1987static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1988{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001989 struct ftrace_func_probe *entry =
1990 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001991
1992 if (entry->ops->free)
1993 entry->ops->free(&entry->data);
1994 kfree(entry);
1995}
1996
1997
1998int
Steven Rostedtb6887d72009-02-17 12:32:04 -05001999register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002000 void *data)
2001{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002002 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002003 struct ftrace_page *pg;
2004 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002005 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002006 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002007 int count = 0;
2008 char *search;
2009
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002010 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002011 len = strlen(search);
2012
Steven Rostedtb6887d72009-02-17 12:32:04 -05002013 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002014 if (WARN_ON(not))
2015 return -EINVAL;
2016
2017 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002018
Steven Rostedt45a4a232011-04-21 23:16:46 -04002019 if (unlikely(ftrace_disabled))
2020 goto out_unlock;
2021
2022 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002023
2024 if (!ftrace_match_record(rec, search, len, type))
2025 continue;
2026
2027 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2028 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002029 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002030 if (!count)
2031 count = -ENOMEM;
2032 goto out_unlock;
2033 }
2034
2035 count++;
2036
2037 entry->data = data;
2038
2039 /*
2040 * The caller might want to do something special
2041 * for each function we find. We call the callback
2042 * to give the caller an opportunity to do so.
2043 */
2044 if (ops->callback) {
2045 if (ops->callback(rec->ip, &entry->data) < 0) {
2046 /* caller does not like this func */
2047 kfree(entry);
2048 continue;
2049 }
2050 }
2051
2052 entry->ops = ops;
2053 entry->ip = rec->ip;
2054
2055 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2056 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2057
2058 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002059 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002060
2061 out_unlock:
2062 mutex_unlock(&ftrace_lock);
2063
2064 return count;
2065}
2066
2067enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002068 PROBE_TEST_FUNC = 1,
2069 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002070};
2071
2072static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002073__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002074 void *data, int flags)
2075{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002076 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002077 struct hlist_node *n, *tmp;
2078 char str[KSYM_SYMBOL_LEN];
2079 int type = MATCH_FULL;
2080 int i, len = 0;
2081 char *search;
2082
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002083 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002084 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002085 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002086 int not;
2087
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002088 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002089 len = strlen(search);
2090
Steven Rostedtb6887d72009-02-17 12:32:04 -05002091 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002092 if (WARN_ON(not))
2093 return;
2094 }
2095
2096 mutex_lock(&ftrace_lock);
2097 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2098 struct hlist_head *hhd = &ftrace_func_hash[i];
2099
2100 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2101
2102 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002103 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002104 continue;
2105
Steven Rostedtb6887d72009-02-17 12:32:04 -05002106 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002107 continue;
2108
2109 /* do this last, since it is the most expensive */
2110 if (glob) {
2111 kallsyms_lookup(entry->ip, NULL, NULL,
2112 NULL, str);
2113 if (!ftrace_match(str, glob, len, type))
2114 continue;
2115 }
2116
2117 hlist_del(&entry->node);
2118 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2119 }
2120 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002121 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002122 mutex_unlock(&ftrace_lock);
2123}
2124
2125void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002126unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002127 void *data)
2128{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002129 __unregister_ftrace_function_probe(glob, ops, data,
2130 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002131}
2132
2133void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002134unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002135{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002136 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002137}
2138
Steven Rostedtb6887d72009-02-17 12:32:04 -05002139void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002140{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002141 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002142}
2143
Steven Rostedtf6180772009-02-14 00:40:25 -05002144static LIST_HEAD(ftrace_commands);
2145static DEFINE_MUTEX(ftrace_cmd_mutex);
2146
2147int register_ftrace_command(struct ftrace_func_command *cmd)
2148{
2149 struct ftrace_func_command *p;
2150 int ret = 0;
2151
2152 mutex_lock(&ftrace_cmd_mutex);
2153 list_for_each_entry(p, &ftrace_commands, list) {
2154 if (strcmp(cmd->name, p->name) == 0) {
2155 ret = -EBUSY;
2156 goto out_unlock;
2157 }
2158 }
2159 list_add(&cmd->list, &ftrace_commands);
2160 out_unlock:
2161 mutex_unlock(&ftrace_cmd_mutex);
2162
2163 return ret;
2164}
2165
2166int unregister_ftrace_command(struct ftrace_func_command *cmd)
2167{
2168 struct ftrace_func_command *p, *n;
2169 int ret = -ENODEV;
2170
2171 mutex_lock(&ftrace_cmd_mutex);
2172 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2173 if (strcmp(cmd->name, p->name) == 0) {
2174 ret = 0;
2175 list_del_init(&p->list);
2176 goto out_unlock;
2177 }
2178 }
2179 out_unlock:
2180 mutex_unlock(&ftrace_cmd_mutex);
2181
2182 return ret;
2183}
2184
Steven Rostedt64e7c442009-02-13 17:08:48 -05002185static int ftrace_process_regex(char *buff, int len, int enable)
2186{
Steven Rostedtf6180772009-02-14 00:40:25 -05002187 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002188 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002189 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002190
2191 func = strsep(&next, ":");
2192
2193 if (!next) {
Li Zefan311d16d2009-12-08 11:15:11 +08002194 if (ftrace_match_records(func, len, enable))
2195 return 0;
2196 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002197 }
2198
Steven Rostedtf6180772009-02-14 00:40:25 -05002199 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002200
2201 command = strsep(&next, ":");
2202
Steven Rostedtf6180772009-02-14 00:40:25 -05002203 mutex_lock(&ftrace_cmd_mutex);
2204 list_for_each_entry(p, &ftrace_commands, list) {
2205 if (strcmp(p->name, command) == 0) {
2206 ret = p->func(func, command, next, enable);
2207 goto out_unlock;
2208 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002209 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002210 out_unlock:
2211 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002212
Steven Rostedtf6180772009-02-14 00:40:25 -05002213 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002214}
2215
Ingo Molnare309b412008-05-12 21:20:51 +02002216static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002217ftrace_regex_write(struct file *file, const char __user *ubuf,
2218 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002219{
2220 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002221 struct trace_parser *parser;
2222 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002223
Li Zefan4ba79782009-09-22 13:52:20 +08002224 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002225 return 0;
2226
Steven Rostedt41c52c02008-05-22 11:46:33 -04002227 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002228
Steven Rostedt45a4a232011-04-21 23:16:46 -04002229 ret = -ENODEV;
2230 if (unlikely(ftrace_disabled))
2231 goto out_unlock;
2232
Steven Rostedt5072c592008-05-12 21:20:43 +02002233 if (file->f_mode & FMODE_READ) {
2234 struct seq_file *m = file->private_data;
2235 iter = m->private;
2236 } else
2237 iter = file->private_data;
2238
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002239 parser = &iter->parser;
2240 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002241
Li Zefan4ba79782009-09-22 13:52:20 +08002242 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002243 !trace_parser_cont(parser)) {
2244 ret = ftrace_process_regex(parser->buffer,
2245 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002246 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002247 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002248 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002249 }
2250
Steven Rostedt5072c592008-05-12 21:20:43 +02002251 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002252out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002253 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002254
Steven Rostedt5072c592008-05-12 21:20:43 +02002255 return ret;
2256}
2257
Steven Rostedt41c52c02008-05-22 11:46:33 -04002258static ssize_t
2259ftrace_filter_write(struct file *file, const char __user *ubuf,
2260 size_t cnt, loff_t *ppos)
2261{
2262 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2263}
2264
2265static ssize_t
2266ftrace_notrace_write(struct file *file, const char __user *ubuf,
2267 size_t cnt, loff_t *ppos)
2268{
2269 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2270}
2271
2272static void
2273ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2274{
2275 if (unlikely(ftrace_disabled))
2276 return;
2277
2278 mutex_lock(&ftrace_regex_lock);
2279 if (reset)
2280 ftrace_filter_reset(enable);
2281 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002282 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002283 mutex_unlock(&ftrace_regex_lock);
2284}
2285
Steven Rostedt77a2b372008-05-12 21:20:45 +02002286/**
2287 * ftrace_set_filter - set a function to filter on in ftrace
2288 * @buf - the string that holds the function filter text.
2289 * @len - the length of the string.
2290 * @reset - non zero to reset all filters before applying this filter.
2291 *
2292 * Filters denote which functions should be enabled when tracing is enabled.
2293 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2294 */
Ingo Molnare309b412008-05-12 21:20:51 +02002295void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002296{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002297 ftrace_set_regex(buf, len, reset, 1);
2298}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002299
Steven Rostedt41c52c02008-05-22 11:46:33 -04002300/**
2301 * ftrace_set_notrace - set a function to not trace in ftrace
2302 * @buf - the string that holds the function notrace text.
2303 * @len - the length of the string.
2304 * @reset - non zero to reset all filters before applying this filter.
2305 *
2306 * Notrace Filters denote which functions should not be enabled when tracing
2307 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2308 * for tracing.
2309 */
2310void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2311{
2312 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002313}
2314
Steven Rostedt2af15d62009-05-28 13:37:24 -04002315/*
2316 * command line interface to allow users to set filters on boot up.
2317 */
2318#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2319static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2320static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2321
2322static int __init set_ftrace_notrace(char *str)
2323{
2324 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2325 return 1;
2326}
2327__setup("ftrace_notrace=", set_ftrace_notrace);
2328
2329static int __init set_ftrace_filter(char *str)
2330{
2331 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2332 return 1;
2333}
2334__setup("ftrace_filter=", set_ftrace_filter);
2335
Stefan Assmann369bc182009-10-12 22:17:21 +02002336#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002337static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002338static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2339
Stefan Assmann369bc182009-10-12 22:17:21 +02002340static int __init set_graph_function(char *str)
2341{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002342 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002343 return 1;
2344}
2345__setup("ftrace_graph_filter=", set_graph_function);
2346
2347static void __init set_ftrace_early_graph(char *buf)
2348{
2349 int ret;
2350 char *func;
2351
2352 while (buf) {
2353 func = strsep(&buf, ",");
2354 /* we allow only one expression at a time */
2355 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2356 func);
2357 if (ret)
2358 printk(KERN_DEBUG "ftrace: function %s not "
2359 "traceable\n", func);
2360 }
2361}
2362#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2363
Steven Rostedt2af15d62009-05-28 13:37:24 -04002364static void __init set_ftrace_early_filter(char *buf, int enable)
2365{
2366 char *func;
2367
2368 while (buf) {
2369 func = strsep(&buf, ",");
2370 ftrace_set_regex(func, strlen(func), 0, enable);
2371 }
2372}
2373
2374static void __init set_ftrace_early_filters(void)
2375{
2376 if (ftrace_filter_buf[0])
2377 set_ftrace_early_filter(ftrace_filter_buf, 1);
2378 if (ftrace_notrace_buf[0])
2379 set_ftrace_early_filter(ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002380#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2381 if (ftrace_graph_buf[0])
2382 set_ftrace_early_graph(ftrace_graph_buf);
2383#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002384}
2385
Ingo Molnare309b412008-05-12 21:20:51 +02002386static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002387ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002388{
2389 struct seq_file *m = (struct seq_file *)file->private_data;
2390 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002391 struct trace_parser *parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02002392
Steven Rostedt41c52c02008-05-22 11:46:33 -04002393 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002394 if (file->f_mode & FMODE_READ) {
2395 iter = m->private;
2396
2397 seq_release(inode, file);
2398 } else
2399 iter = file->private_data;
2400
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002401 parser = &iter->parser;
2402 if (trace_parser_loaded(parser)) {
2403 parser->buffer[parser->idx] = 0;
2404 ftrace_match_records(parser->buffer, parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002405 }
2406
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002407 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002408 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002409 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002410 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002411
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002412 trace_parser_put(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002413 kfree(iter);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002414
Steven Rostedt41c52c02008-05-22 11:46:33 -04002415 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002416 return 0;
2417}
2418
Steven Rostedt41c52c02008-05-22 11:46:33 -04002419static int
2420ftrace_filter_release(struct inode *inode, struct file *file)
2421{
2422 return ftrace_regex_release(inode, file, 1);
2423}
2424
2425static int
2426ftrace_notrace_release(struct inode *inode, struct file *file)
2427{
2428 return ftrace_regex_release(inode, file, 0);
2429}
2430
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002431static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002432 .open = ftrace_avail_open,
2433 .read = seq_read,
2434 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002435 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002436};
2437
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002438static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002439 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002440 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002441 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002442 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002443 .release = ftrace_filter_release,
2444};
2445
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002446static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002447 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002448 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002449 .write = ftrace_notrace_write,
2450 .llseek = ftrace_regex_lseek,
2451 .release = ftrace_notrace_release,
2452};
2453
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002454#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2455
2456static DEFINE_MUTEX(graph_lock);
2457
2458int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002459int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002460unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2461
2462static void *
Li Zefan85951842009-06-24 09:54:00 +08002463__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002464{
Li Zefan85951842009-06-24 09:54:00 +08002465 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002466 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002467 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002468}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002469
Li Zefan85951842009-06-24 09:54:00 +08002470static void *
2471g_next(struct seq_file *m, void *v, loff_t *pos)
2472{
2473 (*pos)++;
2474 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002475}
2476
2477static void *g_start(struct seq_file *m, loff_t *pos)
2478{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002479 mutex_lock(&graph_lock);
2480
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002481 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002482 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002483 return (void *)1;
2484
Li Zefan85951842009-06-24 09:54:00 +08002485 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002486}
2487
2488static void g_stop(struct seq_file *m, void *p)
2489{
2490 mutex_unlock(&graph_lock);
2491}
2492
2493static int g_show(struct seq_file *m, void *v)
2494{
2495 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002496
2497 if (!ptr)
2498 return 0;
2499
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002500 if (ptr == (unsigned long *)1) {
2501 seq_printf(m, "#### all functions enabled ####\n");
2502 return 0;
2503 }
2504
Steven Rostedtb375a112009-09-17 00:05:58 -04002505 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002506
2507 return 0;
2508}
2509
James Morris88e9d342009-09-22 16:43:43 -07002510static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002511 .start = g_start,
2512 .next = g_next,
2513 .stop = g_stop,
2514 .show = g_show,
2515};
2516
2517static int
2518ftrace_graph_open(struct inode *inode, struct file *file)
2519{
2520 int ret = 0;
2521
2522 if (unlikely(ftrace_disabled))
2523 return -ENODEV;
2524
2525 mutex_lock(&graph_lock);
2526 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002527 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002528 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002529 ftrace_graph_count = 0;
2530 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2531 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002532 mutex_unlock(&graph_lock);
2533
Li Zefana4ec5e02009-09-18 14:06:28 +08002534 if (file->f_mode & FMODE_READ)
2535 ret = seq_open(file, &ftrace_graph_seq_ops);
2536
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002537 return ret;
2538}
2539
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002540static int
Li Zefan87827112009-07-23 11:29:11 +08002541ftrace_graph_release(struct inode *inode, struct file *file)
2542{
2543 if (file->f_mode & FMODE_READ)
2544 seq_release(inode, file);
2545 return 0;
2546}
2547
2548static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002549ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002550{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002551 struct dyn_ftrace *rec;
2552 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002553 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002554 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002555 int type, not;
2556 char *search;
2557 bool exists;
2558 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002559
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002560 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002561 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002562 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2563 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002564
2565 search_len = strlen(search);
2566
Steven Rostedt52baf112009-02-14 01:15:39 -05002567 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002568
2569 if (unlikely(ftrace_disabled)) {
2570 mutex_unlock(&ftrace_lock);
2571 return -ENODEV;
2572 }
2573
Steven Rostedt265c8312009-02-13 12:43:56 -05002574 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002575
Steven Rostedt45a4a232011-04-21 23:16:46 -04002576 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05002577 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002578
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002579 if (ftrace_match_record(rec, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002580 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002581 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002582 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002583 if (array[i] == rec->ip) {
2584 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002585 break;
2586 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002587 }
2588
2589 if (!not) {
2590 fail = 0;
2591 if (!exists) {
2592 array[(*idx)++] = rec->ip;
2593 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2594 goto out;
2595 }
2596 } else {
2597 if (exists) {
2598 array[i] = array[--(*idx)];
2599 array[*idx] = 0;
2600 fail = 0;
2601 }
2602 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002603 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002604 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002605out:
Steven Rostedt52baf112009-02-14 01:15:39 -05002606 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002607
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002608 if (fail)
2609 return -EINVAL;
2610
2611 ftrace_graph_filter_enabled = 1;
2612 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002613}
2614
2615static ssize_t
2616ftrace_graph_write(struct file *file, const char __user *ubuf,
2617 size_t cnt, loff_t *ppos)
2618{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002619 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08002620 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002621
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002622 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002623 return 0;
2624
2625 mutex_lock(&graph_lock);
2626
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002627 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2628 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08002629 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002630 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002631
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002632 read = trace_get_user(&parser, ubuf, cnt, ppos);
2633
Li Zefan4ba79782009-09-22 13:52:20 +08002634 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002635 parser.buffer[parser.idx] = 0;
2636
2637 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08002638 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002639 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002640 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08002641 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002642 }
2643
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002644 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08002645
2646out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002647 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08002648out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002649 mutex_unlock(&graph_lock);
2650
2651 return ret;
2652}
2653
2654static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08002655 .open = ftrace_graph_open,
2656 .read = seq_read,
2657 .write = ftrace_graph_write,
2658 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002659 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002660};
2661#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2662
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002663static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002664{
Steven Rostedt5072c592008-05-12 21:20:43 +02002665
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002666 trace_create_file("available_filter_functions", 0444,
2667 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002668
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002669 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2670 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002671
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002672 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002673 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002674
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002675#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002676 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002677 NULL,
2678 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002679#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2680
Steven Rostedt5072c592008-05-12 21:20:43 +02002681 return 0;
2682}
2683
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002684static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08002685 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002686 unsigned long *end)
2687{
2688 unsigned long *p;
2689 unsigned long addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002690
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002691 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002692 p = start;
2693 while (p < end) {
2694 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002695 /*
2696 * Some architecture linkers will pad between
2697 * the different mcount_loc sections of different
2698 * object files to satisfy alignments.
2699 * Skip any NULL pointers.
2700 */
2701 if (!addr)
2702 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002703 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002704 }
2705
Steven Rostedt31e88902008-11-14 16:21:19 -08002706 ftrace_update_code(mod);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002707 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002708
2709 return 0;
2710}
2711
Steven Rostedt93eb6772009-04-15 13:24:06 -04002712#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002713void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002714{
2715 struct dyn_ftrace *rec;
2716 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04002717
Steven Rostedt93eb6772009-04-15 13:24:06 -04002718 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002719
2720 if (ftrace_disabled)
2721 goto out_unlock;
2722
Steven Rostedt93eb6772009-04-15 13:24:06 -04002723 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002724 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04002725 /*
2726 * rec->ip is changed in ftrace_free_rec()
2727 * It should not between s and e if record was freed.
2728 */
2729 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2730 ftrace_free_rec(rec);
2731 }
2732 } while_for_each_ftrace_rec();
Steven Rostedt45a4a232011-04-21 23:16:46 -04002733 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04002734 mutex_unlock(&ftrace_lock);
2735}
2736
2737static void ftrace_init_module(struct module *mod,
2738 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002739{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002740 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002741 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002742 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002743}
2744
Steven Rostedt93eb6772009-04-15 13:24:06 -04002745static int ftrace_module_notify(struct notifier_block *self,
2746 unsigned long val, void *data)
2747{
2748 struct module *mod = data;
2749
2750 switch (val) {
2751 case MODULE_STATE_COMING:
2752 ftrace_init_module(mod, mod->ftrace_callsites,
2753 mod->ftrace_callsites +
2754 mod->num_ftrace_callsites);
2755 break;
2756 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002757 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04002758 break;
2759 }
2760
2761 return 0;
2762}
2763#else
2764static int ftrace_module_notify(struct notifier_block *self,
2765 unsigned long val, void *data)
2766{
2767 return 0;
2768}
2769#endif /* CONFIG_MODULES */
2770
2771struct notifier_block ftrace_module_nb = {
2772 .notifier_call = ftrace_module_notify,
2773 .priority = 0,
2774};
2775
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002776extern unsigned long __start_mcount_loc[];
2777extern unsigned long __stop_mcount_loc[];
2778
2779void __init ftrace_init(void)
2780{
2781 unsigned long count, addr, flags;
2782 int ret;
2783
2784 /* Keep the ftrace pointer to the stub */
2785 addr = (unsigned long)ftrace_stub;
2786
2787 local_irq_save(flags);
2788 ftrace_dyn_arch_init(&addr);
2789 local_irq_restore(flags);
2790
2791 /* ftrace_dyn_arch_init places the return code in addr */
2792 if (addr)
2793 goto failed;
2794
2795 count = __stop_mcount_loc - __start_mcount_loc;
2796
2797 ret = ftrace_dyn_table_alloc(count);
2798 if (ret)
2799 goto failed;
2800
2801 last_ftrace_enabled = ftrace_enabled = 1;
2802
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002803 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08002804 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002805 __stop_mcount_loc);
2806
Steven Rostedt93eb6772009-04-15 13:24:06 -04002807 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002808 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002809 pr_warning("Failed to register trace ftrace module notifier\n");
2810
Steven Rostedt2af15d62009-05-28 13:37:24 -04002811 set_ftrace_early_filters();
2812
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002813 return;
2814 failed:
2815 ftrace_disabled = 1;
2816}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002817
Steven Rostedt3d083392008-05-12 21:20:42 +02002818#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002819
2820static int __init ftrace_nodyn_init(void)
2821{
2822 ftrace_enabled = 1;
2823 return 0;
2824}
2825device_initcall(ftrace_nodyn_init);
2826
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002827static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2828static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002829/* Keep as macros so we do not need to define the commands */
2830# define ftrace_startup(command) do { } while (0)
2831# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002832# define ftrace_startup_sysctl() do { } while (0)
2833# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002834#endif /* CONFIG_DYNAMIC_FTRACE */
2835
Steven Rostedte32d8952008-12-04 00:26:41 -05002836static void clear_ftrace_swapper(void)
2837{
2838 struct task_struct *p;
2839 int cpu;
2840
2841 get_online_cpus();
2842 for_each_online_cpu(cpu) {
2843 p = idle_task(cpu);
2844 clear_tsk_trace_trace(p);
2845 }
2846 put_online_cpus();
2847}
2848
2849static void set_ftrace_swapper(void)
2850{
2851 struct task_struct *p;
2852 int cpu;
2853
2854 get_online_cpus();
2855 for_each_online_cpu(cpu) {
2856 p = idle_task(cpu);
2857 set_tsk_trace_trace(p);
2858 }
2859 put_online_cpus();
2860}
2861
2862static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002863{
2864 struct task_struct *p;
2865
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002866 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002867 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002868 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002869 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002870 rcu_read_unlock();
2871
Steven Rostedte32d8952008-12-04 00:26:41 -05002872 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002873}
2874
Steven Rostedte32d8952008-12-04 00:26:41 -05002875static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002876{
2877 struct task_struct *p;
2878
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002879 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002880 do_each_pid_task(pid, PIDTYPE_PID, p) {
2881 set_tsk_trace_trace(p);
2882 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002883 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002884}
2885
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002886static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002887{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002888 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002889 clear_ftrace_swapper();
2890 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002891 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05002892}
2893
2894static void set_ftrace_pid_task(struct pid *pid)
2895{
2896 if (pid == ftrace_swapper_pid)
2897 set_ftrace_swapper();
2898 else
2899 set_ftrace_pid(pid);
2900}
2901
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002902static int ftrace_pid_add(int p)
2903{
2904 struct pid *pid;
2905 struct ftrace_pid *fpid;
2906 int ret = -EINVAL;
2907
2908 mutex_lock(&ftrace_lock);
2909
2910 if (!p)
2911 pid = ftrace_swapper_pid;
2912 else
2913 pid = find_get_pid(p);
2914
2915 if (!pid)
2916 goto out;
2917
2918 ret = 0;
2919
2920 list_for_each_entry(fpid, &ftrace_pids, list)
2921 if (fpid->pid == pid)
2922 goto out_put;
2923
2924 ret = -ENOMEM;
2925
2926 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2927 if (!fpid)
2928 goto out_put;
2929
2930 list_add(&fpid->list, &ftrace_pids);
2931 fpid->pid = pid;
2932
2933 set_ftrace_pid_task(pid);
2934
2935 ftrace_update_pid_func();
2936 ftrace_startup_enable(0);
2937
2938 mutex_unlock(&ftrace_lock);
2939 return 0;
2940
2941out_put:
2942 if (pid != ftrace_swapper_pid)
2943 put_pid(pid);
2944
2945out:
2946 mutex_unlock(&ftrace_lock);
2947 return ret;
2948}
2949
2950static void ftrace_pid_reset(void)
2951{
2952 struct ftrace_pid *fpid, *safe;
2953
2954 mutex_lock(&ftrace_lock);
2955 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2956 struct pid *pid = fpid->pid;
2957
2958 clear_ftrace_pid_task(pid);
2959
2960 list_del(&fpid->list);
2961 kfree(fpid);
2962 }
2963
2964 ftrace_update_pid_func();
2965 ftrace_startup_enable(0);
2966
2967 mutex_unlock(&ftrace_lock);
2968}
2969
2970static void *fpid_start(struct seq_file *m, loff_t *pos)
2971{
2972 mutex_lock(&ftrace_lock);
2973
2974 if (list_empty(&ftrace_pids) && (!*pos))
2975 return (void *) 1;
2976
2977 return seq_list_start(&ftrace_pids, *pos);
2978}
2979
2980static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2981{
2982 if (v == (void *)1)
2983 return NULL;
2984
2985 return seq_list_next(v, &ftrace_pids, pos);
2986}
2987
2988static void fpid_stop(struct seq_file *m, void *p)
2989{
2990 mutex_unlock(&ftrace_lock);
2991}
2992
2993static int fpid_show(struct seq_file *m, void *v)
2994{
2995 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2996
2997 if (v == (void *)1) {
2998 seq_printf(m, "no pid\n");
2999 return 0;
3000 }
3001
3002 if (fpid->pid == ftrace_swapper_pid)
3003 seq_printf(m, "swapper tasks\n");
3004 else
3005 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3006
3007 return 0;
3008}
3009
3010static const struct seq_operations ftrace_pid_sops = {
3011 .start = fpid_start,
3012 .next = fpid_next,
3013 .stop = fpid_stop,
3014 .show = fpid_show,
3015};
3016
3017static int
3018ftrace_pid_open(struct inode *inode, struct file *file)
3019{
3020 int ret = 0;
3021
3022 if ((file->f_mode & FMODE_WRITE) &&
3023 (file->f_flags & O_TRUNC))
3024 ftrace_pid_reset();
3025
3026 if (file->f_mode & FMODE_READ)
3027 ret = seq_open(file, &ftrace_pid_sops);
3028
3029 return ret;
3030}
3031
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003032static ssize_t
3033ftrace_pid_write(struct file *filp, const char __user *ubuf,
3034 size_t cnt, loff_t *ppos)
3035{
Ingo Molnar457dc922009-11-23 11:03:28 +01003036 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003037 long val;
3038 int ret;
3039
3040 if (cnt >= sizeof(buf))
3041 return -EINVAL;
3042
3043 if (copy_from_user(&buf, ubuf, cnt))
3044 return -EFAULT;
3045
3046 buf[cnt] = 0;
3047
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003048 /*
3049 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3050 * to clean the filter quietly.
3051 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003052 tmp = strstrip(buf);
3053 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003054 return 1;
3055
Ingo Molnar457dc922009-11-23 11:03:28 +01003056 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003057 if (ret < 0)
3058 return ret;
3059
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003060 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003061
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003062 return ret ? ret : cnt;
3063}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003064
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003065static int
3066ftrace_pid_release(struct inode *inode, struct file *file)
3067{
3068 if (file->f_mode & FMODE_READ)
3069 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003070
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003071 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003072}
3073
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003074static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003075 .open = ftrace_pid_open,
3076 .write = ftrace_pid_write,
3077 .read = seq_read,
3078 .llseek = seq_lseek,
3079 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003080};
3081
3082static __init int ftrace_init_debugfs(void)
3083{
3084 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003085
3086 d_tracer = tracing_init_dentry();
3087 if (!d_tracer)
3088 return 0;
3089
3090 ftrace_init_dyn_debugfs(d_tracer);
3091
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003092 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3093 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003094
3095 ftrace_profile_debugfs(d_tracer);
3096
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003097 return 0;
3098}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003099fs_initcall(ftrace_init_debugfs);
3100
Steven Rostedt3d083392008-05-12 21:20:42 +02003101/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003102 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003103 *
3104 * This function should be used by panic code. It stops ftrace
3105 * but in a not so nice way. If you need to simply kill ftrace
3106 * from a non-atomic section, use ftrace_kill.
3107 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003108void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003109{
3110 ftrace_disabled = 1;
3111 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003112 clear_ftrace_function();
3113}
3114
3115/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003116 * register_ftrace_function - register a function for profiling
3117 * @ops - ops structure that holds the function for profiling.
3118 *
3119 * Register a function to be called by all functions in the
3120 * kernel.
3121 *
3122 * Note: @ops->func and all the functions it calls must be labeled
3123 * with "notrace", otherwise it will go into a
3124 * recursive loop.
3125 */
3126int register_ftrace_function(struct ftrace_ops *ops)
3127{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003128 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003129
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003130 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003131
Steven Rostedt45a4a232011-04-21 23:16:46 -04003132 if (unlikely(ftrace_disabled))
3133 goto out_unlock;
3134
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003135 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003136 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003137
Steven Rostedt45a4a232011-04-21 23:16:46 -04003138 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003139 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003140 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003141}
3142
3143/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003144 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003145 * @ops - ops structure that holds the function to unregister
3146 *
3147 * Unregister a function that was added to be called by ftrace profiling.
3148 */
3149int unregister_ftrace_function(struct ftrace_ops *ops)
3150{
3151 int ret;
3152
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003153 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003154 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003155 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003156 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003157
3158 return ret;
3159}
3160
Ingo Molnare309b412008-05-12 21:20:51 +02003161int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003162ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003163 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003164 loff_t *ppos)
3165{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003166 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003167
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003168 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003169
Steven Rostedt45a4a232011-04-21 23:16:46 -04003170 if (unlikely(ftrace_disabled))
3171 goto out;
3172
3173 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003174
Li Zefana32c7762009-06-26 16:55:51 +08003175 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003176 goto out;
3177
Li Zefana32c7762009-06-26 16:55:51 +08003178 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003179
3180 if (ftrace_enabled) {
3181
3182 ftrace_startup_sysctl();
3183
3184 /* we are starting ftrace again */
3185 if (ftrace_list != &ftrace_list_end) {
3186 if (ftrace_list->next == &ftrace_list_end)
3187 ftrace_trace_function = ftrace_list->func;
3188 else
3189 ftrace_trace_function = ftrace_list_func;
3190 }
3191
3192 } else {
3193 /* stopping ftrace calls (just send to ftrace_stub) */
3194 ftrace_trace_function = ftrace_stub;
3195
3196 ftrace_shutdown_sysctl();
3197 }
3198
3199 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003200 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003201 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003202}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003203
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003204#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003205
Steven Rostedt597af812009-04-03 15:24:12 -04003206static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003207static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003208
Steven Rostedte49dc192008-12-02 23:50:05 -05003209int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3210{
3211 return 0;
3212}
3213
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003214/* The callbacks that hook a function */
3215trace_func_graph_ret_t ftrace_graph_return =
3216 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003217trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003218
3219/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3220static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3221{
3222 int i;
3223 int ret = 0;
3224 unsigned long flags;
3225 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3226 struct task_struct *g, *t;
3227
3228 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3229 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3230 * sizeof(struct ftrace_ret_stack),
3231 GFP_KERNEL);
3232 if (!ret_stack_list[i]) {
3233 start = 0;
3234 end = i;
3235 ret = -ENOMEM;
3236 goto free;
3237 }
3238 }
3239
3240 read_lock_irqsave(&tasklist_lock, flags);
3241 do_each_thread(g, t) {
3242 if (start == end) {
3243 ret = -EAGAIN;
3244 goto unlock;
3245 }
3246
3247 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003248 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003249 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003250 t->curr_ret_stack = -1;
3251 /* Make sure the tasks see the -1 first: */
3252 smp_wmb();
3253 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003254 }
3255 } while_each_thread(g, t);
3256
3257unlock:
3258 read_unlock_irqrestore(&tasklist_lock, flags);
3259free:
3260 for (i = start; i < end; i++)
3261 kfree(ret_stack_list[i]);
3262 return ret;
3263}
3264
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003265static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04003266ftrace_graph_probe_sched_switch(void *ignore,
3267 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003268{
3269 unsigned long long timestamp;
3270 int index;
3271
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003272 /*
3273 * Does the user want to count the time a function was asleep.
3274 * If so, do not update the time stamps.
3275 */
3276 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3277 return;
3278
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003279 timestamp = trace_clock_local();
3280
3281 prev->ftrace_timestamp = timestamp;
3282
3283 /* only process tasks that we timestamped */
3284 if (!next->ftrace_timestamp)
3285 return;
3286
3287 /*
3288 * Update all the counters in next to make up for the
3289 * time next was sleeping.
3290 */
3291 timestamp -= next->ftrace_timestamp;
3292
3293 for (index = next->curr_ret_stack; index >= 0; index--)
3294 next->ret_stack[index].calltime += timestamp;
3295}
3296
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003297/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003298static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003299{
3300 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003301 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003302
3303 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3304 sizeof(struct ftrace_ret_stack *),
3305 GFP_KERNEL);
3306
3307 if (!ret_stack_list)
3308 return -ENOMEM;
3309
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003310 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003311 for_each_online_cpu(cpu) {
3312 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05003313 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04003314 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003315
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003316 do {
3317 ret = alloc_retstack_tasklist(ret_stack_list);
3318 } while (ret == -EAGAIN);
3319
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003320 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04003321 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003322 if (ret)
3323 pr_info("ftrace_graph: Couldn't activate tracepoint"
3324 " probe to kernel_sched_switch\n");
3325 }
3326
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003327 kfree(ret_stack_list);
3328 return ret;
3329}
3330
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003331/*
3332 * Hibernation protection.
3333 * The state of the current task is too much unstable during
3334 * suspend/restore to disk. We want to protect against that.
3335 */
3336static int
3337ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3338 void *unused)
3339{
3340 switch (state) {
3341 case PM_HIBERNATION_PREPARE:
3342 pause_graph_tracing();
3343 break;
3344
3345 case PM_POST_HIBERNATION:
3346 unpause_graph_tracing();
3347 break;
3348 }
3349 return NOTIFY_DONE;
3350}
3351
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003352int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3353 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003354{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003355 int ret = 0;
3356
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003357 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003358
Steven Rostedt05ce5812009-03-24 00:18:31 -04003359 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003360 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003361 ret = -EBUSY;
3362 goto out;
3363 }
3364
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003365 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3366 register_pm_notifier(&ftrace_suspend_notifier);
3367
Steven Rostedt597af812009-04-03 15:24:12 -04003368 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003369 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003370 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003371 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003372 goto out;
3373 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003374
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003375 ftrace_graph_return = retfunc;
3376 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003377
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003378 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003379
3380out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003381 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003382 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003383}
3384
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003385void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003386{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003387 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003388
Steven Rostedt597af812009-04-03 15:24:12 -04003389 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003390 goto out;
3391
Steven Rostedt597af812009-04-03 15:24:12 -04003392 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003393 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003394 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003395 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003396 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04003397 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003398
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003399 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003400 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003401}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003402
Steven Rostedt868baf02011-02-10 21:26:13 -05003403static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
3404
3405static void
3406graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
3407{
3408 atomic_set(&t->tracing_graph_pause, 0);
3409 atomic_set(&t->trace_overrun, 0);
3410 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003411 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05003412 smp_wmb();
3413 t->ret_stack = ret_stack;
3414}
3415
3416/*
3417 * Allocate a return stack for the idle task. May be the first
3418 * time through, or it may be done by CPU hotplug online.
3419 */
3420void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
3421{
3422 t->curr_ret_stack = -1;
3423 /*
3424 * The idle task has no parent, it either has its own
3425 * stack or no stack at all.
3426 */
3427 if (t->ret_stack)
3428 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
3429
3430 if (ftrace_graph_active) {
3431 struct ftrace_ret_stack *ret_stack;
3432
3433 ret_stack = per_cpu(idle_ret_stack, cpu);
3434 if (!ret_stack) {
3435 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3436 * sizeof(struct ftrace_ret_stack),
3437 GFP_KERNEL);
3438 if (!ret_stack)
3439 return;
3440 per_cpu(idle_ret_stack, cpu) = ret_stack;
3441 }
3442 graph_init_task(t, ret_stack);
3443 }
3444}
3445
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003446/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003447void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003448{
Steven Rostedt84047e32009-06-02 16:51:55 -04003449 /* Make sure we do not use the parent ret_stack */
3450 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05003451 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04003452
Steven Rostedt597af812009-04-03 15:24:12 -04003453 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003454 struct ftrace_ret_stack *ret_stack;
3455
3456 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003457 * sizeof(struct ftrace_ret_stack),
3458 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003459 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003460 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05003461 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04003462 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003463}
3464
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003465void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003466{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003467 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3468
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003469 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003470 /* NULL must become visible to IRQs before we free it: */
3471 barrier();
3472
3473 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003474}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003475
3476void ftrace_graph_stop(void)
3477{
3478 ftrace_stop();
3479}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003480#endif