blob: 9abaaf46f21215194e9d60409456096e7c5f06a5 [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) {
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001090 /* Skip over free records */
1091 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05001092 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001093
Steven Rostedt265c8312009-02-13 12:43:56 -05001094 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001095 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001096 ftrace_bug(failed, rec->ip);
1097 /* Stop processing */
1098 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001099 }
1100 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001101}
1102
Ingo Molnare309b412008-05-12 21:20:51 +02001103static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001104ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001105{
1106 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001107 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001108
1109 ip = rec->ip;
1110
Steven Rostedt45a4a232011-04-21 23:16:46 -04001111 if (unlikely(ftrace_disabled))
1112 return 0;
1113
Shaohua Li25aac9d2009-01-09 11:29:40 +08001114 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001115 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001116 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301117 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001118 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301119 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001120}
1121
Steven Rostedt000ab692009-02-17 13:35:06 -05001122/*
1123 * archs can override this function if they must do something
1124 * before the modifying code is performed.
1125 */
1126int __weak ftrace_arch_code_modify_prepare(void)
1127{
1128 return 0;
1129}
1130
1131/*
1132 * archs can override this function if they must do something
1133 * after the modifying code is performed.
1134 */
1135int __weak ftrace_arch_code_modify_post_process(void)
1136{
1137 return 0;
1138}
1139
Ingo Molnare309b412008-05-12 21:20:51 +02001140static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001141{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001142 int *command = data;
1143
Steven Rostedta3583242008-11-11 15:01:42 -05001144 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001145 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001146 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001147 ftrace_replace_code(0);
1148
1149 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1150 ftrace_update_ftrace_func(ftrace_trace_function);
1151
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001152 if (*command & FTRACE_START_FUNC_RET)
1153 ftrace_enable_ftrace_graph_caller();
1154 else if (*command & FTRACE_STOP_FUNC_RET)
1155 ftrace_disable_ftrace_graph_caller();
1156
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001157 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001158}
1159
Ingo Molnare309b412008-05-12 21:20:51 +02001160static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001161{
Steven Rostedt000ab692009-02-17 13:35:06 -05001162 int ret;
1163
1164 ret = ftrace_arch_code_modify_prepare();
1165 FTRACE_WARN_ON(ret);
1166 if (ret)
1167 return;
1168
Rusty Russell784e2d72008-07-28 12:16:31 -05001169 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001170
1171 ret = ftrace_arch_code_modify_post_process();
1172 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001173}
1174
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001175static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001176static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001177
1178static void ftrace_startup_enable(int command)
1179{
1180 if (saved_ftrace_func != ftrace_trace_function) {
1181 saved_ftrace_func = ftrace_trace_function;
1182 command |= FTRACE_UPDATE_TRACE_FUNC;
1183 }
1184
1185 if (!command || !ftrace_enabled)
1186 return;
1187
1188 ftrace_run_update_code(command);
1189}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001190
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001191static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001192{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001193 if (unlikely(ftrace_disabled))
1194 return;
1195
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001196 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001197 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001198
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001199 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001200}
1201
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001202static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001203{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001204 if (unlikely(ftrace_disabled))
1205 return;
1206
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001207 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001208 /*
1209 * Just warn in case of unbalance, no need to kill ftrace, it's not
1210 * critical but the ftrace_call callers may be never nopped again after
1211 * further ftrace uses.
1212 */
1213 WARN_ON_ONCE(ftrace_start_up < 0);
1214
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001215 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001216 command |= FTRACE_DISABLE_CALLS;
1217
1218 if (saved_ftrace_func != ftrace_trace_function) {
1219 saved_ftrace_func = ftrace_trace_function;
1220 command |= FTRACE_UPDATE_TRACE_FUNC;
1221 }
1222
1223 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001224 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001225
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001226 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001227}
1228
Ingo Molnare309b412008-05-12 21:20:51 +02001229static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001230{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001231 if (unlikely(ftrace_disabled))
1232 return;
1233
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001234 /* Force update next time */
1235 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001236 /* ftrace_start_up is true if we want ftrace running */
1237 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001238 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001239}
1240
Ingo Molnare309b412008-05-12 21:20:51 +02001241static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001242{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001243 if (unlikely(ftrace_disabled))
1244 return;
1245
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001246 /* ftrace_start_up is true if ftrace is running */
1247 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001248 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001249}
1250
Steven Rostedt3d083392008-05-12 21:20:42 +02001251static cycle_t ftrace_update_time;
1252static unsigned long ftrace_update_cnt;
1253unsigned long ftrace_update_tot_cnt;
1254
Steven Rostedt31e88902008-11-14 16:21:19 -08001255static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001256{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001257 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301258 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001259
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001260 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001261 ftrace_update_cnt = 0;
1262
Lai Jiangshane94142a2009-03-13 17:51:27 +08001263 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301264
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001265 /* If something went wrong, bail without enabling anything */
1266 if (unlikely(ftrace_disabled))
1267 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001268
Lai Jiangshane94142a2009-03-13 17:51:27 +08001269 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001270 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001271 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301272
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001273 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001274 * Do the initial record conversion from mcount jump
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001275 * to the NOP instructions.
1276 */
1277 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001278 ftrace_free_rec(p);
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001279 /* Game over */
1280 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001281 }
1282
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001283 ftrace_update_cnt++;
1284
1285 /*
1286 * If the tracing is enabled, go ahead and enable the record.
1287 *
1288 * The reason not to enable the record immediatelly is the
1289 * inherent check of ftrace_make_nop/ftrace_make_call for
1290 * correct previous instructions. Making first the NOP
1291 * conversion puts the module to the correct state, thus
1292 * passing the ftrace_make_call check.
1293 */
1294 if (ftrace_start_up) {
1295 int failed = __ftrace_replace_code(p, 1);
1296 if (failed) {
1297 ftrace_bug(failed, p->ip);
1298 ftrace_free_rec(p);
1299 }
1300 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001301 }
1302
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001303 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001304 ftrace_update_time = stop - start;
1305 ftrace_update_tot_cnt += ftrace_update_cnt;
1306
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001307 return 0;
1308}
1309
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001310static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001311{
1312 struct ftrace_page *pg;
1313 int cnt;
1314 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001315
1316 /* allocate a few pages */
1317 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1318 if (!ftrace_pages_start)
1319 return -1;
1320
1321 /*
1322 * Allocate a few more pages.
1323 *
1324 * TODO: have some parser search vmlinux before
1325 * final linking to find all calls to ftrace.
1326 * Then we can:
1327 * a) know how many pages to allocate.
1328 * and/or
1329 * b) set up the table then.
1330 *
1331 * The dynamic code is still necessary for
1332 * modules.
1333 */
1334
1335 pg = ftrace_pages = ftrace_pages_start;
1336
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001337 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001338 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001339 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001340
1341 for (i = 0; i < cnt; i++) {
1342 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1343
1344 /* If we fail, we'll try later anyway */
1345 if (!pg->next)
1346 break;
1347
1348 pg = pg->next;
1349 }
1350
1351 return 0;
1352}
1353
Steven Rostedt5072c592008-05-12 21:20:43 +02001354enum {
1355 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001356 FTRACE_ITER_NOTRACE = (1 << 1),
Steven Rostedt3499e462011-04-21 22:59:12 -04001357 FTRACE_ITER_PRINTALL = (1 << 2),
1358 FTRACE_ITER_HASH = (1 << 3),
Steven Rostedt5072c592008-05-12 21:20:43 +02001359};
1360
1361#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1362
1363struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001364 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001365 loff_t func_pos;
1366 struct ftrace_page *pg;
1367 struct dyn_ftrace *func;
1368 struct ftrace_func_probe *probe;
1369 struct trace_parser parser;
1370 int hidx;
1371 int idx;
1372 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02001373};
1374
Ingo Molnare309b412008-05-12 21:20:51 +02001375static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001376t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001377{
1378 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001379 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001380 struct hlist_head *hhd;
1381
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001382 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001383 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001384
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001385 if (iter->probe)
1386 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001387 retry:
1388 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1389 return NULL;
1390
1391 hhd = &ftrace_func_hash[iter->hidx];
1392
1393 if (hlist_empty(hhd)) {
1394 iter->hidx++;
1395 hnd = NULL;
1396 goto retry;
1397 }
1398
1399 if (!hnd)
1400 hnd = hhd->first;
1401 else {
1402 hnd = hnd->next;
1403 if (!hnd) {
1404 iter->hidx++;
1405 goto retry;
1406 }
1407 }
1408
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001409 if (WARN_ON_ONCE(!hnd))
1410 return NULL;
1411
1412 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1413
1414 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001415}
1416
1417static void *t_hash_start(struct seq_file *m, loff_t *pos)
1418{
1419 struct ftrace_iterator *iter = m->private;
1420 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001421 loff_t l;
1422
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001423 if (iter->func_pos > *pos)
1424 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001425
Li Zefand82d6242009-06-24 09:54:54 +08001426 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001427 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001428 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08001429 if (!p)
1430 break;
1431 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001432 if (!p)
1433 return NULL;
1434
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001435 /* Only set this if we have an item */
1436 iter->flags |= FTRACE_ITER_HASH;
1437
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001438 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001439}
1440
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001441static int
1442t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001443{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001444 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001445
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001446 rec = iter->probe;
1447 if (WARN_ON_ONCE(!rec))
1448 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001449
Steven Rostedt809dcf22009-02-16 23:06:01 -05001450 if (rec->ops->print)
1451 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1452
Steven Rostedtb375a112009-09-17 00:05:58 -04001453 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001454
1455 if (rec->data)
1456 seq_printf(m, ":%p", rec->data);
1457 seq_putc(m, '\n');
1458
1459 return 0;
1460}
1461
1462static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001463t_next(struct seq_file *m, void *v, loff_t *pos)
1464{
1465 struct ftrace_iterator *iter = m->private;
1466 struct dyn_ftrace *rec = NULL;
1467
Steven Rostedt45a4a232011-04-21 23:16:46 -04001468 if (unlikely(ftrace_disabled))
1469 return NULL;
1470
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001471 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001472 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001473
Steven Rostedt5072c592008-05-12 21:20:43 +02001474 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01001475 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02001476
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001477 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001478 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001479
Steven Rostedt5072c592008-05-12 21:20:43 +02001480 retry:
1481 if (iter->idx >= iter->pg->index) {
1482 if (iter->pg->next) {
1483 iter->pg = iter->pg->next;
1484 iter->idx = 0;
1485 goto retry;
1486 }
1487 } else {
1488 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001489 if ((rec->flags & FTRACE_FL_FREE) ||
1490
Steven Rostedt0183fb12008-11-07 22:36:02 -05001491 ((iter->flags & FTRACE_ITER_FILTER) &&
1492 !(rec->flags & FTRACE_FL_FILTER)) ||
1493
Steven Rostedt41c52c02008-05-22 11:46:33 -04001494 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1495 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001496 rec = NULL;
1497 goto retry;
1498 }
1499 }
1500
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001501 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001502 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001503
1504 iter->func = rec;
1505
1506 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02001507}
1508
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001509static void reset_iter_read(struct ftrace_iterator *iter)
1510{
1511 iter->pos = 0;
1512 iter->func_pos = 0;
1513 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02001514}
1515
1516static void *t_start(struct seq_file *m, loff_t *pos)
1517{
1518 struct ftrace_iterator *iter = m->private;
1519 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001520 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001521
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001522 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04001523
1524 if (unlikely(ftrace_disabled))
1525 return NULL;
1526
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001527 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001528 * If an lseek was done, then reset and start from beginning.
1529 */
1530 if (*pos < iter->pos)
1531 reset_iter_read(iter);
1532
1533 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001534 * For set_ftrace_filter reading, if we have the filter
1535 * off, we can short cut and just print out that all
1536 * functions are enabled.
1537 */
1538 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1539 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001540 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001541 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07001542 /* reset in case of seek/pread */
1543 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001544 return iter;
1545 }
1546
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001547 if (iter->flags & FTRACE_ITER_HASH)
1548 return t_hash_start(m, pos);
1549
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001550 /*
1551 * Unfortunately, we need to restart at ftrace_pages_start
1552 * every time we let go of the ftrace_mutex. This is because
1553 * those pointers can change without the lock.
1554 */
Li Zefan694ce0a2009-06-24 09:54:19 +08001555 iter->pg = ftrace_pages_start;
1556 iter->idx = 0;
1557 for (l = 0; l <= *pos; ) {
1558 p = t_next(m, p, &l);
1559 if (!p)
1560 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001561 }
walimis5821e1b2008-11-15 15:19:06 +08001562
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001563 if (!p) {
1564 if (iter->flags & FTRACE_ITER_FILTER)
1565 return t_hash_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001566
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001567 return NULL;
1568 }
1569
1570 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02001571}
1572
1573static void t_stop(struct seq_file *m, void *p)
1574{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001575 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001576}
1577
1578static int t_show(struct seq_file *m, void *v)
1579{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001580 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001581 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02001582
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001583 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001584 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001585
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001586 if (iter->flags & FTRACE_ITER_PRINTALL) {
1587 seq_printf(m, "#### all functions enabled ####\n");
1588 return 0;
1589 }
1590
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001591 rec = iter->func;
1592
Steven Rostedt5072c592008-05-12 21:20:43 +02001593 if (!rec)
1594 return 0;
1595
Steven Rostedtb375a112009-09-17 00:05:58 -04001596 seq_printf(m, "%ps\n", (void *)rec->ip);
Steven Rostedt5072c592008-05-12 21:20:43 +02001597
1598 return 0;
1599}
1600
James Morris88e9d342009-09-22 16:43:43 -07001601static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001602 .start = t_start,
1603 .next = t_next,
1604 .stop = t_stop,
1605 .show = t_show,
1606};
1607
Ingo Molnare309b412008-05-12 21:20:51 +02001608static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001609ftrace_avail_open(struct inode *inode, struct file *file)
1610{
1611 struct ftrace_iterator *iter;
1612 int ret;
1613
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001614 if (unlikely(ftrace_disabled))
1615 return -ENODEV;
1616
Steven Rostedt5072c592008-05-12 21:20:43 +02001617 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1618 if (!iter)
1619 return -ENOMEM;
1620
1621 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001622
1623 ret = seq_open(file, &show_ftrace_seq_ops);
1624 if (!ret) {
1625 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001626
Steven Rostedt5072c592008-05-12 21:20:43 +02001627 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001628 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001629 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001630 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001631
1632 return ret;
1633}
1634
Steven Rostedt41c52c02008-05-22 11:46:33 -04001635static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001636{
1637 struct ftrace_page *pg;
1638 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001639 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001640
Steven Rostedt52baf112009-02-14 01:15:39 -05001641 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001642 if (enable)
1643 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001644 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001645 rec->flags &= ~type;
1646 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001647 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001648}
1649
Ingo Molnare309b412008-05-12 21:20:51 +02001650static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001651ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001652{
1653 struct ftrace_iterator *iter;
1654 int ret = 0;
1655
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001656 if (unlikely(ftrace_disabled))
1657 return -ENODEV;
1658
Steven Rostedt5072c592008-05-12 21:20:43 +02001659 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1660 if (!iter)
1661 return -ENOMEM;
1662
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001663 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1664 kfree(iter);
1665 return -ENOMEM;
1666 }
1667
Steven Rostedt41c52c02008-05-22 11:46:33 -04001668 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001669 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04001670 (file->f_flags & O_TRUNC))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001671 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001672
1673 if (file->f_mode & FMODE_READ) {
1674 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001675 iter->flags = enable ? FTRACE_ITER_FILTER :
1676 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001677
1678 ret = seq_open(file, &show_ftrace_seq_ops);
1679 if (!ret) {
1680 struct seq_file *m = file->private_data;
1681 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08001682 } else {
1683 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02001684 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08001685 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001686 } else
1687 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001688 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001689
1690 return ret;
1691}
1692
Steven Rostedt41c52c02008-05-22 11:46:33 -04001693static int
1694ftrace_filter_open(struct inode *inode, struct file *file)
1695{
1696 return ftrace_regex_open(inode, file, 1);
1697}
1698
1699static int
1700ftrace_notrace_open(struct inode *inode, struct file *file)
1701{
1702 return ftrace_regex_open(inode, file, 0);
1703}
1704
Ingo Molnare309b412008-05-12 21:20:51 +02001705static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001706ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001707{
1708 loff_t ret;
1709
1710 if (file->f_mode & FMODE_READ)
1711 ret = seq_lseek(file, offset, origin);
1712 else
1713 file->f_pos = ret = 1;
1714
1715 return ret;
1716}
1717
Steven Rostedt64e7c442009-02-13 17:08:48 -05001718static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001719{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001720 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08001721 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001722
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001723 switch (type) {
1724 case MATCH_FULL:
1725 if (strcmp(str, regex) == 0)
1726 matched = 1;
1727 break;
1728 case MATCH_FRONT_ONLY:
1729 if (strncmp(str, regex, len) == 0)
1730 matched = 1;
1731 break;
1732 case MATCH_MIDDLE_ONLY:
1733 if (strstr(str, regex))
1734 matched = 1;
1735 break;
1736 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08001737 slen = strlen(str);
1738 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001739 matched = 1;
1740 break;
1741 }
1742
1743 return matched;
1744}
1745
Steven Rostedt64e7c442009-02-13 17:08:48 -05001746static int
1747ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1748{
1749 char str[KSYM_SYMBOL_LEN];
1750
1751 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1752 return ftrace_match(str, regex, len, type);
1753}
1754
Li Zefan311d16d2009-12-08 11:15:11 +08001755static int ftrace_match_records(char *buff, int len, int enable)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001756{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001757 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001758 struct ftrace_page *pg;
1759 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001760 unsigned long flag;
1761 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001762 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001763 int not;
Li Zefan311d16d2009-12-08 11:15:11 +08001764 int found = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001765
Steven Rostedt6a24a242009-02-17 11:20:26 -05001766 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001767 type = filter_parse_regex(buff, len, &search, &not);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001768
1769 search_len = strlen(search);
1770
Steven Rostedt52baf112009-02-14 01:15:39 -05001771 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001772 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001773
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001774 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001775 if (not)
1776 rec->flags &= ~flag;
1777 else
1778 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001779 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001780 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001781 /*
1782 * Only enable filtering if we have a function that
1783 * is filtered on.
1784 */
1785 if (enable && (rec->flags & FTRACE_FL_FILTER))
1786 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001787 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001788 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001789
1790 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02001791}
1792
Steven Rostedt64e7c442009-02-13 17:08:48 -05001793static int
1794ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1795 char *regex, int len, int type)
1796{
1797 char str[KSYM_SYMBOL_LEN];
1798 char *modname;
1799
1800 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1801
1802 if (!modname || strcmp(modname, mod))
1803 return 0;
1804
1805 /* blank search means to match all funcs in the mod */
1806 if (len)
1807 return ftrace_match(str, regex, len, type);
1808 else
1809 return 1;
1810}
1811
Li Zefan311d16d2009-12-08 11:15:11 +08001812static int ftrace_match_module_records(char *buff, char *mod, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001813{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001814 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001815 struct ftrace_page *pg;
1816 struct dyn_ftrace *rec;
1817 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001818 char *search = buff;
1819 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001820 int not = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08001821 int found = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001822
Steven Rostedt6a24a242009-02-17 11:20:26 -05001823 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1824
Steven Rostedt64e7c442009-02-13 17:08:48 -05001825 /* blank or '*' mean the same */
1826 if (strcmp(buff, "*") == 0)
1827 buff[0] = 0;
1828
1829 /* handle the case of 'dont filter this module' */
1830 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1831 buff[0] = 0;
1832 not = 1;
1833 }
1834
1835 if (strlen(buff)) {
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001836 type = filter_parse_regex(buff, strlen(buff), &search, &not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001837 search_len = strlen(search);
1838 }
1839
Steven Rostedt52baf112009-02-14 01:15:39 -05001840 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001841
Steven Rostedt45a4a232011-04-21 23:16:46 -04001842 if (unlikely(ftrace_disabled))
1843 goto out_unlock;
1844
1845 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt64e7c442009-02-13 17:08:48 -05001846
1847 if (ftrace_match_module_record(rec, mod,
1848 search, search_len, type)) {
1849 if (not)
1850 rec->flags &= ~flag;
1851 else
1852 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001853 found = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001854 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001855 if (enable && (rec->flags & FTRACE_FL_FILTER))
1856 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001857
1858 } while_for_each_ftrace_rec();
Steven Rostedt45a4a232011-04-21 23:16:46 -04001859 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05001860 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001861
1862 return found;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001863}
1864
Steven Rostedtf6180772009-02-14 00:40:25 -05001865/*
1866 * We register the module command as a template to show others how
1867 * to register the a command as well.
1868 */
1869
1870static int
1871ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1872{
1873 char *mod;
1874
1875 /*
1876 * cmd == 'mod' because we only registered this func
1877 * for the 'mod' ftrace_func_command.
1878 * But if you register one func with multiple commands,
1879 * you can tell which command was used by the cmd
1880 * parameter.
1881 */
1882
1883 /* we must have a module name */
1884 if (!param)
1885 return -EINVAL;
1886
1887 mod = strsep(&param, ":");
1888 if (!strlen(mod))
1889 return -EINVAL;
1890
Li Zefan311d16d2009-12-08 11:15:11 +08001891 if (ftrace_match_module_records(func, mod, enable))
1892 return 0;
1893 return -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05001894}
1895
1896static struct ftrace_func_command ftrace_mod_cmd = {
1897 .name = "mod",
1898 .func = ftrace_mod_callback,
1899};
1900
1901static int __init ftrace_mod_cmd_init(void)
1902{
1903 return register_ftrace_command(&ftrace_mod_cmd);
1904}
1905device_initcall(ftrace_mod_cmd_init);
1906
Steven Rostedt59df055f2009-02-14 15:29:06 -05001907static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001908function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001909{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001910 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001911 struct hlist_head *hhd;
1912 struct hlist_node *n;
1913 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001914
1915 key = hash_long(ip, FTRACE_HASH_BITS);
1916
1917 hhd = &ftrace_func_hash[key];
1918
1919 if (hlist_empty(hhd))
1920 return;
1921
1922 /*
1923 * Disable preemption for these calls to prevent a RCU grace
1924 * period. This syncs the hash iteration and freeing of items
1925 * on the hash. rcu_read_lock is too dangerous here.
1926 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04001927 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05001928 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1929 if (entry->ip == ip)
1930 entry->ops->func(ip, parent_ip, &entry->data);
1931 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04001932 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05001933}
1934
Steven Rostedtb6887d72009-02-17 12:32:04 -05001935static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001936{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001937 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001938};
1939
Steven Rostedtb6887d72009-02-17 12:32:04 -05001940static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001941
Steven Rostedtb6887d72009-02-17 12:32:04 -05001942static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001943{
1944 int i;
1945
Steven Rostedtb6887d72009-02-17 12:32:04 -05001946 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001947 return;
1948
1949 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1950 struct hlist_head *hhd = &ftrace_func_hash[i];
1951 if (hhd->first)
1952 break;
1953 }
1954 /* Nothing registered? */
1955 if (i == FTRACE_FUNC_HASHSIZE)
1956 return;
1957
Steven Rostedtb6887d72009-02-17 12:32:04 -05001958 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001959 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001960 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001961}
1962
Steven Rostedtb6887d72009-02-17 12:32:04 -05001963static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001964{
1965 int i;
1966
Steven Rostedtb6887d72009-02-17 12:32:04 -05001967 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001968 return;
1969
1970 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1971 struct hlist_head *hhd = &ftrace_func_hash[i];
1972 if (hhd->first)
1973 return;
1974 }
1975
1976 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05001977 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001978 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001979 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001980}
1981
1982
1983static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1984{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001985 struct ftrace_func_probe *entry =
1986 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001987
1988 if (entry->ops->free)
1989 entry->ops->free(&entry->data);
1990 kfree(entry);
1991}
1992
1993
1994int
Steven Rostedtb6887d72009-02-17 12:32:04 -05001995register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001996 void *data)
1997{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001998 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001999 struct ftrace_page *pg;
2000 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002001 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002002 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002003 int count = 0;
2004 char *search;
2005
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002006 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002007 len = strlen(search);
2008
Steven Rostedtb6887d72009-02-17 12:32:04 -05002009 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002010 if (WARN_ON(not))
2011 return -EINVAL;
2012
2013 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002014
Steven Rostedt45a4a232011-04-21 23:16:46 -04002015 if (unlikely(ftrace_disabled))
2016 goto out_unlock;
2017
2018 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002019
2020 if (!ftrace_match_record(rec, search, len, type))
2021 continue;
2022
2023 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2024 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002025 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002026 if (!count)
2027 count = -ENOMEM;
2028 goto out_unlock;
2029 }
2030
2031 count++;
2032
2033 entry->data = data;
2034
2035 /*
2036 * The caller might want to do something special
2037 * for each function we find. We call the callback
2038 * to give the caller an opportunity to do so.
2039 */
2040 if (ops->callback) {
2041 if (ops->callback(rec->ip, &entry->data) < 0) {
2042 /* caller does not like this func */
2043 kfree(entry);
2044 continue;
2045 }
2046 }
2047
2048 entry->ops = ops;
2049 entry->ip = rec->ip;
2050
2051 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2052 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2053
2054 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002055 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002056
2057 out_unlock:
2058 mutex_unlock(&ftrace_lock);
2059
2060 return count;
2061}
2062
2063enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002064 PROBE_TEST_FUNC = 1,
2065 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002066};
2067
2068static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002069__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002070 void *data, int flags)
2071{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002072 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002073 struct hlist_node *n, *tmp;
2074 char str[KSYM_SYMBOL_LEN];
2075 int type = MATCH_FULL;
2076 int i, len = 0;
2077 char *search;
2078
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002079 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002080 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002081 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002082 int not;
2083
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002084 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002085 len = strlen(search);
2086
Steven Rostedtb6887d72009-02-17 12:32:04 -05002087 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002088 if (WARN_ON(not))
2089 return;
2090 }
2091
2092 mutex_lock(&ftrace_lock);
2093 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2094 struct hlist_head *hhd = &ftrace_func_hash[i];
2095
2096 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2097
2098 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002099 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002100 continue;
2101
Steven Rostedtb6887d72009-02-17 12:32:04 -05002102 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002103 continue;
2104
2105 /* do this last, since it is the most expensive */
2106 if (glob) {
2107 kallsyms_lookup(entry->ip, NULL, NULL,
2108 NULL, str);
2109 if (!ftrace_match(str, glob, len, type))
2110 continue;
2111 }
2112
2113 hlist_del(&entry->node);
2114 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2115 }
2116 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002117 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002118 mutex_unlock(&ftrace_lock);
2119}
2120
2121void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002122unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002123 void *data)
2124{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002125 __unregister_ftrace_function_probe(glob, ops, data,
2126 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002127}
2128
2129void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002130unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002131{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002132 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002133}
2134
Steven Rostedtb6887d72009-02-17 12:32:04 -05002135void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002136{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002137 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002138}
2139
Steven Rostedtf6180772009-02-14 00:40:25 -05002140static LIST_HEAD(ftrace_commands);
2141static DEFINE_MUTEX(ftrace_cmd_mutex);
2142
2143int register_ftrace_command(struct ftrace_func_command *cmd)
2144{
2145 struct ftrace_func_command *p;
2146 int ret = 0;
2147
2148 mutex_lock(&ftrace_cmd_mutex);
2149 list_for_each_entry(p, &ftrace_commands, list) {
2150 if (strcmp(cmd->name, p->name) == 0) {
2151 ret = -EBUSY;
2152 goto out_unlock;
2153 }
2154 }
2155 list_add(&cmd->list, &ftrace_commands);
2156 out_unlock:
2157 mutex_unlock(&ftrace_cmd_mutex);
2158
2159 return ret;
2160}
2161
2162int unregister_ftrace_command(struct ftrace_func_command *cmd)
2163{
2164 struct ftrace_func_command *p, *n;
2165 int ret = -ENODEV;
2166
2167 mutex_lock(&ftrace_cmd_mutex);
2168 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2169 if (strcmp(cmd->name, p->name) == 0) {
2170 ret = 0;
2171 list_del_init(&p->list);
2172 goto out_unlock;
2173 }
2174 }
2175 out_unlock:
2176 mutex_unlock(&ftrace_cmd_mutex);
2177
2178 return ret;
2179}
2180
Steven Rostedt64e7c442009-02-13 17:08:48 -05002181static int ftrace_process_regex(char *buff, int len, int enable)
2182{
Steven Rostedtf6180772009-02-14 00:40:25 -05002183 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002184 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002185 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002186
2187 func = strsep(&next, ":");
2188
2189 if (!next) {
Li Zefan311d16d2009-12-08 11:15:11 +08002190 if (ftrace_match_records(func, len, enable))
2191 return 0;
2192 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002193 }
2194
Steven Rostedtf6180772009-02-14 00:40:25 -05002195 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002196
2197 command = strsep(&next, ":");
2198
Steven Rostedtf6180772009-02-14 00:40:25 -05002199 mutex_lock(&ftrace_cmd_mutex);
2200 list_for_each_entry(p, &ftrace_commands, list) {
2201 if (strcmp(p->name, command) == 0) {
2202 ret = p->func(func, command, next, enable);
2203 goto out_unlock;
2204 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002205 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002206 out_unlock:
2207 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002208
Steven Rostedtf6180772009-02-14 00:40:25 -05002209 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002210}
2211
Ingo Molnare309b412008-05-12 21:20:51 +02002212static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002213ftrace_regex_write(struct file *file, const char __user *ubuf,
2214 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002215{
2216 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002217 struct trace_parser *parser;
2218 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002219
Li Zefan4ba79782009-09-22 13:52:20 +08002220 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002221 return 0;
2222
Steven Rostedt41c52c02008-05-22 11:46:33 -04002223 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002224
Steven Rostedt45a4a232011-04-21 23:16:46 -04002225 ret = -ENODEV;
2226 if (unlikely(ftrace_disabled))
2227 goto out_unlock;
2228
Steven Rostedt5072c592008-05-12 21:20:43 +02002229 if (file->f_mode & FMODE_READ) {
2230 struct seq_file *m = file->private_data;
2231 iter = m->private;
2232 } else
2233 iter = file->private_data;
2234
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002235 parser = &iter->parser;
2236 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002237
Li Zefan4ba79782009-09-22 13:52:20 +08002238 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002239 !trace_parser_cont(parser)) {
2240 ret = ftrace_process_regex(parser->buffer,
2241 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002242 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002243 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002244 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002245 }
2246
Steven Rostedt5072c592008-05-12 21:20:43 +02002247 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002248out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002249 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002250
Steven Rostedt5072c592008-05-12 21:20:43 +02002251 return ret;
2252}
2253
Steven Rostedt41c52c02008-05-22 11:46:33 -04002254static ssize_t
2255ftrace_filter_write(struct file *file, const char __user *ubuf,
2256 size_t cnt, loff_t *ppos)
2257{
2258 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2259}
2260
2261static ssize_t
2262ftrace_notrace_write(struct file *file, const char __user *ubuf,
2263 size_t cnt, loff_t *ppos)
2264{
2265 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2266}
2267
2268static void
2269ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2270{
2271 if (unlikely(ftrace_disabled))
2272 return;
2273
2274 mutex_lock(&ftrace_regex_lock);
2275 if (reset)
2276 ftrace_filter_reset(enable);
2277 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002278 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002279 mutex_unlock(&ftrace_regex_lock);
2280}
2281
Steven Rostedt77a2b372008-05-12 21:20:45 +02002282/**
2283 * ftrace_set_filter - set a function to filter on in ftrace
2284 * @buf - the string that holds the function filter text.
2285 * @len - the length of the string.
2286 * @reset - non zero to reset all filters before applying this filter.
2287 *
2288 * Filters denote which functions should be enabled when tracing is enabled.
2289 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2290 */
Ingo Molnare309b412008-05-12 21:20:51 +02002291void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002292{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002293 ftrace_set_regex(buf, len, reset, 1);
2294}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002295
Steven Rostedt41c52c02008-05-22 11:46:33 -04002296/**
2297 * ftrace_set_notrace - set a function to not trace in ftrace
2298 * @buf - the string that holds the function notrace text.
2299 * @len - the length of the string.
2300 * @reset - non zero to reset all filters before applying this filter.
2301 *
2302 * Notrace Filters denote which functions should not be enabled when tracing
2303 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2304 * for tracing.
2305 */
2306void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2307{
2308 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002309}
2310
Steven Rostedt2af15d62009-05-28 13:37:24 -04002311/*
2312 * command line interface to allow users to set filters on boot up.
2313 */
2314#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2315static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2316static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2317
2318static int __init set_ftrace_notrace(char *str)
2319{
2320 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2321 return 1;
2322}
2323__setup("ftrace_notrace=", set_ftrace_notrace);
2324
2325static int __init set_ftrace_filter(char *str)
2326{
2327 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2328 return 1;
2329}
2330__setup("ftrace_filter=", set_ftrace_filter);
2331
Stefan Assmann369bc182009-10-12 22:17:21 +02002332#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002333static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002334static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2335
Stefan Assmann369bc182009-10-12 22:17:21 +02002336static int __init set_graph_function(char *str)
2337{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002338 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002339 return 1;
2340}
2341__setup("ftrace_graph_filter=", set_graph_function);
2342
2343static void __init set_ftrace_early_graph(char *buf)
2344{
2345 int ret;
2346 char *func;
2347
2348 while (buf) {
2349 func = strsep(&buf, ",");
2350 /* we allow only one expression at a time */
2351 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2352 func);
2353 if (ret)
2354 printk(KERN_DEBUG "ftrace: function %s not "
2355 "traceable\n", func);
2356 }
2357}
2358#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2359
Steven Rostedt2af15d62009-05-28 13:37:24 -04002360static void __init set_ftrace_early_filter(char *buf, int enable)
2361{
2362 char *func;
2363
2364 while (buf) {
2365 func = strsep(&buf, ",");
2366 ftrace_set_regex(func, strlen(func), 0, enable);
2367 }
2368}
2369
2370static void __init set_ftrace_early_filters(void)
2371{
2372 if (ftrace_filter_buf[0])
2373 set_ftrace_early_filter(ftrace_filter_buf, 1);
2374 if (ftrace_notrace_buf[0])
2375 set_ftrace_early_filter(ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002376#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2377 if (ftrace_graph_buf[0])
2378 set_ftrace_early_graph(ftrace_graph_buf);
2379#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002380}
2381
Ingo Molnare309b412008-05-12 21:20:51 +02002382static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002383ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002384{
2385 struct seq_file *m = (struct seq_file *)file->private_data;
2386 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002387 struct trace_parser *parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02002388
Steven Rostedt41c52c02008-05-22 11:46:33 -04002389 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002390 if (file->f_mode & FMODE_READ) {
2391 iter = m->private;
2392
2393 seq_release(inode, file);
2394 } else
2395 iter = file->private_data;
2396
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002397 parser = &iter->parser;
2398 if (trace_parser_loaded(parser)) {
2399 parser->buffer[parser->idx] = 0;
2400 ftrace_match_records(parser->buffer, parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002401 }
2402
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002403 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002404 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002405 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002406 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002407
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002408 trace_parser_put(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002409 kfree(iter);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002410
Steven Rostedt41c52c02008-05-22 11:46:33 -04002411 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002412 return 0;
2413}
2414
Steven Rostedt41c52c02008-05-22 11:46:33 -04002415static int
2416ftrace_filter_release(struct inode *inode, struct file *file)
2417{
2418 return ftrace_regex_release(inode, file, 1);
2419}
2420
2421static int
2422ftrace_notrace_release(struct inode *inode, struct file *file)
2423{
2424 return ftrace_regex_release(inode, file, 0);
2425}
2426
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002427static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002428 .open = ftrace_avail_open,
2429 .read = seq_read,
2430 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002431 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002432};
2433
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002434static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002435 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002436 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002437 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002438 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002439 .release = ftrace_filter_release,
2440};
2441
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002442static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002443 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002444 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002445 .write = ftrace_notrace_write,
2446 .llseek = ftrace_regex_lseek,
2447 .release = ftrace_notrace_release,
2448};
2449
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002450#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2451
2452static DEFINE_MUTEX(graph_lock);
2453
2454int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002455int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002456unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2457
2458static void *
Li Zefan85951842009-06-24 09:54:00 +08002459__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002460{
Li Zefan85951842009-06-24 09:54:00 +08002461 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002462 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002463 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002464}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002465
Li Zefan85951842009-06-24 09:54:00 +08002466static void *
2467g_next(struct seq_file *m, void *v, loff_t *pos)
2468{
2469 (*pos)++;
2470 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002471}
2472
2473static void *g_start(struct seq_file *m, loff_t *pos)
2474{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002475 mutex_lock(&graph_lock);
2476
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002477 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002478 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002479 return (void *)1;
2480
Li Zefan85951842009-06-24 09:54:00 +08002481 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002482}
2483
2484static void g_stop(struct seq_file *m, void *p)
2485{
2486 mutex_unlock(&graph_lock);
2487}
2488
2489static int g_show(struct seq_file *m, void *v)
2490{
2491 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002492
2493 if (!ptr)
2494 return 0;
2495
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002496 if (ptr == (unsigned long *)1) {
2497 seq_printf(m, "#### all functions enabled ####\n");
2498 return 0;
2499 }
2500
Steven Rostedtb375a112009-09-17 00:05:58 -04002501 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002502
2503 return 0;
2504}
2505
James Morris88e9d342009-09-22 16:43:43 -07002506static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002507 .start = g_start,
2508 .next = g_next,
2509 .stop = g_stop,
2510 .show = g_show,
2511};
2512
2513static int
2514ftrace_graph_open(struct inode *inode, struct file *file)
2515{
2516 int ret = 0;
2517
2518 if (unlikely(ftrace_disabled))
2519 return -ENODEV;
2520
2521 mutex_lock(&graph_lock);
2522 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002523 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002524 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002525 ftrace_graph_count = 0;
2526 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2527 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002528 mutex_unlock(&graph_lock);
2529
Li Zefana4ec5e02009-09-18 14:06:28 +08002530 if (file->f_mode & FMODE_READ)
2531 ret = seq_open(file, &ftrace_graph_seq_ops);
2532
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002533 return ret;
2534}
2535
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002536static int
Li Zefan87827112009-07-23 11:29:11 +08002537ftrace_graph_release(struct inode *inode, struct file *file)
2538{
2539 if (file->f_mode & FMODE_READ)
2540 seq_release(inode, file);
2541 return 0;
2542}
2543
2544static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002545ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002546{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002547 struct dyn_ftrace *rec;
2548 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002549 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002550 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002551 int type, not;
2552 char *search;
2553 bool exists;
2554 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002555
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002556 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002557 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002558 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2559 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002560
2561 search_len = strlen(search);
2562
Steven Rostedt52baf112009-02-14 01:15:39 -05002563 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002564
2565 if (unlikely(ftrace_disabled)) {
2566 mutex_unlock(&ftrace_lock);
2567 return -ENODEV;
2568 }
2569
Steven Rostedt265c8312009-02-13 12:43:56 -05002570 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002571
Steven Rostedt45a4a232011-04-21 23:16:46 -04002572 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05002573 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002574
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002575 if (ftrace_match_record(rec, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002576 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002577 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002578 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002579 if (array[i] == rec->ip) {
2580 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002581 break;
2582 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002583 }
2584
2585 if (!not) {
2586 fail = 0;
2587 if (!exists) {
2588 array[(*idx)++] = rec->ip;
2589 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2590 goto out;
2591 }
2592 } else {
2593 if (exists) {
2594 array[i] = array[--(*idx)];
2595 array[*idx] = 0;
2596 fail = 0;
2597 }
2598 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002599 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002600 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002601out:
Steven Rostedt52baf112009-02-14 01:15:39 -05002602 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002603
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002604 if (fail)
2605 return -EINVAL;
2606
2607 ftrace_graph_filter_enabled = 1;
2608 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002609}
2610
2611static ssize_t
2612ftrace_graph_write(struct file *file, const char __user *ubuf,
2613 size_t cnt, loff_t *ppos)
2614{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002615 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08002616 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002617
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002618 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002619 return 0;
2620
2621 mutex_lock(&graph_lock);
2622
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002623 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2624 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08002625 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002626 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002627
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002628 read = trace_get_user(&parser, ubuf, cnt, ppos);
2629
Li Zefan4ba79782009-09-22 13:52:20 +08002630 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002631 parser.buffer[parser.idx] = 0;
2632
2633 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08002634 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002635 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002636 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08002637 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002638 }
2639
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002640 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08002641
2642out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002643 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08002644out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002645 mutex_unlock(&graph_lock);
2646
2647 return ret;
2648}
2649
2650static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08002651 .open = ftrace_graph_open,
2652 .read = seq_read,
2653 .write = ftrace_graph_write,
2654 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002655 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002656};
2657#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2658
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002659static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002660{
Steven Rostedt5072c592008-05-12 21:20:43 +02002661
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002662 trace_create_file("available_filter_functions", 0444,
2663 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002664
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002665 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2666 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002667
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002668 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002669 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002670
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002671#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002672 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002673 NULL,
2674 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002675#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2676
Steven Rostedt5072c592008-05-12 21:20:43 +02002677 return 0;
2678}
2679
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002680static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08002681 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002682 unsigned long *end)
2683{
2684 unsigned long *p;
2685 unsigned long addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002686
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002687 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002688 p = start;
2689 while (p < end) {
2690 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002691 /*
2692 * Some architecture linkers will pad between
2693 * the different mcount_loc sections of different
2694 * object files to satisfy alignments.
2695 * Skip any NULL pointers.
2696 */
2697 if (!addr)
2698 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002699 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002700 }
2701
Steven Rostedt31e88902008-11-14 16:21:19 -08002702 ftrace_update_code(mod);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002703 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002704
2705 return 0;
2706}
2707
Steven Rostedt93eb6772009-04-15 13:24:06 -04002708#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002709void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002710{
2711 struct dyn_ftrace *rec;
2712 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04002713
Steven Rostedt93eb6772009-04-15 13:24:06 -04002714 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002715
2716 if (ftrace_disabled)
2717 goto out_unlock;
2718
Steven Rostedt93eb6772009-04-15 13:24:06 -04002719 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002720 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04002721 /*
2722 * rec->ip is changed in ftrace_free_rec()
2723 * It should not between s and e if record was freed.
2724 */
2725 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2726 ftrace_free_rec(rec);
2727 }
2728 } while_for_each_ftrace_rec();
Steven Rostedt45a4a232011-04-21 23:16:46 -04002729 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04002730 mutex_unlock(&ftrace_lock);
2731}
2732
2733static void ftrace_init_module(struct module *mod,
2734 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002735{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002736 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002737 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002738 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002739}
2740
Steven Rostedt93eb6772009-04-15 13:24:06 -04002741static int ftrace_module_notify(struct notifier_block *self,
2742 unsigned long val, void *data)
2743{
2744 struct module *mod = data;
2745
2746 switch (val) {
2747 case MODULE_STATE_COMING:
2748 ftrace_init_module(mod, mod->ftrace_callsites,
2749 mod->ftrace_callsites +
2750 mod->num_ftrace_callsites);
2751 break;
2752 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002753 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04002754 break;
2755 }
2756
2757 return 0;
2758}
2759#else
2760static int ftrace_module_notify(struct notifier_block *self,
2761 unsigned long val, void *data)
2762{
2763 return 0;
2764}
2765#endif /* CONFIG_MODULES */
2766
2767struct notifier_block ftrace_module_nb = {
2768 .notifier_call = ftrace_module_notify,
2769 .priority = 0,
2770};
2771
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002772extern unsigned long __start_mcount_loc[];
2773extern unsigned long __stop_mcount_loc[];
2774
2775void __init ftrace_init(void)
2776{
2777 unsigned long count, addr, flags;
2778 int ret;
2779
2780 /* Keep the ftrace pointer to the stub */
2781 addr = (unsigned long)ftrace_stub;
2782
2783 local_irq_save(flags);
2784 ftrace_dyn_arch_init(&addr);
2785 local_irq_restore(flags);
2786
2787 /* ftrace_dyn_arch_init places the return code in addr */
2788 if (addr)
2789 goto failed;
2790
2791 count = __stop_mcount_loc - __start_mcount_loc;
2792
2793 ret = ftrace_dyn_table_alloc(count);
2794 if (ret)
2795 goto failed;
2796
2797 last_ftrace_enabled = ftrace_enabled = 1;
2798
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002799 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08002800 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002801 __stop_mcount_loc);
2802
Steven Rostedt93eb6772009-04-15 13:24:06 -04002803 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002804 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002805 pr_warning("Failed to register trace ftrace module notifier\n");
2806
Steven Rostedt2af15d62009-05-28 13:37:24 -04002807 set_ftrace_early_filters();
2808
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002809 return;
2810 failed:
2811 ftrace_disabled = 1;
2812}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002813
Steven Rostedt3d083392008-05-12 21:20:42 +02002814#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002815
2816static int __init ftrace_nodyn_init(void)
2817{
2818 ftrace_enabled = 1;
2819 return 0;
2820}
2821device_initcall(ftrace_nodyn_init);
2822
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002823static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2824static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002825/* Keep as macros so we do not need to define the commands */
2826# define ftrace_startup(command) do { } while (0)
2827# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002828# define ftrace_startup_sysctl() do { } while (0)
2829# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002830#endif /* CONFIG_DYNAMIC_FTRACE */
2831
Steven Rostedte32d8952008-12-04 00:26:41 -05002832static void clear_ftrace_swapper(void)
2833{
2834 struct task_struct *p;
2835 int cpu;
2836
2837 get_online_cpus();
2838 for_each_online_cpu(cpu) {
2839 p = idle_task(cpu);
2840 clear_tsk_trace_trace(p);
2841 }
2842 put_online_cpus();
2843}
2844
2845static void set_ftrace_swapper(void)
2846{
2847 struct task_struct *p;
2848 int cpu;
2849
2850 get_online_cpus();
2851 for_each_online_cpu(cpu) {
2852 p = idle_task(cpu);
2853 set_tsk_trace_trace(p);
2854 }
2855 put_online_cpus();
2856}
2857
2858static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002859{
2860 struct task_struct *p;
2861
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002862 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002863 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002864 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002865 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002866 rcu_read_unlock();
2867
Steven Rostedte32d8952008-12-04 00:26:41 -05002868 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002869}
2870
Steven Rostedte32d8952008-12-04 00:26:41 -05002871static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002872{
2873 struct task_struct *p;
2874
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002875 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002876 do_each_pid_task(pid, PIDTYPE_PID, p) {
2877 set_tsk_trace_trace(p);
2878 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002879 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002880}
2881
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002882static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002883{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002884 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002885 clear_ftrace_swapper();
2886 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002887 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05002888}
2889
2890static void set_ftrace_pid_task(struct pid *pid)
2891{
2892 if (pid == ftrace_swapper_pid)
2893 set_ftrace_swapper();
2894 else
2895 set_ftrace_pid(pid);
2896}
2897
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002898static int ftrace_pid_add(int p)
2899{
2900 struct pid *pid;
2901 struct ftrace_pid *fpid;
2902 int ret = -EINVAL;
2903
2904 mutex_lock(&ftrace_lock);
2905
2906 if (!p)
2907 pid = ftrace_swapper_pid;
2908 else
2909 pid = find_get_pid(p);
2910
2911 if (!pid)
2912 goto out;
2913
2914 ret = 0;
2915
2916 list_for_each_entry(fpid, &ftrace_pids, list)
2917 if (fpid->pid == pid)
2918 goto out_put;
2919
2920 ret = -ENOMEM;
2921
2922 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2923 if (!fpid)
2924 goto out_put;
2925
2926 list_add(&fpid->list, &ftrace_pids);
2927 fpid->pid = pid;
2928
2929 set_ftrace_pid_task(pid);
2930
2931 ftrace_update_pid_func();
2932 ftrace_startup_enable(0);
2933
2934 mutex_unlock(&ftrace_lock);
2935 return 0;
2936
2937out_put:
2938 if (pid != ftrace_swapper_pid)
2939 put_pid(pid);
2940
2941out:
2942 mutex_unlock(&ftrace_lock);
2943 return ret;
2944}
2945
2946static void ftrace_pid_reset(void)
2947{
2948 struct ftrace_pid *fpid, *safe;
2949
2950 mutex_lock(&ftrace_lock);
2951 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2952 struct pid *pid = fpid->pid;
2953
2954 clear_ftrace_pid_task(pid);
2955
2956 list_del(&fpid->list);
2957 kfree(fpid);
2958 }
2959
2960 ftrace_update_pid_func();
2961 ftrace_startup_enable(0);
2962
2963 mutex_unlock(&ftrace_lock);
2964}
2965
2966static void *fpid_start(struct seq_file *m, loff_t *pos)
2967{
2968 mutex_lock(&ftrace_lock);
2969
2970 if (list_empty(&ftrace_pids) && (!*pos))
2971 return (void *) 1;
2972
2973 return seq_list_start(&ftrace_pids, *pos);
2974}
2975
2976static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2977{
2978 if (v == (void *)1)
2979 return NULL;
2980
2981 return seq_list_next(v, &ftrace_pids, pos);
2982}
2983
2984static void fpid_stop(struct seq_file *m, void *p)
2985{
2986 mutex_unlock(&ftrace_lock);
2987}
2988
2989static int fpid_show(struct seq_file *m, void *v)
2990{
2991 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2992
2993 if (v == (void *)1) {
2994 seq_printf(m, "no pid\n");
2995 return 0;
2996 }
2997
2998 if (fpid->pid == ftrace_swapper_pid)
2999 seq_printf(m, "swapper tasks\n");
3000 else
3001 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3002
3003 return 0;
3004}
3005
3006static const struct seq_operations ftrace_pid_sops = {
3007 .start = fpid_start,
3008 .next = fpid_next,
3009 .stop = fpid_stop,
3010 .show = fpid_show,
3011};
3012
3013static int
3014ftrace_pid_open(struct inode *inode, struct file *file)
3015{
3016 int ret = 0;
3017
3018 if ((file->f_mode & FMODE_WRITE) &&
3019 (file->f_flags & O_TRUNC))
3020 ftrace_pid_reset();
3021
3022 if (file->f_mode & FMODE_READ)
3023 ret = seq_open(file, &ftrace_pid_sops);
3024
3025 return ret;
3026}
3027
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003028static ssize_t
3029ftrace_pid_write(struct file *filp, const char __user *ubuf,
3030 size_t cnt, loff_t *ppos)
3031{
Ingo Molnar457dc922009-11-23 11:03:28 +01003032 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003033 long val;
3034 int ret;
3035
3036 if (cnt >= sizeof(buf))
3037 return -EINVAL;
3038
3039 if (copy_from_user(&buf, ubuf, cnt))
3040 return -EFAULT;
3041
3042 buf[cnt] = 0;
3043
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003044 /*
3045 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3046 * to clean the filter quietly.
3047 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003048 tmp = strstrip(buf);
3049 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003050 return 1;
3051
Ingo Molnar457dc922009-11-23 11:03:28 +01003052 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003053 if (ret < 0)
3054 return ret;
3055
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003056 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003057
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003058 return ret ? ret : cnt;
3059}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003060
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003061static int
3062ftrace_pid_release(struct inode *inode, struct file *file)
3063{
3064 if (file->f_mode & FMODE_READ)
3065 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003066
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003067 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003068}
3069
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003070static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003071 .open = ftrace_pid_open,
3072 .write = ftrace_pid_write,
3073 .read = seq_read,
3074 .llseek = seq_lseek,
3075 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003076};
3077
3078static __init int ftrace_init_debugfs(void)
3079{
3080 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003081
3082 d_tracer = tracing_init_dentry();
3083 if (!d_tracer)
3084 return 0;
3085
3086 ftrace_init_dyn_debugfs(d_tracer);
3087
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003088 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3089 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003090
3091 ftrace_profile_debugfs(d_tracer);
3092
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003093 return 0;
3094}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003095fs_initcall(ftrace_init_debugfs);
3096
Steven Rostedt3d083392008-05-12 21:20:42 +02003097/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003098 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003099 *
3100 * This function should be used by panic code. It stops ftrace
3101 * but in a not so nice way. If you need to simply kill ftrace
3102 * from a non-atomic section, use ftrace_kill.
3103 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003104void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003105{
3106 ftrace_disabled = 1;
3107 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003108 clear_ftrace_function();
3109}
3110
3111/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003112 * register_ftrace_function - register a function for profiling
3113 * @ops - ops structure that holds the function for profiling.
3114 *
3115 * Register a function to be called by all functions in the
3116 * kernel.
3117 *
3118 * Note: @ops->func and all the functions it calls must be labeled
3119 * with "notrace", otherwise it will go into a
3120 * recursive loop.
3121 */
3122int register_ftrace_function(struct ftrace_ops *ops)
3123{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003124 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003125
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003126 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003127
Steven Rostedt45a4a232011-04-21 23:16:46 -04003128 if (unlikely(ftrace_disabled))
3129 goto out_unlock;
3130
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003131 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003132 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003133
Steven Rostedt45a4a232011-04-21 23:16:46 -04003134 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003135 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003136 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003137}
3138
3139/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003140 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003141 * @ops - ops structure that holds the function to unregister
3142 *
3143 * Unregister a function that was added to be called by ftrace profiling.
3144 */
3145int unregister_ftrace_function(struct ftrace_ops *ops)
3146{
3147 int ret;
3148
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003149 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003150 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003151 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003152 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003153
3154 return ret;
3155}
3156
Ingo Molnare309b412008-05-12 21:20:51 +02003157int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003158ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003159 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003160 loff_t *ppos)
3161{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003162 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003163
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003164 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003165
Steven Rostedt45a4a232011-04-21 23:16:46 -04003166 if (unlikely(ftrace_disabled))
3167 goto out;
3168
3169 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003170
Li Zefana32c7762009-06-26 16:55:51 +08003171 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003172 goto out;
3173
Li Zefana32c7762009-06-26 16:55:51 +08003174 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003175
3176 if (ftrace_enabled) {
3177
3178 ftrace_startup_sysctl();
3179
3180 /* we are starting ftrace again */
3181 if (ftrace_list != &ftrace_list_end) {
3182 if (ftrace_list->next == &ftrace_list_end)
3183 ftrace_trace_function = ftrace_list->func;
3184 else
3185 ftrace_trace_function = ftrace_list_func;
3186 }
3187
3188 } else {
3189 /* stopping ftrace calls (just send to ftrace_stub) */
3190 ftrace_trace_function = ftrace_stub;
3191
3192 ftrace_shutdown_sysctl();
3193 }
3194
3195 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003196 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003197 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003198}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003199
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003200#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003201
Steven Rostedt597af812009-04-03 15:24:12 -04003202static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003203static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003204
Steven Rostedte49dc192008-12-02 23:50:05 -05003205int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3206{
3207 return 0;
3208}
3209
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003210/* The callbacks that hook a function */
3211trace_func_graph_ret_t ftrace_graph_return =
3212 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003213trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003214
3215/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3216static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3217{
3218 int i;
3219 int ret = 0;
3220 unsigned long flags;
3221 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3222 struct task_struct *g, *t;
3223
3224 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3225 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3226 * sizeof(struct ftrace_ret_stack),
3227 GFP_KERNEL);
3228 if (!ret_stack_list[i]) {
3229 start = 0;
3230 end = i;
3231 ret = -ENOMEM;
3232 goto free;
3233 }
3234 }
3235
3236 read_lock_irqsave(&tasklist_lock, flags);
3237 do_each_thread(g, t) {
3238 if (start == end) {
3239 ret = -EAGAIN;
3240 goto unlock;
3241 }
3242
3243 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003244 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003245 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003246 t->curr_ret_stack = -1;
3247 /* Make sure the tasks see the -1 first: */
3248 smp_wmb();
3249 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003250 }
3251 } while_each_thread(g, t);
3252
3253unlock:
3254 read_unlock_irqrestore(&tasklist_lock, flags);
3255free:
3256 for (i = start; i < end; i++)
3257 kfree(ret_stack_list[i]);
3258 return ret;
3259}
3260
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003261static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04003262ftrace_graph_probe_sched_switch(void *ignore,
3263 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003264{
3265 unsigned long long timestamp;
3266 int index;
3267
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003268 /*
3269 * Does the user want to count the time a function was asleep.
3270 * If so, do not update the time stamps.
3271 */
3272 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3273 return;
3274
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003275 timestamp = trace_clock_local();
3276
3277 prev->ftrace_timestamp = timestamp;
3278
3279 /* only process tasks that we timestamped */
3280 if (!next->ftrace_timestamp)
3281 return;
3282
3283 /*
3284 * Update all the counters in next to make up for the
3285 * time next was sleeping.
3286 */
3287 timestamp -= next->ftrace_timestamp;
3288
3289 for (index = next->curr_ret_stack; index >= 0; index--)
3290 next->ret_stack[index].calltime += timestamp;
3291}
3292
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003293/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003294static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003295{
3296 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003297 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003298
3299 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3300 sizeof(struct ftrace_ret_stack *),
3301 GFP_KERNEL);
3302
3303 if (!ret_stack_list)
3304 return -ENOMEM;
3305
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003306 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003307 for_each_online_cpu(cpu) {
3308 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05003309 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04003310 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003311
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003312 do {
3313 ret = alloc_retstack_tasklist(ret_stack_list);
3314 } while (ret == -EAGAIN);
3315
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003316 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04003317 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003318 if (ret)
3319 pr_info("ftrace_graph: Couldn't activate tracepoint"
3320 " probe to kernel_sched_switch\n");
3321 }
3322
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003323 kfree(ret_stack_list);
3324 return ret;
3325}
3326
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003327/*
3328 * Hibernation protection.
3329 * The state of the current task is too much unstable during
3330 * suspend/restore to disk. We want to protect against that.
3331 */
3332static int
3333ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3334 void *unused)
3335{
3336 switch (state) {
3337 case PM_HIBERNATION_PREPARE:
3338 pause_graph_tracing();
3339 break;
3340
3341 case PM_POST_HIBERNATION:
3342 unpause_graph_tracing();
3343 break;
3344 }
3345 return NOTIFY_DONE;
3346}
3347
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003348int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3349 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003350{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003351 int ret = 0;
3352
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003353 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003354
Steven Rostedt05ce5812009-03-24 00:18:31 -04003355 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003356 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003357 ret = -EBUSY;
3358 goto out;
3359 }
3360
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003361 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3362 register_pm_notifier(&ftrace_suspend_notifier);
3363
Steven Rostedt597af812009-04-03 15:24:12 -04003364 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003365 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003366 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003367 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003368 goto out;
3369 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003370
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003371 ftrace_graph_return = retfunc;
3372 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003373
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003374 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003375
3376out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003377 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003378 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003379}
3380
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003381void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003382{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003383 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003384
Steven Rostedt597af812009-04-03 15:24:12 -04003385 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003386 goto out;
3387
Steven Rostedt597af812009-04-03 15:24:12 -04003388 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003389 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003390 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003391 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003392 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04003393 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003394
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003395 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003396 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003397}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003398
Steven Rostedt868baf02011-02-10 21:26:13 -05003399static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
3400
3401static void
3402graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
3403{
3404 atomic_set(&t->tracing_graph_pause, 0);
3405 atomic_set(&t->trace_overrun, 0);
3406 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003407 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05003408 smp_wmb();
3409 t->ret_stack = ret_stack;
3410}
3411
3412/*
3413 * Allocate a return stack for the idle task. May be the first
3414 * time through, or it may be done by CPU hotplug online.
3415 */
3416void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
3417{
3418 t->curr_ret_stack = -1;
3419 /*
3420 * The idle task has no parent, it either has its own
3421 * stack or no stack at all.
3422 */
3423 if (t->ret_stack)
3424 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
3425
3426 if (ftrace_graph_active) {
3427 struct ftrace_ret_stack *ret_stack;
3428
3429 ret_stack = per_cpu(idle_ret_stack, cpu);
3430 if (!ret_stack) {
3431 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3432 * sizeof(struct ftrace_ret_stack),
3433 GFP_KERNEL);
3434 if (!ret_stack)
3435 return;
3436 per_cpu(idle_ret_stack, cpu) = ret_stack;
3437 }
3438 graph_init_task(t, ret_stack);
3439 }
3440}
3441
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003442/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003443void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003444{
Steven Rostedt84047e32009-06-02 16:51:55 -04003445 /* Make sure we do not use the parent ret_stack */
3446 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05003447 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04003448
Steven Rostedt597af812009-04-03 15:24:12 -04003449 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003450 struct ftrace_ret_stack *ret_stack;
3451
3452 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003453 * sizeof(struct ftrace_ret_stack),
3454 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003455 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003456 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05003457 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04003458 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003459}
3460
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003461void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003462{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003463 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3464
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003465 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003466 /* NULL must become visible to IRQs before we free it: */
3467 barrier();
3468
3469 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003470}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003471
3472void ftrace_graph_stop(void)
3473{
3474 ftrace_stop();
3475}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003476#endif