blob: 7cb1f45a1de1ccf61e9cfcf59e0abe0642f12cf2 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
14 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010025#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020026#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020028#include <linux/ctype.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020029#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050030#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080031#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020032
Steven Rostedtad8d75f2009-04-14 19:39:12 -040033#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040034
Abhishek Sagar395a59d2008-06-21 23:47:27 +053035#include <asm/ftrace.h>
Steven Rostedt2af15d62009-05-28 13:37:24 -040036#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053037
Steven Rostedt0706f1c2009-03-23 23:12:58 -040038#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040039#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020040
Steven Rostedt69128962008-10-23 09:33:03 -040041#define FTRACE_WARN_ON(cond) \
42 do { \
43 if (WARN_ON(cond)) \
44 ftrace_kill(); \
45 } while (0)
46
47#define FTRACE_WARN_ON_ONCE(cond) \
48 do { \
49 if (WARN_ON_ONCE(cond)) \
50 ftrace_kill(); \
51 } while (0)
52
Steven Rostedt8fc0c702009-02-16 15:28:00 -050053/* hash bits for specific function selection */
54#define FTRACE_HASH_BITS 7
55#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
56
Steven Rostedt4eebcc82008-05-12 21:20:48 +020057/* ftrace_enabled is a method to turn ftrace on or off */
58int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020059static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020060
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050061/* Quick disabling of function tracer. */
62int function_trace_stop;
63
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040064/* List for set_ftrace_pid's pids. */
65LIST_HEAD(ftrace_pids);
66struct ftrace_pid {
67 struct list_head list;
68 struct pid *pid;
69};
70
Steven Rostedt4eebcc82008-05-12 21:20:48 +020071/*
72 * ftrace_disabled is set when an anomaly is discovered.
73 * ftrace_disabled is much stronger than ftrace_enabled.
74 */
75static int ftrace_disabled __read_mostly;
76
Steven Rostedt52baf112009-02-14 01:15:39 -050077static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020078
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020079static struct ftrace_ops ftrace_list_end __read_mostly =
80{
Steven Rostedtfb9fb012009-03-25 13:26:41 -040081 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020082};
83
84static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
85ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050086ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050087ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020088
Paul E. McKenney3f379b02010-03-05 15:03:25 -080089/*
90 * Traverse the ftrace_list, invoking all entries. The reason that we
91 * can use rcu_dereference_raw() is that elements removed from this list
92 * are simply leaked, so there is no need to interact with a grace-period
93 * mechanism. The rcu_dereference_raw() calls are needed to handle
94 * concurrent insertions into the ftrace_list.
95 *
96 * Silly Alpha and silly pointer-speculation compiler optimizations!
97 */
Ingo Molnarf2252932008-05-22 10:37:48 +020098static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020099{
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800100 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200101
102 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200103 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800104 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200105 };
106}
107
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500108static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
109{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500110 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500111 return;
112
113 ftrace_pid_function(ip, parent_ip);
114}
115
116static void set_ftrace_pid_function(ftrace_func_t func)
117{
118 /* do not set ftrace_pid_function to itself! */
119 if (func != ftrace_pid_func)
120 ftrace_pid_function = func;
121}
122
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200123/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200124 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200125 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200126 * This NULLs the ftrace function and in essence stops
127 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200128 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200129void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200130{
Steven Rostedt3d083392008-05-12 21:20:42 +0200131 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500132 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500133 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200134}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200135
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500136#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
137/*
138 * For those archs that do not test ftrace_trace_stop in their
139 * mcount call site, we need to do it from C.
140 */
141static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
142{
143 if (function_trace_stop)
144 return;
145
146 __ftrace_trace_function(ip, parent_ip);
147}
148#endif
149
Ingo Molnare309b412008-05-12 21:20:51 +0200150static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200151{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200152 ops->next = ftrace_list;
153 /*
154 * We are entering ops into the ftrace_list but another
155 * CPU might be walking that list. We need to make sure
156 * the ops->next pointer is valid before another CPU sees
157 * the ops pointer included into the ftrace_list.
158 */
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800159 rcu_assign_pointer(ftrace_list, ops);
Steven Rostedt3d083392008-05-12 21:20:42 +0200160
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200161 if (ftrace_enabled) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500162 ftrace_func_t func;
163
164 if (ops->next == &ftrace_list_end)
165 func = ops->func;
166 else
167 func = ftrace_list_func;
168
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400169 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500170 set_ftrace_pid_function(func);
171 func = ftrace_pid_func;
172 }
173
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200174 /*
175 * For one func, simply call it directly.
176 * For more than one func, call the chain.
177 */
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500178#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500179 ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500180#else
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500181 __ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500182 ftrace_trace_function = ftrace_test_stop_func;
183#endif
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200184 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200185
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200186 return 0;
187}
188
Ingo Molnare309b412008-05-12 21:20:51 +0200189static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200190{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200191 struct ftrace_ops **p;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200192
193 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200194 * If we are removing the last function, then simply point
195 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200196 */
197 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
198 ftrace_trace_function = ftrace_stub;
199 ftrace_list = &ftrace_list_end;
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500200 return 0;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200201 }
202
203 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
204 if (*p == ops)
205 break;
206
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500207 if (*p != ops)
208 return -1;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200209
210 *p = (*p)->next;
211
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200212 if (ftrace_enabled) {
213 /* If we only have one func left, then call that directly */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500214 if (ftrace_list->next == &ftrace_list_end) {
215 ftrace_func_t func = ftrace_list->func;
216
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400217 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500218 set_ftrace_pid_function(func);
219 func = ftrace_pid_func;
220 }
221#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
222 ftrace_trace_function = func;
223#else
224 __ftrace_trace_function = func;
225#endif
226 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200227 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200228
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500229 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200230}
231
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500232static void ftrace_update_pid_func(void)
233{
234 ftrace_func_t func;
235
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500236 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900237 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500238
Matt Fleming33974092009-09-28 16:43:01 +0100239#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500240 func = ftrace_trace_function;
Matt Fleming33974092009-09-28 16:43:01 +0100241#else
242 func = __ftrace_trace_function;
243#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500244
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400245 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500246 set_ftrace_pid_function(func);
247 func = ftrace_pid_func;
248 } else {
Liming Wang66eafeb2008-12-02 10:33:08 +0800249 if (func == ftrace_pid_func)
250 func = ftrace_pid_function;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500251 }
252
253#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
254 ftrace_trace_function = func;
255#else
256 __ftrace_trace_function = func;
257#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500258}
259
Steven Rostedt493762f2009-03-23 17:12:36 -0400260#ifdef CONFIG_FUNCTION_PROFILER
261struct ftrace_profile {
262 struct hlist_node node;
263 unsigned long ip;
264 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400265#ifdef CONFIG_FUNCTION_GRAPH_TRACER
266 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400267 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400268#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400269};
270
271struct ftrace_profile_page {
272 struct ftrace_profile_page *next;
273 unsigned long index;
274 struct ftrace_profile records[];
275};
276
Steven Rostedtcafb1682009-03-24 20:50:39 -0400277struct ftrace_profile_stat {
278 atomic_t disabled;
279 struct hlist_head *hash;
280 struct ftrace_profile_page *pages;
281 struct ftrace_profile_page *start;
282 struct tracer_stat stat;
283};
284
Steven Rostedt493762f2009-03-23 17:12:36 -0400285#define PROFILE_RECORDS_SIZE \
286 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
287
288#define PROFILES_PER_PAGE \
289 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
290
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400291static int ftrace_profile_bits __read_mostly;
292static int ftrace_profile_enabled __read_mostly;
293
294/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400295static DEFINE_MUTEX(ftrace_profile_lock);
296
Steven Rostedtcafb1682009-03-24 20:50:39 -0400297static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400298
299#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
300
Steven Rostedt493762f2009-03-23 17:12:36 -0400301static void *
302function_stat_next(void *v, int idx)
303{
304 struct ftrace_profile *rec = v;
305 struct ftrace_profile_page *pg;
306
307 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
308
309 again:
Li Zefan0296e422009-06-26 11:15:37 +0800310 if (idx != 0)
311 rec++;
312
Steven Rostedt493762f2009-03-23 17:12:36 -0400313 if ((void *)rec >= (void *)&pg->records[pg->index]) {
314 pg = pg->next;
315 if (!pg)
316 return NULL;
317 rec = &pg->records[0];
318 if (!rec->counter)
319 goto again;
320 }
321
322 return rec;
323}
324
325static void *function_stat_start(struct tracer_stat *trace)
326{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400327 struct ftrace_profile_stat *stat =
328 container_of(trace, struct ftrace_profile_stat, stat);
329
330 if (!stat || !stat->start)
331 return NULL;
332
333 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400334}
335
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400336#ifdef CONFIG_FUNCTION_GRAPH_TRACER
337/* function graph compares on total time */
338static int function_stat_cmp(void *p1, void *p2)
339{
340 struct ftrace_profile *a = p1;
341 struct ftrace_profile *b = p2;
342
343 if (a->time < b->time)
344 return -1;
345 if (a->time > b->time)
346 return 1;
347 else
348 return 0;
349}
350#else
351/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400352static int function_stat_cmp(void *p1, void *p2)
353{
354 struct ftrace_profile *a = p1;
355 struct ftrace_profile *b = p2;
356
357 if (a->counter < b->counter)
358 return -1;
359 if (a->counter > b->counter)
360 return 1;
361 else
362 return 0;
363}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400364#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400365
366static int function_stat_headers(struct seq_file *m)
367{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400368#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400369 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400370 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400371 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400372 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400373#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400374 seq_printf(m, " Function Hit\n"
375 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400376#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400377 return 0;
378}
379
380static int function_stat_show(struct seq_file *m, void *v)
381{
382 struct ftrace_profile *rec = v;
383 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800384 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400385#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400386 static struct trace_seq s;
387 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400388 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400389#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800390 mutex_lock(&ftrace_profile_lock);
391
392 /* we raced with function_profile_reset() */
393 if (unlikely(rec->counter == 0)) {
394 ret = -EBUSY;
395 goto out;
396 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400397
398 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400399 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400400
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400401#ifdef CONFIG_FUNCTION_GRAPH_TRACER
402 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400403 avg = rec->time;
404 do_div(avg, rec->counter);
405
Chase Douglase330b3b2010-04-26 14:02:05 -0400406 /* Sample standard deviation (s^2) */
407 if (rec->counter <= 1)
408 stddev = 0;
409 else {
410 stddev = rec->time_squared - rec->counter * avg * avg;
411 /*
412 * Divide only 1000 for ns^2 -> us^2 conversion.
413 * trace_print_graph_duration will divide 1000 again.
414 */
415 do_div(stddev, (rec->counter - 1) * 1000);
416 }
417
Steven Rostedt34886c82009-03-25 21:00:47 -0400418 trace_seq_init(&s);
419 trace_print_graph_duration(rec->time, &s);
420 trace_seq_puts(&s, " ");
421 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400422 trace_seq_puts(&s, " ");
423 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400424 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400425#endif
426 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800427out:
428 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400429
Li Zefan3aaba202010-08-23 16:50:12 +0800430 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400431}
432
Steven Rostedtcafb1682009-03-24 20:50:39 -0400433static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400434{
435 struct ftrace_profile_page *pg;
436
Steven Rostedtcafb1682009-03-24 20:50:39 -0400437 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400438
439 while (pg) {
440 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
441 pg->index = 0;
442 pg = pg->next;
443 }
444
Steven Rostedtcafb1682009-03-24 20:50:39 -0400445 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400446 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
447}
448
Steven Rostedtcafb1682009-03-24 20:50:39 -0400449int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400450{
451 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400452 int functions;
453 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400454 int i;
455
456 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400457 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400458 return 0;
459
Steven Rostedtcafb1682009-03-24 20:50:39 -0400460 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
461 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400462 return -ENOMEM;
463
Steven Rostedt318e0a72009-03-25 20:06:34 -0400464#ifdef CONFIG_DYNAMIC_FTRACE
465 functions = ftrace_update_tot_cnt;
466#else
467 /*
468 * We do not know the number of functions that exist because
469 * dynamic tracing is what counts them. With past experience
470 * we have around 20K functions. That should be more than enough.
471 * It is highly unlikely we will execute every function in
472 * the kernel.
473 */
474 functions = 20000;
475#endif
476
Steven Rostedtcafb1682009-03-24 20:50:39 -0400477 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400478
Steven Rostedt318e0a72009-03-25 20:06:34 -0400479 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
480
481 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400482 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400483 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400484 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400485 pg = pg->next;
486 }
487
488 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400489
490 out_free:
491 pg = stat->start;
492 while (pg) {
493 unsigned long tmp = (unsigned long)pg;
494
495 pg = pg->next;
496 free_page(tmp);
497 }
498
499 free_page((unsigned long)stat->pages);
500 stat->pages = NULL;
501 stat->start = NULL;
502
503 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400504}
505
Steven Rostedtcafb1682009-03-24 20:50:39 -0400506static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400507{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400508 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400509 int size;
510
Steven Rostedtcafb1682009-03-24 20:50:39 -0400511 stat = &per_cpu(ftrace_profile_stats, cpu);
512
513 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400514 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400515 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400516 return 0;
517 }
518
519 /*
520 * We are profiling all functions, but usually only a few thousand
521 * functions are hit. We'll make a hash of 1024 items.
522 */
523 size = FTRACE_PROFILE_HASH_SIZE;
524
Steven Rostedtcafb1682009-03-24 20:50:39 -0400525 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400526
Steven Rostedtcafb1682009-03-24 20:50:39 -0400527 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400528 return -ENOMEM;
529
Steven Rostedtcafb1682009-03-24 20:50:39 -0400530 if (!ftrace_profile_bits) {
531 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400532
Steven Rostedtcafb1682009-03-24 20:50:39 -0400533 for (; size; size >>= 1)
534 ftrace_profile_bits++;
535 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400536
Steven Rostedt318e0a72009-03-25 20:06:34 -0400537 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400538 if (ftrace_profile_pages_init(stat) < 0) {
539 kfree(stat->hash);
540 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400541 return -ENOMEM;
542 }
543
544 return 0;
545}
546
Steven Rostedtcafb1682009-03-24 20:50:39 -0400547static int ftrace_profile_init(void)
548{
549 int cpu;
550 int ret = 0;
551
552 for_each_online_cpu(cpu) {
553 ret = ftrace_profile_init_cpu(cpu);
554 if (ret)
555 break;
556 }
557
558 return ret;
559}
560
Steven Rostedt493762f2009-03-23 17:12:36 -0400561/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400562static struct ftrace_profile *
563ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400564{
565 struct ftrace_profile *rec;
566 struct hlist_head *hhd;
567 struct hlist_node *n;
568 unsigned long key;
569
570 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400571 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400572
573 if (hlist_empty(hhd))
574 return NULL;
575
576 hlist_for_each_entry_rcu(rec, n, hhd, node) {
577 if (rec->ip == ip)
578 return rec;
579 }
580
581 return NULL;
582}
583
Steven Rostedtcafb1682009-03-24 20:50:39 -0400584static void ftrace_add_profile(struct ftrace_profile_stat *stat,
585 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400586{
587 unsigned long key;
588
589 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400590 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400591}
592
Steven Rostedt318e0a72009-03-25 20:06:34 -0400593/*
594 * The memory is already allocated, this simply finds a new record to use.
595 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400596static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400597ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400598{
599 struct ftrace_profile *rec = NULL;
600
Steven Rostedt318e0a72009-03-25 20:06:34 -0400601 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400602 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400603 goto out;
604
Steven Rostedt493762f2009-03-23 17:12:36 -0400605 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400606 * Try to find the function again since an NMI
607 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400608 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400609 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400610 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400611 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400612
Steven Rostedtcafb1682009-03-24 20:50:39 -0400613 if (stat->pages->index == PROFILES_PER_PAGE) {
614 if (!stat->pages->next)
615 goto out;
616 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400617 }
618
Steven Rostedtcafb1682009-03-24 20:50:39 -0400619 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400620 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400621 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400622
Steven Rostedt493762f2009-03-23 17:12:36 -0400623 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400624 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400625
626 return rec;
627}
628
Steven Rostedt493762f2009-03-23 17:12:36 -0400629static void
630function_profile_call(unsigned long ip, unsigned long parent_ip)
631{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400632 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400633 struct ftrace_profile *rec;
634 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400635
636 if (!ftrace_profile_enabled)
637 return;
638
Steven Rostedt493762f2009-03-23 17:12:36 -0400639 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400640
641 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400642 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400643 goto out;
644
645 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400646 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400647 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400648 if (!rec)
649 goto out;
650 }
651
652 rec->counter++;
653 out:
654 local_irq_restore(flags);
655}
656
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400657#ifdef CONFIG_FUNCTION_GRAPH_TRACER
658static int profile_graph_entry(struct ftrace_graph_ent *trace)
659{
660 function_profile_call(trace->func, 0);
661 return 1;
662}
663
664static void profile_graph_return(struct ftrace_graph_ret *trace)
665{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400666 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400667 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400668 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400670
671 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400672 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400673 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400674 goto out;
675
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400676 /* If the calltime was zero'd ignore it */
677 if (!trace->calltime)
678 goto out;
679
Steven Rostedta2a16d62009-03-24 23:17:58 -0400680 calltime = trace->rettime - trace->calltime;
681
682 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
683 int index;
684
685 index = trace->depth;
686
687 /* Append this call time to the parent time to subtract */
688 if (index)
689 current->ret_stack[index - 1].subtime += calltime;
690
691 if (current->ret_stack[index].subtime < calltime)
692 calltime -= current->ret_stack[index].subtime;
693 else
694 calltime = 0;
695 }
696
Steven Rostedtcafb1682009-03-24 20:50:39 -0400697 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400698 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400699 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400700 rec->time_squared += calltime * calltime;
701 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400702
Steven Rostedtcafb1682009-03-24 20:50:39 -0400703 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400704 local_irq_restore(flags);
705}
706
707static int register_ftrace_profiler(void)
708{
709 return register_ftrace_graph(&profile_graph_return,
710 &profile_graph_entry);
711}
712
713static void unregister_ftrace_profiler(void)
714{
715 unregister_ftrace_graph();
716}
717#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400718static struct ftrace_ops ftrace_profile_ops __read_mostly =
719{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400720 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400721};
722
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400723static int register_ftrace_profiler(void)
724{
725 return register_ftrace_function(&ftrace_profile_ops);
726}
727
728static void unregister_ftrace_profiler(void)
729{
730 unregister_ftrace_function(&ftrace_profile_ops);
731}
732#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
733
Steven Rostedt493762f2009-03-23 17:12:36 -0400734static ssize_t
735ftrace_profile_write(struct file *filp, const char __user *ubuf,
736 size_t cnt, loff_t *ppos)
737{
738 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400739 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400740 int ret;
741
742 if (cnt >= sizeof(buf))
743 return -EINVAL;
744
745 if (copy_from_user(&buf, ubuf, cnt))
746 return -EFAULT;
747
748 buf[cnt] = 0;
749
750 ret = strict_strtoul(buf, 10, &val);
751 if (ret < 0)
752 return ret;
753
754 val = !!val;
755
756 mutex_lock(&ftrace_profile_lock);
757 if (ftrace_profile_enabled ^ val) {
758 if (val) {
759 ret = ftrace_profile_init();
760 if (ret < 0) {
761 cnt = ret;
762 goto out;
763 }
764
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400765 ret = register_ftrace_profiler();
766 if (ret < 0) {
767 cnt = ret;
768 goto out;
769 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400770 ftrace_profile_enabled = 1;
771 } else {
772 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400773 /*
774 * unregister_ftrace_profiler calls stop_machine
775 * so this acts like an synchronize_sched.
776 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400777 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400778 }
779 }
780 out:
781 mutex_unlock(&ftrace_profile_lock);
782
Jiri Olsacf8517c2009-10-23 19:36:16 -0400783 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400784
785 return cnt;
786}
787
788static ssize_t
789ftrace_profile_read(struct file *filp, char __user *ubuf,
790 size_t cnt, loff_t *ppos)
791{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400792 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400793 int r;
794
795 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
796 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
797}
798
799static const struct file_operations ftrace_profile_fops = {
800 .open = tracing_open_generic,
801 .read = ftrace_profile_read,
802 .write = ftrace_profile_write,
803};
804
Steven Rostedtcafb1682009-03-24 20:50:39 -0400805/* used to initialize the real stat files */
806static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400807 .name = "functions",
808 .stat_start = function_stat_start,
809 .stat_next = function_stat_next,
810 .stat_cmp = function_stat_cmp,
811 .stat_headers = function_stat_headers,
812 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400813};
814
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400815static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400816{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400817 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400818 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400820 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400821 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400822
Steven Rostedtcafb1682009-03-24 20:50:39 -0400823 for_each_possible_cpu(cpu) {
824 stat = &per_cpu(ftrace_profile_stats, cpu);
825
826 /* allocate enough for function name + cpu number */
827 name = kmalloc(32, GFP_KERNEL);
828 if (!name) {
829 /*
830 * The files created are permanent, if something happens
831 * we still do not free memory.
832 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400833 WARN(1,
834 "Could not allocate stat file for cpu %d\n",
835 cpu);
836 return;
837 }
838 stat->stat = function_stats;
839 snprintf(name, 32, "function%d", cpu);
840 stat->stat.name = name;
841 ret = register_stat_tracer(&stat->stat);
842 if (ret) {
843 WARN(1,
844 "Could not register function stat for cpu %d\n",
845 cpu);
846 kfree(name);
847 return;
848 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400849 }
850
851 entry = debugfs_create_file("function_profile_enabled", 0644,
852 d_tracer, NULL, &ftrace_profile_fops);
853 if (!entry)
854 pr_warning("Could not create debugfs "
855 "'function_profile_enabled' entry\n");
856}
857
858#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400859static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400860{
861}
862#endif /* CONFIG_FUNCTION_PROFILER */
863
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100864static struct pid * const ftrace_swapper_pid = &init_struct_pid;
865
Steven Rostedt3d083392008-05-12 21:20:42 +0200866#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100867
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400868#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400869# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400870#endif
871
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500872static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
873
Steven Rostedtb6887d72009-02-17 12:32:04 -0500874struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500875 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500876 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500877 unsigned long flags;
878 unsigned long ip;
879 void *data;
880 struct rcu_head rcu;
881};
882
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200883enum {
884 FTRACE_ENABLE_CALLS = (1 << 0),
885 FTRACE_DISABLE_CALLS = (1 << 1),
886 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
887 FTRACE_ENABLE_MCOUNT = (1 << 3),
888 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500889 FTRACE_START_FUNC_RET = (1 << 5),
890 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200891};
892
Steven Rostedt5072c592008-05-12 21:20:43 +0200893static int ftrace_filtered;
894
Lai Jiangshane94142a2009-03-13 17:51:27 +0800895static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200896
Steven Rostedt41c52c02008-05-22 11:46:33 -0400897static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200898
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200899struct ftrace_page {
900 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500901 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200902 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700903};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200904
905#define ENTRIES_PER_PAGE \
906 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
907
908/* estimate from running different kernels */
909#define NR_TO_INIT 10000
910
911static struct ftrace_page *ftrace_pages_start;
912static struct ftrace_page *ftrace_pages;
913
Steven Rostedt37ad5082008-05-12 21:20:48 +0200914static struct dyn_ftrace *ftrace_free_records;
915
Steven Rostedt265c8312009-02-13 12:43:56 -0500916/*
917 * This is a double for. Do not use 'break' to break out of the loop,
918 * you must use a goto.
919 */
920#define do_for_each_ftrace_rec(pg, rec) \
921 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
922 int _____i; \
923 for (_____i = 0; _____i < pg->index; _____i++) { \
924 rec = &pg->records[_____i];
925
926#define while_for_each_ftrace_rec() \
927 } \
928 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530929
Ingo Molnare309b412008-05-12 21:20:51 +0200930static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad5082008-05-12 21:20:48 +0200931{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800932 rec->freelist = ftrace_free_records;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200933 ftrace_free_records = rec;
934 rec->flags |= FTRACE_FL_FREE;
935}
936
Ingo Molnare309b412008-05-12 21:20:51 +0200937static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200938{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200939 struct dyn_ftrace *rec;
940
941 /* First check for freed records */
942 if (ftrace_free_records) {
943 rec = ftrace_free_records;
944
Steven Rostedt37ad5082008-05-12 21:20:48 +0200945 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt69128962008-10-23 09:33:03 -0400946 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad5082008-05-12 21:20:48 +0200947 ftrace_free_records = NULL;
948 return NULL;
949 }
950
Lai Jiangshanee000b72009-03-24 13:38:06 +0800951 ftrace_free_records = rec->freelist;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200952 memset(rec, 0, sizeof(*rec));
953 return rec;
954 }
955
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200956 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400957 if (!ftrace_pages->next) {
958 /* allocate another page */
959 ftrace_pages->next =
960 (void *)get_zeroed_page(GFP_KERNEL);
961 if (!ftrace_pages->next)
962 return NULL;
963 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200964 ftrace_pages = ftrace_pages->next;
965 }
966
967 return &ftrace_pages->records[ftrace_pages->index++];
968}
969
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400970static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200971ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200972{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400973 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200974
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800975 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400976 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200977
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400978 rec = ftrace_alloc_dyn_node(ip);
979 if (!rec)
980 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200981
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400982 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +0800983 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +0800984 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200985
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400986 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200987}
988
Steven Rostedt05736a42008-09-22 14:55:47 -0700989static void print_ip_ins(const char *fmt, unsigned char *p)
990{
991 int i;
992
993 printk(KERN_CONT "%s", fmt);
994
995 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
996 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
997}
998
Steven Rostedt31e88902008-11-14 16:21:19 -0800999static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001000{
1001 switch (failed) {
1002 case -EFAULT:
1003 FTRACE_WARN_ON_ONCE(1);
1004 pr_info("ftrace faulted on modifying ");
1005 print_ip_sym(ip);
1006 break;
1007 case -EINVAL:
1008 FTRACE_WARN_ON_ONCE(1);
1009 pr_info("ftrace failed to modify ");
1010 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001011 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001012 printk(KERN_CONT "\n");
1013 break;
1014 case -EPERM:
1015 FTRACE_WARN_ON_ONCE(1);
1016 pr_info("ftrace faulted on writing ");
1017 print_ip_sym(ip);
1018 break;
1019 default:
1020 FTRACE_WARN_ON_ONCE(1);
1021 pr_info("ftrace faulted on unknown error ");
1022 print_ip_sym(ip);
1023 }
1024}
1025
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001026
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001027/* Return 1 if the address range is reserved for ftrace */
1028int ftrace_text_reserved(void *start, void *end)
1029{
1030 struct dyn_ftrace *rec;
1031 struct ftrace_page *pg;
1032
1033 do_for_each_ftrace_rec(pg, rec) {
1034 if (rec->ip <= (unsigned long)end &&
1035 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1036 return 1;
1037 } while_for_each_ftrace_rec();
1038 return 0;
1039}
1040
1041
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301042static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001043__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001044{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001045 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001046 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001047
Shaohua Lif0001202009-01-09 11:29:42 +08001048 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001049
Steven Rostedt982c3502008-11-15 16:31:41 -05001050 /*
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001051 * If this record is not to be traced or we want to disable it,
1052 * then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001053 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001054 * If we want to enable it and filtering is off, then enable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001055 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001056 * If we want to enable it and filtering is on, enable it only if
1057 * it's filtered
Steven Rostedt982c3502008-11-15 16:31:41 -05001058 */
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001059 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1060 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1061 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001062 }
1063
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001064 /* If the state of this record hasn't changed, then do nothing */
1065 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1066 return 0;
1067
1068 if (flag) {
1069 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001070 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001071 }
1072
1073 rec->flags &= ~FTRACE_FL_ENABLED;
1074 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001075}
1076
1077static void ftrace_replace_code(int enable)
1078{
Steven Rostedt37ad5082008-05-12 21:20:48 +02001079 struct dyn_ftrace *rec;
1080 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001081 int failed;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001082
Steven Rostedt265c8312009-02-13 12:43:56 -05001083 do_for_each_ftrace_rec(pg, rec) {
1084 /*
Zhaoleifa9d13c2009-03-13 17:16:34 +08001085 * Skip over free records, records that have
1086 * failed and not converted.
Steven Rostedt265c8312009-02-13 12:43:56 -05001087 */
1088 if (rec->flags & FTRACE_FL_FREE ||
Zhaoleifa9d13c2009-03-13 17:16:34 +08001089 rec->flags & FTRACE_FL_FAILED ||
Frederic Weisbecker03303542009-03-16 22:41:00 +01001090 !(rec->flags & FTRACE_FL_CONVERTED))
Steven Rostedt265c8312009-02-13 12:43:56 -05001091 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001092
Steven Rostedt265c8312009-02-13 12:43:56 -05001093 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001094 if (failed) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001095 rec->flags |= FTRACE_FL_FAILED;
Steven Rostedt3279ba32009-10-07 16:57:56 -04001096 ftrace_bug(failed, rec->ip);
1097 /* Stop processing */
1098 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001099 }
1100 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001101}
1102
Ingo Molnare309b412008-05-12 21:20:51 +02001103static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001104ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001105{
1106 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001107 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001108
1109 ip = rec->ip;
1110
Shaohua Li25aac9d2009-01-09 11:29:40 +08001111 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001112 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001113 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001114 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301115 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001116 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301117 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001118}
1119
Steven Rostedt000ab692009-02-17 13:35:06 -05001120/*
1121 * archs can override this function if they must do something
1122 * before the modifying code is performed.
1123 */
1124int __weak ftrace_arch_code_modify_prepare(void)
1125{
1126 return 0;
1127}
1128
1129/*
1130 * archs can override this function if they must do something
1131 * after the modifying code is performed.
1132 */
1133int __weak ftrace_arch_code_modify_post_process(void)
1134{
1135 return 0;
1136}
1137
Ingo Molnare309b412008-05-12 21:20:51 +02001138static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001139{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001140 int *command = data;
1141
Steven Rostedta3583242008-11-11 15:01:42 -05001142 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001143 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001144 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001145 ftrace_replace_code(0);
1146
1147 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1148 ftrace_update_ftrace_func(ftrace_trace_function);
1149
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001150 if (*command & FTRACE_START_FUNC_RET)
1151 ftrace_enable_ftrace_graph_caller();
1152 else if (*command & FTRACE_STOP_FUNC_RET)
1153 ftrace_disable_ftrace_graph_caller();
1154
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001155 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001156}
1157
Ingo Molnare309b412008-05-12 21:20:51 +02001158static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001159{
Steven Rostedt000ab692009-02-17 13:35:06 -05001160 int ret;
1161
1162 ret = ftrace_arch_code_modify_prepare();
1163 FTRACE_WARN_ON(ret);
1164 if (ret)
1165 return;
1166
Rusty Russell784e2d72008-07-28 12:16:31 -05001167 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001168
1169 ret = ftrace_arch_code_modify_post_process();
1170 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001171}
1172
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001173static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001174static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001175
1176static void ftrace_startup_enable(int command)
1177{
1178 if (saved_ftrace_func != ftrace_trace_function) {
1179 saved_ftrace_func = ftrace_trace_function;
1180 command |= FTRACE_UPDATE_TRACE_FUNC;
1181 }
1182
1183 if (!command || !ftrace_enabled)
1184 return;
1185
1186 ftrace_run_update_code(command);
1187}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001188
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001189static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001190{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001191 if (unlikely(ftrace_disabled))
1192 return;
1193
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001194 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001195 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001196
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001197 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001198}
1199
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001200static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001201{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001202 if (unlikely(ftrace_disabled))
1203 return;
1204
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001205 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001206 /*
1207 * Just warn in case of unbalance, no need to kill ftrace, it's not
1208 * critical but the ftrace_call callers may be never nopped again after
1209 * further ftrace uses.
1210 */
1211 WARN_ON_ONCE(ftrace_start_up < 0);
1212
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001213 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001214 command |= FTRACE_DISABLE_CALLS;
1215
1216 if (saved_ftrace_func != ftrace_trace_function) {
1217 saved_ftrace_func = ftrace_trace_function;
1218 command |= FTRACE_UPDATE_TRACE_FUNC;
1219 }
1220
1221 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001222 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001223
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001224 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001225}
1226
Ingo Molnare309b412008-05-12 21:20:51 +02001227static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001228{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001229 int command = FTRACE_ENABLE_MCOUNT;
1230
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001231 if (unlikely(ftrace_disabled))
1232 return;
1233
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001234 /* Force update next time */
1235 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001236 /* ftrace_start_up is true if we want ftrace running */
1237 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001238 command |= FTRACE_ENABLE_CALLS;
1239
1240 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001241}
1242
Ingo Molnare309b412008-05-12 21:20:51 +02001243static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001244{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001245 int command = FTRACE_DISABLE_MCOUNT;
1246
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001247 if (unlikely(ftrace_disabled))
1248 return;
1249
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001250 /* ftrace_start_up is true if ftrace is running */
1251 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001252 command |= FTRACE_DISABLE_CALLS;
1253
1254 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001255}
1256
Steven Rostedt3d083392008-05-12 21:20:42 +02001257static cycle_t ftrace_update_time;
1258static unsigned long ftrace_update_cnt;
1259unsigned long ftrace_update_tot_cnt;
1260
Steven Rostedt31e88902008-11-14 16:21:19 -08001261static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001262{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001263 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301264 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001265
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001266 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001267 ftrace_update_cnt = 0;
1268
Lai Jiangshane94142a2009-03-13 17:51:27 +08001269 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301270
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001271 /* If something went wrong, bail without enabling anything */
1272 if (unlikely(ftrace_disabled))
1273 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001274
Lai Jiangshane94142a2009-03-13 17:51:27 +08001275 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001276 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001277 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301278
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001279 /*
1280 * Do the initial record convertion from mcount jump
1281 * to the NOP instructions.
1282 */
1283 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001284 ftrace_free_rec(p);
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001285 continue;
1286 }
1287
1288 p->flags |= FTRACE_FL_CONVERTED;
1289 ftrace_update_cnt++;
1290
1291 /*
1292 * If the tracing is enabled, go ahead and enable the record.
1293 *
1294 * The reason not to enable the record immediatelly is the
1295 * inherent check of ftrace_make_nop/ftrace_make_call for
1296 * correct previous instructions. Making first the NOP
1297 * conversion puts the module to the correct state, thus
1298 * passing the ftrace_make_call check.
1299 */
1300 if (ftrace_start_up) {
1301 int failed = __ftrace_replace_code(p, 1);
1302 if (failed) {
1303 ftrace_bug(failed, p->ip);
1304 ftrace_free_rec(p);
1305 }
1306 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001307 }
1308
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001309 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001310 ftrace_update_time = stop - start;
1311 ftrace_update_tot_cnt += ftrace_update_cnt;
1312
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001313 return 0;
1314}
1315
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001316static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001317{
1318 struct ftrace_page *pg;
1319 int cnt;
1320 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001321
1322 /* allocate a few pages */
1323 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1324 if (!ftrace_pages_start)
1325 return -1;
1326
1327 /*
1328 * Allocate a few more pages.
1329 *
1330 * TODO: have some parser search vmlinux before
1331 * final linking to find all calls to ftrace.
1332 * Then we can:
1333 * a) know how many pages to allocate.
1334 * and/or
1335 * b) set up the table then.
1336 *
1337 * The dynamic code is still necessary for
1338 * modules.
1339 */
1340
1341 pg = ftrace_pages = ftrace_pages_start;
1342
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001343 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001344 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001345 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001346
1347 for (i = 0; i < cnt; i++) {
1348 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1349
1350 /* If we fail, we'll try later anyway */
1351 if (!pg->next)
1352 break;
1353
1354 pg = pg->next;
1355 }
1356
1357 return 0;
1358}
1359
Steven Rostedt5072c592008-05-12 21:20:43 +02001360enum {
1361 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001362 FTRACE_ITER_NOTRACE = (1 << 1),
1363 FTRACE_ITER_FAILURES = (1 << 2),
1364 FTRACE_ITER_PRINTALL = (1 << 3),
1365 FTRACE_ITER_HASH = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02001366};
1367
1368#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1369
1370struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +02001371 struct ftrace_page *pg;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001372 int hidx;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001373 int idx;
Steven Rostedt5072c592008-05-12 21:20:43 +02001374 unsigned flags;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001375 struct trace_parser parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02001376};
1377
Ingo Molnare309b412008-05-12 21:20:51 +02001378static void *
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001379t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1380{
1381 struct ftrace_iterator *iter = m->private;
1382 struct hlist_node *hnd = v;
1383 struct hlist_head *hhd;
1384
1385 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1386
1387 (*pos)++;
1388
1389 retry:
1390 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1391 return NULL;
1392
1393 hhd = &ftrace_func_hash[iter->hidx];
1394
1395 if (hlist_empty(hhd)) {
1396 iter->hidx++;
1397 hnd = NULL;
1398 goto retry;
1399 }
1400
1401 if (!hnd)
1402 hnd = hhd->first;
1403 else {
1404 hnd = hnd->next;
1405 if (!hnd) {
1406 iter->hidx++;
1407 goto retry;
1408 }
1409 }
1410
1411 return hnd;
1412}
1413
1414static void *t_hash_start(struct seq_file *m, loff_t *pos)
1415{
1416 struct ftrace_iterator *iter = m->private;
1417 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001418 loff_t l;
1419
1420 if (!(iter->flags & FTRACE_ITER_HASH))
1421 *pos = 0;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001422
1423 iter->flags |= FTRACE_ITER_HASH;
1424
Li Zefand82d6242009-06-24 09:54:54 +08001425 iter->hidx = 0;
1426 for (l = 0; l <= *pos; ) {
1427 p = t_hash_next(m, p, &l);
1428 if (!p)
1429 break;
1430 }
1431 return p;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001432}
1433
1434static int t_hash_show(struct seq_file *m, void *v)
1435{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001436 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001437 struct hlist_node *hnd = v;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001438
Steven Rostedtb6887d72009-02-17 12:32:04 -05001439 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001440
Steven Rostedt809dcf22009-02-16 23:06:01 -05001441 if (rec->ops->print)
1442 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1443
Steven Rostedtb375a112009-09-17 00:05:58 -04001444 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001445
1446 if (rec->data)
1447 seq_printf(m, ":%p", rec->data);
1448 seq_putc(m, '\n');
1449
1450 return 0;
1451}
1452
1453static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001454t_next(struct seq_file *m, void *v, loff_t *pos)
1455{
1456 struct ftrace_iterator *iter = m->private;
1457 struct dyn_ftrace *rec = NULL;
1458
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001459 if (iter->flags & FTRACE_ITER_HASH)
1460 return t_hash_next(m, v, pos);
1461
Steven Rostedt5072c592008-05-12 21:20:43 +02001462 (*pos)++;
1463
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001464 if (iter->flags & FTRACE_ITER_PRINTALL)
1465 return NULL;
1466
Steven Rostedt5072c592008-05-12 21:20:43 +02001467 retry:
1468 if (iter->idx >= iter->pg->index) {
1469 if (iter->pg->next) {
1470 iter->pg = iter->pg->next;
1471 iter->idx = 0;
1472 goto retry;
1473 }
1474 } else {
1475 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001476 if ((rec->flags & FTRACE_FL_FREE) ||
1477
1478 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301479 (rec->flags & FTRACE_FL_FAILED)) ||
1480
1481 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -04001482 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301483
Steven Rostedt0183fb12008-11-07 22:36:02 -05001484 ((iter->flags & FTRACE_ITER_FILTER) &&
1485 !(rec->flags & FTRACE_FL_FILTER)) ||
1486
Steven Rostedt41c52c02008-05-22 11:46:33 -04001487 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1488 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001489 rec = NULL;
1490 goto retry;
1491 }
1492 }
1493
Steven Rostedt5072c592008-05-12 21:20:43 +02001494 return rec;
1495}
1496
1497static void *t_start(struct seq_file *m, loff_t *pos)
1498{
1499 struct ftrace_iterator *iter = m->private;
1500 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001501 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001502
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001503 mutex_lock(&ftrace_lock);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001504 /*
1505 * For set_ftrace_filter reading, if we have the filter
1506 * off, we can short cut and just print out that all
1507 * functions are enabled.
1508 */
1509 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1510 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001511 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001512 iter->flags |= FTRACE_ITER_PRINTALL;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001513 return iter;
1514 }
1515
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001516 if (iter->flags & FTRACE_ITER_HASH)
1517 return t_hash_start(m, pos);
1518
Li Zefan694ce0a2009-06-24 09:54:19 +08001519 iter->pg = ftrace_pages_start;
1520 iter->idx = 0;
1521 for (l = 0; l <= *pos; ) {
1522 p = t_next(m, p, &l);
1523 if (!p)
1524 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001525 }
walimis5821e1b2008-11-15 15:19:06 +08001526
Li Zefan694ce0a2009-06-24 09:54:19 +08001527 if (!p && iter->flags & FTRACE_ITER_FILTER)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001528 return t_hash_start(m, pos);
1529
Steven Rostedt5072c592008-05-12 21:20:43 +02001530 return p;
1531}
1532
1533static void t_stop(struct seq_file *m, void *p)
1534{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001535 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001536}
1537
1538static int t_show(struct seq_file *m, void *v)
1539{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001540 struct ftrace_iterator *iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02001541 struct dyn_ftrace *rec = v;
Steven Rostedt5072c592008-05-12 21:20:43 +02001542
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001543 if (iter->flags & FTRACE_ITER_HASH)
1544 return t_hash_show(m, v);
1545
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001546 if (iter->flags & FTRACE_ITER_PRINTALL) {
1547 seq_printf(m, "#### all functions enabled ####\n");
1548 return 0;
1549 }
1550
Steven Rostedt5072c592008-05-12 21:20:43 +02001551 if (!rec)
1552 return 0;
1553
Steven Rostedtb375a112009-09-17 00:05:58 -04001554 seq_printf(m, "%ps\n", (void *)rec->ip);
Steven Rostedt5072c592008-05-12 21:20:43 +02001555
1556 return 0;
1557}
1558
James Morris88e9d342009-09-22 16:43:43 -07001559static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001560 .start = t_start,
1561 .next = t_next,
1562 .stop = t_stop,
1563 .show = t_show,
1564};
1565
Ingo Molnare309b412008-05-12 21:20:51 +02001566static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001567ftrace_avail_open(struct inode *inode, struct file *file)
1568{
1569 struct ftrace_iterator *iter;
1570 int ret;
1571
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001572 if (unlikely(ftrace_disabled))
1573 return -ENODEV;
1574
Steven Rostedt5072c592008-05-12 21:20:43 +02001575 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1576 if (!iter)
1577 return -ENOMEM;
1578
1579 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001580
1581 ret = seq_open(file, &show_ftrace_seq_ops);
1582 if (!ret) {
1583 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001584
Steven Rostedt5072c592008-05-12 21:20:43 +02001585 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001586 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001587 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001588 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001589
1590 return ret;
1591}
1592
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301593static int
1594ftrace_failures_open(struct inode *inode, struct file *file)
1595{
1596 int ret;
1597 struct seq_file *m;
1598 struct ftrace_iterator *iter;
1599
1600 ret = ftrace_avail_open(inode, file);
1601 if (!ret) {
1602 m = (struct seq_file *)file->private_data;
1603 iter = (struct ftrace_iterator *)m->private;
1604 iter->flags = FTRACE_ITER_FAILURES;
1605 }
1606
1607 return ret;
1608}
1609
1610
Steven Rostedt41c52c02008-05-22 11:46:33 -04001611static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001612{
1613 struct ftrace_page *pg;
1614 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001615 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001616
Steven Rostedt52baf112009-02-14 01:15:39 -05001617 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001618 if (enable)
1619 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001620 do_for_each_ftrace_rec(pg, rec) {
1621 if (rec->flags & FTRACE_FL_FAILED)
1622 continue;
1623 rec->flags &= ~type;
1624 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001625 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001626}
1627
Ingo Molnare309b412008-05-12 21:20:51 +02001628static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001629ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001630{
1631 struct ftrace_iterator *iter;
1632 int ret = 0;
1633
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001634 if (unlikely(ftrace_disabled))
1635 return -ENODEV;
1636
Steven Rostedt5072c592008-05-12 21:20:43 +02001637 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1638 if (!iter)
1639 return -ENOMEM;
1640
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001641 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1642 kfree(iter);
1643 return -ENOMEM;
1644 }
1645
Steven Rostedt41c52c02008-05-22 11:46:33 -04001646 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001647 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04001648 (file->f_flags & O_TRUNC))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001649 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001650
1651 if (file->f_mode & FMODE_READ) {
1652 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001653 iter->flags = enable ? FTRACE_ITER_FILTER :
1654 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001655
1656 ret = seq_open(file, &show_ftrace_seq_ops);
1657 if (!ret) {
1658 struct seq_file *m = file->private_data;
1659 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08001660 } else {
1661 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02001662 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08001663 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001664 } else
1665 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001666 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001667
1668 return ret;
1669}
1670
Steven Rostedt41c52c02008-05-22 11:46:33 -04001671static int
1672ftrace_filter_open(struct inode *inode, struct file *file)
1673{
1674 return ftrace_regex_open(inode, file, 1);
1675}
1676
1677static int
1678ftrace_notrace_open(struct inode *inode, struct file *file)
1679{
1680 return ftrace_regex_open(inode, file, 0);
1681}
1682
Ingo Molnare309b412008-05-12 21:20:51 +02001683static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001684ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001685{
1686 loff_t ret;
1687
1688 if (file->f_mode & FMODE_READ)
1689 ret = seq_lseek(file, offset, origin);
1690 else
1691 file->f_pos = ret = 1;
1692
1693 return ret;
1694}
1695
Steven Rostedt64e7c442009-02-13 17:08:48 -05001696static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001697{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001698 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08001699 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001700
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001701 switch (type) {
1702 case MATCH_FULL:
1703 if (strcmp(str, regex) == 0)
1704 matched = 1;
1705 break;
1706 case MATCH_FRONT_ONLY:
1707 if (strncmp(str, regex, len) == 0)
1708 matched = 1;
1709 break;
1710 case MATCH_MIDDLE_ONLY:
1711 if (strstr(str, regex))
1712 matched = 1;
1713 break;
1714 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08001715 slen = strlen(str);
1716 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001717 matched = 1;
1718 break;
1719 }
1720
1721 return matched;
1722}
1723
Steven Rostedt64e7c442009-02-13 17:08:48 -05001724static int
1725ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1726{
1727 char str[KSYM_SYMBOL_LEN];
1728
1729 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1730 return ftrace_match(str, regex, len, type);
1731}
1732
Li Zefan311d16d2009-12-08 11:15:11 +08001733static int ftrace_match_records(char *buff, int len, int enable)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001734{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001735 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001736 struct ftrace_page *pg;
1737 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001738 unsigned long flag;
1739 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001740 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001741 int not;
Li Zefan311d16d2009-12-08 11:15:11 +08001742 int found = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001743
Steven Rostedt6a24a242009-02-17 11:20:26 -05001744 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001745 type = filter_parse_regex(buff, len, &search, &not);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001746
1747 search_len = strlen(search);
1748
Steven Rostedt52baf112009-02-14 01:15:39 -05001749 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001750 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001751
Steven Rostedt265c8312009-02-13 12:43:56 -05001752 if (rec->flags & FTRACE_FL_FAILED)
1753 continue;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001754
1755 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001756 if (not)
1757 rec->flags &= ~flag;
1758 else
1759 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001760 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001761 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001762 /*
1763 * Only enable filtering if we have a function that
1764 * is filtered on.
1765 */
1766 if (enable && (rec->flags & FTRACE_FL_FILTER))
1767 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001768 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001769 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001770
1771 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02001772}
1773
Steven Rostedt64e7c442009-02-13 17:08:48 -05001774static int
1775ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1776 char *regex, int len, int type)
1777{
1778 char str[KSYM_SYMBOL_LEN];
1779 char *modname;
1780
1781 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1782
1783 if (!modname || strcmp(modname, mod))
1784 return 0;
1785
1786 /* blank search means to match all funcs in the mod */
1787 if (len)
1788 return ftrace_match(str, regex, len, type);
1789 else
1790 return 1;
1791}
1792
Li Zefan311d16d2009-12-08 11:15:11 +08001793static int ftrace_match_module_records(char *buff, char *mod, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001794{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001795 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001796 struct ftrace_page *pg;
1797 struct dyn_ftrace *rec;
1798 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001799 char *search = buff;
1800 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001801 int not = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08001802 int found = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001803
Steven Rostedt6a24a242009-02-17 11:20:26 -05001804 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1805
Steven Rostedt64e7c442009-02-13 17:08:48 -05001806 /* blank or '*' mean the same */
1807 if (strcmp(buff, "*") == 0)
1808 buff[0] = 0;
1809
1810 /* handle the case of 'dont filter this module' */
1811 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1812 buff[0] = 0;
1813 not = 1;
1814 }
1815
1816 if (strlen(buff)) {
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001817 type = filter_parse_regex(buff, strlen(buff), &search, &not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001818 search_len = strlen(search);
1819 }
1820
Steven Rostedt52baf112009-02-14 01:15:39 -05001821 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001822 do_for_each_ftrace_rec(pg, rec) {
1823
1824 if (rec->flags & FTRACE_FL_FAILED)
1825 continue;
1826
1827 if (ftrace_match_module_record(rec, mod,
1828 search, search_len, type)) {
1829 if (not)
1830 rec->flags &= ~flag;
1831 else
1832 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001833 found = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001834 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001835 if (enable && (rec->flags & FTRACE_FL_FILTER))
1836 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001837
1838 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001839 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001840
1841 return found;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001842}
1843
Steven Rostedtf6180772009-02-14 00:40:25 -05001844/*
1845 * We register the module command as a template to show others how
1846 * to register the a command as well.
1847 */
1848
1849static int
1850ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1851{
1852 char *mod;
1853
1854 /*
1855 * cmd == 'mod' because we only registered this func
1856 * for the 'mod' ftrace_func_command.
1857 * But if you register one func with multiple commands,
1858 * you can tell which command was used by the cmd
1859 * parameter.
1860 */
1861
1862 /* we must have a module name */
1863 if (!param)
1864 return -EINVAL;
1865
1866 mod = strsep(&param, ":");
1867 if (!strlen(mod))
1868 return -EINVAL;
1869
Li Zefan311d16d2009-12-08 11:15:11 +08001870 if (ftrace_match_module_records(func, mod, enable))
1871 return 0;
1872 return -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05001873}
1874
1875static struct ftrace_func_command ftrace_mod_cmd = {
1876 .name = "mod",
1877 .func = ftrace_mod_callback,
1878};
1879
1880static int __init ftrace_mod_cmd_init(void)
1881{
1882 return register_ftrace_command(&ftrace_mod_cmd);
1883}
1884device_initcall(ftrace_mod_cmd_init);
1885
Steven Rostedt59df055f2009-02-14 15:29:06 -05001886static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001887function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001888{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001889 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001890 struct hlist_head *hhd;
1891 struct hlist_node *n;
1892 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001893
1894 key = hash_long(ip, FTRACE_HASH_BITS);
1895
1896 hhd = &ftrace_func_hash[key];
1897
1898 if (hlist_empty(hhd))
1899 return;
1900
1901 /*
1902 * Disable preemption for these calls to prevent a RCU grace
1903 * period. This syncs the hash iteration and freeing of items
1904 * on the hash. rcu_read_lock is too dangerous here.
1905 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04001906 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05001907 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1908 if (entry->ip == ip)
1909 entry->ops->func(ip, parent_ip, &entry->data);
1910 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04001911 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05001912}
1913
Steven Rostedtb6887d72009-02-17 12:32:04 -05001914static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001915{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001916 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001917};
1918
Steven Rostedtb6887d72009-02-17 12:32:04 -05001919static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001920
Steven Rostedtb6887d72009-02-17 12:32:04 -05001921static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001922{
1923 int i;
1924
Steven Rostedtb6887d72009-02-17 12:32:04 -05001925 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001926 return;
1927
1928 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1929 struct hlist_head *hhd = &ftrace_func_hash[i];
1930 if (hhd->first)
1931 break;
1932 }
1933 /* Nothing registered? */
1934 if (i == FTRACE_FUNC_HASHSIZE)
1935 return;
1936
Steven Rostedtb6887d72009-02-17 12:32:04 -05001937 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001938 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001939 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001940}
1941
Steven Rostedtb6887d72009-02-17 12:32:04 -05001942static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001943{
1944 int i;
1945
Steven Rostedtb6887d72009-02-17 12:32:04 -05001946 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001947 return;
1948
1949 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1950 struct hlist_head *hhd = &ftrace_func_hash[i];
1951 if (hhd->first)
1952 return;
1953 }
1954
1955 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05001956 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001957 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001958 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001959}
1960
1961
1962static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1963{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001964 struct ftrace_func_probe *entry =
1965 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001966
1967 if (entry->ops->free)
1968 entry->ops->free(&entry->data);
1969 kfree(entry);
1970}
1971
1972
1973int
Steven Rostedtb6887d72009-02-17 12:32:04 -05001974register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001975 void *data)
1976{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001977 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001978 struct ftrace_page *pg;
1979 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001980 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001981 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001982 int count = 0;
1983 char *search;
1984
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001985 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001986 len = strlen(search);
1987
Steven Rostedtb6887d72009-02-17 12:32:04 -05001988 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001989 if (WARN_ON(not))
1990 return -EINVAL;
1991
1992 mutex_lock(&ftrace_lock);
1993 do_for_each_ftrace_rec(pg, rec) {
1994
1995 if (rec->flags & FTRACE_FL_FAILED)
1996 continue;
1997
1998 if (!ftrace_match_record(rec, search, len, type))
1999 continue;
2000
2001 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2002 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002003 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002004 if (!count)
2005 count = -ENOMEM;
2006 goto out_unlock;
2007 }
2008
2009 count++;
2010
2011 entry->data = data;
2012
2013 /*
2014 * The caller might want to do something special
2015 * for each function we find. We call the callback
2016 * to give the caller an opportunity to do so.
2017 */
2018 if (ops->callback) {
2019 if (ops->callback(rec->ip, &entry->data) < 0) {
2020 /* caller does not like this func */
2021 kfree(entry);
2022 continue;
2023 }
2024 }
2025
2026 entry->ops = ops;
2027 entry->ip = rec->ip;
2028
2029 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2030 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2031
2032 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002033 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002034
2035 out_unlock:
2036 mutex_unlock(&ftrace_lock);
2037
2038 return count;
2039}
2040
2041enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002042 PROBE_TEST_FUNC = 1,
2043 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002044};
2045
2046static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002047__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002048 void *data, int flags)
2049{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002050 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002051 struct hlist_node *n, *tmp;
2052 char str[KSYM_SYMBOL_LEN];
2053 int type = MATCH_FULL;
2054 int i, len = 0;
2055 char *search;
2056
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002057 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002058 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002059 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002060 int not;
2061
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002062 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002063 len = strlen(search);
2064
Steven Rostedtb6887d72009-02-17 12:32:04 -05002065 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002066 if (WARN_ON(not))
2067 return;
2068 }
2069
2070 mutex_lock(&ftrace_lock);
2071 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2072 struct hlist_head *hhd = &ftrace_func_hash[i];
2073
2074 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2075
2076 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002077 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002078 continue;
2079
Steven Rostedtb6887d72009-02-17 12:32:04 -05002080 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002081 continue;
2082
2083 /* do this last, since it is the most expensive */
2084 if (glob) {
2085 kallsyms_lookup(entry->ip, NULL, NULL,
2086 NULL, str);
2087 if (!ftrace_match(str, glob, len, type))
2088 continue;
2089 }
2090
2091 hlist_del(&entry->node);
2092 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2093 }
2094 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002095 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002096 mutex_unlock(&ftrace_lock);
2097}
2098
2099void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002100unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002101 void *data)
2102{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002103 __unregister_ftrace_function_probe(glob, ops, data,
2104 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002105}
2106
2107void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002108unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002109{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002110 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002111}
2112
Steven Rostedtb6887d72009-02-17 12:32:04 -05002113void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002114{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002115 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002116}
2117
Steven Rostedtf6180772009-02-14 00:40:25 -05002118static LIST_HEAD(ftrace_commands);
2119static DEFINE_MUTEX(ftrace_cmd_mutex);
2120
2121int register_ftrace_command(struct ftrace_func_command *cmd)
2122{
2123 struct ftrace_func_command *p;
2124 int ret = 0;
2125
2126 mutex_lock(&ftrace_cmd_mutex);
2127 list_for_each_entry(p, &ftrace_commands, list) {
2128 if (strcmp(cmd->name, p->name) == 0) {
2129 ret = -EBUSY;
2130 goto out_unlock;
2131 }
2132 }
2133 list_add(&cmd->list, &ftrace_commands);
2134 out_unlock:
2135 mutex_unlock(&ftrace_cmd_mutex);
2136
2137 return ret;
2138}
2139
2140int unregister_ftrace_command(struct ftrace_func_command *cmd)
2141{
2142 struct ftrace_func_command *p, *n;
2143 int ret = -ENODEV;
2144
2145 mutex_lock(&ftrace_cmd_mutex);
2146 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2147 if (strcmp(cmd->name, p->name) == 0) {
2148 ret = 0;
2149 list_del_init(&p->list);
2150 goto out_unlock;
2151 }
2152 }
2153 out_unlock:
2154 mutex_unlock(&ftrace_cmd_mutex);
2155
2156 return ret;
2157}
2158
Steven Rostedt64e7c442009-02-13 17:08:48 -05002159static int ftrace_process_regex(char *buff, int len, int enable)
2160{
Steven Rostedtf6180772009-02-14 00:40:25 -05002161 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002162 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002163 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002164
2165 func = strsep(&next, ":");
2166
2167 if (!next) {
Li Zefan311d16d2009-12-08 11:15:11 +08002168 if (ftrace_match_records(func, len, enable))
2169 return 0;
2170 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002171 }
2172
Steven Rostedtf6180772009-02-14 00:40:25 -05002173 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002174
2175 command = strsep(&next, ":");
2176
Steven Rostedtf6180772009-02-14 00:40:25 -05002177 mutex_lock(&ftrace_cmd_mutex);
2178 list_for_each_entry(p, &ftrace_commands, list) {
2179 if (strcmp(p->name, command) == 0) {
2180 ret = p->func(func, command, next, enable);
2181 goto out_unlock;
2182 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002183 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002184 out_unlock:
2185 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002186
Steven Rostedtf6180772009-02-14 00:40:25 -05002187 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002188}
2189
Ingo Molnare309b412008-05-12 21:20:51 +02002190static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002191ftrace_regex_write(struct file *file, const char __user *ubuf,
2192 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002193{
2194 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002195 struct trace_parser *parser;
2196 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002197
Li Zefan4ba79782009-09-22 13:52:20 +08002198 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002199 return 0;
2200
Steven Rostedt41c52c02008-05-22 11:46:33 -04002201 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002202
2203 if (file->f_mode & FMODE_READ) {
2204 struct seq_file *m = file->private_data;
2205 iter = m->private;
2206 } else
2207 iter = file->private_data;
2208
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002209 parser = &iter->parser;
2210 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002211
Li Zefan4ba79782009-09-22 13:52:20 +08002212 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002213 !trace_parser_cont(parser)) {
2214 ret = ftrace_process_regex(parser->buffer,
2215 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002216 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002217 if (ret)
Li Zefaned146b22009-11-03 08:55:38 +08002218 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002219 }
2220
Steven Rostedt5072c592008-05-12 21:20:43 +02002221 ret = read;
Li Zefaned146b22009-11-03 08:55:38 +08002222out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002223 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b22009-11-03 08:55:38 +08002224
Steven Rostedt5072c592008-05-12 21:20:43 +02002225 return ret;
2226}
2227
Steven Rostedt41c52c02008-05-22 11:46:33 -04002228static ssize_t
2229ftrace_filter_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, 1);
2233}
2234
2235static ssize_t
2236ftrace_notrace_write(struct file *file, const char __user *ubuf,
2237 size_t cnt, loff_t *ppos)
2238{
2239 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2240}
2241
2242static void
2243ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2244{
2245 if (unlikely(ftrace_disabled))
2246 return;
2247
2248 mutex_lock(&ftrace_regex_lock);
2249 if (reset)
2250 ftrace_filter_reset(enable);
2251 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002252 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002253 mutex_unlock(&ftrace_regex_lock);
2254}
2255
Steven Rostedt77a2b372008-05-12 21:20:45 +02002256/**
2257 * ftrace_set_filter - set a function to filter on in ftrace
2258 * @buf - the string that holds the function filter text.
2259 * @len - the length of the string.
2260 * @reset - non zero to reset all filters before applying this filter.
2261 *
2262 * Filters denote which functions should be enabled when tracing is enabled.
2263 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2264 */
Ingo Molnare309b412008-05-12 21:20:51 +02002265void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002266{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002267 ftrace_set_regex(buf, len, reset, 1);
2268}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002269
Steven Rostedt41c52c02008-05-22 11:46:33 -04002270/**
2271 * ftrace_set_notrace - set a function to not trace in ftrace
2272 * @buf - the string that holds the function notrace text.
2273 * @len - the length of the string.
2274 * @reset - non zero to reset all filters before applying this filter.
2275 *
2276 * Notrace Filters denote which functions should not be enabled when tracing
2277 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2278 * for tracing.
2279 */
2280void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2281{
2282 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002283}
2284
Steven Rostedt2af15d62009-05-28 13:37:24 -04002285/*
2286 * command line interface to allow users to set filters on boot up.
2287 */
2288#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2289static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2290static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2291
2292static int __init set_ftrace_notrace(char *str)
2293{
2294 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2295 return 1;
2296}
2297__setup("ftrace_notrace=", set_ftrace_notrace);
2298
2299static int __init set_ftrace_filter(char *str)
2300{
2301 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2302 return 1;
2303}
2304__setup("ftrace_filter=", set_ftrace_filter);
2305
Stefan Assmann369bc182009-10-12 22:17:21 +02002306#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002307static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002308static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2309
Stefan Assmann369bc182009-10-12 22:17:21 +02002310static int __init set_graph_function(char *str)
2311{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002312 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002313 return 1;
2314}
2315__setup("ftrace_graph_filter=", set_graph_function);
2316
2317static void __init set_ftrace_early_graph(char *buf)
2318{
2319 int ret;
2320 char *func;
2321
2322 while (buf) {
2323 func = strsep(&buf, ",");
2324 /* we allow only one expression at a time */
2325 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2326 func);
2327 if (ret)
2328 printk(KERN_DEBUG "ftrace: function %s not "
2329 "traceable\n", func);
2330 }
2331}
2332#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2333
Steven Rostedt2af15d62009-05-28 13:37:24 -04002334static void __init set_ftrace_early_filter(char *buf, int enable)
2335{
2336 char *func;
2337
2338 while (buf) {
2339 func = strsep(&buf, ",");
2340 ftrace_set_regex(func, strlen(func), 0, enable);
2341 }
2342}
2343
2344static void __init set_ftrace_early_filters(void)
2345{
2346 if (ftrace_filter_buf[0])
2347 set_ftrace_early_filter(ftrace_filter_buf, 1);
2348 if (ftrace_notrace_buf[0])
2349 set_ftrace_early_filter(ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002350#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2351 if (ftrace_graph_buf[0])
2352 set_ftrace_early_graph(ftrace_graph_buf);
2353#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002354}
2355
Ingo Molnare309b412008-05-12 21:20:51 +02002356static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002357ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002358{
2359 struct seq_file *m = (struct seq_file *)file->private_data;
2360 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002361 struct trace_parser *parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02002362
Steven Rostedt41c52c02008-05-22 11:46:33 -04002363 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002364 if (file->f_mode & FMODE_READ) {
2365 iter = m->private;
2366
2367 seq_release(inode, file);
2368 } else
2369 iter = file->private_data;
2370
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002371 parser = &iter->parser;
2372 if (trace_parser_loaded(parser)) {
2373 parser->buffer[parser->idx] = 0;
2374 ftrace_match_records(parser->buffer, parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002375 }
2376
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002377 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002378 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002379 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002380 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002381
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002382 trace_parser_put(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002383 kfree(iter);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002384
Steven Rostedt41c52c02008-05-22 11:46:33 -04002385 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002386 return 0;
2387}
2388
Steven Rostedt41c52c02008-05-22 11:46:33 -04002389static int
2390ftrace_filter_release(struct inode *inode, struct file *file)
2391{
2392 return ftrace_regex_release(inode, file, 1);
2393}
2394
2395static int
2396ftrace_notrace_release(struct inode *inode, struct file *file)
2397{
2398 return ftrace_regex_release(inode, file, 0);
2399}
2400
Steven Rostedt5e2336a02009-03-05 21:44:55 -05002401static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002402 .open = ftrace_avail_open,
2403 .read = seq_read,
2404 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002405 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002406};
2407
Steven Rostedt5e2336a02009-03-05 21:44:55 -05002408static const struct file_operations ftrace_failures_fops = {
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302409 .open = ftrace_failures_open,
2410 .read = seq_read,
2411 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002412 .release = seq_release_private,
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302413};
2414
Steven Rostedt5e2336a02009-03-05 21:44:55 -05002415static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002416 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002417 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002418 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002419 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002420 .release = ftrace_filter_release,
2421};
2422
Steven Rostedt5e2336a02009-03-05 21:44:55 -05002423static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002424 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002425 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002426 .write = ftrace_notrace_write,
2427 .llseek = ftrace_regex_lseek,
2428 .release = ftrace_notrace_release,
2429};
2430
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002431#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2432
2433static DEFINE_MUTEX(graph_lock);
2434
2435int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002436int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002437unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2438
2439static void *
Li Zefan85951842009-06-24 09:54:00 +08002440__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002441{
Li Zefan85951842009-06-24 09:54:00 +08002442 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002443 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002444 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002445}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002446
Li Zefan85951842009-06-24 09:54:00 +08002447static void *
2448g_next(struct seq_file *m, void *v, loff_t *pos)
2449{
2450 (*pos)++;
2451 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002452}
2453
2454static void *g_start(struct seq_file *m, loff_t *pos)
2455{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002456 mutex_lock(&graph_lock);
2457
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002458 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002459 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002460 return (void *)1;
2461
Li Zefan85951842009-06-24 09:54:00 +08002462 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002463}
2464
2465static void g_stop(struct seq_file *m, void *p)
2466{
2467 mutex_unlock(&graph_lock);
2468}
2469
2470static int g_show(struct seq_file *m, void *v)
2471{
2472 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002473
2474 if (!ptr)
2475 return 0;
2476
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002477 if (ptr == (unsigned long *)1) {
2478 seq_printf(m, "#### all functions enabled ####\n");
2479 return 0;
2480 }
2481
Steven Rostedtb375a112009-09-17 00:05:58 -04002482 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002483
2484 return 0;
2485}
2486
James Morris88e9d342009-09-22 16:43:43 -07002487static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002488 .start = g_start,
2489 .next = g_next,
2490 .stop = g_stop,
2491 .show = g_show,
2492};
2493
2494static int
2495ftrace_graph_open(struct inode *inode, struct file *file)
2496{
2497 int ret = 0;
2498
2499 if (unlikely(ftrace_disabled))
2500 return -ENODEV;
2501
2502 mutex_lock(&graph_lock);
2503 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002504 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002505 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002506 ftrace_graph_count = 0;
2507 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2508 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002509 mutex_unlock(&graph_lock);
2510
Li Zefana4ec5e02009-09-18 14:06:28 +08002511 if (file->f_mode & FMODE_READ)
2512 ret = seq_open(file, &ftrace_graph_seq_ops);
2513
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002514 return ret;
2515}
2516
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002517static int
Li Zefan87827112009-07-23 11:29:11 +08002518ftrace_graph_release(struct inode *inode, struct file *file)
2519{
2520 if (file->f_mode & FMODE_READ)
2521 seq_release(inode, file);
2522 return 0;
2523}
2524
2525static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002526ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002527{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002528 struct dyn_ftrace *rec;
2529 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002530 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002531 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002532 int type, not;
2533 char *search;
2534 bool exists;
2535 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002536
2537 if (ftrace_disabled)
2538 return -ENODEV;
2539
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002540 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002541 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002542 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2543 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002544
2545 search_len = strlen(search);
2546
Steven Rostedt52baf112009-02-14 01:15:39 -05002547 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05002548 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002549
Steven Rostedt265c8312009-02-13 12:43:56 -05002550 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2551 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002552
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002553 if (ftrace_match_record(rec, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002554 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002555 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002556 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002557 if (array[i] == rec->ip) {
2558 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002559 break;
2560 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002561 }
2562
2563 if (!not) {
2564 fail = 0;
2565 if (!exists) {
2566 array[(*idx)++] = rec->ip;
2567 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2568 goto out;
2569 }
2570 } else {
2571 if (exists) {
2572 array[i] = array[--(*idx)];
2573 array[*idx] = 0;
2574 fail = 0;
2575 }
2576 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002577 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002578 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002579out:
Steven Rostedt52baf112009-02-14 01:15:39 -05002580 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002581
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002582 if (fail)
2583 return -EINVAL;
2584
2585 ftrace_graph_filter_enabled = 1;
2586 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002587}
2588
2589static ssize_t
2590ftrace_graph_write(struct file *file, const char __user *ubuf,
2591 size_t cnt, loff_t *ppos)
2592{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002593 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08002594 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002595
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002596 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002597 return 0;
2598
2599 mutex_lock(&graph_lock);
2600
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002601 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2602 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08002603 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002604 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002605
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002606 read = trace_get_user(&parser, ubuf, cnt, ppos);
2607
Li Zefan4ba79782009-09-22 13:52:20 +08002608 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002609 parser.buffer[parser.idx] = 0;
2610
2611 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08002612 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002613 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002614 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08002615 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002616 }
2617
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002618 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08002619
2620out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002621 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08002622out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002623 mutex_unlock(&graph_lock);
2624
2625 return ret;
2626}
2627
2628static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08002629 .open = ftrace_graph_open,
2630 .read = seq_read,
2631 .write = ftrace_graph_write,
2632 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002633};
2634#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2635
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002636static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002637{
Steven Rostedt5072c592008-05-12 21:20:43 +02002638
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002639 trace_create_file("available_filter_functions", 0444,
2640 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002641
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002642 trace_create_file("failures", 0444,
2643 d_tracer, NULL, &ftrace_failures_fops);
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302644
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002645 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2646 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002647
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002648 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002649 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002650
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002651#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002652 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002653 NULL,
2654 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002655#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2656
Steven Rostedt5072c592008-05-12 21:20:43 +02002657 return 0;
2658}
2659
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002660static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08002661 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002662 unsigned long *end)
2663{
2664 unsigned long *p;
2665 unsigned long addr;
2666 unsigned long flags;
2667
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002668 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002669 p = start;
2670 while (p < end) {
2671 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002672 /*
2673 * Some architecture linkers will pad between
2674 * the different mcount_loc sections of different
2675 * object files to satisfy alignments.
2676 * Skip any NULL pointers.
2677 */
2678 if (!addr)
2679 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002680 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002681 }
2682
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002683 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002684 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08002685 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002686 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002687 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002688
2689 return 0;
2690}
2691
Steven Rostedt93eb6772009-04-15 13:24:06 -04002692#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002693void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002694{
2695 struct dyn_ftrace *rec;
2696 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04002697
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002698 if (ftrace_disabled)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002699 return;
2700
2701 mutex_lock(&ftrace_lock);
2702 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002703 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04002704 /*
2705 * rec->ip is changed in ftrace_free_rec()
2706 * It should not between s and e if record was freed.
2707 */
2708 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2709 ftrace_free_rec(rec);
2710 }
2711 } while_for_each_ftrace_rec();
2712 mutex_unlock(&ftrace_lock);
2713}
2714
2715static void ftrace_init_module(struct module *mod,
2716 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002717{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002718 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002719 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002720 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002721}
2722
Steven Rostedt93eb6772009-04-15 13:24:06 -04002723static int ftrace_module_notify(struct notifier_block *self,
2724 unsigned long val, void *data)
2725{
2726 struct module *mod = data;
2727
2728 switch (val) {
2729 case MODULE_STATE_COMING:
2730 ftrace_init_module(mod, mod->ftrace_callsites,
2731 mod->ftrace_callsites +
2732 mod->num_ftrace_callsites);
2733 break;
2734 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002735 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04002736 break;
2737 }
2738
2739 return 0;
2740}
2741#else
2742static int ftrace_module_notify(struct notifier_block *self,
2743 unsigned long val, void *data)
2744{
2745 return 0;
2746}
2747#endif /* CONFIG_MODULES */
2748
2749struct notifier_block ftrace_module_nb = {
2750 .notifier_call = ftrace_module_notify,
2751 .priority = 0,
2752};
2753
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002754extern unsigned long __start_mcount_loc[];
2755extern unsigned long __stop_mcount_loc[];
2756
2757void __init ftrace_init(void)
2758{
2759 unsigned long count, addr, flags;
2760 int ret;
2761
2762 /* Keep the ftrace pointer to the stub */
2763 addr = (unsigned long)ftrace_stub;
2764
2765 local_irq_save(flags);
2766 ftrace_dyn_arch_init(&addr);
2767 local_irq_restore(flags);
2768
2769 /* ftrace_dyn_arch_init places the return code in addr */
2770 if (addr)
2771 goto failed;
2772
2773 count = __stop_mcount_loc - __start_mcount_loc;
2774
2775 ret = ftrace_dyn_table_alloc(count);
2776 if (ret)
2777 goto failed;
2778
2779 last_ftrace_enabled = ftrace_enabled = 1;
2780
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002781 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08002782 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002783 __stop_mcount_loc);
2784
Steven Rostedt93eb6772009-04-15 13:24:06 -04002785 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002786 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002787 pr_warning("Failed to register trace ftrace module notifier\n");
2788
Steven Rostedt2af15d62009-05-28 13:37:24 -04002789 set_ftrace_early_filters();
2790
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002791 return;
2792 failed:
2793 ftrace_disabled = 1;
2794}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002795
Steven Rostedt3d083392008-05-12 21:20:42 +02002796#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002797
2798static int __init ftrace_nodyn_init(void)
2799{
2800 ftrace_enabled = 1;
2801 return 0;
2802}
2803device_initcall(ftrace_nodyn_init);
2804
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002805static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2806static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002807/* Keep as macros so we do not need to define the commands */
2808# define ftrace_startup(command) do { } while (0)
2809# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002810# define ftrace_startup_sysctl() do { } while (0)
2811# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002812#endif /* CONFIG_DYNAMIC_FTRACE */
2813
Steven Rostedte32d8952008-12-04 00:26:41 -05002814static void clear_ftrace_swapper(void)
2815{
2816 struct task_struct *p;
2817 int cpu;
2818
2819 get_online_cpus();
2820 for_each_online_cpu(cpu) {
2821 p = idle_task(cpu);
2822 clear_tsk_trace_trace(p);
2823 }
2824 put_online_cpus();
2825}
2826
2827static void set_ftrace_swapper(void)
2828{
2829 struct task_struct *p;
2830 int cpu;
2831
2832 get_online_cpus();
2833 for_each_online_cpu(cpu) {
2834 p = idle_task(cpu);
2835 set_tsk_trace_trace(p);
2836 }
2837 put_online_cpus();
2838}
2839
2840static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002841{
2842 struct task_struct *p;
2843
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002844 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002845 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002846 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002847 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002848 rcu_read_unlock();
2849
Steven Rostedte32d8952008-12-04 00:26:41 -05002850 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002851}
2852
Steven Rostedte32d8952008-12-04 00:26:41 -05002853static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002854{
2855 struct task_struct *p;
2856
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002857 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002858 do_each_pid_task(pid, PIDTYPE_PID, p) {
2859 set_tsk_trace_trace(p);
2860 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002861 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002862}
2863
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002864static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002865{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002866 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002867 clear_ftrace_swapper();
2868 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002869 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05002870}
2871
2872static void set_ftrace_pid_task(struct pid *pid)
2873{
2874 if (pid == ftrace_swapper_pid)
2875 set_ftrace_swapper();
2876 else
2877 set_ftrace_pid(pid);
2878}
2879
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002880static int ftrace_pid_add(int p)
2881{
2882 struct pid *pid;
2883 struct ftrace_pid *fpid;
2884 int ret = -EINVAL;
2885
2886 mutex_lock(&ftrace_lock);
2887
2888 if (!p)
2889 pid = ftrace_swapper_pid;
2890 else
2891 pid = find_get_pid(p);
2892
2893 if (!pid)
2894 goto out;
2895
2896 ret = 0;
2897
2898 list_for_each_entry(fpid, &ftrace_pids, list)
2899 if (fpid->pid == pid)
2900 goto out_put;
2901
2902 ret = -ENOMEM;
2903
2904 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2905 if (!fpid)
2906 goto out_put;
2907
2908 list_add(&fpid->list, &ftrace_pids);
2909 fpid->pid = pid;
2910
2911 set_ftrace_pid_task(pid);
2912
2913 ftrace_update_pid_func();
2914 ftrace_startup_enable(0);
2915
2916 mutex_unlock(&ftrace_lock);
2917 return 0;
2918
2919out_put:
2920 if (pid != ftrace_swapper_pid)
2921 put_pid(pid);
2922
2923out:
2924 mutex_unlock(&ftrace_lock);
2925 return ret;
2926}
2927
2928static void ftrace_pid_reset(void)
2929{
2930 struct ftrace_pid *fpid, *safe;
2931
2932 mutex_lock(&ftrace_lock);
2933 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2934 struct pid *pid = fpid->pid;
2935
2936 clear_ftrace_pid_task(pid);
2937
2938 list_del(&fpid->list);
2939 kfree(fpid);
2940 }
2941
2942 ftrace_update_pid_func();
2943 ftrace_startup_enable(0);
2944
2945 mutex_unlock(&ftrace_lock);
2946}
2947
2948static void *fpid_start(struct seq_file *m, loff_t *pos)
2949{
2950 mutex_lock(&ftrace_lock);
2951
2952 if (list_empty(&ftrace_pids) && (!*pos))
2953 return (void *) 1;
2954
2955 return seq_list_start(&ftrace_pids, *pos);
2956}
2957
2958static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2959{
2960 if (v == (void *)1)
2961 return NULL;
2962
2963 return seq_list_next(v, &ftrace_pids, pos);
2964}
2965
2966static void fpid_stop(struct seq_file *m, void *p)
2967{
2968 mutex_unlock(&ftrace_lock);
2969}
2970
2971static int fpid_show(struct seq_file *m, void *v)
2972{
2973 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2974
2975 if (v == (void *)1) {
2976 seq_printf(m, "no pid\n");
2977 return 0;
2978 }
2979
2980 if (fpid->pid == ftrace_swapper_pid)
2981 seq_printf(m, "swapper tasks\n");
2982 else
2983 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2984
2985 return 0;
2986}
2987
2988static const struct seq_operations ftrace_pid_sops = {
2989 .start = fpid_start,
2990 .next = fpid_next,
2991 .stop = fpid_stop,
2992 .show = fpid_show,
2993};
2994
2995static int
2996ftrace_pid_open(struct inode *inode, struct file *file)
2997{
2998 int ret = 0;
2999
3000 if ((file->f_mode & FMODE_WRITE) &&
3001 (file->f_flags & O_TRUNC))
3002 ftrace_pid_reset();
3003
3004 if (file->f_mode & FMODE_READ)
3005 ret = seq_open(file, &ftrace_pid_sops);
3006
3007 return ret;
3008}
3009
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003010static ssize_t
3011ftrace_pid_write(struct file *filp, const char __user *ubuf,
3012 size_t cnt, loff_t *ppos)
3013{
Ingo Molnar457dc922009-11-23 11:03:28 +01003014 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003015 long val;
3016 int ret;
3017
3018 if (cnt >= sizeof(buf))
3019 return -EINVAL;
3020
3021 if (copy_from_user(&buf, ubuf, cnt))
3022 return -EFAULT;
3023
3024 buf[cnt] = 0;
3025
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003026 /*
3027 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3028 * to clean the filter quietly.
3029 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003030 tmp = strstrip(buf);
3031 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003032 return 1;
3033
Ingo Molnar457dc922009-11-23 11:03:28 +01003034 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003035 if (ret < 0)
3036 return ret;
3037
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003038 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003039
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003040 return ret ? ret : cnt;
3041}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003042
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003043static int
3044ftrace_pid_release(struct inode *inode, struct file *file)
3045{
3046 if (file->f_mode & FMODE_READ)
3047 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003048
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003049 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003050}
3051
Steven Rostedt5e2336a02009-03-05 21:44:55 -05003052static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003053 .open = ftrace_pid_open,
3054 .write = ftrace_pid_write,
3055 .read = seq_read,
3056 .llseek = seq_lseek,
3057 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003058};
3059
3060static __init int ftrace_init_debugfs(void)
3061{
3062 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003063
3064 d_tracer = tracing_init_dentry();
3065 if (!d_tracer)
3066 return 0;
3067
3068 ftrace_init_dyn_debugfs(d_tracer);
3069
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003070 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3071 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003072
3073 ftrace_profile_debugfs(d_tracer);
3074
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003075 return 0;
3076}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003077fs_initcall(ftrace_init_debugfs);
3078
Steven Rostedt3d083392008-05-12 21:20:42 +02003079/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003080 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003081 *
3082 * This function should be used by panic code. It stops ftrace
3083 * but in a not so nice way. If you need to simply kill ftrace
3084 * from a non-atomic section, use ftrace_kill.
3085 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003086void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003087{
3088 ftrace_disabled = 1;
3089 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003090 clear_ftrace_function();
3091}
3092
3093/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003094 * register_ftrace_function - register a function for profiling
3095 * @ops - ops structure that holds the function for profiling.
3096 *
3097 * Register a function to be called by all functions in the
3098 * kernel.
3099 *
3100 * Note: @ops->func and all the functions it calls must be labeled
3101 * with "notrace", otherwise it will go into a
3102 * recursive loop.
3103 */
3104int register_ftrace_function(struct ftrace_ops *ops)
3105{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003106 int ret;
3107
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003108 if (unlikely(ftrace_disabled))
3109 return -1;
3110
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003111 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003112
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003113 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003114 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003115
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003116 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003117 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003118}
3119
3120/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003121 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003122 * @ops - ops structure that holds the function to unregister
3123 *
3124 * Unregister a function that was added to be called by ftrace profiling.
3125 */
3126int unregister_ftrace_function(struct ftrace_ops *ops)
3127{
3128 int ret;
3129
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003130 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003131 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003132 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003133 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003134
3135 return ret;
3136}
3137
Ingo Molnare309b412008-05-12 21:20:51 +02003138int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003139ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003140 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003141 loff_t *ppos)
3142{
3143 int ret;
3144
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003145 if (unlikely(ftrace_disabled))
3146 return -ENODEV;
3147
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003148 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003149
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003150 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003151
Li Zefana32c7762009-06-26 16:55:51 +08003152 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003153 goto out;
3154
Li Zefana32c7762009-06-26 16:55:51 +08003155 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003156
3157 if (ftrace_enabled) {
3158
3159 ftrace_startup_sysctl();
3160
3161 /* we are starting ftrace again */
3162 if (ftrace_list != &ftrace_list_end) {
3163 if (ftrace_list->next == &ftrace_list_end)
3164 ftrace_trace_function = ftrace_list->func;
3165 else
3166 ftrace_trace_function = ftrace_list_func;
3167 }
3168
3169 } else {
3170 /* stopping ftrace calls (just send to ftrace_stub) */
3171 ftrace_trace_function = ftrace_stub;
3172
3173 ftrace_shutdown_sysctl();
3174 }
3175
3176 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003177 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003178 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003179}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003180
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003181#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003182
Steven Rostedt597af812009-04-03 15:24:12 -04003183static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003184static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003185
Steven Rostedte49dc192008-12-02 23:50:05 -05003186int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3187{
3188 return 0;
3189}
3190
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003191/* The callbacks that hook a function */
3192trace_func_graph_ret_t ftrace_graph_return =
3193 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003194trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003195
3196/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3197static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3198{
3199 int i;
3200 int ret = 0;
3201 unsigned long flags;
3202 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3203 struct task_struct *g, *t;
3204
3205 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3206 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3207 * sizeof(struct ftrace_ret_stack),
3208 GFP_KERNEL);
3209 if (!ret_stack_list[i]) {
3210 start = 0;
3211 end = i;
3212 ret = -ENOMEM;
3213 goto free;
3214 }
3215 }
3216
3217 read_lock_irqsave(&tasklist_lock, flags);
3218 do_each_thread(g, t) {
3219 if (start == end) {
3220 ret = -EAGAIN;
3221 goto unlock;
3222 }
3223
3224 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003225 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003226 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003227 t->curr_ret_stack = -1;
3228 /* Make sure the tasks see the -1 first: */
3229 smp_wmb();
3230 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003231 }
3232 } while_each_thread(g, t);
3233
3234unlock:
3235 read_unlock_irqrestore(&tasklist_lock, flags);
3236free:
3237 for (i = start; i < end; i++)
3238 kfree(ret_stack_list[i]);
3239 return ret;
3240}
3241
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003242static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04003243ftrace_graph_probe_sched_switch(void *ignore,
3244 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003245{
3246 unsigned long long timestamp;
3247 int index;
3248
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003249 /*
3250 * Does the user want to count the time a function was asleep.
3251 * If so, do not update the time stamps.
3252 */
3253 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3254 return;
3255
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003256 timestamp = trace_clock_local();
3257
3258 prev->ftrace_timestamp = timestamp;
3259
3260 /* only process tasks that we timestamped */
3261 if (!next->ftrace_timestamp)
3262 return;
3263
3264 /*
3265 * Update all the counters in next to make up for the
3266 * time next was sleeping.
3267 */
3268 timestamp -= next->ftrace_timestamp;
3269
3270 for (index = next->curr_ret_stack; index >= 0; index--)
3271 next->ret_stack[index].calltime += timestamp;
3272}
3273
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003274/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003275static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003276{
3277 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003278 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003279
3280 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3281 sizeof(struct ftrace_ret_stack *),
3282 GFP_KERNEL);
3283
3284 if (!ret_stack_list)
3285 return -ENOMEM;
3286
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003287 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003288 for_each_online_cpu(cpu) {
3289 if (!idle_task(cpu)->ret_stack)
3290 ftrace_graph_init_task(idle_task(cpu));
3291 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003292
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003293 do {
3294 ret = alloc_retstack_tasklist(ret_stack_list);
3295 } while (ret == -EAGAIN);
3296
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003297 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04003298 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003299 if (ret)
3300 pr_info("ftrace_graph: Couldn't activate tracepoint"
3301 " probe to kernel_sched_switch\n");
3302 }
3303
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003304 kfree(ret_stack_list);
3305 return ret;
3306}
3307
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003308/*
3309 * Hibernation protection.
3310 * The state of the current task is too much unstable during
3311 * suspend/restore to disk. We want to protect against that.
3312 */
3313static int
3314ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3315 void *unused)
3316{
3317 switch (state) {
3318 case PM_HIBERNATION_PREPARE:
3319 pause_graph_tracing();
3320 break;
3321
3322 case PM_POST_HIBERNATION:
3323 unpause_graph_tracing();
3324 break;
3325 }
3326 return NOTIFY_DONE;
3327}
3328
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003329int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3330 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003331{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003332 int ret = 0;
3333
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003334 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003335
Steven Rostedt05ce5812009-03-24 00:18:31 -04003336 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003337 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003338 ret = -EBUSY;
3339 goto out;
3340 }
3341
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003342 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3343 register_pm_notifier(&ftrace_suspend_notifier);
3344
Steven Rostedt597af812009-04-03 15:24:12 -04003345 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003346 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003347 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003348 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003349 goto out;
3350 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003351
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003352 ftrace_graph_return = retfunc;
3353 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003354
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003355 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003356
3357out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003358 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003359 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003360}
3361
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003362void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003363{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003364 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003365
Steven Rostedt597af812009-04-03 15:24:12 -04003366 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003367 goto out;
3368
Steven Rostedt597af812009-04-03 15:24:12 -04003369 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003370 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003371 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003372 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003373 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04003374 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003375
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003376 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003377 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003378}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003379
3380/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003381void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003382{
Steven Rostedt84047e32009-06-02 16:51:55 -04003383 /* Make sure we do not use the parent ret_stack */
3384 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05003385 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04003386
Steven Rostedt597af812009-04-03 15:24:12 -04003387 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003388 struct ftrace_ret_stack *ret_stack;
3389
3390 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003391 * sizeof(struct ftrace_ret_stack),
3392 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003393 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003394 return;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003395 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003396 atomic_set(&t->trace_overrun, 0);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003397 t->ftrace_timestamp = 0;
Steven Rostedt82310a32009-06-02 12:26:07 -04003398 /* make curr_ret_stack visable before we add the ret_stack */
3399 smp_wmb();
3400 t->ret_stack = ret_stack;
Steven Rostedt84047e32009-06-02 16:51:55 -04003401 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003402}
3403
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003404void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003405{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003406 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3407
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003408 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003409 /* NULL must become visible to IRQs before we free it: */
3410 barrier();
3411
3412 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003413}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003414
3415void ftrace_graph_stop(void)
3416{
3417 ftrace_stop();
3418}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003419#endif