blob: 5e1ad476309060b6b697b6475f7fc5de96bbc5a2 [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) \
42 do { \
43 if (WARN_ON(cond)) \
44 ftrace_kill(); \
45 } while (0)
46
47#define FTRACE_WARN_ON_ONCE(cond) \
48 do { \
49 if (WARN_ON_ONCE(cond)) \
50 ftrace_kill(); \
51 } while (0)
52
Steven Rostedt8fc0c702009-02-16 15:28:00 -050053/* hash bits for specific function selection */
54#define FTRACE_HASH_BITS 7
55#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
56
Steven Rostedt4eebcc82008-05-12 21:20:48 +020057/* ftrace_enabled is a method to turn ftrace on or off */
58int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020059static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020060
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050061/* Quick disabling of function tracer. */
62int function_trace_stop;
63
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040064/* List for set_ftrace_pid's pids. */
65LIST_HEAD(ftrace_pids);
66struct ftrace_pid {
67 struct list_head list;
68 struct pid *pid;
69};
70
Steven Rostedt4eebcc82008-05-12 21:20:48 +020071/*
72 * ftrace_disabled is set when an anomaly is discovered.
73 * ftrace_disabled is much stronger than ftrace_enabled.
74 */
75static int ftrace_disabled __read_mostly;
76
Steven Rostedt52baf112009-02-14 01:15:39 -050077static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020078
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020079static struct ftrace_ops ftrace_list_end __read_mostly =
80{
Steven Rostedtfb9fb012009-03-25 13:26:41 -040081 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020082};
83
84static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
85ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050086ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050087ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020088
Paul E. McKenney3f379b02010-03-05 15:03:25 -080089/*
90 * Traverse the ftrace_list, invoking all entries. The reason that we
91 * can use rcu_dereference_raw() is that elements removed from this list
92 * are simply leaked, so there is no need to interact with a grace-period
93 * mechanism. The rcu_dereference_raw() calls are needed to handle
94 * concurrent insertions into the ftrace_list.
95 *
96 * Silly Alpha and silly pointer-speculation compiler optimizations!
97 */
Ingo Molnarf2252932008-05-22 10:37:48 +020098static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020099{
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800100 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200101
102 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200103 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800104 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200105 };
106}
107
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500108static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
109{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500110 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500111 return;
112
113 ftrace_pid_function(ip, parent_ip);
114}
115
116static void set_ftrace_pid_function(ftrace_func_t func)
117{
118 /* do not set ftrace_pid_function to itself! */
119 if (func != ftrace_pid_func)
120 ftrace_pid_function = func;
121}
122
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200123/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200124 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200125 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200126 * This NULLs the ftrace function and in essence stops
127 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200128 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200129void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200130{
Steven Rostedt3d083392008-05-12 21:20:42 +0200131 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500132 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500133 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200134}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200135
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500136#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
137/*
138 * For those archs that do not test ftrace_trace_stop in their
139 * mcount call site, we need to do it from C.
140 */
141static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
142{
143 if (function_trace_stop)
144 return;
145
146 __ftrace_trace_function(ip, parent_ip);
147}
148#endif
149
Ingo Molnare309b412008-05-12 21:20:51 +0200150static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200151{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200152 ops->next = ftrace_list;
153 /*
154 * We are entering ops into the ftrace_list but another
155 * CPU might be walking that list. We need to make sure
156 * the ops->next pointer is valid before another CPU sees
157 * the ops pointer included into the ftrace_list.
158 */
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800159 rcu_assign_pointer(ftrace_list, ops);
Steven Rostedt3d083392008-05-12 21:20:42 +0200160
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200161 if (ftrace_enabled) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500162 ftrace_func_t func;
163
164 if (ops->next == &ftrace_list_end)
165 func = ops->func;
166 else
167 func = ftrace_list_func;
168
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400169 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500170 set_ftrace_pid_function(func);
171 func = ftrace_pid_func;
172 }
173
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200174 /*
175 * For one func, simply call it directly.
176 * For more than one func, call the chain.
177 */
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500178#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500179 ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500180#else
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500181 __ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500182 ftrace_trace_function = ftrace_test_stop_func;
183#endif
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200184 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200185
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200186 return 0;
187}
188
Ingo Molnare309b412008-05-12 21:20:51 +0200189static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200190{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200191 struct ftrace_ops **p;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200192
193 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200194 * If we are removing the last function, then simply point
195 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200196 */
197 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
198 ftrace_trace_function = ftrace_stub;
199 ftrace_list = &ftrace_list_end;
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500200 return 0;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200201 }
202
203 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
204 if (*p == ops)
205 break;
206
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500207 if (*p != ops)
208 return -1;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200209
210 *p = (*p)->next;
211
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200212 if (ftrace_enabled) {
213 /* If we only have one func left, then call that directly */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500214 if (ftrace_list->next == &ftrace_list_end) {
215 ftrace_func_t func = ftrace_list->func;
216
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400217 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500218 set_ftrace_pid_function(func);
219 func = ftrace_pid_func;
220 }
221#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
222 ftrace_trace_function = func;
223#else
224 __ftrace_trace_function = func;
225#endif
226 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200227 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200228
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500229 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200230}
231
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500232static void ftrace_update_pid_func(void)
233{
234 ftrace_func_t func;
235
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500236 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900237 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500238
Matt Fleming33974092009-09-28 16:43:01 +0100239#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500240 func = ftrace_trace_function;
Matt Fleming33974092009-09-28 16:43:01 +0100241#else
242 func = __ftrace_trace_function;
243#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500244
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400245 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500246 set_ftrace_pid_function(func);
247 func = ftrace_pid_func;
248 } else {
Liming Wang66eafeb2008-12-02 10:33:08 +0800249 if (func == ftrace_pid_func)
250 func = ftrace_pid_function;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500251 }
252
253#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
254 ftrace_trace_function = func;
255#else
256 __ftrace_trace_function = func;
257#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500258}
259
Steven Rostedt493762f2009-03-23 17:12:36 -0400260#ifdef CONFIG_FUNCTION_PROFILER
261struct ftrace_profile {
262 struct hlist_node node;
263 unsigned long ip;
264 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400265#ifdef CONFIG_FUNCTION_GRAPH_TRACER
266 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400267 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400268#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400269};
270
271struct ftrace_profile_page {
272 struct ftrace_profile_page *next;
273 unsigned long index;
274 struct ftrace_profile records[];
275};
276
Steven Rostedtcafb1682009-03-24 20:50:39 -0400277struct ftrace_profile_stat {
278 atomic_t disabled;
279 struct hlist_head *hash;
280 struct ftrace_profile_page *pages;
281 struct ftrace_profile_page *start;
282 struct tracer_stat stat;
283};
284
Steven Rostedt493762f2009-03-23 17:12:36 -0400285#define PROFILE_RECORDS_SIZE \
286 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
287
288#define PROFILES_PER_PAGE \
289 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
290
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400291static int ftrace_profile_bits __read_mostly;
292static int ftrace_profile_enabled __read_mostly;
293
294/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400295static DEFINE_MUTEX(ftrace_profile_lock);
296
Steven Rostedtcafb1682009-03-24 20:50:39 -0400297static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400298
299#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
300
Steven Rostedt493762f2009-03-23 17:12:36 -0400301static void *
302function_stat_next(void *v, int idx)
303{
304 struct ftrace_profile *rec = v;
305 struct ftrace_profile_page *pg;
306
307 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
308
309 again:
Li Zefan0296e422009-06-26 11:15:37 +0800310 if (idx != 0)
311 rec++;
312
Steven Rostedt493762f2009-03-23 17:12:36 -0400313 if ((void *)rec >= (void *)&pg->records[pg->index]) {
314 pg = pg->next;
315 if (!pg)
316 return NULL;
317 rec = &pg->records[0];
318 if (!rec->counter)
319 goto again;
320 }
321
322 return rec;
323}
324
325static void *function_stat_start(struct tracer_stat *trace)
326{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400327 struct ftrace_profile_stat *stat =
328 container_of(trace, struct ftrace_profile_stat, stat);
329
330 if (!stat || !stat->start)
331 return NULL;
332
333 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400334}
335
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400336#ifdef CONFIG_FUNCTION_GRAPH_TRACER
337/* function graph compares on total time */
338static int function_stat_cmp(void *p1, void *p2)
339{
340 struct ftrace_profile *a = p1;
341 struct ftrace_profile *b = p2;
342
343 if (a->time < b->time)
344 return -1;
345 if (a->time > b->time)
346 return 1;
347 else
348 return 0;
349}
350#else
351/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400352static int function_stat_cmp(void *p1, void *p2)
353{
354 struct ftrace_profile *a = p1;
355 struct ftrace_profile *b = p2;
356
357 if (a->counter < b->counter)
358 return -1;
359 if (a->counter > b->counter)
360 return 1;
361 else
362 return 0;
363}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400364#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400365
366static int function_stat_headers(struct seq_file *m)
367{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400368#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400369 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400370 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400371 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400372 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400373#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400374 seq_printf(m, " Function Hit\n"
375 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400376#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400377 return 0;
378}
379
380static int function_stat_show(struct seq_file *m, void *v)
381{
382 struct ftrace_profile *rec = v;
383 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800384 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400385#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400386 static struct trace_seq s;
387 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400388 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400389#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800390 mutex_lock(&ftrace_profile_lock);
391
392 /* we raced with function_profile_reset() */
393 if (unlikely(rec->counter == 0)) {
394 ret = -EBUSY;
395 goto out;
396 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400397
398 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400399 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400400
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400401#ifdef CONFIG_FUNCTION_GRAPH_TRACER
402 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400403 avg = rec->time;
404 do_div(avg, rec->counter);
405
Chase Douglase330b3b2010-04-26 14:02:05 -0400406 /* Sample standard deviation (s^2) */
407 if (rec->counter <= 1)
408 stddev = 0;
409 else {
410 stddev = rec->time_squared - rec->counter * avg * avg;
411 /*
412 * Divide only 1000 for ns^2 -> us^2 conversion.
413 * trace_print_graph_duration will divide 1000 again.
414 */
415 do_div(stddev, (rec->counter - 1) * 1000);
416 }
417
Steven Rostedt34886c82009-03-25 21:00:47 -0400418 trace_seq_init(&s);
419 trace_print_graph_duration(rec->time, &s);
420 trace_seq_puts(&s, " ");
421 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400422 trace_seq_puts(&s, " ");
423 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400424 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400425#endif
426 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800427out:
428 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400429
Li Zefan3aaba202010-08-23 16:50:12 +0800430 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400431}
432
Steven Rostedtcafb1682009-03-24 20:50:39 -0400433static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400434{
435 struct ftrace_profile_page *pg;
436
Steven Rostedtcafb1682009-03-24 20:50:39 -0400437 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400438
439 while (pg) {
440 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
441 pg->index = 0;
442 pg = pg->next;
443 }
444
Steven Rostedtcafb1682009-03-24 20:50:39 -0400445 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400446 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
447}
448
Steven Rostedtcafb1682009-03-24 20:50:39 -0400449int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400450{
451 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400452 int functions;
453 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400454 int i;
455
456 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400457 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400458 return 0;
459
Steven Rostedtcafb1682009-03-24 20:50:39 -0400460 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
461 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400462 return -ENOMEM;
463
Steven Rostedt318e0a72009-03-25 20:06:34 -0400464#ifdef CONFIG_DYNAMIC_FTRACE
465 functions = ftrace_update_tot_cnt;
466#else
467 /*
468 * We do not know the number of functions that exist because
469 * dynamic tracing is what counts them. With past experience
470 * we have around 20K functions. That should be more than enough.
471 * It is highly unlikely we will execute every function in
472 * the kernel.
473 */
474 functions = 20000;
475#endif
476
Steven Rostedtcafb1682009-03-24 20:50:39 -0400477 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400478
Steven Rostedt318e0a72009-03-25 20:06:34 -0400479 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
480
481 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400482 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400483 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400484 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400485 pg = pg->next;
486 }
487
488 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400489
490 out_free:
491 pg = stat->start;
492 while (pg) {
493 unsigned long tmp = (unsigned long)pg;
494
495 pg = pg->next;
496 free_page(tmp);
497 }
498
499 free_page((unsigned long)stat->pages);
500 stat->pages = NULL;
501 stat->start = NULL;
502
503 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400504}
505
Steven Rostedtcafb1682009-03-24 20:50:39 -0400506static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400507{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400508 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400509 int size;
510
Steven Rostedtcafb1682009-03-24 20:50:39 -0400511 stat = &per_cpu(ftrace_profile_stats, cpu);
512
513 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400514 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400515 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400516 return 0;
517 }
518
519 /*
520 * We are profiling all functions, but usually only a few thousand
521 * functions are hit. We'll make a hash of 1024 items.
522 */
523 size = FTRACE_PROFILE_HASH_SIZE;
524
Steven Rostedtcafb1682009-03-24 20:50:39 -0400525 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400526
Steven Rostedtcafb1682009-03-24 20:50:39 -0400527 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400528 return -ENOMEM;
529
Steven Rostedtcafb1682009-03-24 20:50:39 -0400530 if (!ftrace_profile_bits) {
531 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400532
Steven Rostedtcafb1682009-03-24 20:50:39 -0400533 for (; size; size >>= 1)
534 ftrace_profile_bits++;
535 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400536
Steven Rostedt318e0a72009-03-25 20:06:34 -0400537 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400538 if (ftrace_profile_pages_init(stat) < 0) {
539 kfree(stat->hash);
540 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400541 return -ENOMEM;
542 }
543
544 return 0;
545}
546
Steven Rostedtcafb1682009-03-24 20:50:39 -0400547static int ftrace_profile_init(void)
548{
549 int cpu;
550 int ret = 0;
551
552 for_each_online_cpu(cpu) {
553 ret = ftrace_profile_init_cpu(cpu);
554 if (ret)
555 break;
556 }
557
558 return ret;
559}
560
Steven Rostedt493762f2009-03-23 17:12:36 -0400561/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400562static struct ftrace_profile *
563ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400564{
565 struct ftrace_profile *rec;
566 struct hlist_head *hhd;
567 struct hlist_node *n;
568 unsigned long key;
569
570 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400571 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400572
573 if (hlist_empty(hhd))
574 return NULL;
575
576 hlist_for_each_entry_rcu(rec, n, hhd, node) {
577 if (rec->ip == ip)
578 return rec;
579 }
580
581 return NULL;
582}
583
Steven Rostedtcafb1682009-03-24 20:50:39 -0400584static void ftrace_add_profile(struct ftrace_profile_stat *stat,
585 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400586{
587 unsigned long key;
588
589 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400590 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400591}
592
Steven Rostedt318e0a72009-03-25 20:06:34 -0400593/*
594 * The memory is already allocated, this simply finds a new record to use.
595 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400596static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400597ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400598{
599 struct ftrace_profile *rec = NULL;
600
Steven Rostedt318e0a72009-03-25 20:06:34 -0400601 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400602 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400603 goto out;
604
Steven Rostedt493762f2009-03-23 17:12:36 -0400605 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400606 * Try to find the function again since an NMI
607 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400608 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400609 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400610 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400611 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400612
Steven Rostedtcafb1682009-03-24 20:50:39 -0400613 if (stat->pages->index == PROFILES_PER_PAGE) {
614 if (!stat->pages->next)
615 goto out;
616 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400617 }
618
Steven Rostedtcafb1682009-03-24 20:50:39 -0400619 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400620 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400621 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400622
Steven Rostedt493762f2009-03-23 17:12:36 -0400623 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400624 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400625
626 return rec;
627}
628
Steven Rostedt493762f2009-03-23 17:12:36 -0400629static void
630function_profile_call(unsigned long ip, unsigned long parent_ip)
631{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400632 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400633 struct ftrace_profile *rec;
634 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400635
636 if (!ftrace_profile_enabled)
637 return;
638
Steven Rostedt493762f2009-03-23 17:12:36 -0400639 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400640
641 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400642 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400643 goto out;
644
645 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400646 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400647 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400648 if (!rec)
649 goto out;
650 }
651
652 rec->counter++;
653 out:
654 local_irq_restore(flags);
655}
656
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400657#ifdef CONFIG_FUNCTION_GRAPH_TRACER
658static int profile_graph_entry(struct ftrace_graph_ent *trace)
659{
660 function_profile_call(trace->func, 0);
661 return 1;
662}
663
664static void profile_graph_return(struct ftrace_graph_ret *trace)
665{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400666 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400667 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400668 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400670
671 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400672 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400673 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400674 goto out;
675
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400676 /* If the calltime was zero'd ignore it */
677 if (!trace->calltime)
678 goto out;
679
Steven Rostedta2a16d62009-03-24 23:17:58 -0400680 calltime = trace->rettime - trace->calltime;
681
682 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
683 int index;
684
685 index = trace->depth;
686
687 /* Append this call time to the parent time to subtract */
688 if (index)
689 current->ret_stack[index - 1].subtime += calltime;
690
691 if (current->ret_stack[index].subtime < calltime)
692 calltime -= current->ret_stack[index].subtime;
693 else
694 calltime = 0;
695 }
696
Steven Rostedtcafb1682009-03-24 20:50:39 -0400697 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400698 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400699 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400700 rec->time_squared += calltime * calltime;
701 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400702
Steven Rostedtcafb1682009-03-24 20:50:39 -0400703 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400704 local_irq_restore(flags);
705}
706
707static int register_ftrace_profiler(void)
708{
709 return register_ftrace_graph(&profile_graph_return,
710 &profile_graph_entry);
711}
712
713static void unregister_ftrace_profiler(void)
714{
715 unregister_ftrace_graph();
716}
717#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400718static struct ftrace_ops ftrace_profile_ops __read_mostly =
719{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400720 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400721};
722
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400723static int register_ftrace_profiler(void)
724{
725 return register_ftrace_function(&ftrace_profile_ops);
726}
727
728static void unregister_ftrace_profiler(void)
729{
730 unregister_ftrace_function(&ftrace_profile_ops);
731}
732#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
733
Steven Rostedt493762f2009-03-23 17:12:36 -0400734static ssize_t
735ftrace_profile_write(struct file *filp, const char __user *ubuf,
736 size_t cnt, loff_t *ppos)
737{
738 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400739 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400740 int ret;
741
742 if (cnt >= sizeof(buf))
743 return -EINVAL;
744
745 if (copy_from_user(&buf, ubuf, cnt))
746 return -EFAULT;
747
748 buf[cnt] = 0;
749
750 ret = strict_strtoul(buf, 10, &val);
751 if (ret < 0)
752 return ret;
753
754 val = !!val;
755
756 mutex_lock(&ftrace_profile_lock);
757 if (ftrace_profile_enabled ^ val) {
758 if (val) {
759 ret = ftrace_profile_init();
760 if (ret < 0) {
761 cnt = ret;
762 goto out;
763 }
764
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400765 ret = register_ftrace_profiler();
766 if (ret < 0) {
767 cnt = ret;
768 goto out;
769 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400770 ftrace_profile_enabled = 1;
771 } else {
772 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400773 /*
774 * unregister_ftrace_profiler calls stop_machine
775 * so this acts like an synchronize_sched.
776 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400777 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400778 }
779 }
780 out:
781 mutex_unlock(&ftrace_profile_lock);
782
Jiri Olsacf8517c2009-10-23 19:36:16 -0400783 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400784
785 return cnt;
786}
787
788static ssize_t
789ftrace_profile_read(struct file *filp, char __user *ubuf,
790 size_t cnt, loff_t *ppos)
791{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400792 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400793 int r;
794
795 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
796 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
797}
798
799static const struct file_operations ftrace_profile_fops = {
800 .open = tracing_open_generic,
801 .read = ftrace_profile_read,
802 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200803 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400804};
805
Steven Rostedtcafb1682009-03-24 20:50:39 -0400806/* used to initialize the real stat files */
807static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400808 .name = "functions",
809 .stat_start = function_stat_start,
810 .stat_next = function_stat_next,
811 .stat_cmp = function_stat_cmp,
812 .stat_headers = function_stat_headers,
813 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400814};
815
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400816static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400817{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400818 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400819 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400820 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400821 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400822 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400823
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824 for_each_possible_cpu(cpu) {
825 stat = &per_cpu(ftrace_profile_stats, cpu);
826
827 /* allocate enough for function name + cpu number */
828 name = kmalloc(32, GFP_KERNEL);
829 if (!name) {
830 /*
831 * The files created are permanent, if something happens
832 * we still do not free memory.
833 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400834 WARN(1,
835 "Could not allocate stat file for cpu %d\n",
836 cpu);
837 return;
838 }
839 stat->stat = function_stats;
840 snprintf(name, 32, "function%d", cpu);
841 stat->stat.name = name;
842 ret = register_stat_tracer(&stat->stat);
843 if (ret) {
844 WARN(1,
845 "Could not register function stat for cpu %d\n",
846 cpu);
847 kfree(name);
848 return;
849 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400850 }
851
852 entry = debugfs_create_file("function_profile_enabled", 0644,
853 d_tracer, NULL, &ftrace_profile_fops);
854 if (!entry)
855 pr_warning("Could not create debugfs "
856 "'function_profile_enabled' entry\n");
857}
858
859#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400860static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400861{
862}
863#endif /* CONFIG_FUNCTION_PROFILER */
864
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100865static struct pid * const ftrace_swapper_pid = &init_struct_pid;
866
Steven Rostedt3d083392008-05-12 21:20:42 +0200867#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100868
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400869#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400870# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400871#endif
872
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500873static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
874
Steven Rostedtb6887d72009-02-17 12:32:04 -0500875struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500876 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500877 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500878 unsigned long flags;
879 unsigned long ip;
880 void *data;
881 struct rcu_head rcu;
882};
883
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200884enum {
885 FTRACE_ENABLE_CALLS = (1 << 0),
886 FTRACE_DISABLE_CALLS = (1 << 1),
887 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
888 FTRACE_ENABLE_MCOUNT = (1 << 3),
889 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500890 FTRACE_START_FUNC_RET = (1 << 5),
891 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200892};
893
Steven Rostedt5072c592008-05-12 21:20:43 +0200894static int ftrace_filtered;
895
Lai Jiangshane94142a2009-03-13 17:51:27 +0800896static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200897
Steven Rostedt41c52c02008-05-22 11:46:33 -0400898static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200899
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200900struct ftrace_page {
901 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500902 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200903 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700904};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200905
906#define ENTRIES_PER_PAGE \
907 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
908
909/* estimate from running different kernels */
910#define NR_TO_INIT 10000
911
912static struct ftrace_page *ftrace_pages_start;
913static struct ftrace_page *ftrace_pages;
914
Steven Rostedt37ad5082008-05-12 21:20:48 +0200915static struct dyn_ftrace *ftrace_free_records;
916
Steven Rostedt265c8312009-02-13 12:43:56 -0500917/*
918 * This is a double for. Do not use 'break' to break out of the loop,
919 * you must use a goto.
920 */
921#define do_for_each_ftrace_rec(pg, rec) \
922 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
923 int _____i; \
924 for (_____i = 0; _____i < pg->index; _____i++) { \
925 rec = &pg->records[_____i];
926
927#define while_for_each_ftrace_rec() \
928 } \
929 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530930
Ingo Molnare309b412008-05-12 21:20:51 +0200931static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad5082008-05-12 21:20:48 +0200932{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800933 rec->freelist = ftrace_free_records;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200934 ftrace_free_records = rec;
935 rec->flags |= FTRACE_FL_FREE;
936}
937
Ingo Molnare309b412008-05-12 21:20:51 +0200938static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200939{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200940 struct dyn_ftrace *rec;
941
942 /* First check for freed records */
943 if (ftrace_free_records) {
944 rec = ftrace_free_records;
945
Steven Rostedt37ad5082008-05-12 21:20:48 +0200946 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt69128962008-10-23 09:33:03 -0400947 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad5082008-05-12 21:20:48 +0200948 ftrace_free_records = NULL;
949 return NULL;
950 }
951
Lai Jiangshanee000b72009-03-24 13:38:06 +0800952 ftrace_free_records = rec->freelist;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200953 memset(rec, 0, sizeof(*rec));
954 return rec;
955 }
956
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200957 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400958 if (!ftrace_pages->next) {
959 /* allocate another page */
960 ftrace_pages->next =
961 (void *)get_zeroed_page(GFP_KERNEL);
962 if (!ftrace_pages->next)
963 return NULL;
964 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200965 ftrace_pages = ftrace_pages->next;
966 }
967
968 return &ftrace_pages->records[ftrace_pages->index++];
969}
970
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400971static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200972ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200973{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400974 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200975
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800976 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400977 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200978
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400979 rec = ftrace_alloc_dyn_node(ip);
980 if (!rec)
981 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200982
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400983 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +0800984 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +0800985 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200986
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400987 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200988}
989
Steven Rostedt05736a42008-09-22 14:55:47 -0700990static void print_ip_ins(const char *fmt, unsigned char *p)
991{
992 int i;
993
994 printk(KERN_CONT "%s", fmt);
995
996 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
997 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
998}
999
Steven Rostedt31e88902008-11-14 16:21:19 -08001000static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001001{
1002 switch (failed) {
1003 case -EFAULT:
1004 FTRACE_WARN_ON_ONCE(1);
1005 pr_info("ftrace faulted on modifying ");
1006 print_ip_sym(ip);
1007 break;
1008 case -EINVAL:
1009 FTRACE_WARN_ON_ONCE(1);
1010 pr_info("ftrace failed to modify ");
1011 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001012 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001013 printk(KERN_CONT "\n");
1014 break;
1015 case -EPERM:
1016 FTRACE_WARN_ON_ONCE(1);
1017 pr_info("ftrace faulted on writing ");
1018 print_ip_sym(ip);
1019 break;
1020 default:
1021 FTRACE_WARN_ON_ONCE(1);
1022 pr_info("ftrace faulted on unknown error ");
1023 print_ip_sym(ip);
1024 }
1025}
1026
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001027
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001028/* Return 1 if the address range is reserved for ftrace */
1029int ftrace_text_reserved(void *start, void *end)
1030{
1031 struct dyn_ftrace *rec;
1032 struct ftrace_page *pg;
1033
1034 do_for_each_ftrace_rec(pg, rec) {
1035 if (rec->ip <= (unsigned long)end &&
1036 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1037 return 1;
1038 } while_for_each_ftrace_rec();
1039 return 0;
1040}
1041
1042
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301043static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001044__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001045{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001046 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001047 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001048
Shaohua Lif0001202009-01-09 11:29:42 +08001049 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001050
Steven Rostedt982c3502008-11-15 16:31:41 -05001051 /*
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001052 * If this record is not to be traced or we want to disable it,
1053 * then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001054 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001055 * If we want to enable it and filtering is off, then enable 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 on, enable it only if
1058 * it's filtered
Steven Rostedt982c3502008-11-15 16:31:41 -05001059 */
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001060 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1061 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1062 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001063 }
1064
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001065 /* If the state of this record hasn't changed, then do nothing */
1066 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1067 return 0;
1068
1069 if (flag) {
1070 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001071 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001072 }
1073
1074 rec->flags &= ~FTRACE_FL_ENABLED;
1075 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001076}
1077
1078static void ftrace_replace_code(int enable)
1079{
Steven Rostedt37ad5082008-05-12 21:20:48 +02001080 struct dyn_ftrace *rec;
1081 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001082 int failed;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001083
Steven Rostedt265c8312009-02-13 12:43:56 -05001084 do_for_each_ftrace_rec(pg, rec) {
1085 /*
Zhaoleifa9d13c2009-03-13 17:16:34 +08001086 * Skip over free records, records that have
1087 * failed and not converted.
Steven Rostedt265c8312009-02-13 12:43:56 -05001088 */
1089 if (rec->flags & FTRACE_FL_FREE ||
Zhaoleifa9d13c2009-03-13 17:16:34 +08001090 rec->flags & FTRACE_FL_FAILED ||
Frederic Weisbecker03303542009-03-16 22:41:00 +01001091 !(rec->flags & FTRACE_FL_CONVERTED))
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 Rostedt265c8312009-02-13 12:43:56 -05001096 rec->flags |= FTRACE_FL_FAILED;
Steven Rostedt3279ba32009-10-07 16:57:56 -04001097 ftrace_bug(failed, rec->ip);
1098 /* Stop processing */
1099 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001100 }
1101 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001102}
1103
Ingo Molnare309b412008-05-12 21:20:51 +02001104static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001105ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001106{
1107 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001108 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001109
1110 ip = rec->ip;
1111
Shaohua Li25aac9d2009-01-09 11:29:40 +08001112 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001113 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001114 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001115 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301116 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001117 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301118 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001119}
1120
Steven Rostedt000ab692009-02-17 13:35:06 -05001121/*
1122 * archs can override this function if they must do something
1123 * before the modifying code is performed.
1124 */
1125int __weak ftrace_arch_code_modify_prepare(void)
1126{
1127 return 0;
1128}
1129
1130/*
1131 * archs can override this function if they must do something
1132 * after the modifying code is performed.
1133 */
1134int __weak ftrace_arch_code_modify_post_process(void)
1135{
1136 return 0;
1137}
1138
Ingo Molnare309b412008-05-12 21:20:51 +02001139static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001140{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001141 int *command = data;
1142
Steven Rostedta3583242008-11-11 15:01:42 -05001143 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001144 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001145 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001146 ftrace_replace_code(0);
1147
1148 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1149 ftrace_update_ftrace_func(ftrace_trace_function);
1150
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001151 if (*command & FTRACE_START_FUNC_RET)
1152 ftrace_enable_ftrace_graph_caller();
1153 else if (*command & FTRACE_STOP_FUNC_RET)
1154 ftrace_disable_ftrace_graph_caller();
1155
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001156 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001157}
1158
Ingo Molnare309b412008-05-12 21:20:51 +02001159static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001160{
Steven Rostedt000ab692009-02-17 13:35:06 -05001161 int ret;
1162
1163 ret = ftrace_arch_code_modify_prepare();
1164 FTRACE_WARN_ON(ret);
1165 if (ret)
1166 return;
1167
Rusty Russell784e2d72008-07-28 12:16:31 -05001168 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001169
1170 ret = ftrace_arch_code_modify_post_process();
1171 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001172}
1173
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001174static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001175static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001176
1177static void ftrace_startup_enable(int command)
1178{
1179 if (saved_ftrace_func != ftrace_trace_function) {
1180 saved_ftrace_func = ftrace_trace_function;
1181 command |= FTRACE_UPDATE_TRACE_FUNC;
1182 }
1183
1184 if (!command || !ftrace_enabled)
1185 return;
1186
1187 ftrace_run_update_code(command);
1188}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001189
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001190static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001191{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001192 if (unlikely(ftrace_disabled))
1193 return;
1194
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001195 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001196 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001197
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001198 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001199}
1200
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001201static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001202{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001203 if (unlikely(ftrace_disabled))
1204 return;
1205
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001206 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001207 /*
1208 * Just warn in case of unbalance, no need to kill ftrace, it's not
1209 * critical but the ftrace_call callers may be never nopped again after
1210 * further ftrace uses.
1211 */
1212 WARN_ON_ONCE(ftrace_start_up < 0);
1213
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001214 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001215 command |= FTRACE_DISABLE_CALLS;
1216
1217 if (saved_ftrace_func != ftrace_trace_function) {
1218 saved_ftrace_func = ftrace_trace_function;
1219 command |= FTRACE_UPDATE_TRACE_FUNC;
1220 }
1221
1222 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001223 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001224
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001225 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001226}
1227
Ingo Molnare309b412008-05-12 21:20:51 +02001228static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001229{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001230 int command = FTRACE_ENABLE_MCOUNT;
1231
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001232 if (unlikely(ftrace_disabled))
1233 return;
1234
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001235 /* Force update next time */
1236 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001237 /* ftrace_start_up is true if we want ftrace running */
1238 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001239 command |= FTRACE_ENABLE_CALLS;
1240
1241 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001242}
1243
Ingo Molnare309b412008-05-12 21:20:51 +02001244static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001245{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001246 int command = FTRACE_DISABLE_MCOUNT;
1247
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001248 if (unlikely(ftrace_disabled))
1249 return;
1250
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001251 /* ftrace_start_up is true if ftrace is running */
1252 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001253 command |= FTRACE_DISABLE_CALLS;
1254
1255 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001256}
1257
Steven Rostedt3d083392008-05-12 21:20:42 +02001258static cycle_t ftrace_update_time;
1259static unsigned long ftrace_update_cnt;
1260unsigned long ftrace_update_tot_cnt;
1261
Steven Rostedt31e88902008-11-14 16:21:19 -08001262static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001263{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001264 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301265 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001266
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001267 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001268 ftrace_update_cnt = 0;
1269
Lai Jiangshane94142a2009-03-13 17:51:27 +08001270 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301271
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001272 /* If something went wrong, bail without enabling anything */
1273 if (unlikely(ftrace_disabled))
1274 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001275
Lai Jiangshane94142a2009-03-13 17:51:27 +08001276 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001277 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001278 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301279
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001280 /*
1281 * Do the initial record convertion from mcount jump
1282 * to the NOP instructions.
1283 */
1284 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001285 ftrace_free_rec(p);
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001286 continue;
1287 }
1288
1289 p->flags |= FTRACE_FL_CONVERTED;
1290 ftrace_update_cnt++;
1291
1292 /*
1293 * If the tracing is enabled, go ahead and enable the record.
1294 *
1295 * The reason not to enable the record immediatelly is the
1296 * inherent check of ftrace_make_nop/ftrace_make_call for
1297 * correct previous instructions. Making first the NOP
1298 * conversion puts the module to the correct state, thus
1299 * passing the ftrace_make_call check.
1300 */
1301 if (ftrace_start_up) {
1302 int failed = __ftrace_replace_code(p, 1);
1303 if (failed) {
1304 ftrace_bug(failed, p->ip);
1305 ftrace_free_rec(p);
1306 }
1307 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001308 }
1309
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001310 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001311 ftrace_update_time = stop - start;
1312 ftrace_update_tot_cnt += ftrace_update_cnt;
1313
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001314 return 0;
1315}
1316
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001317static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001318{
1319 struct ftrace_page *pg;
1320 int cnt;
1321 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001322
1323 /* allocate a few pages */
1324 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1325 if (!ftrace_pages_start)
1326 return -1;
1327
1328 /*
1329 * Allocate a few more pages.
1330 *
1331 * TODO: have some parser search vmlinux before
1332 * final linking to find all calls to ftrace.
1333 * Then we can:
1334 * a) know how many pages to allocate.
1335 * and/or
1336 * b) set up the table then.
1337 *
1338 * The dynamic code is still necessary for
1339 * modules.
1340 */
1341
1342 pg = ftrace_pages = ftrace_pages_start;
1343
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001344 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001345 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001346 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001347
1348 for (i = 0; i < cnt; i++) {
1349 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1350
1351 /* If we fail, we'll try later anyway */
1352 if (!pg->next)
1353 break;
1354
1355 pg = pg->next;
1356 }
1357
1358 return 0;
1359}
1360
Steven Rostedt5072c592008-05-12 21:20:43 +02001361enum {
1362 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001363 FTRACE_ITER_NOTRACE = (1 << 1),
1364 FTRACE_ITER_FAILURES = (1 << 2),
1365 FTRACE_ITER_PRINTALL = (1 << 3),
1366 FTRACE_ITER_HASH = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02001367};
1368
1369#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1370
1371struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +02001372 struct ftrace_page *pg;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001373 int hidx;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001374 int idx;
Steven Rostedt5072c592008-05-12 21:20:43 +02001375 unsigned flags;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001376 struct trace_parser parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02001377};
1378
Ingo Molnare309b412008-05-12 21:20:51 +02001379static void *
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001380t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1381{
1382 struct ftrace_iterator *iter = m->private;
1383 struct hlist_node *hnd = v;
1384 struct hlist_head *hhd;
1385
1386 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1387
1388 (*pos)++;
1389
1390 retry:
1391 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1392 return NULL;
1393
1394 hhd = &ftrace_func_hash[iter->hidx];
1395
1396 if (hlist_empty(hhd)) {
1397 iter->hidx++;
1398 hnd = NULL;
1399 goto retry;
1400 }
1401
1402 if (!hnd)
1403 hnd = hhd->first;
1404 else {
1405 hnd = hnd->next;
1406 if (!hnd) {
1407 iter->hidx++;
1408 goto retry;
1409 }
1410 }
1411
1412 return hnd;
1413}
1414
1415static void *t_hash_start(struct seq_file *m, loff_t *pos)
1416{
1417 struct ftrace_iterator *iter = m->private;
1418 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001419 loff_t l;
1420
1421 if (!(iter->flags & FTRACE_ITER_HASH))
1422 *pos = 0;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001423
1424 iter->flags |= FTRACE_ITER_HASH;
1425
Li Zefand82d6242009-06-24 09:54:54 +08001426 iter->hidx = 0;
1427 for (l = 0; l <= *pos; ) {
1428 p = t_hash_next(m, p, &l);
1429 if (!p)
1430 break;
1431 }
1432 return p;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001433}
1434
1435static int t_hash_show(struct seq_file *m, void *v)
1436{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001437 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001438 struct hlist_node *hnd = v;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001439
Steven Rostedtb6887d72009-02-17 12:32:04 -05001440 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001441
Steven Rostedt809dcf22009-02-16 23:06:01 -05001442 if (rec->ops->print)
1443 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1444
Steven Rostedtb375a112009-09-17 00:05:58 -04001445 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001446
1447 if (rec->data)
1448 seq_printf(m, ":%p", rec->data);
1449 seq_putc(m, '\n');
1450
1451 return 0;
1452}
1453
1454static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001455t_next(struct seq_file *m, void *v, loff_t *pos)
1456{
1457 struct ftrace_iterator *iter = m->private;
1458 struct dyn_ftrace *rec = NULL;
1459
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001460 if (iter->flags & FTRACE_ITER_HASH)
1461 return t_hash_next(m, v, pos);
1462
Steven Rostedt5072c592008-05-12 21:20:43 +02001463 (*pos)++;
1464
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001465 if (iter->flags & FTRACE_ITER_PRINTALL)
1466 return NULL;
1467
Steven Rostedt5072c592008-05-12 21:20:43 +02001468 retry:
1469 if (iter->idx >= iter->pg->index) {
1470 if (iter->pg->next) {
1471 iter->pg = iter->pg->next;
1472 iter->idx = 0;
1473 goto retry;
1474 }
1475 } else {
1476 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001477 if ((rec->flags & FTRACE_FL_FREE) ||
1478
1479 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301480 (rec->flags & FTRACE_FL_FAILED)) ||
1481
1482 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -04001483 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301484
Steven Rostedt0183fb12008-11-07 22:36:02 -05001485 ((iter->flags & FTRACE_ITER_FILTER) &&
1486 !(rec->flags & FTRACE_FL_FILTER)) ||
1487
Steven Rostedt41c52c02008-05-22 11:46:33 -04001488 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1489 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001490 rec = NULL;
1491 goto retry;
1492 }
1493 }
1494
Steven Rostedt5072c592008-05-12 21:20:43 +02001495 return rec;
1496}
1497
1498static void *t_start(struct seq_file *m, loff_t *pos)
1499{
1500 struct ftrace_iterator *iter = m->private;
1501 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001502 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001503
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001504 mutex_lock(&ftrace_lock);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001505 /*
1506 * For set_ftrace_filter reading, if we have the filter
1507 * off, we can short cut and just print out that all
1508 * functions are enabled.
1509 */
1510 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1511 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001512 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001513 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07001514 /* reset in case of seek/pread */
1515 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001516 return iter;
1517 }
1518
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001519 if (iter->flags & FTRACE_ITER_HASH)
1520 return t_hash_start(m, pos);
1521
Li Zefan694ce0a2009-06-24 09:54:19 +08001522 iter->pg = ftrace_pages_start;
1523 iter->idx = 0;
1524 for (l = 0; l <= *pos; ) {
1525 p = t_next(m, p, &l);
1526 if (!p)
1527 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001528 }
walimis5821e1b2008-11-15 15:19:06 +08001529
Li Zefan694ce0a2009-06-24 09:54:19 +08001530 if (!p && iter->flags & FTRACE_ITER_FILTER)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001531 return t_hash_start(m, pos);
1532
Steven Rostedt5072c592008-05-12 21:20:43 +02001533 return p;
1534}
1535
1536static void t_stop(struct seq_file *m, void *p)
1537{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001538 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001539}
1540
1541static int t_show(struct seq_file *m, void *v)
1542{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001543 struct ftrace_iterator *iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02001544 struct dyn_ftrace *rec = v;
Steven Rostedt5072c592008-05-12 21:20:43 +02001545
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001546 if (iter->flags & FTRACE_ITER_HASH)
1547 return t_hash_show(m, v);
1548
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001549 if (iter->flags & FTRACE_ITER_PRINTALL) {
1550 seq_printf(m, "#### all functions enabled ####\n");
1551 return 0;
1552 }
1553
Steven Rostedt5072c592008-05-12 21:20:43 +02001554 if (!rec)
1555 return 0;
1556
Steven Rostedtb375a112009-09-17 00:05:58 -04001557 seq_printf(m, "%ps\n", (void *)rec->ip);
Steven Rostedt5072c592008-05-12 21:20:43 +02001558
1559 return 0;
1560}
1561
James Morris88e9d342009-09-22 16:43:43 -07001562static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001563 .start = t_start,
1564 .next = t_next,
1565 .stop = t_stop,
1566 .show = t_show,
1567};
1568
Ingo Molnare309b412008-05-12 21:20:51 +02001569static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001570ftrace_avail_open(struct inode *inode, struct file *file)
1571{
1572 struct ftrace_iterator *iter;
1573 int ret;
1574
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001575 if (unlikely(ftrace_disabled))
1576 return -ENODEV;
1577
Steven Rostedt5072c592008-05-12 21:20:43 +02001578 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1579 if (!iter)
1580 return -ENOMEM;
1581
1582 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001583
1584 ret = seq_open(file, &show_ftrace_seq_ops);
1585 if (!ret) {
1586 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001587
Steven Rostedt5072c592008-05-12 21:20:43 +02001588 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001589 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001590 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001591 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001592
1593 return ret;
1594}
1595
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301596static int
1597ftrace_failures_open(struct inode *inode, struct file *file)
1598{
1599 int ret;
1600 struct seq_file *m;
1601 struct ftrace_iterator *iter;
1602
1603 ret = ftrace_avail_open(inode, file);
1604 if (!ret) {
1605 m = (struct seq_file *)file->private_data;
1606 iter = (struct ftrace_iterator *)m->private;
1607 iter->flags = FTRACE_ITER_FAILURES;
1608 }
1609
1610 return ret;
1611}
1612
1613
Steven Rostedt41c52c02008-05-22 11:46:33 -04001614static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001615{
1616 struct ftrace_page *pg;
1617 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001618 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001619
Steven Rostedt52baf112009-02-14 01:15:39 -05001620 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001621 if (enable)
1622 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001623 do_for_each_ftrace_rec(pg, rec) {
1624 if (rec->flags & FTRACE_FL_FAILED)
1625 continue;
1626 rec->flags &= ~type;
1627 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001628 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001629}
1630
Ingo Molnare309b412008-05-12 21:20:51 +02001631static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001632ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001633{
1634 struct ftrace_iterator *iter;
1635 int ret = 0;
1636
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001637 if (unlikely(ftrace_disabled))
1638 return -ENODEV;
1639
Steven Rostedt5072c592008-05-12 21:20:43 +02001640 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1641 if (!iter)
1642 return -ENOMEM;
1643
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001644 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1645 kfree(iter);
1646 return -ENOMEM;
1647 }
1648
Steven Rostedt41c52c02008-05-22 11:46:33 -04001649 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001650 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04001651 (file->f_flags & O_TRUNC))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001652 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001653
1654 if (file->f_mode & FMODE_READ) {
1655 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001656 iter->flags = enable ? FTRACE_ITER_FILTER :
1657 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001658
1659 ret = seq_open(file, &show_ftrace_seq_ops);
1660 if (!ret) {
1661 struct seq_file *m = file->private_data;
1662 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08001663 } else {
1664 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02001665 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08001666 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001667 } else
1668 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001669 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001670
1671 return ret;
1672}
1673
Steven Rostedt41c52c02008-05-22 11:46:33 -04001674static int
1675ftrace_filter_open(struct inode *inode, struct file *file)
1676{
1677 return ftrace_regex_open(inode, file, 1);
1678}
1679
1680static int
1681ftrace_notrace_open(struct inode *inode, struct file *file)
1682{
1683 return ftrace_regex_open(inode, file, 0);
1684}
1685
Ingo Molnare309b412008-05-12 21:20:51 +02001686static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001687ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001688{
1689 loff_t ret;
1690
1691 if (file->f_mode & FMODE_READ)
1692 ret = seq_lseek(file, offset, origin);
1693 else
1694 file->f_pos = ret = 1;
1695
1696 return ret;
1697}
1698
Steven Rostedt64e7c442009-02-13 17:08:48 -05001699static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001700{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001701 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08001702 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001703
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001704 switch (type) {
1705 case MATCH_FULL:
1706 if (strcmp(str, regex) == 0)
1707 matched = 1;
1708 break;
1709 case MATCH_FRONT_ONLY:
1710 if (strncmp(str, regex, len) == 0)
1711 matched = 1;
1712 break;
1713 case MATCH_MIDDLE_ONLY:
1714 if (strstr(str, regex))
1715 matched = 1;
1716 break;
1717 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08001718 slen = strlen(str);
1719 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001720 matched = 1;
1721 break;
1722 }
1723
1724 return matched;
1725}
1726
Steven Rostedt64e7c442009-02-13 17:08:48 -05001727static int
1728ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1729{
1730 char str[KSYM_SYMBOL_LEN];
1731
1732 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1733 return ftrace_match(str, regex, len, type);
1734}
1735
Li Zefan311d16d2009-12-08 11:15:11 +08001736static int ftrace_match_records(char *buff, int len, int enable)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001737{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001738 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001739 struct ftrace_page *pg;
1740 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001741 unsigned long flag;
1742 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001743 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001744 int not;
Li Zefan311d16d2009-12-08 11:15:11 +08001745 int found = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001746
Steven Rostedt6a24a242009-02-17 11:20:26 -05001747 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001748 type = filter_parse_regex(buff, len, &search, &not);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001749
1750 search_len = strlen(search);
1751
Steven Rostedt52baf112009-02-14 01:15:39 -05001752 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001753 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001754
Steven Rostedt265c8312009-02-13 12:43:56 -05001755 if (rec->flags & FTRACE_FL_FAILED)
1756 continue;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001757
1758 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001759 if (not)
1760 rec->flags &= ~flag;
1761 else
1762 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001763 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001764 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001765 /*
1766 * Only enable filtering if we have a function that
1767 * is filtered on.
1768 */
1769 if (enable && (rec->flags & FTRACE_FL_FILTER))
1770 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001771 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001772 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001773
1774 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02001775}
1776
Steven Rostedt64e7c442009-02-13 17:08:48 -05001777static int
1778ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1779 char *regex, int len, int type)
1780{
1781 char str[KSYM_SYMBOL_LEN];
1782 char *modname;
1783
1784 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1785
1786 if (!modname || strcmp(modname, mod))
1787 return 0;
1788
1789 /* blank search means to match all funcs in the mod */
1790 if (len)
1791 return ftrace_match(str, regex, len, type);
1792 else
1793 return 1;
1794}
1795
Li Zefan311d16d2009-12-08 11:15:11 +08001796static int ftrace_match_module_records(char *buff, char *mod, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001797{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001798 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001799 struct ftrace_page *pg;
1800 struct dyn_ftrace *rec;
1801 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001802 char *search = buff;
1803 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001804 int not = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08001805 int found = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001806
Steven Rostedt6a24a242009-02-17 11:20:26 -05001807 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1808
Steven Rostedt64e7c442009-02-13 17:08:48 -05001809 /* blank or '*' mean the same */
1810 if (strcmp(buff, "*") == 0)
1811 buff[0] = 0;
1812
1813 /* handle the case of 'dont filter this module' */
1814 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1815 buff[0] = 0;
1816 not = 1;
1817 }
1818
1819 if (strlen(buff)) {
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001820 type = filter_parse_regex(buff, strlen(buff), &search, &not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001821 search_len = strlen(search);
1822 }
1823
Steven Rostedt52baf112009-02-14 01:15:39 -05001824 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001825 do_for_each_ftrace_rec(pg, rec) {
1826
1827 if (rec->flags & FTRACE_FL_FAILED)
1828 continue;
1829
1830 if (ftrace_match_module_record(rec, mod,
1831 search, search_len, type)) {
1832 if (not)
1833 rec->flags &= ~flag;
1834 else
1835 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001836 found = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001837 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001838 if (enable && (rec->flags & FTRACE_FL_FILTER))
1839 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001840
1841 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001842 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001843
1844 return found;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001845}
1846
Steven Rostedtf6180772009-02-14 00:40:25 -05001847/*
1848 * We register the module command as a template to show others how
1849 * to register the a command as well.
1850 */
1851
1852static int
1853ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1854{
1855 char *mod;
1856
1857 /*
1858 * cmd == 'mod' because we only registered this func
1859 * for the 'mod' ftrace_func_command.
1860 * But if you register one func with multiple commands,
1861 * you can tell which command was used by the cmd
1862 * parameter.
1863 */
1864
1865 /* we must have a module name */
1866 if (!param)
1867 return -EINVAL;
1868
1869 mod = strsep(&param, ":");
1870 if (!strlen(mod))
1871 return -EINVAL;
1872
Li Zefan311d16d2009-12-08 11:15:11 +08001873 if (ftrace_match_module_records(func, mod, enable))
1874 return 0;
1875 return -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05001876}
1877
1878static struct ftrace_func_command ftrace_mod_cmd = {
1879 .name = "mod",
1880 .func = ftrace_mod_callback,
1881};
1882
1883static int __init ftrace_mod_cmd_init(void)
1884{
1885 return register_ftrace_command(&ftrace_mod_cmd);
1886}
1887device_initcall(ftrace_mod_cmd_init);
1888
Steven Rostedt59df055f2009-02-14 15:29:06 -05001889static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001890function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001891{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001892 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001893 struct hlist_head *hhd;
1894 struct hlist_node *n;
1895 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001896
1897 key = hash_long(ip, FTRACE_HASH_BITS);
1898
1899 hhd = &ftrace_func_hash[key];
1900
1901 if (hlist_empty(hhd))
1902 return;
1903
1904 /*
1905 * Disable preemption for these calls to prevent a RCU grace
1906 * period. This syncs the hash iteration and freeing of items
1907 * on the hash. rcu_read_lock is too dangerous here.
1908 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04001909 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05001910 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1911 if (entry->ip == ip)
1912 entry->ops->func(ip, parent_ip, &entry->data);
1913 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04001914 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05001915}
1916
Steven Rostedtb6887d72009-02-17 12:32:04 -05001917static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001918{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001919 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001920};
1921
Steven Rostedtb6887d72009-02-17 12:32:04 -05001922static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001923
Steven Rostedtb6887d72009-02-17 12:32:04 -05001924static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001925{
1926 int i;
1927
Steven Rostedtb6887d72009-02-17 12:32:04 -05001928 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001929 return;
1930
1931 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1932 struct hlist_head *hhd = &ftrace_func_hash[i];
1933 if (hhd->first)
1934 break;
1935 }
1936 /* Nothing registered? */
1937 if (i == FTRACE_FUNC_HASHSIZE)
1938 return;
1939
Steven Rostedtb6887d72009-02-17 12:32:04 -05001940 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001941 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001942 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001943}
1944
Steven Rostedtb6887d72009-02-17 12:32:04 -05001945static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001946{
1947 int i;
1948
Steven Rostedtb6887d72009-02-17 12:32:04 -05001949 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001950 return;
1951
1952 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1953 struct hlist_head *hhd = &ftrace_func_hash[i];
1954 if (hhd->first)
1955 return;
1956 }
1957
1958 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05001959 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001960 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001961 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001962}
1963
1964
1965static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1966{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001967 struct ftrace_func_probe *entry =
1968 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001969
1970 if (entry->ops->free)
1971 entry->ops->free(&entry->data);
1972 kfree(entry);
1973}
1974
1975
1976int
Steven Rostedtb6887d72009-02-17 12:32:04 -05001977register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001978 void *data)
1979{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001980 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001981 struct ftrace_page *pg;
1982 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001983 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001984 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001985 int count = 0;
1986 char *search;
1987
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001988 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001989 len = strlen(search);
1990
Steven Rostedtb6887d72009-02-17 12:32:04 -05001991 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001992 if (WARN_ON(not))
1993 return -EINVAL;
1994
1995 mutex_lock(&ftrace_lock);
1996 do_for_each_ftrace_rec(pg, rec) {
1997
1998 if (rec->flags & FTRACE_FL_FAILED)
1999 continue;
2000
2001 if (!ftrace_match_record(rec, search, len, type))
2002 continue;
2003
2004 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2005 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002006 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002007 if (!count)
2008 count = -ENOMEM;
2009 goto out_unlock;
2010 }
2011
2012 count++;
2013
2014 entry->data = data;
2015
2016 /*
2017 * The caller might want to do something special
2018 * for each function we find. We call the callback
2019 * to give the caller an opportunity to do so.
2020 */
2021 if (ops->callback) {
2022 if (ops->callback(rec->ip, &entry->data) < 0) {
2023 /* caller does not like this func */
2024 kfree(entry);
2025 continue;
2026 }
2027 }
2028
2029 entry->ops = ops;
2030 entry->ip = rec->ip;
2031
2032 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2033 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2034
2035 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002036 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002037
2038 out_unlock:
2039 mutex_unlock(&ftrace_lock);
2040
2041 return count;
2042}
2043
2044enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002045 PROBE_TEST_FUNC = 1,
2046 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002047};
2048
2049static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002050__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002051 void *data, int flags)
2052{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002053 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002054 struct hlist_node *n, *tmp;
2055 char str[KSYM_SYMBOL_LEN];
2056 int type = MATCH_FULL;
2057 int i, len = 0;
2058 char *search;
2059
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002060 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002061 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002062 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002063 int not;
2064
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002065 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002066 len = strlen(search);
2067
Steven Rostedtb6887d72009-02-17 12:32:04 -05002068 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002069 if (WARN_ON(not))
2070 return;
2071 }
2072
2073 mutex_lock(&ftrace_lock);
2074 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2075 struct hlist_head *hhd = &ftrace_func_hash[i];
2076
2077 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2078
2079 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002080 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002081 continue;
2082
Steven Rostedtb6887d72009-02-17 12:32:04 -05002083 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002084 continue;
2085
2086 /* do this last, since it is the most expensive */
2087 if (glob) {
2088 kallsyms_lookup(entry->ip, NULL, NULL,
2089 NULL, str);
2090 if (!ftrace_match(str, glob, len, type))
2091 continue;
2092 }
2093
2094 hlist_del(&entry->node);
2095 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2096 }
2097 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002098 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002099 mutex_unlock(&ftrace_lock);
2100}
2101
2102void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002103unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002104 void *data)
2105{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002106 __unregister_ftrace_function_probe(glob, ops, data,
2107 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002108}
2109
2110void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002111unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002112{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002113 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002114}
2115
Steven Rostedtb6887d72009-02-17 12:32:04 -05002116void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002117{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002118 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002119}
2120
Steven Rostedtf6180772009-02-14 00:40:25 -05002121static LIST_HEAD(ftrace_commands);
2122static DEFINE_MUTEX(ftrace_cmd_mutex);
2123
2124int register_ftrace_command(struct ftrace_func_command *cmd)
2125{
2126 struct ftrace_func_command *p;
2127 int ret = 0;
2128
2129 mutex_lock(&ftrace_cmd_mutex);
2130 list_for_each_entry(p, &ftrace_commands, list) {
2131 if (strcmp(cmd->name, p->name) == 0) {
2132 ret = -EBUSY;
2133 goto out_unlock;
2134 }
2135 }
2136 list_add(&cmd->list, &ftrace_commands);
2137 out_unlock:
2138 mutex_unlock(&ftrace_cmd_mutex);
2139
2140 return ret;
2141}
2142
2143int unregister_ftrace_command(struct ftrace_func_command *cmd)
2144{
2145 struct ftrace_func_command *p, *n;
2146 int ret = -ENODEV;
2147
2148 mutex_lock(&ftrace_cmd_mutex);
2149 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2150 if (strcmp(cmd->name, p->name) == 0) {
2151 ret = 0;
2152 list_del_init(&p->list);
2153 goto out_unlock;
2154 }
2155 }
2156 out_unlock:
2157 mutex_unlock(&ftrace_cmd_mutex);
2158
2159 return ret;
2160}
2161
Steven Rostedt64e7c442009-02-13 17:08:48 -05002162static int ftrace_process_regex(char *buff, int len, int enable)
2163{
Steven Rostedtf6180772009-02-14 00:40:25 -05002164 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002165 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002166 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002167
2168 func = strsep(&next, ":");
2169
2170 if (!next) {
Li Zefan311d16d2009-12-08 11:15:11 +08002171 if (ftrace_match_records(func, len, enable))
2172 return 0;
2173 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002174 }
2175
Steven Rostedtf6180772009-02-14 00:40:25 -05002176 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002177
2178 command = strsep(&next, ":");
2179
Steven Rostedtf6180772009-02-14 00:40:25 -05002180 mutex_lock(&ftrace_cmd_mutex);
2181 list_for_each_entry(p, &ftrace_commands, list) {
2182 if (strcmp(p->name, command) == 0) {
2183 ret = p->func(func, command, next, enable);
2184 goto out_unlock;
2185 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002186 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002187 out_unlock:
2188 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002189
Steven Rostedtf6180772009-02-14 00:40:25 -05002190 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002191}
2192
Ingo Molnare309b412008-05-12 21:20:51 +02002193static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002194ftrace_regex_write(struct file *file, const char __user *ubuf,
2195 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002196{
2197 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002198 struct trace_parser *parser;
2199 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002200
Li Zefan4ba79782009-09-22 13:52:20 +08002201 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002202 return 0;
2203
Steven Rostedt41c52c02008-05-22 11:46:33 -04002204 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002205
2206 if (file->f_mode & FMODE_READ) {
2207 struct seq_file *m = file->private_data;
2208 iter = m->private;
2209 } else
2210 iter = file->private_data;
2211
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002212 parser = &iter->parser;
2213 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002214
Li Zefan4ba79782009-09-22 13:52:20 +08002215 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002216 !trace_parser_cont(parser)) {
2217 ret = ftrace_process_regex(parser->buffer,
2218 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002219 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002220 if (ret)
Li Zefaned146b22009-11-03 08:55:38 +08002221 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002222 }
2223
Steven Rostedt5072c592008-05-12 21:20:43 +02002224 ret = read;
Li Zefaned146b22009-11-03 08:55:38 +08002225out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002226 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b22009-11-03 08:55:38 +08002227
Steven Rostedt5072c592008-05-12 21:20:43 +02002228 return ret;
2229}
2230
Steven Rostedt41c52c02008-05-22 11:46:33 -04002231static ssize_t
2232ftrace_filter_write(struct file *file, const char __user *ubuf,
2233 size_t cnt, loff_t *ppos)
2234{
2235 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2236}
2237
2238static ssize_t
2239ftrace_notrace_write(struct file *file, const char __user *ubuf,
2240 size_t cnt, loff_t *ppos)
2241{
2242 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2243}
2244
2245static void
2246ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2247{
2248 if (unlikely(ftrace_disabled))
2249 return;
2250
2251 mutex_lock(&ftrace_regex_lock);
2252 if (reset)
2253 ftrace_filter_reset(enable);
2254 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002255 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002256 mutex_unlock(&ftrace_regex_lock);
2257}
2258
Steven Rostedt77a2b372008-05-12 21:20:45 +02002259/**
2260 * ftrace_set_filter - set a function to filter on in ftrace
2261 * @buf - the string that holds the function filter text.
2262 * @len - the length of the string.
2263 * @reset - non zero to reset all filters before applying this filter.
2264 *
2265 * Filters denote which functions should be enabled when tracing is enabled.
2266 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2267 */
Ingo Molnare309b412008-05-12 21:20:51 +02002268void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002269{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002270 ftrace_set_regex(buf, len, reset, 1);
2271}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002272
Steven Rostedt41c52c02008-05-22 11:46:33 -04002273/**
2274 * ftrace_set_notrace - set a function to not trace in ftrace
2275 * @buf - the string that holds the function notrace text.
2276 * @len - the length of the string.
2277 * @reset - non zero to reset all filters before applying this filter.
2278 *
2279 * Notrace Filters denote which functions should not be enabled when tracing
2280 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2281 * for tracing.
2282 */
2283void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2284{
2285 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002286}
2287
Steven Rostedt2af15d62009-05-28 13:37:24 -04002288/*
2289 * command line interface to allow users to set filters on boot up.
2290 */
2291#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2292static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2293static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2294
2295static int __init set_ftrace_notrace(char *str)
2296{
2297 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2298 return 1;
2299}
2300__setup("ftrace_notrace=", set_ftrace_notrace);
2301
2302static int __init set_ftrace_filter(char *str)
2303{
2304 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2305 return 1;
2306}
2307__setup("ftrace_filter=", set_ftrace_filter);
2308
Stefan Assmann369bc182009-10-12 22:17:21 +02002309#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002310static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002311static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2312
Stefan Assmann369bc182009-10-12 22:17:21 +02002313static int __init set_graph_function(char *str)
2314{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002315 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002316 return 1;
2317}
2318__setup("ftrace_graph_filter=", set_graph_function);
2319
2320static void __init set_ftrace_early_graph(char *buf)
2321{
2322 int ret;
2323 char *func;
2324
2325 while (buf) {
2326 func = strsep(&buf, ",");
2327 /* we allow only one expression at a time */
2328 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2329 func);
2330 if (ret)
2331 printk(KERN_DEBUG "ftrace: function %s not "
2332 "traceable\n", func);
2333 }
2334}
2335#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2336
Steven Rostedt2af15d62009-05-28 13:37:24 -04002337static void __init set_ftrace_early_filter(char *buf, int enable)
2338{
2339 char *func;
2340
2341 while (buf) {
2342 func = strsep(&buf, ",");
2343 ftrace_set_regex(func, strlen(func), 0, enable);
2344 }
2345}
2346
2347static void __init set_ftrace_early_filters(void)
2348{
2349 if (ftrace_filter_buf[0])
2350 set_ftrace_early_filter(ftrace_filter_buf, 1);
2351 if (ftrace_notrace_buf[0])
2352 set_ftrace_early_filter(ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002353#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2354 if (ftrace_graph_buf[0])
2355 set_ftrace_early_graph(ftrace_graph_buf);
2356#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002357}
2358
Ingo Molnare309b412008-05-12 21:20:51 +02002359static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002360ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002361{
2362 struct seq_file *m = (struct seq_file *)file->private_data;
2363 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002364 struct trace_parser *parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02002365
Steven Rostedt41c52c02008-05-22 11:46:33 -04002366 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002367 if (file->f_mode & FMODE_READ) {
2368 iter = m->private;
2369
2370 seq_release(inode, file);
2371 } else
2372 iter = file->private_data;
2373
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002374 parser = &iter->parser;
2375 if (trace_parser_loaded(parser)) {
2376 parser->buffer[parser->idx] = 0;
2377 ftrace_match_records(parser->buffer, parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002378 }
2379
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002380 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002381 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002382 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002383 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002384
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002385 trace_parser_put(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002386 kfree(iter);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002387
Steven Rostedt41c52c02008-05-22 11:46:33 -04002388 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002389 return 0;
2390}
2391
Steven Rostedt41c52c02008-05-22 11:46:33 -04002392static int
2393ftrace_filter_release(struct inode *inode, struct file *file)
2394{
2395 return ftrace_regex_release(inode, file, 1);
2396}
2397
2398static int
2399ftrace_notrace_release(struct inode *inode, struct file *file)
2400{
2401 return ftrace_regex_release(inode, file, 0);
2402}
2403
Steven Rostedt5e2336a02009-03-05 21:44:55 -05002404static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002405 .open = ftrace_avail_open,
2406 .read = seq_read,
2407 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002408 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002409};
2410
Steven Rostedt5e2336a02009-03-05 21:44:55 -05002411static const struct file_operations ftrace_failures_fops = {
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302412 .open = ftrace_failures_open,
2413 .read = seq_read,
2414 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002415 .release = seq_release_private,
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302416};
2417
Steven Rostedt5e2336a02009-03-05 21:44:55 -05002418static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002419 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002420 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002421 .write = ftrace_filter_write,
Steven Rostedt9c55cb12010-09-08 11:20:37 -04002422 .llseek = no_llseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002423 .release = ftrace_filter_release,
2424};
2425
Steven Rostedt5e2336a02009-03-05 21:44:55 -05002426static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002427 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002428 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002429 .write = ftrace_notrace_write,
2430 .llseek = ftrace_regex_lseek,
2431 .release = ftrace_notrace_release,
2432};
2433
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002434#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2435
2436static DEFINE_MUTEX(graph_lock);
2437
2438int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002439int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002440unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2441
2442static void *
Li Zefan85951842009-06-24 09:54:00 +08002443__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002444{
Li Zefan85951842009-06-24 09:54:00 +08002445 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002446 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002447 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002448}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002449
Li Zefan85951842009-06-24 09:54:00 +08002450static void *
2451g_next(struct seq_file *m, void *v, loff_t *pos)
2452{
2453 (*pos)++;
2454 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002455}
2456
2457static void *g_start(struct seq_file *m, loff_t *pos)
2458{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002459 mutex_lock(&graph_lock);
2460
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002461 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002462 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002463 return (void *)1;
2464
Li Zefan85951842009-06-24 09:54:00 +08002465 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002466}
2467
2468static void g_stop(struct seq_file *m, void *p)
2469{
2470 mutex_unlock(&graph_lock);
2471}
2472
2473static int g_show(struct seq_file *m, void *v)
2474{
2475 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002476
2477 if (!ptr)
2478 return 0;
2479
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002480 if (ptr == (unsigned long *)1) {
2481 seq_printf(m, "#### all functions enabled ####\n");
2482 return 0;
2483 }
2484
Steven Rostedtb375a112009-09-17 00:05:58 -04002485 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002486
2487 return 0;
2488}
2489
James Morris88e9d342009-09-22 16:43:43 -07002490static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002491 .start = g_start,
2492 .next = g_next,
2493 .stop = g_stop,
2494 .show = g_show,
2495};
2496
2497static int
2498ftrace_graph_open(struct inode *inode, struct file *file)
2499{
2500 int ret = 0;
2501
2502 if (unlikely(ftrace_disabled))
2503 return -ENODEV;
2504
2505 mutex_lock(&graph_lock);
2506 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002507 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002508 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002509 ftrace_graph_count = 0;
2510 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2511 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002512 mutex_unlock(&graph_lock);
2513
Li Zefana4ec5e02009-09-18 14:06:28 +08002514 if (file->f_mode & FMODE_READ)
2515 ret = seq_open(file, &ftrace_graph_seq_ops);
2516
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002517 return ret;
2518}
2519
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002520static int
Li Zefan87827112009-07-23 11:29:11 +08002521ftrace_graph_release(struct inode *inode, struct file *file)
2522{
2523 if (file->f_mode & FMODE_READ)
2524 seq_release(inode, file);
2525 return 0;
2526}
2527
2528static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002529ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002530{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002531 struct dyn_ftrace *rec;
2532 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002533 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002534 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002535 int type, not;
2536 char *search;
2537 bool exists;
2538 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002539
2540 if (ftrace_disabled)
2541 return -ENODEV;
2542
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002543 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002544 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002545 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2546 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002547
2548 search_len = strlen(search);
2549
Steven Rostedt52baf112009-02-14 01:15:39 -05002550 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05002551 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002552
Steven Rostedt265c8312009-02-13 12:43:56 -05002553 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2554 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002555
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002556 if (ftrace_match_record(rec, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002557 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002558 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002559 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002560 if (array[i] == rec->ip) {
2561 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002562 break;
2563 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002564 }
2565
2566 if (!not) {
2567 fail = 0;
2568 if (!exists) {
2569 array[(*idx)++] = rec->ip;
2570 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2571 goto out;
2572 }
2573 } else {
2574 if (exists) {
2575 array[i] = array[--(*idx)];
2576 array[*idx] = 0;
2577 fail = 0;
2578 }
2579 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002580 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002581 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002582out:
Steven Rostedt52baf112009-02-14 01:15:39 -05002583 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002584
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002585 if (fail)
2586 return -EINVAL;
2587
2588 ftrace_graph_filter_enabled = 1;
2589 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002590}
2591
2592static ssize_t
2593ftrace_graph_write(struct file *file, const char __user *ubuf,
2594 size_t cnt, loff_t *ppos)
2595{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002596 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08002597 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002598
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002599 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002600 return 0;
2601
2602 mutex_lock(&graph_lock);
2603
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002604 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2605 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08002606 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002607 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002608
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002609 read = trace_get_user(&parser, ubuf, cnt, ppos);
2610
Li Zefan4ba79782009-09-22 13:52:20 +08002611 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002612 parser.buffer[parser.idx] = 0;
2613
2614 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08002615 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002616 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002617 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08002618 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002619 }
2620
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002621 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08002622
2623out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002624 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08002625out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002626 mutex_unlock(&graph_lock);
2627
2628 return ret;
2629}
2630
2631static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08002632 .open = ftrace_graph_open,
2633 .read = seq_read,
2634 .write = ftrace_graph_write,
2635 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002636 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002637};
2638#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2639
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002640static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002641{
Steven Rostedt5072c592008-05-12 21:20:43 +02002642
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002643 trace_create_file("available_filter_functions", 0444,
2644 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002645
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002646 trace_create_file("failures", 0444,
2647 d_tracer, NULL, &ftrace_failures_fops);
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302648
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002649 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2650 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002651
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002652 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002653 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002654
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002655#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002656 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002657 NULL,
2658 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002659#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2660
Steven Rostedt5072c592008-05-12 21:20:43 +02002661 return 0;
2662}
2663
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002664static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08002665 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002666 unsigned long *end)
2667{
2668 unsigned long *p;
2669 unsigned long addr;
2670 unsigned long flags;
2671
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002672 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002673 p = start;
2674 while (p < end) {
2675 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002676 /*
2677 * Some architecture linkers will pad between
2678 * the different mcount_loc sections of different
2679 * object files to satisfy alignments.
2680 * Skip any NULL pointers.
2681 */
2682 if (!addr)
2683 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002684 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002685 }
2686
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002687 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002688 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08002689 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002690 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002691 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002692
2693 return 0;
2694}
2695
Steven Rostedt93eb6772009-04-15 13:24:06 -04002696#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002697void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002698{
2699 struct dyn_ftrace *rec;
2700 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04002701
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002702 if (ftrace_disabled)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002703 return;
2704
2705 mutex_lock(&ftrace_lock);
2706 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002707 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04002708 /*
2709 * rec->ip is changed in ftrace_free_rec()
2710 * It should not between s and e if record was freed.
2711 */
2712 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2713 ftrace_free_rec(rec);
2714 }
2715 } while_for_each_ftrace_rec();
2716 mutex_unlock(&ftrace_lock);
2717}
2718
2719static void ftrace_init_module(struct module *mod,
2720 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002721{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002722 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002723 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002724 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002725}
2726
Steven Rostedt93eb6772009-04-15 13:24:06 -04002727static int ftrace_module_notify(struct notifier_block *self,
2728 unsigned long val, void *data)
2729{
2730 struct module *mod = data;
2731
2732 switch (val) {
2733 case MODULE_STATE_COMING:
2734 ftrace_init_module(mod, mod->ftrace_callsites,
2735 mod->ftrace_callsites +
2736 mod->num_ftrace_callsites);
2737 break;
2738 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002739 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04002740 break;
2741 }
2742
2743 return 0;
2744}
2745#else
2746static int ftrace_module_notify(struct notifier_block *self,
2747 unsigned long val, void *data)
2748{
2749 return 0;
2750}
2751#endif /* CONFIG_MODULES */
2752
2753struct notifier_block ftrace_module_nb = {
2754 .notifier_call = ftrace_module_notify,
2755 .priority = 0,
2756};
2757
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002758extern unsigned long __start_mcount_loc[];
2759extern unsigned long __stop_mcount_loc[];
2760
2761void __init ftrace_init(void)
2762{
2763 unsigned long count, addr, flags;
2764 int ret;
2765
2766 /* Keep the ftrace pointer to the stub */
2767 addr = (unsigned long)ftrace_stub;
2768
2769 local_irq_save(flags);
2770 ftrace_dyn_arch_init(&addr);
2771 local_irq_restore(flags);
2772
2773 /* ftrace_dyn_arch_init places the return code in addr */
2774 if (addr)
2775 goto failed;
2776
2777 count = __stop_mcount_loc - __start_mcount_loc;
2778
2779 ret = ftrace_dyn_table_alloc(count);
2780 if (ret)
2781 goto failed;
2782
2783 last_ftrace_enabled = ftrace_enabled = 1;
2784
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002785 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08002786 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002787 __stop_mcount_loc);
2788
Steven Rostedt93eb6772009-04-15 13:24:06 -04002789 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002790 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002791 pr_warning("Failed to register trace ftrace module notifier\n");
2792
Steven Rostedt2af15d62009-05-28 13:37:24 -04002793 set_ftrace_early_filters();
2794
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002795 return;
2796 failed:
2797 ftrace_disabled = 1;
2798}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002799
Steven Rostedt3d083392008-05-12 21:20:42 +02002800#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002801
2802static int __init ftrace_nodyn_init(void)
2803{
2804 ftrace_enabled = 1;
2805 return 0;
2806}
2807device_initcall(ftrace_nodyn_init);
2808
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002809static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2810static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002811/* Keep as macros so we do not need to define the commands */
2812# define ftrace_startup(command) do { } while (0)
2813# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002814# define ftrace_startup_sysctl() do { } while (0)
2815# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002816#endif /* CONFIG_DYNAMIC_FTRACE */
2817
Steven Rostedte32d8952008-12-04 00:26:41 -05002818static void clear_ftrace_swapper(void)
2819{
2820 struct task_struct *p;
2821 int cpu;
2822
2823 get_online_cpus();
2824 for_each_online_cpu(cpu) {
2825 p = idle_task(cpu);
2826 clear_tsk_trace_trace(p);
2827 }
2828 put_online_cpus();
2829}
2830
2831static void set_ftrace_swapper(void)
2832{
2833 struct task_struct *p;
2834 int cpu;
2835
2836 get_online_cpus();
2837 for_each_online_cpu(cpu) {
2838 p = idle_task(cpu);
2839 set_tsk_trace_trace(p);
2840 }
2841 put_online_cpus();
2842}
2843
2844static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002845{
2846 struct task_struct *p;
2847
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002848 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002849 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002850 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002851 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002852 rcu_read_unlock();
2853
Steven Rostedte32d8952008-12-04 00:26:41 -05002854 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002855}
2856
Steven Rostedte32d8952008-12-04 00:26:41 -05002857static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002858{
2859 struct task_struct *p;
2860
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002861 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002862 do_each_pid_task(pid, PIDTYPE_PID, p) {
2863 set_tsk_trace_trace(p);
2864 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002865 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002866}
2867
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002868static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002869{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002870 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002871 clear_ftrace_swapper();
2872 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002873 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05002874}
2875
2876static void set_ftrace_pid_task(struct pid *pid)
2877{
2878 if (pid == ftrace_swapper_pid)
2879 set_ftrace_swapper();
2880 else
2881 set_ftrace_pid(pid);
2882}
2883
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002884static int ftrace_pid_add(int p)
2885{
2886 struct pid *pid;
2887 struct ftrace_pid *fpid;
2888 int ret = -EINVAL;
2889
2890 mutex_lock(&ftrace_lock);
2891
2892 if (!p)
2893 pid = ftrace_swapper_pid;
2894 else
2895 pid = find_get_pid(p);
2896
2897 if (!pid)
2898 goto out;
2899
2900 ret = 0;
2901
2902 list_for_each_entry(fpid, &ftrace_pids, list)
2903 if (fpid->pid == pid)
2904 goto out_put;
2905
2906 ret = -ENOMEM;
2907
2908 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2909 if (!fpid)
2910 goto out_put;
2911
2912 list_add(&fpid->list, &ftrace_pids);
2913 fpid->pid = pid;
2914
2915 set_ftrace_pid_task(pid);
2916
2917 ftrace_update_pid_func();
2918 ftrace_startup_enable(0);
2919
2920 mutex_unlock(&ftrace_lock);
2921 return 0;
2922
2923out_put:
2924 if (pid != ftrace_swapper_pid)
2925 put_pid(pid);
2926
2927out:
2928 mutex_unlock(&ftrace_lock);
2929 return ret;
2930}
2931
2932static void ftrace_pid_reset(void)
2933{
2934 struct ftrace_pid *fpid, *safe;
2935
2936 mutex_lock(&ftrace_lock);
2937 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2938 struct pid *pid = fpid->pid;
2939
2940 clear_ftrace_pid_task(pid);
2941
2942 list_del(&fpid->list);
2943 kfree(fpid);
2944 }
2945
2946 ftrace_update_pid_func();
2947 ftrace_startup_enable(0);
2948
2949 mutex_unlock(&ftrace_lock);
2950}
2951
2952static void *fpid_start(struct seq_file *m, loff_t *pos)
2953{
2954 mutex_lock(&ftrace_lock);
2955
2956 if (list_empty(&ftrace_pids) && (!*pos))
2957 return (void *) 1;
2958
2959 return seq_list_start(&ftrace_pids, *pos);
2960}
2961
2962static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2963{
2964 if (v == (void *)1)
2965 return NULL;
2966
2967 return seq_list_next(v, &ftrace_pids, pos);
2968}
2969
2970static void fpid_stop(struct seq_file *m, void *p)
2971{
2972 mutex_unlock(&ftrace_lock);
2973}
2974
2975static int fpid_show(struct seq_file *m, void *v)
2976{
2977 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2978
2979 if (v == (void *)1) {
2980 seq_printf(m, "no pid\n");
2981 return 0;
2982 }
2983
2984 if (fpid->pid == ftrace_swapper_pid)
2985 seq_printf(m, "swapper tasks\n");
2986 else
2987 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2988
2989 return 0;
2990}
2991
2992static const struct seq_operations ftrace_pid_sops = {
2993 .start = fpid_start,
2994 .next = fpid_next,
2995 .stop = fpid_stop,
2996 .show = fpid_show,
2997};
2998
2999static int
3000ftrace_pid_open(struct inode *inode, struct file *file)
3001{
3002 int ret = 0;
3003
3004 if ((file->f_mode & FMODE_WRITE) &&
3005 (file->f_flags & O_TRUNC))
3006 ftrace_pid_reset();
3007
3008 if (file->f_mode & FMODE_READ)
3009 ret = seq_open(file, &ftrace_pid_sops);
3010
3011 return ret;
3012}
3013
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003014static ssize_t
3015ftrace_pid_write(struct file *filp, const char __user *ubuf,
3016 size_t cnt, loff_t *ppos)
3017{
Ingo Molnar457dc922009-11-23 11:03:28 +01003018 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003019 long val;
3020 int ret;
3021
3022 if (cnt >= sizeof(buf))
3023 return -EINVAL;
3024
3025 if (copy_from_user(&buf, ubuf, cnt))
3026 return -EFAULT;
3027
3028 buf[cnt] = 0;
3029
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003030 /*
3031 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3032 * to clean the filter quietly.
3033 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003034 tmp = strstrip(buf);
3035 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003036 return 1;
3037
Ingo Molnar457dc922009-11-23 11:03:28 +01003038 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003039 if (ret < 0)
3040 return ret;
3041
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003042 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003043
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003044 return ret ? ret : cnt;
3045}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003046
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003047static int
3048ftrace_pid_release(struct inode *inode, struct file *file)
3049{
3050 if (file->f_mode & FMODE_READ)
3051 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003052
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003053 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003054}
3055
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003056static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003057 .open = ftrace_pid_open,
3058 .write = ftrace_pid_write,
3059 .read = seq_read,
3060 .llseek = seq_lseek,
3061 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003062};
3063
3064static __init int ftrace_init_debugfs(void)
3065{
3066 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003067
3068 d_tracer = tracing_init_dentry();
3069 if (!d_tracer)
3070 return 0;
3071
3072 ftrace_init_dyn_debugfs(d_tracer);
3073
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003074 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3075 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003076
3077 ftrace_profile_debugfs(d_tracer);
3078
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003079 return 0;
3080}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003081fs_initcall(ftrace_init_debugfs);
3082
Steven Rostedt3d083392008-05-12 21:20:42 +02003083/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003084 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003085 *
3086 * This function should be used by panic code. It stops ftrace
3087 * but in a not so nice way. If you need to simply kill ftrace
3088 * from a non-atomic section, use ftrace_kill.
3089 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003090void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003091{
3092 ftrace_disabled = 1;
3093 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003094 clear_ftrace_function();
3095}
3096
3097/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003098 * register_ftrace_function - register a function for profiling
3099 * @ops - ops structure that holds the function for profiling.
3100 *
3101 * Register a function to be called by all functions in the
3102 * kernel.
3103 *
3104 * Note: @ops->func and all the functions it calls must be labeled
3105 * with "notrace", otherwise it will go into a
3106 * recursive loop.
3107 */
3108int register_ftrace_function(struct ftrace_ops *ops)
3109{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003110 int ret;
3111
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003112 if (unlikely(ftrace_disabled))
3113 return -1;
3114
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003115 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003116
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003117 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003118 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003119
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003120 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003121 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003122}
3123
3124/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003125 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003126 * @ops - ops structure that holds the function to unregister
3127 *
3128 * Unregister a function that was added to be called by ftrace profiling.
3129 */
3130int unregister_ftrace_function(struct ftrace_ops *ops)
3131{
3132 int ret;
3133
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003134 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003135 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003136 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003137 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003138
3139 return ret;
3140}
3141
Ingo Molnare309b412008-05-12 21:20:51 +02003142int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003143ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003144 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003145 loff_t *ppos)
3146{
3147 int ret;
3148
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003149 if (unlikely(ftrace_disabled))
3150 return -ENODEV;
3151
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003152 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003153
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003154 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003155
Li Zefana32c7762009-06-26 16:55:51 +08003156 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003157 goto out;
3158
Li Zefana32c7762009-06-26 16:55:51 +08003159 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003160
3161 if (ftrace_enabled) {
3162
3163 ftrace_startup_sysctl();
3164
3165 /* we are starting ftrace again */
3166 if (ftrace_list != &ftrace_list_end) {
3167 if (ftrace_list->next == &ftrace_list_end)
3168 ftrace_trace_function = ftrace_list->func;
3169 else
3170 ftrace_trace_function = ftrace_list_func;
3171 }
3172
3173 } else {
3174 /* stopping ftrace calls (just send to ftrace_stub) */
3175 ftrace_trace_function = ftrace_stub;
3176
3177 ftrace_shutdown_sysctl();
3178 }
3179
3180 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003181 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003182 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003183}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003184
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003185#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003186
Steven Rostedt597af812009-04-03 15:24:12 -04003187static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003188static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003189
Steven Rostedte49dc192008-12-02 23:50:05 -05003190int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3191{
3192 return 0;
3193}
3194
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003195/* The callbacks that hook a function */
3196trace_func_graph_ret_t ftrace_graph_return =
3197 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003198trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003199
3200/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3201static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3202{
3203 int i;
3204 int ret = 0;
3205 unsigned long flags;
3206 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3207 struct task_struct *g, *t;
3208
3209 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3210 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3211 * sizeof(struct ftrace_ret_stack),
3212 GFP_KERNEL);
3213 if (!ret_stack_list[i]) {
3214 start = 0;
3215 end = i;
3216 ret = -ENOMEM;
3217 goto free;
3218 }
3219 }
3220
3221 read_lock_irqsave(&tasklist_lock, flags);
3222 do_each_thread(g, t) {
3223 if (start == end) {
3224 ret = -EAGAIN;
3225 goto unlock;
3226 }
3227
3228 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003229 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003230 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003231 t->curr_ret_stack = -1;
3232 /* Make sure the tasks see the -1 first: */
3233 smp_wmb();
3234 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003235 }
3236 } while_each_thread(g, t);
3237
3238unlock:
3239 read_unlock_irqrestore(&tasklist_lock, flags);
3240free:
3241 for (i = start; i < end; i++)
3242 kfree(ret_stack_list[i]);
3243 return ret;
3244}
3245
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003246static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04003247ftrace_graph_probe_sched_switch(void *ignore,
3248 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003249{
3250 unsigned long long timestamp;
3251 int index;
3252
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003253 /*
3254 * Does the user want to count the time a function was asleep.
3255 * If so, do not update the time stamps.
3256 */
3257 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3258 return;
3259
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003260 timestamp = trace_clock_local();
3261
3262 prev->ftrace_timestamp = timestamp;
3263
3264 /* only process tasks that we timestamped */
3265 if (!next->ftrace_timestamp)
3266 return;
3267
3268 /*
3269 * Update all the counters in next to make up for the
3270 * time next was sleeping.
3271 */
3272 timestamp -= next->ftrace_timestamp;
3273
3274 for (index = next->curr_ret_stack; index >= 0; index--)
3275 next->ret_stack[index].calltime += timestamp;
3276}
3277
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003278/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003279static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003280{
3281 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003282 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003283
3284 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3285 sizeof(struct ftrace_ret_stack *),
3286 GFP_KERNEL);
3287
3288 if (!ret_stack_list)
3289 return -ENOMEM;
3290
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003291 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003292 for_each_online_cpu(cpu) {
3293 if (!idle_task(cpu)->ret_stack)
3294 ftrace_graph_init_task(idle_task(cpu));
3295 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003296
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003297 do {
3298 ret = alloc_retstack_tasklist(ret_stack_list);
3299 } while (ret == -EAGAIN);
3300
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003301 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04003302 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003303 if (ret)
3304 pr_info("ftrace_graph: Couldn't activate tracepoint"
3305 " probe to kernel_sched_switch\n");
3306 }
3307
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003308 kfree(ret_stack_list);
3309 return ret;
3310}
3311
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003312/*
3313 * Hibernation protection.
3314 * The state of the current task is too much unstable during
3315 * suspend/restore to disk. We want to protect against that.
3316 */
3317static int
3318ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3319 void *unused)
3320{
3321 switch (state) {
3322 case PM_HIBERNATION_PREPARE:
3323 pause_graph_tracing();
3324 break;
3325
3326 case PM_POST_HIBERNATION:
3327 unpause_graph_tracing();
3328 break;
3329 }
3330 return NOTIFY_DONE;
3331}
3332
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003333int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3334 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003335{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003336 int ret = 0;
3337
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003338 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003339
Steven Rostedt05ce5812009-03-24 00:18:31 -04003340 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003341 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003342 ret = -EBUSY;
3343 goto out;
3344 }
3345
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003346 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3347 register_pm_notifier(&ftrace_suspend_notifier);
3348
Steven Rostedt597af812009-04-03 15:24:12 -04003349 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003350 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003351 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003352 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003353 goto out;
3354 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003355
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003356 ftrace_graph_return = retfunc;
3357 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003358
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003359 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003360
3361out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003362 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003363 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003364}
3365
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003366void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003367{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003368 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003369
Steven Rostedt597af812009-04-03 15:24:12 -04003370 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003371 goto out;
3372
Steven Rostedt597af812009-04-03 15:24:12 -04003373 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003374 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003375 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003376 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003377 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04003378 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003379
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003380 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003381 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003382}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003383
3384/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003385void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003386{
Steven Rostedt84047e32009-06-02 16:51:55 -04003387 /* Make sure we do not use the parent ret_stack */
3388 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05003389 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04003390
Steven Rostedt597af812009-04-03 15:24:12 -04003391 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003392 struct ftrace_ret_stack *ret_stack;
3393
3394 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003395 * sizeof(struct ftrace_ret_stack),
3396 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003397 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003398 return;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003399 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003400 atomic_set(&t->trace_overrun, 0);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003401 t->ftrace_timestamp = 0;
Steven Rostedt82310a32009-06-02 12:26:07 -04003402 /* make curr_ret_stack visable before we add the ret_stack */
3403 smp_wmb();
3404 t->ret_stack = ret_stack;
Steven Rostedt84047e32009-06-02 16:51:55 -04003405 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003406}
3407
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003408void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003409{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003410 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3411
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003412 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003413 /* NULL must become visible to IRQs before we free it: */
3414 barrier();
3415
3416 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003417}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003418
3419void ftrace_graph_stop(void)
3420{
3421 ftrace_stop();
3422}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003423#endif