blob: 0d88ce9b9fb8828c9a81fdffcd47763ae5cc2543 [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];
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400384#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400385 static DEFINE_MUTEX(mutex);
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
Steven Rostedt493762f2009-03-23 17:12:36 -0400390
391 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400392 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400393
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400394#ifdef CONFIG_FUNCTION_GRAPH_TRACER
395 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400396 avg = rec->time;
397 do_div(avg, rec->counter);
398
Chase Douglase330b3b2010-04-26 14:02:05 -0400399 /* Sample standard deviation (s^2) */
400 if (rec->counter <= 1)
401 stddev = 0;
402 else {
403 stddev = rec->time_squared - rec->counter * avg * avg;
404 /*
405 * Divide only 1000 for ns^2 -> us^2 conversion.
406 * trace_print_graph_duration will divide 1000 again.
407 */
408 do_div(stddev, (rec->counter - 1) * 1000);
409 }
410
Steven Rostedt34886c82009-03-25 21:00:47 -0400411 mutex_lock(&mutex);
412 trace_seq_init(&s);
413 trace_print_graph_duration(rec->time, &s);
414 trace_seq_puts(&s, " ");
415 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400416 trace_seq_puts(&s, " ");
417 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400418 trace_print_seq(m, &s);
419 mutex_unlock(&mutex);
420#endif
421 seq_putc(m, '\n');
422
Steven Rostedt493762f2009-03-23 17:12:36 -0400423 return 0;
424}
425
Steven Rostedtcafb1682009-03-24 20:50:39 -0400426static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400427{
428 struct ftrace_profile_page *pg;
429
Steven Rostedtcafb1682009-03-24 20:50:39 -0400430 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400431
432 while (pg) {
433 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
434 pg->index = 0;
435 pg = pg->next;
436 }
437
Steven Rostedtcafb1682009-03-24 20:50:39 -0400438 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400439 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
440}
441
Steven Rostedtcafb1682009-03-24 20:50:39 -0400442int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400443{
444 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400445 int functions;
446 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400447 int i;
448
449 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400450 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400451 return 0;
452
Steven Rostedtcafb1682009-03-24 20:50:39 -0400453 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
454 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400455 return -ENOMEM;
456
Steven Rostedt318e0a72009-03-25 20:06:34 -0400457#ifdef CONFIG_DYNAMIC_FTRACE
458 functions = ftrace_update_tot_cnt;
459#else
460 /*
461 * We do not know the number of functions that exist because
462 * dynamic tracing is what counts them. With past experience
463 * we have around 20K functions. That should be more than enough.
464 * It is highly unlikely we will execute every function in
465 * the kernel.
466 */
467 functions = 20000;
468#endif
469
Steven Rostedtcafb1682009-03-24 20:50:39 -0400470 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400471
Steven Rostedt318e0a72009-03-25 20:06:34 -0400472 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
473
474 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400475 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400476 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400477 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400478 pg = pg->next;
479 }
480
481 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400482
483 out_free:
484 pg = stat->start;
485 while (pg) {
486 unsigned long tmp = (unsigned long)pg;
487
488 pg = pg->next;
489 free_page(tmp);
490 }
491
492 free_page((unsigned long)stat->pages);
493 stat->pages = NULL;
494 stat->start = NULL;
495
496 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400497}
498
Steven Rostedtcafb1682009-03-24 20:50:39 -0400499static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400500{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400501 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400502 int size;
503
Steven Rostedtcafb1682009-03-24 20:50:39 -0400504 stat = &per_cpu(ftrace_profile_stats, cpu);
505
506 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400507 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400508 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400509 return 0;
510 }
511
512 /*
513 * We are profiling all functions, but usually only a few thousand
514 * functions are hit. We'll make a hash of 1024 items.
515 */
516 size = FTRACE_PROFILE_HASH_SIZE;
517
Steven Rostedtcafb1682009-03-24 20:50:39 -0400518 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400519
Steven Rostedtcafb1682009-03-24 20:50:39 -0400520 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400521 return -ENOMEM;
522
Steven Rostedtcafb1682009-03-24 20:50:39 -0400523 if (!ftrace_profile_bits) {
524 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400525
Steven Rostedtcafb1682009-03-24 20:50:39 -0400526 for (; size; size >>= 1)
527 ftrace_profile_bits++;
528 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400529
Steven Rostedt318e0a72009-03-25 20:06:34 -0400530 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400531 if (ftrace_profile_pages_init(stat) < 0) {
532 kfree(stat->hash);
533 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400534 return -ENOMEM;
535 }
536
537 return 0;
538}
539
Steven Rostedtcafb1682009-03-24 20:50:39 -0400540static int ftrace_profile_init(void)
541{
542 int cpu;
543 int ret = 0;
544
545 for_each_online_cpu(cpu) {
546 ret = ftrace_profile_init_cpu(cpu);
547 if (ret)
548 break;
549 }
550
551 return ret;
552}
553
Steven Rostedt493762f2009-03-23 17:12:36 -0400554/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400555static struct ftrace_profile *
556ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400557{
558 struct ftrace_profile *rec;
559 struct hlist_head *hhd;
560 struct hlist_node *n;
561 unsigned long key;
562
563 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400564 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400565
566 if (hlist_empty(hhd))
567 return NULL;
568
569 hlist_for_each_entry_rcu(rec, n, hhd, node) {
570 if (rec->ip == ip)
571 return rec;
572 }
573
574 return NULL;
575}
576
Steven Rostedtcafb1682009-03-24 20:50:39 -0400577static void ftrace_add_profile(struct ftrace_profile_stat *stat,
578 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400579{
580 unsigned long key;
581
582 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400583 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400584}
585
Steven Rostedt318e0a72009-03-25 20:06:34 -0400586/*
587 * The memory is already allocated, this simply finds a new record to use.
588 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400589static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400590ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400591{
592 struct ftrace_profile *rec = NULL;
593
Steven Rostedt318e0a72009-03-25 20:06:34 -0400594 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400595 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400596 goto out;
597
Steven Rostedt493762f2009-03-23 17:12:36 -0400598 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400599 * Try to find the function again since an NMI
600 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400601 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400602 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400603 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400604 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400605
Steven Rostedtcafb1682009-03-24 20:50:39 -0400606 if (stat->pages->index == PROFILES_PER_PAGE) {
607 if (!stat->pages->next)
608 goto out;
609 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400610 }
611
Steven Rostedtcafb1682009-03-24 20:50:39 -0400612 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400613 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400614 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400615
Steven Rostedt493762f2009-03-23 17:12:36 -0400616 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400617 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400618
619 return rec;
620}
621
Steven Rostedt493762f2009-03-23 17:12:36 -0400622static void
623function_profile_call(unsigned long ip, unsigned long parent_ip)
624{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400625 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400626 struct ftrace_profile *rec;
627 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400628
629 if (!ftrace_profile_enabled)
630 return;
631
Steven Rostedt493762f2009-03-23 17:12:36 -0400632 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400633
634 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400635 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400636 goto out;
637
638 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400639 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400640 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400641 if (!rec)
642 goto out;
643 }
644
645 rec->counter++;
646 out:
647 local_irq_restore(flags);
648}
649
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400650#ifdef CONFIG_FUNCTION_GRAPH_TRACER
651static int profile_graph_entry(struct ftrace_graph_ent *trace)
652{
653 function_profile_call(trace->func, 0);
654 return 1;
655}
656
657static void profile_graph_return(struct ftrace_graph_ret *trace)
658{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400659 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400660 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400661 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400662 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400663
664 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400665 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400666 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400667 goto out;
668
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400669 /* If the calltime was zero'd ignore it */
670 if (!trace->calltime)
671 goto out;
672
Steven Rostedta2a16d62009-03-24 23:17:58 -0400673 calltime = trace->rettime - trace->calltime;
674
675 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
676 int index;
677
678 index = trace->depth;
679
680 /* Append this call time to the parent time to subtract */
681 if (index)
682 current->ret_stack[index - 1].subtime += calltime;
683
684 if (current->ret_stack[index].subtime < calltime)
685 calltime -= current->ret_stack[index].subtime;
686 else
687 calltime = 0;
688 }
689
Steven Rostedtcafb1682009-03-24 20:50:39 -0400690 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400691 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400692 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400693 rec->time_squared += calltime * calltime;
694 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400695
Steven Rostedtcafb1682009-03-24 20:50:39 -0400696 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400697 local_irq_restore(flags);
698}
699
700static int register_ftrace_profiler(void)
701{
702 return register_ftrace_graph(&profile_graph_return,
703 &profile_graph_entry);
704}
705
706static void unregister_ftrace_profiler(void)
707{
708 unregister_ftrace_graph();
709}
710#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400711static struct ftrace_ops ftrace_profile_ops __read_mostly =
712{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400713 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400714};
715
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400716static int register_ftrace_profiler(void)
717{
718 return register_ftrace_function(&ftrace_profile_ops);
719}
720
721static void unregister_ftrace_profiler(void)
722{
723 unregister_ftrace_function(&ftrace_profile_ops);
724}
725#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
726
Steven Rostedt493762f2009-03-23 17:12:36 -0400727static ssize_t
728ftrace_profile_write(struct file *filp, const char __user *ubuf,
729 size_t cnt, loff_t *ppos)
730{
731 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400732 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400733 int ret;
734
735 if (cnt >= sizeof(buf))
736 return -EINVAL;
737
738 if (copy_from_user(&buf, ubuf, cnt))
739 return -EFAULT;
740
741 buf[cnt] = 0;
742
743 ret = strict_strtoul(buf, 10, &val);
744 if (ret < 0)
745 return ret;
746
747 val = !!val;
748
749 mutex_lock(&ftrace_profile_lock);
750 if (ftrace_profile_enabled ^ val) {
751 if (val) {
752 ret = ftrace_profile_init();
753 if (ret < 0) {
754 cnt = ret;
755 goto out;
756 }
757
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400758 ret = register_ftrace_profiler();
759 if (ret < 0) {
760 cnt = ret;
761 goto out;
762 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400763 ftrace_profile_enabled = 1;
764 } else {
765 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400766 /*
767 * unregister_ftrace_profiler calls stop_machine
768 * so this acts like an synchronize_sched.
769 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400770 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400771 }
772 }
773 out:
774 mutex_unlock(&ftrace_profile_lock);
775
Jiri Olsacf8517c2009-10-23 19:36:16 -0400776 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400777
778 return cnt;
779}
780
781static ssize_t
782ftrace_profile_read(struct file *filp, char __user *ubuf,
783 size_t cnt, loff_t *ppos)
784{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400785 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400786 int r;
787
788 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
789 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
790}
791
792static const struct file_operations ftrace_profile_fops = {
793 .open = tracing_open_generic,
794 .read = ftrace_profile_read,
795 .write = ftrace_profile_write,
796};
797
Steven Rostedtcafb1682009-03-24 20:50:39 -0400798/* used to initialize the real stat files */
799static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400800 .name = "functions",
801 .stat_start = function_stat_start,
802 .stat_next = function_stat_next,
803 .stat_cmp = function_stat_cmp,
804 .stat_headers = function_stat_headers,
805 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400806};
807
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400808static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400809{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400810 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400811 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400812 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400813 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400814 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400815
Steven Rostedtcafb1682009-03-24 20:50:39 -0400816 for_each_possible_cpu(cpu) {
817 stat = &per_cpu(ftrace_profile_stats, cpu);
818
819 /* allocate enough for function name + cpu number */
820 name = kmalloc(32, GFP_KERNEL);
821 if (!name) {
822 /*
823 * The files created are permanent, if something happens
824 * we still do not free memory.
825 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400826 WARN(1,
827 "Could not allocate stat file for cpu %d\n",
828 cpu);
829 return;
830 }
831 stat->stat = function_stats;
832 snprintf(name, 32, "function%d", cpu);
833 stat->stat.name = name;
834 ret = register_stat_tracer(&stat->stat);
835 if (ret) {
836 WARN(1,
837 "Could not register function stat for cpu %d\n",
838 cpu);
839 kfree(name);
840 return;
841 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400842 }
843
844 entry = debugfs_create_file("function_profile_enabled", 0644,
845 d_tracer, NULL, &ftrace_profile_fops);
846 if (!entry)
847 pr_warning("Could not create debugfs "
848 "'function_profile_enabled' entry\n");
849}
850
851#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400852static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400853{
854}
855#endif /* CONFIG_FUNCTION_PROFILER */
856
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100857static struct pid * const ftrace_swapper_pid = &init_struct_pid;
858
Steven Rostedt3d083392008-05-12 21:20:42 +0200859#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100860
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400861#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400862# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400863#endif
864
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500865static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
866
Steven Rostedtb6887d72009-02-17 12:32:04 -0500867struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500868 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500869 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500870 unsigned long flags;
871 unsigned long ip;
872 void *data;
873 struct rcu_head rcu;
874};
875
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200876enum {
877 FTRACE_ENABLE_CALLS = (1 << 0),
878 FTRACE_DISABLE_CALLS = (1 << 1),
879 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
880 FTRACE_ENABLE_MCOUNT = (1 << 3),
881 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500882 FTRACE_START_FUNC_RET = (1 << 5),
883 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200884};
885
Steven Rostedt5072c592008-05-12 21:20:43 +0200886static int ftrace_filtered;
887
Lai Jiangshane94142a2009-03-13 17:51:27 +0800888static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200889
Steven Rostedt41c52c02008-05-22 11:46:33 -0400890static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200891
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200892struct ftrace_page {
893 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500894 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200895 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700896};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200897
898#define ENTRIES_PER_PAGE \
899 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
900
901/* estimate from running different kernels */
902#define NR_TO_INIT 10000
903
904static struct ftrace_page *ftrace_pages_start;
905static struct ftrace_page *ftrace_pages;
906
Steven Rostedt37ad5082008-05-12 21:20:48 +0200907static struct dyn_ftrace *ftrace_free_records;
908
Steven Rostedt265c8312009-02-13 12:43:56 -0500909/*
910 * This is a double for. Do not use 'break' to break out of the loop,
911 * you must use a goto.
912 */
913#define do_for_each_ftrace_rec(pg, rec) \
914 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
915 int _____i; \
916 for (_____i = 0; _____i < pg->index; _____i++) { \
917 rec = &pg->records[_____i];
918
919#define while_for_each_ftrace_rec() \
920 } \
921 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530922
Ingo Molnare309b412008-05-12 21:20:51 +0200923static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad5082008-05-12 21:20:48 +0200924{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800925 rec->freelist = ftrace_free_records;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200926 ftrace_free_records = rec;
927 rec->flags |= FTRACE_FL_FREE;
928}
929
Ingo Molnare309b412008-05-12 21:20:51 +0200930static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200931{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200932 struct dyn_ftrace *rec;
933
934 /* First check for freed records */
935 if (ftrace_free_records) {
936 rec = ftrace_free_records;
937
Steven Rostedt37ad5082008-05-12 21:20:48 +0200938 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt69128962008-10-23 09:33:03 -0400939 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad5082008-05-12 21:20:48 +0200940 ftrace_free_records = NULL;
941 return NULL;
942 }
943
Lai Jiangshanee000b72009-03-24 13:38:06 +0800944 ftrace_free_records = rec->freelist;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200945 memset(rec, 0, sizeof(*rec));
946 return rec;
947 }
948
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200949 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400950 if (!ftrace_pages->next) {
951 /* allocate another page */
952 ftrace_pages->next =
953 (void *)get_zeroed_page(GFP_KERNEL);
954 if (!ftrace_pages->next)
955 return NULL;
956 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200957 ftrace_pages = ftrace_pages->next;
958 }
959
960 return &ftrace_pages->records[ftrace_pages->index++];
961}
962
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400963static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200964ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200965{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400966 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200967
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800968 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400969 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200970
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400971 rec = ftrace_alloc_dyn_node(ip);
972 if (!rec)
973 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200974
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400975 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +0800976 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +0800977 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200978
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400979 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200980}
981
Steven Rostedt05736a42008-09-22 14:55:47 -0700982static void print_ip_ins(const char *fmt, unsigned char *p)
983{
984 int i;
985
986 printk(KERN_CONT "%s", fmt);
987
988 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
989 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
990}
991
Steven Rostedt31e88902008-11-14 16:21:19 -0800992static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800993{
994 switch (failed) {
995 case -EFAULT:
996 FTRACE_WARN_ON_ONCE(1);
997 pr_info("ftrace faulted on modifying ");
998 print_ip_sym(ip);
999 break;
1000 case -EINVAL:
1001 FTRACE_WARN_ON_ONCE(1);
1002 pr_info("ftrace failed to modify ");
1003 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001004 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001005 printk(KERN_CONT "\n");
1006 break;
1007 case -EPERM:
1008 FTRACE_WARN_ON_ONCE(1);
1009 pr_info("ftrace faulted on writing ");
1010 print_ip_sym(ip);
1011 break;
1012 default:
1013 FTRACE_WARN_ON_ONCE(1);
1014 pr_info("ftrace faulted on unknown error ");
1015 print_ip_sym(ip);
1016 }
1017}
1018
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001019
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001020/* Return 1 if the address range is reserved for ftrace */
1021int ftrace_text_reserved(void *start, void *end)
1022{
1023 struct dyn_ftrace *rec;
1024 struct ftrace_page *pg;
1025
1026 do_for_each_ftrace_rec(pg, rec) {
1027 if (rec->ip <= (unsigned long)end &&
1028 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1029 return 1;
1030 } while_for_each_ftrace_rec();
1031 return 0;
1032}
1033
1034
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301035static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001036__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001037{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001038 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001039 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001040
Shaohua Lif0001202009-01-09 11:29:42 +08001041 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001042
Steven Rostedt982c3502008-11-15 16:31:41 -05001043 /*
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001044 * If this record is not to be traced or we want to disable it,
1045 * then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001046 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001047 * If we want to enable it and filtering is off, then enable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001048 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001049 * If we want to enable it and filtering is on, enable it only if
1050 * it's filtered
Steven Rostedt982c3502008-11-15 16:31:41 -05001051 */
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001052 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1053 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1054 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001055 }
1056
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001057 /* If the state of this record hasn't changed, then do nothing */
1058 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1059 return 0;
1060
1061 if (flag) {
1062 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001063 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001064 }
1065
1066 rec->flags &= ~FTRACE_FL_ENABLED;
1067 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001068}
1069
1070static void ftrace_replace_code(int enable)
1071{
Steven Rostedt37ad5082008-05-12 21:20:48 +02001072 struct dyn_ftrace *rec;
1073 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001074 int failed;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001075
Steven Rostedt265c8312009-02-13 12:43:56 -05001076 do_for_each_ftrace_rec(pg, rec) {
1077 /*
Zhaoleifa9d13c2009-03-13 17:16:34 +08001078 * Skip over free records, records that have
1079 * failed and not converted.
Steven Rostedt265c8312009-02-13 12:43:56 -05001080 */
1081 if (rec->flags & FTRACE_FL_FREE ||
Zhaoleifa9d13c2009-03-13 17:16:34 +08001082 rec->flags & FTRACE_FL_FAILED ||
Frederic Weisbecker03303542009-03-16 22:41:00 +01001083 !(rec->flags & FTRACE_FL_CONVERTED))
Steven Rostedt265c8312009-02-13 12:43:56 -05001084 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001085
Steven Rostedt265c8312009-02-13 12:43:56 -05001086 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001087 if (failed) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001088 rec->flags |= FTRACE_FL_FAILED;
Steven Rostedt3279ba32009-10-07 16:57:56 -04001089 ftrace_bug(failed, rec->ip);
1090 /* Stop processing */
1091 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001092 }
1093 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001094}
1095
Ingo Molnare309b412008-05-12 21:20:51 +02001096static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001097ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001098{
1099 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001100 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001101
1102 ip = rec->ip;
1103
Shaohua Li25aac9d2009-01-09 11:29:40 +08001104 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001105 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001106 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001107 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301108 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001109 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301110 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001111}
1112
Steven Rostedt000ab692009-02-17 13:35:06 -05001113/*
1114 * archs can override this function if they must do something
1115 * before the modifying code is performed.
1116 */
1117int __weak ftrace_arch_code_modify_prepare(void)
1118{
1119 return 0;
1120}
1121
1122/*
1123 * archs can override this function if they must do something
1124 * after the modifying code is performed.
1125 */
1126int __weak ftrace_arch_code_modify_post_process(void)
1127{
1128 return 0;
1129}
1130
Ingo Molnare309b412008-05-12 21:20:51 +02001131static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001132{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001133 int *command = data;
1134
Steven Rostedta3583242008-11-11 15:01:42 -05001135 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001136 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001137 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001138 ftrace_replace_code(0);
1139
1140 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1141 ftrace_update_ftrace_func(ftrace_trace_function);
1142
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001143 if (*command & FTRACE_START_FUNC_RET)
1144 ftrace_enable_ftrace_graph_caller();
1145 else if (*command & FTRACE_STOP_FUNC_RET)
1146 ftrace_disable_ftrace_graph_caller();
1147
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001148 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001149}
1150
Ingo Molnare309b412008-05-12 21:20:51 +02001151static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001152{
Steven Rostedt000ab692009-02-17 13:35:06 -05001153 int ret;
1154
1155 ret = ftrace_arch_code_modify_prepare();
1156 FTRACE_WARN_ON(ret);
1157 if (ret)
1158 return;
1159
Rusty Russell784e2d72008-07-28 12:16:31 -05001160 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001161
1162 ret = ftrace_arch_code_modify_post_process();
1163 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001164}
1165
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001166static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001167static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001168
1169static void ftrace_startup_enable(int command)
1170{
1171 if (saved_ftrace_func != ftrace_trace_function) {
1172 saved_ftrace_func = ftrace_trace_function;
1173 command |= FTRACE_UPDATE_TRACE_FUNC;
1174 }
1175
1176 if (!command || !ftrace_enabled)
1177 return;
1178
1179 ftrace_run_update_code(command);
1180}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001181
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001182static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001183{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001184 if (unlikely(ftrace_disabled))
1185 return;
1186
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001187 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001188 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001189
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001190 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001191}
1192
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001193static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001194{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001195 if (unlikely(ftrace_disabled))
1196 return;
1197
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001198 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001199 /*
1200 * Just warn in case of unbalance, no need to kill ftrace, it's not
1201 * critical but the ftrace_call callers may be never nopped again after
1202 * further ftrace uses.
1203 */
1204 WARN_ON_ONCE(ftrace_start_up < 0);
1205
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001206 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001207 command |= FTRACE_DISABLE_CALLS;
1208
1209 if (saved_ftrace_func != ftrace_trace_function) {
1210 saved_ftrace_func = ftrace_trace_function;
1211 command |= FTRACE_UPDATE_TRACE_FUNC;
1212 }
1213
1214 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001215 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001216
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001217 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001218}
1219
Ingo Molnare309b412008-05-12 21:20:51 +02001220static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001221{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001222 int command = FTRACE_ENABLE_MCOUNT;
1223
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001224 if (unlikely(ftrace_disabled))
1225 return;
1226
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001227 /* Force update next time */
1228 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001229 /* ftrace_start_up is true if we want ftrace running */
1230 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001231 command |= FTRACE_ENABLE_CALLS;
1232
1233 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001234}
1235
Ingo Molnare309b412008-05-12 21:20:51 +02001236static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001237{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001238 int command = FTRACE_DISABLE_MCOUNT;
1239
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001240 if (unlikely(ftrace_disabled))
1241 return;
1242
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001243 /* ftrace_start_up is true if ftrace is running */
1244 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001245 command |= FTRACE_DISABLE_CALLS;
1246
1247 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001248}
1249
Steven Rostedt3d083392008-05-12 21:20:42 +02001250static cycle_t ftrace_update_time;
1251static unsigned long ftrace_update_cnt;
1252unsigned long ftrace_update_tot_cnt;
1253
Steven Rostedt31e88902008-11-14 16:21:19 -08001254static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001255{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001256 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301257 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001258
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001259 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001260 ftrace_update_cnt = 0;
1261
Lai Jiangshane94142a2009-03-13 17:51:27 +08001262 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301263
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001264 /* If something went wrong, bail without enabling anything */
1265 if (unlikely(ftrace_disabled))
1266 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001267
Lai Jiangshane94142a2009-03-13 17:51:27 +08001268 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001269 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001270 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301271
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001272 /*
1273 * Do the initial record convertion from mcount jump
1274 * to the NOP instructions.
1275 */
1276 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001277 ftrace_free_rec(p);
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001278 continue;
1279 }
1280
1281 p->flags |= FTRACE_FL_CONVERTED;
1282 ftrace_update_cnt++;
1283
1284 /*
1285 * If the tracing is enabled, go ahead and enable the record.
1286 *
1287 * The reason not to enable the record immediatelly is the
1288 * inherent check of ftrace_make_nop/ftrace_make_call for
1289 * correct previous instructions. Making first the NOP
1290 * conversion puts the module to the correct state, thus
1291 * passing the ftrace_make_call check.
1292 */
1293 if (ftrace_start_up) {
1294 int failed = __ftrace_replace_code(p, 1);
1295 if (failed) {
1296 ftrace_bug(failed, p->ip);
1297 ftrace_free_rec(p);
1298 }
1299 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001300 }
1301
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001302 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001303 ftrace_update_time = stop - start;
1304 ftrace_update_tot_cnt += ftrace_update_cnt;
1305
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001306 return 0;
1307}
1308
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001309static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001310{
1311 struct ftrace_page *pg;
1312 int cnt;
1313 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001314
1315 /* allocate a few pages */
1316 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1317 if (!ftrace_pages_start)
1318 return -1;
1319
1320 /*
1321 * Allocate a few more pages.
1322 *
1323 * TODO: have some parser search vmlinux before
1324 * final linking to find all calls to ftrace.
1325 * Then we can:
1326 * a) know how many pages to allocate.
1327 * and/or
1328 * b) set up the table then.
1329 *
1330 * The dynamic code is still necessary for
1331 * modules.
1332 */
1333
1334 pg = ftrace_pages = ftrace_pages_start;
1335
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001336 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001337 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001338 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001339
1340 for (i = 0; i < cnt; i++) {
1341 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1342
1343 /* If we fail, we'll try later anyway */
1344 if (!pg->next)
1345 break;
1346
1347 pg = pg->next;
1348 }
1349
1350 return 0;
1351}
1352
Steven Rostedt5072c592008-05-12 21:20:43 +02001353enum {
1354 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001355 FTRACE_ITER_NOTRACE = (1 << 1),
1356 FTRACE_ITER_FAILURES = (1 << 2),
1357 FTRACE_ITER_PRINTALL = (1 << 3),
1358 FTRACE_ITER_HASH = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02001359};
1360
1361#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1362
1363struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +02001364 struct ftrace_page *pg;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001365 int hidx;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001366 int idx;
Steven Rostedt5072c592008-05-12 21:20:43 +02001367 unsigned flags;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001368 struct trace_parser parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02001369};
1370
Ingo Molnare309b412008-05-12 21:20:51 +02001371static void *
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001372t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1373{
1374 struct ftrace_iterator *iter = m->private;
1375 struct hlist_node *hnd = v;
1376 struct hlist_head *hhd;
1377
1378 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1379
1380 (*pos)++;
1381
1382 retry:
1383 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1384 return NULL;
1385
1386 hhd = &ftrace_func_hash[iter->hidx];
1387
1388 if (hlist_empty(hhd)) {
1389 iter->hidx++;
1390 hnd = NULL;
1391 goto retry;
1392 }
1393
1394 if (!hnd)
1395 hnd = hhd->first;
1396 else {
1397 hnd = hnd->next;
1398 if (!hnd) {
1399 iter->hidx++;
1400 goto retry;
1401 }
1402 }
1403
1404 return hnd;
1405}
1406
1407static void *t_hash_start(struct seq_file *m, loff_t *pos)
1408{
1409 struct ftrace_iterator *iter = m->private;
1410 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001411 loff_t l;
1412
1413 if (!(iter->flags & FTRACE_ITER_HASH))
1414 *pos = 0;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001415
1416 iter->flags |= FTRACE_ITER_HASH;
1417
Li Zefand82d6242009-06-24 09:54:54 +08001418 iter->hidx = 0;
1419 for (l = 0; l <= *pos; ) {
1420 p = t_hash_next(m, p, &l);
1421 if (!p)
1422 break;
1423 }
1424 return p;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001425}
1426
1427static int t_hash_show(struct seq_file *m, void *v)
1428{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001429 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001430 struct hlist_node *hnd = v;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001431
Steven Rostedtb6887d72009-02-17 12:32:04 -05001432 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001433
Steven Rostedt809dcf22009-02-16 23:06:01 -05001434 if (rec->ops->print)
1435 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1436
Steven Rostedtb375a112009-09-17 00:05:58 -04001437 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001438
1439 if (rec->data)
1440 seq_printf(m, ":%p", rec->data);
1441 seq_putc(m, '\n');
1442
1443 return 0;
1444}
1445
1446static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001447t_next(struct seq_file *m, void *v, loff_t *pos)
1448{
1449 struct ftrace_iterator *iter = m->private;
1450 struct dyn_ftrace *rec = NULL;
1451
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001452 if (iter->flags & FTRACE_ITER_HASH)
1453 return t_hash_next(m, v, pos);
1454
Steven Rostedt5072c592008-05-12 21:20:43 +02001455 (*pos)++;
1456
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001457 if (iter->flags & FTRACE_ITER_PRINTALL)
1458 return NULL;
1459
Steven Rostedt5072c592008-05-12 21:20:43 +02001460 retry:
1461 if (iter->idx >= iter->pg->index) {
1462 if (iter->pg->next) {
1463 iter->pg = iter->pg->next;
1464 iter->idx = 0;
1465 goto retry;
1466 }
1467 } else {
1468 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001469 if ((rec->flags & FTRACE_FL_FREE) ||
1470
1471 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301472 (rec->flags & FTRACE_FL_FAILED)) ||
1473
1474 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -04001475 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301476
Steven Rostedt0183fb12008-11-07 22:36:02 -05001477 ((iter->flags & FTRACE_ITER_FILTER) &&
1478 !(rec->flags & FTRACE_FL_FILTER)) ||
1479
Steven Rostedt41c52c02008-05-22 11:46:33 -04001480 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1481 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001482 rec = NULL;
1483 goto retry;
1484 }
1485 }
1486
Steven Rostedt5072c592008-05-12 21:20:43 +02001487 return rec;
1488}
1489
1490static void *t_start(struct seq_file *m, loff_t *pos)
1491{
1492 struct ftrace_iterator *iter = m->private;
1493 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001494 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001495
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001496 mutex_lock(&ftrace_lock);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001497 /*
1498 * For set_ftrace_filter reading, if we have the filter
1499 * off, we can short cut and just print out that all
1500 * functions are enabled.
1501 */
1502 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1503 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001504 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001505 iter->flags |= FTRACE_ITER_PRINTALL;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001506 return iter;
1507 }
1508
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001509 if (iter->flags & FTRACE_ITER_HASH)
1510 return t_hash_start(m, pos);
1511
Li Zefan694ce0a2009-06-24 09:54:19 +08001512 iter->pg = ftrace_pages_start;
1513 iter->idx = 0;
1514 for (l = 0; l <= *pos; ) {
1515 p = t_next(m, p, &l);
1516 if (!p)
1517 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001518 }
walimis5821e1b2008-11-15 15:19:06 +08001519
Li Zefan694ce0a2009-06-24 09:54:19 +08001520 if (!p && iter->flags & FTRACE_ITER_FILTER)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001521 return t_hash_start(m, pos);
1522
Steven Rostedt5072c592008-05-12 21:20:43 +02001523 return p;
1524}
1525
1526static void t_stop(struct seq_file *m, void *p)
1527{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001528 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001529}
1530
1531static int t_show(struct seq_file *m, void *v)
1532{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001533 struct ftrace_iterator *iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02001534 struct dyn_ftrace *rec = v;
Steven Rostedt5072c592008-05-12 21:20:43 +02001535
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001536 if (iter->flags & FTRACE_ITER_HASH)
1537 return t_hash_show(m, v);
1538
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001539 if (iter->flags & FTRACE_ITER_PRINTALL) {
1540 seq_printf(m, "#### all functions enabled ####\n");
1541 return 0;
1542 }
1543
Steven Rostedt5072c592008-05-12 21:20:43 +02001544 if (!rec)
1545 return 0;
1546
Steven Rostedtb375a112009-09-17 00:05:58 -04001547 seq_printf(m, "%ps\n", (void *)rec->ip);
Steven Rostedt5072c592008-05-12 21:20:43 +02001548
1549 return 0;
1550}
1551
James Morris88e9d342009-09-22 16:43:43 -07001552static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001553 .start = t_start,
1554 .next = t_next,
1555 .stop = t_stop,
1556 .show = t_show,
1557};
1558
Ingo Molnare309b412008-05-12 21:20:51 +02001559static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001560ftrace_avail_open(struct inode *inode, struct file *file)
1561{
1562 struct ftrace_iterator *iter;
1563 int ret;
1564
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001565 if (unlikely(ftrace_disabled))
1566 return -ENODEV;
1567
Steven Rostedt5072c592008-05-12 21:20:43 +02001568 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1569 if (!iter)
1570 return -ENOMEM;
1571
1572 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001573
1574 ret = seq_open(file, &show_ftrace_seq_ops);
1575 if (!ret) {
1576 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001577
Steven Rostedt5072c592008-05-12 21:20:43 +02001578 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001579 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001580 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001581 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001582
1583 return ret;
1584}
1585
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301586static int
1587ftrace_failures_open(struct inode *inode, struct file *file)
1588{
1589 int ret;
1590 struct seq_file *m;
1591 struct ftrace_iterator *iter;
1592
1593 ret = ftrace_avail_open(inode, file);
1594 if (!ret) {
1595 m = (struct seq_file *)file->private_data;
1596 iter = (struct ftrace_iterator *)m->private;
1597 iter->flags = FTRACE_ITER_FAILURES;
1598 }
1599
1600 return ret;
1601}
1602
1603
Steven Rostedt41c52c02008-05-22 11:46:33 -04001604static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001605{
1606 struct ftrace_page *pg;
1607 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001608 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001609
Steven Rostedt52baf112009-02-14 01:15:39 -05001610 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001611 if (enable)
1612 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001613 do_for_each_ftrace_rec(pg, rec) {
1614 if (rec->flags & FTRACE_FL_FAILED)
1615 continue;
1616 rec->flags &= ~type;
1617 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001618 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001619}
1620
Ingo Molnare309b412008-05-12 21:20:51 +02001621static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001622ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001623{
1624 struct ftrace_iterator *iter;
1625 int ret = 0;
1626
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001627 if (unlikely(ftrace_disabled))
1628 return -ENODEV;
1629
Steven Rostedt5072c592008-05-12 21:20:43 +02001630 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1631 if (!iter)
1632 return -ENOMEM;
1633
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001634 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1635 kfree(iter);
1636 return -ENOMEM;
1637 }
1638
Steven Rostedt41c52c02008-05-22 11:46:33 -04001639 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001640 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04001641 (file->f_flags & O_TRUNC))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001642 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001643
1644 if (file->f_mode & FMODE_READ) {
1645 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001646 iter->flags = enable ? FTRACE_ITER_FILTER :
1647 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001648
1649 ret = seq_open(file, &show_ftrace_seq_ops);
1650 if (!ret) {
1651 struct seq_file *m = file->private_data;
1652 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08001653 } else {
1654 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02001655 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08001656 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001657 } else
1658 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001659 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001660
1661 return ret;
1662}
1663
Steven Rostedt41c52c02008-05-22 11:46:33 -04001664static int
1665ftrace_filter_open(struct inode *inode, struct file *file)
1666{
1667 return ftrace_regex_open(inode, file, 1);
1668}
1669
1670static int
1671ftrace_notrace_open(struct inode *inode, struct file *file)
1672{
1673 return ftrace_regex_open(inode, file, 0);
1674}
1675
Ingo Molnare309b412008-05-12 21:20:51 +02001676static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001677ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001678{
1679 loff_t ret;
1680
1681 if (file->f_mode & FMODE_READ)
1682 ret = seq_lseek(file, offset, origin);
1683 else
1684 file->f_pos = ret = 1;
1685
1686 return ret;
1687}
1688
Steven Rostedt64e7c442009-02-13 17:08:48 -05001689static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001690{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001691 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08001692 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001693
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001694 switch (type) {
1695 case MATCH_FULL:
1696 if (strcmp(str, regex) == 0)
1697 matched = 1;
1698 break;
1699 case MATCH_FRONT_ONLY:
1700 if (strncmp(str, regex, len) == 0)
1701 matched = 1;
1702 break;
1703 case MATCH_MIDDLE_ONLY:
1704 if (strstr(str, regex))
1705 matched = 1;
1706 break;
1707 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08001708 slen = strlen(str);
1709 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001710 matched = 1;
1711 break;
1712 }
1713
1714 return matched;
1715}
1716
Steven Rostedt64e7c442009-02-13 17:08:48 -05001717static int
1718ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1719{
1720 char str[KSYM_SYMBOL_LEN];
1721
1722 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1723 return ftrace_match(str, regex, len, type);
1724}
1725
Li Zefan311d16d2009-12-08 11:15:11 +08001726static int ftrace_match_records(char *buff, int len, int enable)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001727{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001728 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001729 struct ftrace_page *pg;
1730 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001731 unsigned long flag;
1732 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001733 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001734 int not;
Li Zefan311d16d2009-12-08 11:15:11 +08001735 int found = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001736
Steven Rostedt6a24a242009-02-17 11:20:26 -05001737 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001738 type = filter_parse_regex(buff, len, &search, &not);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001739
1740 search_len = strlen(search);
1741
Steven Rostedt52baf112009-02-14 01:15:39 -05001742 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001743 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001744
Steven Rostedt265c8312009-02-13 12:43:56 -05001745 if (rec->flags & FTRACE_FL_FAILED)
1746 continue;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001747
1748 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001749 if (not)
1750 rec->flags &= ~flag;
1751 else
1752 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001753 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001754 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001755 /*
1756 * Only enable filtering if we have a function that
1757 * is filtered on.
1758 */
1759 if (enable && (rec->flags & FTRACE_FL_FILTER))
1760 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001761 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001762 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001763
1764 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02001765}
1766
Steven Rostedt64e7c442009-02-13 17:08:48 -05001767static int
1768ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1769 char *regex, int len, int type)
1770{
1771 char str[KSYM_SYMBOL_LEN];
1772 char *modname;
1773
1774 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1775
1776 if (!modname || strcmp(modname, mod))
1777 return 0;
1778
1779 /* blank search means to match all funcs in the mod */
1780 if (len)
1781 return ftrace_match(str, regex, len, type);
1782 else
1783 return 1;
1784}
1785
Li Zefan311d16d2009-12-08 11:15:11 +08001786static int ftrace_match_module_records(char *buff, char *mod, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001787{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001788 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001789 struct ftrace_page *pg;
1790 struct dyn_ftrace *rec;
1791 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001792 char *search = buff;
1793 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001794 int not = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08001795 int found = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001796
Steven Rostedt6a24a242009-02-17 11:20:26 -05001797 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1798
Steven Rostedt64e7c442009-02-13 17:08:48 -05001799 /* blank or '*' mean the same */
1800 if (strcmp(buff, "*") == 0)
1801 buff[0] = 0;
1802
1803 /* handle the case of 'dont filter this module' */
1804 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1805 buff[0] = 0;
1806 not = 1;
1807 }
1808
1809 if (strlen(buff)) {
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001810 type = filter_parse_regex(buff, strlen(buff), &search, &not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001811 search_len = strlen(search);
1812 }
1813
Steven Rostedt52baf112009-02-14 01:15:39 -05001814 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001815 do_for_each_ftrace_rec(pg, rec) {
1816
1817 if (rec->flags & FTRACE_FL_FAILED)
1818 continue;
1819
1820 if (ftrace_match_module_record(rec, mod,
1821 search, search_len, type)) {
1822 if (not)
1823 rec->flags &= ~flag;
1824 else
1825 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001826 found = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001827 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001828 if (enable && (rec->flags & FTRACE_FL_FILTER))
1829 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001830
1831 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001832 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001833
1834 return found;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001835}
1836
Steven Rostedtf6180772009-02-14 00:40:25 -05001837/*
1838 * We register the module command as a template to show others how
1839 * to register the a command as well.
1840 */
1841
1842static int
1843ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1844{
1845 char *mod;
1846
1847 /*
1848 * cmd == 'mod' because we only registered this func
1849 * for the 'mod' ftrace_func_command.
1850 * But if you register one func with multiple commands,
1851 * you can tell which command was used by the cmd
1852 * parameter.
1853 */
1854
1855 /* we must have a module name */
1856 if (!param)
1857 return -EINVAL;
1858
1859 mod = strsep(&param, ":");
1860 if (!strlen(mod))
1861 return -EINVAL;
1862
Li Zefan311d16d2009-12-08 11:15:11 +08001863 if (ftrace_match_module_records(func, mod, enable))
1864 return 0;
1865 return -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05001866}
1867
1868static struct ftrace_func_command ftrace_mod_cmd = {
1869 .name = "mod",
1870 .func = ftrace_mod_callback,
1871};
1872
1873static int __init ftrace_mod_cmd_init(void)
1874{
1875 return register_ftrace_command(&ftrace_mod_cmd);
1876}
1877device_initcall(ftrace_mod_cmd_init);
1878
Steven Rostedt59df055f2009-02-14 15:29:06 -05001879static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001880function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001881{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001882 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001883 struct hlist_head *hhd;
1884 struct hlist_node *n;
1885 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001886
1887 key = hash_long(ip, FTRACE_HASH_BITS);
1888
1889 hhd = &ftrace_func_hash[key];
1890
1891 if (hlist_empty(hhd))
1892 return;
1893
1894 /*
1895 * Disable preemption for these calls to prevent a RCU grace
1896 * period. This syncs the hash iteration and freeing of items
1897 * on the hash. rcu_read_lock is too dangerous here.
1898 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04001899 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05001900 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1901 if (entry->ip == ip)
1902 entry->ops->func(ip, parent_ip, &entry->data);
1903 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04001904 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05001905}
1906
Steven Rostedtb6887d72009-02-17 12:32:04 -05001907static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001908{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001909 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001910};
1911
Steven Rostedtb6887d72009-02-17 12:32:04 -05001912static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001913
Steven Rostedtb6887d72009-02-17 12:32:04 -05001914static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001915{
1916 int i;
1917
Steven Rostedtb6887d72009-02-17 12:32:04 -05001918 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001919 return;
1920
1921 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1922 struct hlist_head *hhd = &ftrace_func_hash[i];
1923 if (hhd->first)
1924 break;
1925 }
1926 /* Nothing registered? */
1927 if (i == FTRACE_FUNC_HASHSIZE)
1928 return;
1929
Steven Rostedtb6887d72009-02-17 12:32:04 -05001930 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001931 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001932 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001933}
1934
Steven Rostedtb6887d72009-02-17 12:32:04 -05001935static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001936{
1937 int i;
1938
Steven Rostedtb6887d72009-02-17 12:32:04 -05001939 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001940 return;
1941
1942 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1943 struct hlist_head *hhd = &ftrace_func_hash[i];
1944 if (hhd->first)
1945 return;
1946 }
1947
1948 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05001949 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001950 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001951 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001952}
1953
1954
1955static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1956{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001957 struct ftrace_func_probe *entry =
1958 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001959
1960 if (entry->ops->free)
1961 entry->ops->free(&entry->data);
1962 kfree(entry);
1963}
1964
1965
1966int
Steven Rostedtb6887d72009-02-17 12:32:04 -05001967register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001968 void *data)
1969{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001970 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001971 struct ftrace_page *pg;
1972 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001973 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001974 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001975 int count = 0;
1976 char *search;
1977
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001978 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001979 len = strlen(search);
1980
Steven Rostedtb6887d72009-02-17 12:32:04 -05001981 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001982 if (WARN_ON(not))
1983 return -EINVAL;
1984
1985 mutex_lock(&ftrace_lock);
1986 do_for_each_ftrace_rec(pg, rec) {
1987
1988 if (rec->flags & FTRACE_FL_FAILED)
1989 continue;
1990
1991 if (!ftrace_match_record(rec, search, len, type))
1992 continue;
1993
1994 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1995 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05001996 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001997 if (!count)
1998 count = -ENOMEM;
1999 goto out_unlock;
2000 }
2001
2002 count++;
2003
2004 entry->data = data;
2005
2006 /*
2007 * The caller might want to do something special
2008 * for each function we find. We call the callback
2009 * to give the caller an opportunity to do so.
2010 */
2011 if (ops->callback) {
2012 if (ops->callback(rec->ip, &entry->data) < 0) {
2013 /* caller does not like this func */
2014 kfree(entry);
2015 continue;
2016 }
2017 }
2018
2019 entry->ops = ops;
2020 entry->ip = rec->ip;
2021
2022 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2023 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2024
2025 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002026 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002027
2028 out_unlock:
2029 mutex_unlock(&ftrace_lock);
2030
2031 return count;
2032}
2033
2034enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002035 PROBE_TEST_FUNC = 1,
2036 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002037};
2038
2039static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002040__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002041 void *data, int flags)
2042{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002043 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002044 struct hlist_node *n, *tmp;
2045 char str[KSYM_SYMBOL_LEN];
2046 int type = MATCH_FULL;
2047 int i, len = 0;
2048 char *search;
2049
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002050 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002051 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002052 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002053 int not;
2054
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002055 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002056 len = strlen(search);
2057
Steven Rostedtb6887d72009-02-17 12:32:04 -05002058 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002059 if (WARN_ON(not))
2060 return;
2061 }
2062
2063 mutex_lock(&ftrace_lock);
2064 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2065 struct hlist_head *hhd = &ftrace_func_hash[i];
2066
2067 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2068
2069 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002070 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002071 continue;
2072
Steven Rostedtb6887d72009-02-17 12:32:04 -05002073 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002074 continue;
2075
2076 /* do this last, since it is the most expensive */
2077 if (glob) {
2078 kallsyms_lookup(entry->ip, NULL, NULL,
2079 NULL, str);
2080 if (!ftrace_match(str, glob, len, type))
2081 continue;
2082 }
2083
2084 hlist_del(&entry->node);
2085 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2086 }
2087 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002088 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002089 mutex_unlock(&ftrace_lock);
2090}
2091
2092void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002093unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002094 void *data)
2095{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002096 __unregister_ftrace_function_probe(glob, ops, data,
2097 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002098}
2099
2100void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002101unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002102{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002103 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002104}
2105
Steven Rostedtb6887d72009-02-17 12:32:04 -05002106void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002107{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002108 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002109}
2110
Steven Rostedtf6180772009-02-14 00:40:25 -05002111static LIST_HEAD(ftrace_commands);
2112static DEFINE_MUTEX(ftrace_cmd_mutex);
2113
2114int register_ftrace_command(struct ftrace_func_command *cmd)
2115{
2116 struct ftrace_func_command *p;
2117 int ret = 0;
2118
2119 mutex_lock(&ftrace_cmd_mutex);
2120 list_for_each_entry(p, &ftrace_commands, list) {
2121 if (strcmp(cmd->name, p->name) == 0) {
2122 ret = -EBUSY;
2123 goto out_unlock;
2124 }
2125 }
2126 list_add(&cmd->list, &ftrace_commands);
2127 out_unlock:
2128 mutex_unlock(&ftrace_cmd_mutex);
2129
2130 return ret;
2131}
2132
2133int unregister_ftrace_command(struct ftrace_func_command *cmd)
2134{
2135 struct ftrace_func_command *p, *n;
2136 int ret = -ENODEV;
2137
2138 mutex_lock(&ftrace_cmd_mutex);
2139 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2140 if (strcmp(cmd->name, p->name) == 0) {
2141 ret = 0;
2142 list_del_init(&p->list);
2143 goto out_unlock;
2144 }
2145 }
2146 out_unlock:
2147 mutex_unlock(&ftrace_cmd_mutex);
2148
2149 return ret;
2150}
2151
Steven Rostedt64e7c442009-02-13 17:08:48 -05002152static int ftrace_process_regex(char *buff, int len, int enable)
2153{
Steven Rostedtf6180772009-02-14 00:40:25 -05002154 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002155 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002156 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002157
2158 func = strsep(&next, ":");
2159
2160 if (!next) {
Li Zefan311d16d2009-12-08 11:15:11 +08002161 if (ftrace_match_records(func, len, enable))
2162 return 0;
2163 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002164 }
2165
Steven Rostedtf6180772009-02-14 00:40:25 -05002166 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002167
2168 command = strsep(&next, ":");
2169
Steven Rostedtf6180772009-02-14 00:40:25 -05002170 mutex_lock(&ftrace_cmd_mutex);
2171 list_for_each_entry(p, &ftrace_commands, list) {
2172 if (strcmp(p->name, command) == 0) {
2173 ret = p->func(func, command, next, enable);
2174 goto out_unlock;
2175 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002176 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002177 out_unlock:
2178 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002179
Steven Rostedtf6180772009-02-14 00:40:25 -05002180 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002181}
2182
Ingo Molnare309b412008-05-12 21:20:51 +02002183static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002184ftrace_regex_write(struct file *file, const char __user *ubuf,
2185 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002186{
2187 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002188 struct trace_parser *parser;
2189 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002190
Li Zefan4ba79782009-09-22 13:52:20 +08002191 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002192 return 0;
2193
Steven Rostedt41c52c02008-05-22 11:46:33 -04002194 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002195
2196 if (file->f_mode & FMODE_READ) {
2197 struct seq_file *m = file->private_data;
2198 iter = m->private;
2199 } else
2200 iter = file->private_data;
2201
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002202 parser = &iter->parser;
2203 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002204
Li Zefan4ba79782009-09-22 13:52:20 +08002205 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002206 !trace_parser_cont(parser)) {
2207 ret = ftrace_process_regex(parser->buffer,
2208 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002209 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002210 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002211 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002212 }
2213
Steven Rostedt5072c592008-05-12 21:20:43 +02002214 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002215out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002216 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002217
Steven Rostedt5072c592008-05-12 21:20:43 +02002218 return ret;
2219}
2220
Steven Rostedt41c52c02008-05-22 11:46:33 -04002221static ssize_t
2222ftrace_filter_write(struct file *file, const char __user *ubuf,
2223 size_t cnt, loff_t *ppos)
2224{
2225 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2226}
2227
2228static ssize_t
2229ftrace_notrace_write(struct file *file, const char __user *ubuf,
2230 size_t cnt, loff_t *ppos)
2231{
2232 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2233}
2234
2235static void
2236ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2237{
2238 if (unlikely(ftrace_disabled))
2239 return;
2240
2241 mutex_lock(&ftrace_regex_lock);
2242 if (reset)
2243 ftrace_filter_reset(enable);
2244 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002245 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002246 mutex_unlock(&ftrace_regex_lock);
2247}
2248
Steven Rostedt77a2b372008-05-12 21:20:45 +02002249/**
2250 * ftrace_set_filter - set a function to filter on in ftrace
2251 * @buf - the string that holds the function filter text.
2252 * @len - the length of the string.
2253 * @reset - non zero to reset all filters before applying this filter.
2254 *
2255 * Filters denote which functions should be enabled when tracing is enabled.
2256 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2257 */
Ingo Molnare309b412008-05-12 21:20:51 +02002258void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002259{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002260 ftrace_set_regex(buf, len, reset, 1);
2261}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002262
Steven Rostedt41c52c02008-05-22 11:46:33 -04002263/**
2264 * ftrace_set_notrace - set a function to not trace in ftrace
2265 * @buf - the string that holds the function notrace text.
2266 * @len - the length of the string.
2267 * @reset - non zero to reset all filters before applying this filter.
2268 *
2269 * Notrace Filters denote which functions should not be enabled when tracing
2270 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2271 * for tracing.
2272 */
2273void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2274{
2275 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002276}
2277
Steven Rostedt2af15d62009-05-28 13:37:24 -04002278/*
2279 * command line interface to allow users to set filters on boot up.
2280 */
2281#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2282static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2283static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2284
2285static int __init set_ftrace_notrace(char *str)
2286{
2287 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2288 return 1;
2289}
2290__setup("ftrace_notrace=", set_ftrace_notrace);
2291
2292static int __init set_ftrace_filter(char *str)
2293{
2294 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2295 return 1;
2296}
2297__setup("ftrace_filter=", set_ftrace_filter);
2298
Stefan Assmann369bc182009-10-12 22:17:21 +02002299#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002300static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002301static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2302
Stefan Assmann369bc182009-10-12 22:17:21 +02002303static int __init set_graph_function(char *str)
2304{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002305 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002306 return 1;
2307}
2308__setup("ftrace_graph_filter=", set_graph_function);
2309
2310static void __init set_ftrace_early_graph(char *buf)
2311{
2312 int ret;
2313 char *func;
2314
2315 while (buf) {
2316 func = strsep(&buf, ",");
2317 /* we allow only one expression at a time */
2318 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2319 func);
2320 if (ret)
2321 printk(KERN_DEBUG "ftrace: function %s not "
2322 "traceable\n", func);
2323 }
2324}
2325#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2326
Steven Rostedt2af15d62009-05-28 13:37:24 -04002327static void __init set_ftrace_early_filter(char *buf, int enable)
2328{
2329 char *func;
2330
2331 while (buf) {
2332 func = strsep(&buf, ",");
2333 ftrace_set_regex(func, strlen(func), 0, enable);
2334 }
2335}
2336
2337static void __init set_ftrace_early_filters(void)
2338{
2339 if (ftrace_filter_buf[0])
2340 set_ftrace_early_filter(ftrace_filter_buf, 1);
2341 if (ftrace_notrace_buf[0])
2342 set_ftrace_early_filter(ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002343#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2344 if (ftrace_graph_buf[0])
2345 set_ftrace_early_graph(ftrace_graph_buf);
2346#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002347}
2348
Ingo Molnare309b412008-05-12 21:20:51 +02002349static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002350ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002351{
2352 struct seq_file *m = (struct seq_file *)file->private_data;
2353 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002354 struct trace_parser *parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02002355
Steven Rostedt41c52c02008-05-22 11:46:33 -04002356 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002357 if (file->f_mode & FMODE_READ) {
2358 iter = m->private;
2359
2360 seq_release(inode, file);
2361 } else
2362 iter = file->private_data;
2363
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002364 parser = &iter->parser;
2365 if (trace_parser_loaded(parser)) {
2366 parser->buffer[parser->idx] = 0;
2367 ftrace_match_records(parser->buffer, parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002368 }
2369
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002370 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002371 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002372 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002373 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002374
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002375 trace_parser_put(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002376 kfree(iter);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002377
Steven Rostedt41c52c02008-05-22 11:46:33 -04002378 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002379 return 0;
2380}
2381
Steven Rostedt41c52c02008-05-22 11:46:33 -04002382static int
2383ftrace_filter_release(struct inode *inode, struct file *file)
2384{
2385 return ftrace_regex_release(inode, file, 1);
2386}
2387
2388static int
2389ftrace_notrace_release(struct inode *inode, struct file *file)
2390{
2391 return ftrace_regex_release(inode, file, 0);
2392}
2393
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002394static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002395 .open = ftrace_avail_open,
2396 .read = seq_read,
2397 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002398 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002399};
2400
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002401static const struct file_operations ftrace_failures_fops = {
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302402 .open = ftrace_failures_open,
2403 .read = seq_read,
2404 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002405 .release = seq_release_private,
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302406};
2407
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002408static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002409 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002410 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002411 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002412 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002413 .release = ftrace_filter_release,
2414};
2415
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002416static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002417 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002418 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002419 .write = ftrace_notrace_write,
2420 .llseek = ftrace_regex_lseek,
2421 .release = ftrace_notrace_release,
2422};
2423
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002424#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2425
2426static DEFINE_MUTEX(graph_lock);
2427
2428int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002429int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002430unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2431
2432static void *
Li Zefan85951842009-06-24 09:54:00 +08002433__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002434{
Li Zefan85951842009-06-24 09:54:00 +08002435 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002436 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002437 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002438}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002439
Li Zefan85951842009-06-24 09:54:00 +08002440static void *
2441g_next(struct seq_file *m, void *v, loff_t *pos)
2442{
2443 (*pos)++;
2444 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002445}
2446
2447static void *g_start(struct seq_file *m, loff_t *pos)
2448{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002449 mutex_lock(&graph_lock);
2450
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002451 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002452 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002453 return (void *)1;
2454
Li Zefan85951842009-06-24 09:54:00 +08002455 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002456}
2457
2458static void g_stop(struct seq_file *m, void *p)
2459{
2460 mutex_unlock(&graph_lock);
2461}
2462
2463static int g_show(struct seq_file *m, void *v)
2464{
2465 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002466
2467 if (!ptr)
2468 return 0;
2469
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002470 if (ptr == (unsigned long *)1) {
2471 seq_printf(m, "#### all functions enabled ####\n");
2472 return 0;
2473 }
2474
Steven Rostedtb375a112009-09-17 00:05:58 -04002475 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002476
2477 return 0;
2478}
2479
James Morris88e9d342009-09-22 16:43:43 -07002480static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002481 .start = g_start,
2482 .next = g_next,
2483 .stop = g_stop,
2484 .show = g_show,
2485};
2486
2487static int
2488ftrace_graph_open(struct inode *inode, struct file *file)
2489{
2490 int ret = 0;
2491
2492 if (unlikely(ftrace_disabled))
2493 return -ENODEV;
2494
2495 mutex_lock(&graph_lock);
2496 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002497 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002498 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002499 ftrace_graph_count = 0;
2500 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2501 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002502 mutex_unlock(&graph_lock);
2503
Li Zefana4ec5e02009-09-18 14:06:28 +08002504 if (file->f_mode & FMODE_READ)
2505 ret = seq_open(file, &ftrace_graph_seq_ops);
2506
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002507 return ret;
2508}
2509
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002510static int
Li Zefan87827112009-07-23 11:29:11 +08002511ftrace_graph_release(struct inode *inode, struct file *file)
2512{
2513 if (file->f_mode & FMODE_READ)
2514 seq_release(inode, file);
2515 return 0;
2516}
2517
2518static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002519ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002520{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002521 struct dyn_ftrace *rec;
2522 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002523 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002524 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002525 int type, not;
2526 char *search;
2527 bool exists;
2528 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002529
2530 if (ftrace_disabled)
2531 return -ENODEV;
2532
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002533 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002534 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002535 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2536 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002537
2538 search_len = strlen(search);
2539
Steven Rostedt52baf112009-02-14 01:15:39 -05002540 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05002541 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002542
Steven Rostedt265c8312009-02-13 12:43:56 -05002543 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2544 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002545
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002546 if (ftrace_match_record(rec, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002547 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002548 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002549 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002550 if (array[i] == rec->ip) {
2551 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002552 break;
2553 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002554 }
2555
2556 if (!not) {
2557 fail = 0;
2558 if (!exists) {
2559 array[(*idx)++] = rec->ip;
2560 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2561 goto out;
2562 }
2563 } else {
2564 if (exists) {
2565 array[i] = array[--(*idx)];
2566 array[*idx] = 0;
2567 fail = 0;
2568 }
2569 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002570 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002571 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002572out:
Steven Rostedt52baf112009-02-14 01:15:39 -05002573 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002574
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002575 if (fail)
2576 return -EINVAL;
2577
2578 ftrace_graph_filter_enabled = 1;
2579 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002580}
2581
2582static ssize_t
2583ftrace_graph_write(struct file *file, const char __user *ubuf,
2584 size_t cnt, loff_t *ppos)
2585{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002586 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08002587 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002588
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002589 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002590 return 0;
2591
2592 mutex_lock(&graph_lock);
2593
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002594 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2595 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08002596 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002597 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002598
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002599 read = trace_get_user(&parser, ubuf, cnt, ppos);
2600
Li Zefan4ba79782009-09-22 13:52:20 +08002601 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002602 parser.buffer[parser.idx] = 0;
2603
2604 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08002605 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002606 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002607 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08002608 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002609 }
2610
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002611 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08002612
2613out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002614 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08002615out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002616 mutex_unlock(&graph_lock);
2617
2618 return ret;
2619}
2620
2621static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08002622 .open = ftrace_graph_open,
2623 .read = seq_read,
2624 .write = ftrace_graph_write,
2625 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002626};
2627#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2628
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002629static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002630{
Steven Rostedt5072c592008-05-12 21:20:43 +02002631
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002632 trace_create_file("available_filter_functions", 0444,
2633 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002634
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002635 trace_create_file("failures", 0444,
2636 d_tracer, NULL, &ftrace_failures_fops);
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302637
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002638 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2639 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002640
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002641 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002642 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002643
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002644#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002645 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002646 NULL,
2647 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002648#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2649
Steven Rostedt5072c592008-05-12 21:20:43 +02002650 return 0;
2651}
2652
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002653static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08002654 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002655 unsigned long *end)
2656{
2657 unsigned long *p;
2658 unsigned long addr;
2659 unsigned long flags;
2660
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002661 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002662 p = start;
2663 while (p < end) {
2664 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002665 /*
2666 * Some architecture linkers will pad between
2667 * the different mcount_loc sections of different
2668 * object files to satisfy alignments.
2669 * Skip any NULL pointers.
2670 */
2671 if (!addr)
2672 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002673 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002674 }
2675
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002676 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002677 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08002678 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002679 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002680 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002681
2682 return 0;
2683}
2684
Steven Rostedt93eb6772009-04-15 13:24:06 -04002685#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002686void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002687{
2688 struct dyn_ftrace *rec;
2689 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04002690
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002691 if (ftrace_disabled)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002692 return;
2693
2694 mutex_lock(&ftrace_lock);
2695 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002696 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04002697 /*
2698 * rec->ip is changed in ftrace_free_rec()
2699 * It should not between s and e if record was freed.
2700 */
2701 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2702 ftrace_free_rec(rec);
2703 }
2704 } while_for_each_ftrace_rec();
2705 mutex_unlock(&ftrace_lock);
2706}
2707
2708static void ftrace_init_module(struct module *mod,
2709 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002710{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002711 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002712 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002713 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002714}
2715
Steven Rostedt93eb6772009-04-15 13:24:06 -04002716static int ftrace_module_notify(struct notifier_block *self,
2717 unsigned long val, void *data)
2718{
2719 struct module *mod = data;
2720
2721 switch (val) {
2722 case MODULE_STATE_COMING:
2723 ftrace_init_module(mod, mod->ftrace_callsites,
2724 mod->ftrace_callsites +
2725 mod->num_ftrace_callsites);
2726 break;
2727 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002728 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04002729 break;
2730 }
2731
2732 return 0;
2733}
2734#else
2735static int ftrace_module_notify(struct notifier_block *self,
2736 unsigned long val, void *data)
2737{
2738 return 0;
2739}
2740#endif /* CONFIG_MODULES */
2741
2742struct notifier_block ftrace_module_nb = {
2743 .notifier_call = ftrace_module_notify,
2744 .priority = 0,
2745};
2746
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002747extern unsigned long __start_mcount_loc[];
2748extern unsigned long __stop_mcount_loc[];
2749
2750void __init ftrace_init(void)
2751{
2752 unsigned long count, addr, flags;
2753 int ret;
2754
2755 /* Keep the ftrace pointer to the stub */
2756 addr = (unsigned long)ftrace_stub;
2757
2758 local_irq_save(flags);
2759 ftrace_dyn_arch_init(&addr);
2760 local_irq_restore(flags);
2761
2762 /* ftrace_dyn_arch_init places the return code in addr */
2763 if (addr)
2764 goto failed;
2765
2766 count = __stop_mcount_loc - __start_mcount_loc;
2767
2768 ret = ftrace_dyn_table_alloc(count);
2769 if (ret)
2770 goto failed;
2771
2772 last_ftrace_enabled = ftrace_enabled = 1;
2773
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002774 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08002775 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002776 __stop_mcount_loc);
2777
Steven Rostedt93eb6772009-04-15 13:24:06 -04002778 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002779 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002780 pr_warning("Failed to register trace ftrace module notifier\n");
2781
Steven Rostedt2af15d62009-05-28 13:37:24 -04002782 set_ftrace_early_filters();
2783
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002784 return;
2785 failed:
2786 ftrace_disabled = 1;
2787}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002788
Steven Rostedt3d083392008-05-12 21:20:42 +02002789#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002790
2791static int __init ftrace_nodyn_init(void)
2792{
2793 ftrace_enabled = 1;
2794 return 0;
2795}
2796device_initcall(ftrace_nodyn_init);
2797
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002798static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2799static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002800/* Keep as macros so we do not need to define the commands */
2801# define ftrace_startup(command) do { } while (0)
2802# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002803# define ftrace_startup_sysctl() do { } while (0)
2804# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002805#endif /* CONFIG_DYNAMIC_FTRACE */
2806
Steven Rostedte32d8952008-12-04 00:26:41 -05002807static void clear_ftrace_swapper(void)
2808{
2809 struct task_struct *p;
2810 int cpu;
2811
2812 get_online_cpus();
2813 for_each_online_cpu(cpu) {
2814 p = idle_task(cpu);
2815 clear_tsk_trace_trace(p);
2816 }
2817 put_online_cpus();
2818}
2819
2820static void set_ftrace_swapper(void)
2821{
2822 struct task_struct *p;
2823 int cpu;
2824
2825 get_online_cpus();
2826 for_each_online_cpu(cpu) {
2827 p = idle_task(cpu);
2828 set_tsk_trace_trace(p);
2829 }
2830 put_online_cpus();
2831}
2832
2833static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002834{
2835 struct task_struct *p;
2836
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002837 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002838 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002839 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002840 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002841 rcu_read_unlock();
2842
Steven Rostedte32d8952008-12-04 00:26:41 -05002843 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002844}
2845
Steven Rostedte32d8952008-12-04 00:26:41 -05002846static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002847{
2848 struct task_struct *p;
2849
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002850 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002851 do_each_pid_task(pid, PIDTYPE_PID, p) {
2852 set_tsk_trace_trace(p);
2853 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002854 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002855}
2856
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002857static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002858{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002859 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002860 clear_ftrace_swapper();
2861 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002862 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05002863}
2864
2865static void set_ftrace_pid_task(struct pid *pid)
2866{
2867 if (pid == ftrace_swapper_pid)
2868 set_ftrace_swapper();
2869 else
2870 set_ftrace_pid(pid);
2871}
2872
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002873static int ftrace_pid_add(int p)
2874{
2875 struct pid *pid;
2876 struct ftrace_pid *fpid;
2877 int ret = -EINVAL;
2878
2879 mutex_lock(&ftrace_lock);
2880
2881 if (!p)
2882 pid = ftrace_swapper_pid;
2883 else
2884 pid = find_get_pid(p);
2885
2886 if (!pid)
2887 goto out;
2888
2889 ret = 0;
2890
2891 list_for_each_entry(fpid, &ftrace_pids, list)
2892 if (fpid->pid == pid)
2893 goto out_put;
2894
2895 ret = -ENOMEM;
2896
2897 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2898 if (!fpid)
2899 goto out_put;
2900
2901 list_add(&fpid->list, &ftrace_pids);
2902 fpid->pid = pid;
2903
2904 set_ftrace_pid_task(pid);
2905
2906 ftrace_update_pid_func();
2907 ftrace_startup_enable(0);
2908
2909 mutex_unlock(&ftrace_lock);
2910 return 0;
2911
2912out_put:
2913 if (pid != ftrace_swapper_pid)
2914 put_pid(pid);
2915
2916out:
2917 mutex_unlock(&ftrace_lock);
2918 return ret;
2919}
2920
2921static void ftrace_pid_reset(void)
2922{
2923 struct ftrace_pid *fpid, *safe;
2924
2925 mutex_lock(&ftrace_lock);
2926 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2927 struct pid *pid = fpid->pid;
2928
2929 clear_ftrace_pid_task(pid);
2930
2931 list_del(&fpid->list);
2932 kfree(fpid);
2933 }
2934
2935 ftrace_update_pid_func();
2936 ftrace_startup_enable(0);
2937
2938 mutex_unlock(&ftrace_lock);
2939}
2940
2941static void *fpid_start(struct seq_file *m, loff_t *pos)
2942{
2943 mutex_lock(&ftrace_lock);
2944
2945 if (list_empty(&ftrace_pids) && (!*pos))
2946 return (void *) 1;
2947
2948 return seq_list_start(&ftrace_pids, *pos);
2949}
2950
2951static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2952{
2953 if (v == (void *)1)
2954 return NULL;
2955
2956 return seq_list_next(v, &ftrace_pids, pos);
2957}
2958
2959static void fpid_stop(struct seq_file *m, void *p)
2960{
2961 mutex_unlock(&ftrace_lock);
2962}
2963
2964static int fpid_show(struct seq_file *m, void *v)
2965{
2966 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2967
2968 if (v == (void *)1) {
2969 seq_printf(m, "no pid\n");
2970 return 0;
2971 }
2972
2973 if (fpid->pid == ftrace_swapper_pid)
2974 seq_printf(m, "swapper tasks\n");
2975 else
2976 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2977
2978 return 0;
2979}
2980
2981static const struct seq_operations ftrace_pid_sops = {
2982 .start = fpid_start,
2983 .next = fpid_next,
2984 .stop = fpid_stop,
2985 .show = fpid_show,
2986};
2987
2988static int
2989ftrace_pid_open(struct inode *inode, struct file *file)
2990{
2991 int ret = 0;
2992
2993 if ((file->f_mode & FMODE_WRITE) &&
2994 (file->f_flags & O_TRUNC))
2995 ftrace_pid_reset();
2996
2997 if (file->f_mode & FMODE_READ)
2998 ret = seq_open(file, &ftrace_pid_sops);
2999
3000 return ret;
3001}
3002
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003003static ssize_t
3004ftrace_pid_write(struct file *filp, const char __user *ubuf,
3005 size_t cnt, loff_t *ppos)
3006{
Ingo Molnar457dc922009-11-23 11:03:28 +01003007 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003008 long val;
3009 int ret;
3010
3011 if (cnt >= sizeof(buf))
3012 return -EINVAL;
3013
3014 if (copy_from_user(&buf, ubuf, cnt))
3015 return -EFAULT;
3016
3017 buf[cnt] = 0;
3018
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003019 /*
3020 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3021 * to clean the filter quietly.
3022 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003023 tmp = strstrip(buf);
3024 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003025 return 1;
3026
Ingo Molnar457dc922009-11-23 11:03:28 +01003027 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003028 if (ret < 0)
3029 return ret;
3030
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003031 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003032
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003033 return ret ? ret : cnt;
3034}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003035
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003036static int
3037ftrace_pid_release(struct inode *inode, struct file *file)
3038{
3039 if (file->f_mode & FMODE_READ)
3040 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003041
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003042 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003043}
3044
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003045static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003046 .open = ftrace_pid_open,
3047 .write = ftrace_pid_write,
3048 .read = seq_read,
3049 .llseek = seq_lseek,
3050 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003051};
3052
3053static __init int ftrace_init_debugfs(void)
3054{
3055 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003056
3057 d_tracer = tracing_init_dentry();
3058 if (!d_tracer)
3059 return 0;
3060
3061 ftrace_init_dyn_debugfs(d_tracer);
3062
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003063 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3064 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003065
3066 ftrace_profile_debugfs(d_tracer);
3067
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003068 return 0;
3069}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003070fs_initcall(ftrace_init_debugfs);
3071
Steven Rostedt3d083392008-05-12 21:20:42 +02003072/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003073 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003074 *
3075 * This function should be used by panic code. It stops ftrace
3076 * but in a not so nice way. If you need to simply kill ftrace
3077 * from a non-atomic section, use ftrace_kill.
3078 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003079void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003080{
3081 ftrace_disabled = 1;
3082 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003083 clear_ftrace_function();
3084}
3085
3086/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003087 * register_ftrace_function - register a function for profiling
3088 * @ops - ops structure that holds the function for profiling.
3089 *
3090 * Register a function to be called by all functions in the
3091 * kernel.
3092 *
3093 * Note: @ops->func and all the functions it calls must be labeled
3094 * with "notrace", otherwise it will go into a
3095 * recursive loop.
3096 */
3097int register_ftrace_function(struct ftrace_ops *ops)
3098{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003099 int ret;
3100
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003101 if (unlikely(ftrace_disabled))
3102 return -1;
3103
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003104 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003105
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003106 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003107 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003108
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003109 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003110 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003111}
3112
3113/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003114 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003115 * @ops - ops structure that holds the function to unregister
3116 *
3117 * Unregister a function that was added to be called by ftrace profiling.
3118 */
3119int unregister_ftrace_function(struct ftrace_ops *ops)
3120{
3121 int ret;
3122
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003123 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003124 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003125 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003126 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003127
3128 return ret;
3129}
3130
Ingo Molnare309b412008-05-12 21:20:51 +02003131int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003132ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003133 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003134 loff_t *ppos)
3135{
3136 int ret;
3137
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003138 if (unlikely(ftrace_disabled))
3139 return -ENODEV;
3140
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003141 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003142
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003143 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003144
Li Zefana32c7762009-06-26 16:55:51 +08003145 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003146 goto out;
3147
Li Zefana32c7762009-06-26 16:55:51 +08003148 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003149
3150 if (ftrace_enabled) {
3151
3152 ftrace_startup_sysctl();
3153
3154 /* we are starting ftrace again */
3155 if (ftrace_list != &ftrace_list_end) {
3156 if (ftrace_list->next == &ftrace_list_end)
3157 ftrace_trace_function = ftrace_list->func;
3158 else
3159 ftrace_trace_function = ftrace_list_func;
3160 }
3161
3162 } else {
3163 /* stopping ftrace calls (just send to ftrace_stub) */
3164 ftrace_trace_function = ftrace_stub;
3165
3166 ftrace_shutdown_sysctl();
3167 }
3168
3169 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003170 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003171 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003172}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003173
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003174#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003175
Steven Rostedt597af812009-04-03 15:24:12 -04003176static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003177static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003178
Steven Rostedte49dc192008-12-02 23:50:05 -05003179int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3180{
3181 return 0;
3182}
3183
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003184/* The callbacks that hook a function */
3185trace_func_graph_ret_t ftrace_graph_return =
3186 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003187trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003188
3189/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3190static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3191{
3192 int i;
3193 int ret = 0;
3194 unsigned long flags;
3195 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3196 struct task_struct *g, *t;
3197
3198 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3199 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3200 * sizeof(struct ftrace_ret_stack),
3201 GFP_KERNEL);
3202 if (!ret_stack_list[i]) {
3203 start = 0;
3204 end = i;
3205 ret = -ENOMEM;
3206 goto free;
3207 }
3208 }
3209
3210 read_lock_irqsave(&tasklist_lock, flags);
3211 do_each_thread(g, t) {
3212 if (start == end) {
3213 ret = -EAGAIN;
3214 goto unlock;
3215 }
3216
3217 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003218 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003219 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003220 t->curr_ret_stack = -1;
3221 /* Make sure the tasks see the -1 first: */
3222 smp_wmb();
3223 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003224 }
3225 } while_each_thread(g, t);
3226
3227unlock:
3228 read_unlock_irqrestore(&tasklist_lock, flags);
3229free:
3230 for (i = start; i < end; i++)
3231 kfree(ret_stack_list[i]);
3232 return ret;
3233}
3234
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003235static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04003236ftrace_graph_probe_sched_switch(void *ignore,
3237 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003238{
3239 unsigned long long timestamp;
3240 int index;
3241
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003242 /*
3243 * Does the user want to count the time a function was asleep.
3244 * If so, do not update the time stamps.
3245 */
3246 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3247 return;
3248
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003249 timestamp = trace_clock_local();
3250
3251 prev->ftrace_timestamp = timestamp;
3252
3253 /* only process tasks that we timestamped */
3254 if (!next->ftrace_timestamp)
3255 return;
3256
3257 /*
3258 * Update all the counters in next to make up for the
3259 * time next was sleeping.
3260 */
3261 timestamp -= next->ftrace_timestamp;
3262
3263 for (index = next->curr_ret_stack; index >= 0; index--)
3264 next->ret_stack[index].calltime += timestamp;
3265}
3266
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003267/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003268static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003269{
3270 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003271 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003272
3273 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3274 sizeof(struct ftrace_ret_stack *),
3275 GFP_KERNEL);
3276
3277 if (!ret_stack_list)
3278 return -ENOMEM;
3279
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003280 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003281 for_each_online_cpu(cpu) {
3282 if (!idle_task(cpu)->ret_stack)
3283 ftrace_graph_init_task(idle_task(cpu));
3284 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003285
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003286 do {
3287 ret = alloc_retstack_tasklist(ret_stack_list);
3288 } while (ret == -EAGAIN);
3289
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003290 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04003291 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003292 if (ret)
3293 pr_info("ftrace_graph: Couldn't activate tracepoint"
3294 " probe to kernel_sched_switch\n");
3295 }
3296
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003297 kfree(ret_stack_list);
3298 return ret;
3299}
3300
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003301/*
3302 * Hibernation protection.
3303 * The state of the current task is too much unstable during
3304 * suspend/restore to disk. We want to protect against that.
3305 */
3306static int
3307ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3308 void *unused)
3309{
3310 switch (state) {
3311 case PM_HIBERNATION_PREPARE:
3312 pause_graph_tracing();
3313 break;
3314
3315 case PM_POST_HIBERNATION:
3316 unpause_graph_tracing();
3317 break;
3318 }
3319 return NOTIFY_DONE;
3320}
3321
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003322int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3323 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003324{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003325 int ret = 0;
3326
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003327 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003328
Steven Rostedt05ce5812009-03-24 00:18:31 -04003329 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003330 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003331 ret = -EBUSY;
3332 goto out;
3333 }
3334
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003335 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3336 register_pm_notifier(&ftrace_suspend_notifier);
3337
Steven Rostedt597af812009-04-03 15:24:12 -04003338 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003339 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003340 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003341 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003342 goto out;
3343 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003344
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003345 ftrace_graph_return = retfunc;
3346 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003347
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003348 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003349
3350out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003351 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003352 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003353}
3354
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003355void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003356{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003357 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003358
Steven Rostedt597af812009-04-03 15:24:12 -04003359 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003360 goto out;
3361
Steven Rostedt597af812009-04-03 15:24:12 -04003362 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003363 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003364 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003365 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003366 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04003367 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003368
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003369 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003370 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003371}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003372
3373/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003374void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003375{
Steven Rostedt84047e32009-06-02 16:51:55 -04003376 /* Make sure we do not use the parent ret_stack */
3377 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05003378 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04003379
Steven Rostedt597af812009-04-03 15:24:12 -04003380 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003381 struct ftrace_ret_stack *ret_stack;
3382
3383 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003384 * sizeof(struct ftrace_ret_stack),
3385 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003386 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003387 return;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003388 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003389 atomic_set(&t->trace_overrun, 0);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003390 t->ftrace_timestamp = 0;
Steven Rostedt82310a32009-06-02 12:26:07 -04003391 /* make curr_ret_stack visable before we add the ret_stack */
3392 smp_wmb();
3393 t->ret_stack = ret_stack;
Steven Rostedt84047e32009-06-02 16:51:55 -04003394 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003395}
3396
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003397void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003398{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003399 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3400
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003401 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003402 /* NULL must become visible to IRQs before we free it: */
3403 barrier();
3404
3405 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003406}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003407
3408void ftrace_graph_stop(void)
3409{
3410 ftrace_stop();
3411}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003412#endif