blob: 8fef1d99bbbf9dfc1af07416c436687645aec66d [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 Rostedt6912896e2008-10-23 09:33:03 -040041#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040042 ({ \
43 int ___r = cond; \
44 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040045 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040046 ___r; \
47 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040048
49#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040050 ({ \
51 int ___r = cond; \
52 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040053 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040054 ___r; \
55 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040056
Steven Rostedt8fc0c702009-02-16 15:28:00 -050057/* hash bits for specific function selection */
58#define FTRACE_HASH_BITS 7
59#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040060#define FTRACE_HASH_DEFAULT_BITS 10
61#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050062
Steven Rostedt4eebcc82008-05-12 21:20:48 +020063/* ftrace_enabled is a method to turn ftrace on or off */
64int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020065static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020066
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050067/* Quick disabling of function tracer. */
68int function_trace_stop;
69
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040070/* List for set_ftrace_pid's pids. */
71LIST_HEAD(ftrace_pids);
72struct ftrace_pid {
73 struct list_head list;
74 struct pid *pid;
75};
76
Steven Rostedt4eebcc82008-05-12 21:20:48 +020077/*
78 * ftrace_disabled is set when an anomaly is discovered.
79 * ftrace_disabled is much stronger than ftrace_enabled.
80 */
81static int ftrace_disabled __read_mostly;
82
Steven Rostedt52baf112009-02-14 01:15:39 -050083static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020084
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020085static struct ftrace_ops ftrace_list_end __read_mostly =
86{
Steven Rostedtfb9fb012009-03-25 13:26:41 -040087 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020088};
89
90static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
91ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050092ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050093ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020094
Paul E. McKenney3f379b02010-03-05 15:03:25 -080095/*
96 * Traverse the ftrace_list, invoking all entries. The reason that we
97 * can use rcu_dereference_raw() is that elements removed from this list
98 * are simply leaked, so there is no need to interact with a grace-period
99 * mechanism. The rcu_dereference_raw() calls are needed to handle
100 * concurrent insertions into the ftrace_list.
101 *
102 * Silly Alpha and silly pointer-speculation compiler optimizations!
103 */
Ingo Molnarf2252932008-05-22 10:37:48 +0200104static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200105{
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800106 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200107
108 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200109 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800110 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200111 };
112}
113
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500114static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
115{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500116 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500117 return;
118
119 ftrace_pid_function(ip, parent_ip);
120}
121
122static void set_ftrace_pid_function(ftrace_func_t func)
123{
124 /* do not set ftrace_pid_function to itself! */
125 if (func != ftrace_pid_func)
126 ftrace_pid_function = func;
127}
128
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200129/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200130 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200131 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200132 * This NULLs the ftrace function and in essence stops
133 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200134 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200135void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200136{
Steven Rostedt3d083392008-05-12 21:20:42 +0200137 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500138 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500139 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200140}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200141
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500142#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
143/*
144 * For those archs that do not test ftrace_trace_stop in their
145 * mcount call site, we need to do it from C.
146 */
147static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
148{
149 if (function_trace_stop)
150 return;
151
152 __ftrace_trace_function(ip, parent_ip);
153}
154#endif
155
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400156static void update_ftrace_function(void)
157{
158 ftrace_func_t func;
159
160 /*
161 * If there's only one function registered, then call that
162 * function directly. Otherwise, we need to iterate over the
163 * registered callers.
164 */
165 if (ftrace_list == &ftrace_list_end ||
166 ftrace_list->next == &ftrace_list_end)
167 func = ftrace_list->func;
168 else
169 func = ftrace_list_func;
170
171 /* If we filter on pids, update to use the pid function */
172 if (!list_empty(&ftrace_pids)) {
173 set_ftrace_pid_function(func);
174 func = ftrace_pid_func;
175 }
176#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
177 ftrace_trace_function = func;
178#else
179 __ftrace_trace_function = func;
180 ftrace_trace_function = ftrace_test_stop_func;
181#endif
182}
183
Ingo Molnare309b412008-05-12 21:20:51 +0200184static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200185{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200186 ops->next = ftrace_list;
187 /*
188 * We are entering ops into the ftrace_list but another
189 * CPU might be walking that list. We need to make sure
190 * the ops->next pointer is valid before another CPU sees
191 * the ops pointer included into the ftrace_list.
192 */
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800193 rcu_assign_pointer(ftrace_list, ops);
Steven Rostedt3d083392008-05-12 21:20:42 +0200194
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400195 if (ftrace_enabled)
196 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200197
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200198 return 0;
199}
200
Ingo Molnare309b412008-05-12 21:20:51 +0200201static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200202{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200203 struct ftrace_ops **p;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200204
205 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200206 * If we are removing the last function, then simply point
207 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200208 */
209 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
210 ftrace_trace_function = ftrace_stub;
211 ftrace_list = &ftrace_list_end;
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500212 return 0;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200213 }
214
215 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
216 if (*p == ops)
217 break;
218
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500219 if (*p != ops)
220 return -1;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200221
222 *p = (*p)->next;
223
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400224 if (ftrace_enabled)
225 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200226
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500227 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200228}
229
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500230static void ftrace_update_pid_func(void)
231{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400232 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500233 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900234 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500235
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400236 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500237}
238
Steven Rostedt493762f2009-03-23 17:12:36 -0400239#ifdef CONFIG_FUNCTION_PROFILER
240struct ftrace_profile {
241 struct hlist_node node;
242 unsigned long ip;
243 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400244#ifdef CONFIG_FUNCTION_GRAPH_TRACER
245 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400246 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400247#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400248};
249
250struct ftrace_profile_page {
251 struct ftrace_profile_page *next;
252 unsigned long index;
253 struct ftrace_profile records[];
254};
255
Steven Rostedtcafb1682009-03-24 20:50:39 -0400256struct ftrace_profile_stat {
257 atomic_t disabled;
258 struct hlist_head *hash;
259 struct ftrace_profile_page *pages;
260 struct ftrace_profile_page *start;
261 struct tracer_stat stat;
262};
263
Steven Rostedt493762f2009-03-23 17:12:36 -0400264#define PROFILE_RECORDS_SIZE \
265 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
266
267#define PROFILES_PER_PAGE \
268 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
269
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400270static int ftrace_profile_bits __read_mostly;
271static int ftrace_profile_enabled __read_mostly;
272
273/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400274static DEFINE_MUTEX(ftrace_profile_lock);
275
Steven Rostedtcafb1682009-03-24 20:50:39 -0400276static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400277
278#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
279
Steven Rostedt493762f2009-03-23 17:12:36 -0400280static void *
281function_stat_next(void *v, int idx)
282{
283 struct ftrace_profile *rec = v;
284 struct ftrace_profile_page *pg;
285
286 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
287
288 again:
Li Zefan0296e422009-06-26 11:15:37 +0800289 if (idx != 0)
290 rec++;
291
Steven Rostedt493762f2009-03-23 17:12:36 -0400292 if ((void *)rec >= (void *)&pg->records[pg->index]) {
293 pg = pg->next;
294 if (!pg)
295 return NULL;
296 rec = &pg->records[0];
297 if (!rec->counter)
298 goto again;
299 }
300
301 return rec;
302}
303
304static void *function_stat_start(struct tracer_stat *trace)
305{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400306 struct ftrace_profile_stat *stat =
307 container_of(trace, struct ftrace_profile_stat, stat);
308
309 if (!stat || !stat->start)
310 return NULL;
311
312 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400313}
314
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400315#ifdef CONFIG_FUNCTION_GRAPH_TRACER
316/* function graph compares on total time */
317static int function_stat_cmp(void *p1, void *p2)
318{
319 struct ftrace_profile *a = p1;
320 struct ftrace_profile *b = p2;
321
322 if (a->time < b->time)
323 return -1;
324 if (a->time > b->time)
325 return 1;
326 else
327 return 0;
328}
329#else
330/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400331static int function_stat_cmp(void *p1, void *p2)
332{
333 struct ftrace_profile *a = p1;
334 struct ftrace_profile *b = p2;
335
336 if (a->counter < b->counter)
337 return -1;
338 if (a->counter > b->counter)
339 return 1;
340 else
341 return 0;
342}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400343#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400344
345static int function_stat_headers(struct seq_file *m)
346{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400347#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400348 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400349 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400350 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400351 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400352#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400353 seq_printf(m, " Function Hit\n"
354 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400355#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400356 return 0;
357}
358
359static int function_stat_show(struct seq_file *m, void *v)
360{
361 struct ftrace_profile *rec = v;
362 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800363 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400364#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400365 static struct trace_seq s;
366 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400367 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400368#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800369 mutex_lock(&ftrace_profile_lock);
370
371 /* we raced with function_profile_reset() */
372 if (unlikely(rec->counter == 0)) {
373 ret = -EBUSY;
374 goto out;
375 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400376
377 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400378 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400379
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400380#ifdef CONFIG_FUNCTION_GRAPH_TRACER
381 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400382 avg = rec->time;
383 do_div(avg, rec->counter);
384
Chase Douglase330b3b2010-04-26 14:02:05 -0400385 /* Sample standard deviation (s^2) */
386 if (rec->counter <= 1)
387 stddev = 0;
388 else {
389 stddev = rec->time_squared - rec->counter * avg * avg;
390 /*
391 * Divide only 1000 for ns^2 -> us^2 conversion.
392 * trace_print_graph_duration will divide 1000 again.
393 */
394 do_div(stddev, (rec->counter - 1) * 1000);
395 }
396
Steven Rostedt34886c82009-03-25 21:00:47 -0400397 trace_seq_init(&s);
398 trace_print_graph_duration(rec->time, &s);
399 trace_seq_puts(&s, " ");
400 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400401 trace_seq_puts(&s, " ");
402 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400403 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400404#endif
405 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800406out:
407 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400408
Li Zefan3aaba202010-08-23 16:50:12 +0800409 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400410}
411
Steven Rostedtcafb1682009-03-24 20:50:39 -0400412static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400413{
414 struct ftrace_profile_page *pg;
415
Steven Rostedtcafb1682009-03-24 20:50:39 -0400416 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400417
418 while (pg) {
419 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
420 pg->index = 0;
421 pg = pg->next;
422 }
423
Steven Rostedtcafb1682009-03-24 20:50:39 -0400424 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400425 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
426}
427
Steven Rostedtcafb1682009-03-24 20:50:39 -0400428int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400429{
430 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400431 int functions;
432 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400433 int i;
434
435 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400436 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400437 return 0;
438
Steven Rostedtcafb1682009-03-24 20:50:39 -0400439 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
440 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400441 return -ENOMEM;
442
Steven Rostedt318e0a72009-03-25 20:06:34 -0400443#ifdef CONFIG_DYNAMIC_FTRACE
444 functions = ftrace_update_tot_cnt;
445#else
446 /*
447 * We do not know the number of functions that exist because
448 * dynamic tracing is what counts them. With past experience
449 * we have around 20K functions. That should be more than enough.
450 * It is highly unlikely we will execute every function in
451 * the kernel.
452 */
453 functions = 20000;
454#endif
455
Steven Rostedtcafb1682009-03-24 20:50:39 -0400456 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400457
Steven Rostedt318e0a72009-03-25 20:06:34 -0400458 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
459
460 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400461 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400462 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400463 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400464 pg = pg->next;
465 }
466
467 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400468
469 out_free:
470 pg = stat->start;
471 while (pg) {
472 unsigned long tmp = (unsigned long)pg;
473
474 pg = pg->next;
475 free_page(tmp);
476 }
477
478 free_page((unsigned long)stat->pages);
479 stat->pages = NULL;
480 stat->start = NULL;
481
482 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400483}
484
Steven Rostedtcafb1682009-03-24 20:50:39 -0400485static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400486{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400487 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400488 int size;
489
Steven Rostedtcafb1682009-03-24 20:50:39 -0400490 stat = &per_cpu(ftrace_profile_stats, cpu);
491
492 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400493 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400494 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400495 return 0;
496 }
497
498 /*
499 * We are profiling all functions, but usually only a few thousand
500 * functions are hit. We'll make a hash of 1024 items.
501 */
502 size = FTRACE_PROFILE_HASH_SIZE;
503
Steven Rostedtcafb1682009-03-24 20:50:39 -0400504 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400505
Steven Rostedtcafb1682009-03-24 20:50:39 -0400506 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400507 return -ENOMEM;
508
Steven Rostedtcafb1682009-03-24 20:50:39 -0400509 if (!ftrace_profile_bits) {
510 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400511
Steven Rostedtcafb1682009-03-24 20:50:39 -0400512 for (; size; size >>= 1)
513 ftrace_profile_bits++;
514 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400515
Steven Rostedt318e0a72009-03-25 20:06:34 -0400516 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400517 if (ftrace_profile_pages_init(stat) < 0) {
518 kfree(stat->hash);
519 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400520 return -ENOMEM;
521 }
522
523 return 0;
524}
525
Steven Rostedtcafb1682009-03-24 20:50:39 -0400526static int ftrace_profile_init(void)
527{
528 int cpu;
529 int ret = 0;
530
531 for_each_online_cpu(cpu) {
532 ret = ftrace_profile_init_cpu(cpu);
533 if (ret)
534 break;
535 }
536
537 return ret;
538}
539
Steven Rostedt493762f2009-03-23 17:12:36 -0400540/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400541static struct ftrace_profile *
542ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400543{
544 struct ftrace_profile *rec;
545 struct hlist_head *hhd;
546 struct hlist_node *n;
547 unsigned long key;
548
549 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400550 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400551
552 if (hlist_empty(hhd))
553 return NULL;
554
555 hlist_for_each_entry_rcu(rec, n, hhd, node) {
556 if (rec->ip == ip)
557 return rec;
558 }
559
560 return NULL;
561}
562
Steven Rostedtcafb1682009-03-24 20:50:39 -0400563static void ftrace_add_profile(struct ftrace_profile_stat *stat,
564 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400565{
566 unsigned long key;
567
568 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400569 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400570}
571
Steven Rostedt318e0a72009-03-25 20:06:34 -0400572/*
573 * The memory is already allocated, this simply finds a new record to use.
574 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400575static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400576ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400577{
578 struct ftrace_profile *rec = NULL;
579
Steven Rostedt318e0a72009-03-25 20:06:34 -0400580 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400581 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400582 goto out;
583
Steven Rostedt493762f2009-03-23 17:12:36 -0400584 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400585 * Try to find the function again since an NMI
586 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400587 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400588 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400589 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400590 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400591
Steven Rostedtcafb1682009-03-24 20:50:39 -0400592 if (stat->pages->index == PROFILES_PER_PAGE) {
593 if (!stat->pages->next)
594 goto out;
595 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400596 }
597
Steven Rostedtcafb1682009-03-24 20:50:39 -0400598 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400599 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400600 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400601
Steven Rostedt493762f2009-03-23 17:12:36 -0400602 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400603 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400604
605 return rec;
606}
607
Steven Rostedt493762f2009-03-23 17:12:36 -0400608static void
609function_profile_call(unsigned long ip, unsigned long parent_ip)
610{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400611 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400612 struct ftrace_profile *rec;
613 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400614
615 if (!ftrace_profile_enabled)
616 return;
617
Steven Rostedt493762f2009-03-23 17:12:36 -0400618 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400619
620 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400621 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400622 goto out;
623
624 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400625 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400626 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400627 if (!rec)
628 goto out;
629 }
630
631 rec->counter++;
632 out:
633 local_irq_restore(flags);
634}
635
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400636#ifdef CONFIG_FUNCTION_GRAPH_TRACER
637static int profile_graph_entry(struct ftrace_graph_ent *trace)
638{
639 function_profile_call(trace->func, 0);
640 return 1;
641}
642
643static void profile_graph_return(struct ftrace_graph_ret *trace)
644{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400645 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400646 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400647 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400648 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400649
650 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400651 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400652 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400653 goto out;
654
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400655 /* If the calltime was zero'd ignore it */
656 if (!trace->calltime)
657 goto out;
658
Steven Rostedta2a16d62009-03-24 23:17:58 -0400659 calltime = trace->rettime - trace->calltime;
660
661 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
662 int index;
663
664 index = trace->depth;
665
666 /* Append this call time to the parent time to subtract */
667 if (index)
668 current->ret_stack[index - 1].subtime += calltime;
669
670 if (current->ret_stack[index].subtime < calltime)
671 calltime -= current->ret_stack[index].subtime;
672 else
673 calltime = 0;
674 }
675
Steven Rostedtcafb1682009-03-24 20:50:39 -0400676 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400677 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400678 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400679 rec->time_squared += calltime * calltime;
680 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400681
Steven Rostedtcafb1682009-03-24 20:50:39 -0400682 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400683 local_irq_restore(flags);
684}
685
686static int register_ftrace_profiler(void)
687{
688 return register_ftrace_graph(&profile_graph_return,
689 &profile_graph_entry);
690}
691
692static void unregister_ftrace_profiler(void)
693{
694 unregister_ftrace_graph();
695}
696#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400697static struct ftrace_ops ftrace_profile_ops __read_mostly =
698{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400699 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400700};
701
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400702static int register_ftrace_profiler(void)
703{
704 return register_ftrace_function(&ftrace_profile_ops);
705}
706
707static void unregister_ftrace_profiler(void)
708{
709 unregister_ftrace_function(&ftrace_profile_ops);
710}
711#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
712
Steven Rostedt493762f2009-03-23 17:12:36 -0400713static ssize_t
714ftrace_profile_write(struct file *filp, const char __user *ubuf,
715 size_t cnt, loff_t *ppos)
716{
717 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400718 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400719 int ret;
720
721 if (cnt >= sizeof(buf))
722 return -EINVAL;
723
724 if (copy_from_user(&buf, ubuf, cnt))
725 return -EFAULT;
726
727 buf[cnt] = 0;
728
729 ret = strict_strtoul(buf, 10, &val);
730 if (ret < 0)
731 return ret;
732
733 val = !!val;
734
735 mutex_lock(&ftrace_profile_lock);
736 if (ftrace_profile_enabled ^ val) {
737 if (val) {
738 ret = ftrace_profile_init();
739 if (ret < 0) {
740 cnt = ret;
741 goto out;
742 }
743
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400744 ret = register_ftrace_profiler();
745 if (ret < 0) {
746 cnt = ret;
747 goto out;
748 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 ftrace_profile_enabled = 1;
750 } else {
751 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400752 /*
753 * unregister_ftrace_profiler calls stop_machine
754 * so this acts like an synchronize_sched.
755 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400756 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400757 }
758 }
759 out:
760 mutex_unlock(&ftrace_profile_lock);
761
Jiri Olsacf8517c2009-10-23 19:36:16 -0400762 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400763
764 return cnt;
765}
766
767static ssize_t
768ftrace_profile_read(struct file *filp, char __user *ubuf,
769 size_t cnt, loff_t *ppos)
770{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400771 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400772 int r;
773
774 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
775 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
776}
777
778static const struct file_operations ftrace_profile_fops = {
779 .open = tracing_open_generic,
780 .read = ftrace_profile_read,
781 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200782 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400783};
784
Steven Rostedtcafb1682009-03-24 20:50:39 -0400785/* used to initialize the real stat files */
786static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400787 .name = "functions",
788 .stat_start = function_stat_start,
789 .stat_next = function_stat_next,
790 .stat_cmp = function_stat_cmp,
791 .stat_headers = function_stat_headers,
792 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400793};
794
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400795static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400796{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400797 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400798 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400799 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400800 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400801 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400802
Steven Rostedtcafb1682009-03-24 20:50:39 -0400803 for_each_possible_cpu(cpu) {
804 stat = &per_cpu(ftrace_profile_stats, cpu);
805
806 /* allocate enough for function name + cpu number */
807 name = kmalloc(32, GFP_KERNEL);
808 if (!name) {
809 /*
810 * The files created are permanent, if something happens
811 * we still do not free memory.
812 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400813 WARN(1,
814 "Could not allocate stat file for cpu %d\n",
815 cpu);
816 return;
817 }
818 stat->stat = function_stats;
819 snprintf(name, 32, "function%d", cpu);
820 stat->stat.name = name;
821 ret = register_stat_tracer(&stat->stat);
822 if (ret) {
823 WARN(1,
824 "Could not register function stat for cpu %d\n",
825 cpu);
826 kfree(name);
827 return;
828 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400829 }
830
831 entry = debugfs_create_file("function_profile_enabled", 0644,
832 d_tracer, NULL, &ftrace_profile_fops);
833 if (!entry)
834 pr_warning("Could not create debugfs "
835 "'function_profile_enabled' entry\n");
836}
837
838#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400839static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400840{
841}
842#endif /* CONFIG_FUNCTION_PROFILER */
843
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100844static struct pid * const ftrace_swapper_pid = &init_struct_pid;
845
Steven Rostedt3d083392008-05-12 21:20:42 +0200846#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100847
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400848#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400849# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400850#endif
851
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500852static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
853
Steven Rostedtb6887d72009-02-17 12:32:04 -0500854struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500855 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500856 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500857 unsigned long flags;
858 unsigned long ip;
859 void *data;
860 struct rcu_head rcu;
861};
862
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200863enum {
864 FTRACE_ENABLE_CALLS = (1 << 0),
865 FTRACE_DISABLE_CALLS = (1 << 1),
866 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
Steven Rostedt79e406d2010-09-14 22:19:46 -0400867 FTRACE_START_FUNC_RET = (1 << 3),
868 FTRACE_STOP_FUNC_RET = (1 << 4),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200869};
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400870struct ftrace_func_entry {
871 struct hlist_node hlist;
872 unsigned long ip;
873};
874
875struct ftrace_hash {
876 unsigned long size_bits;
877 struct hlist_head *buckets;
878 unsigned long count;
879};
880
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400881/*
882 * We make these constant because no one should touch them,
883 * but they are used as the default "empty hash", to avoid allocating
884 * it all the time. These are in a read only section such that if
885 * anyone does try to modify it, it will cause an exception.
886 */
887static const struct hlist_head empty_buckets[1];
888static const struct ftrace_hash empty_hash = {
889 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400890};
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400891#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +0200892
Steven Rostedted926f92011-05-03 13:25:24 -0400893enum {
894 FTRACE_OPS_FL_ENABLED = 1,
895};
896
Steven Rostedtf45948e2011-05-02 12:29:25 -0400897struct ftrace_ops global_ops = {
898 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400899 .notrace_hash = EMPTY_HASH,
900 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -0400901};
902
Lai Jiangshane94142a2009-03-13 17:51:27 +0800903static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200904
Steven Rostedt41c52c02008-05-22 11:46:33 -0400905static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200906
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200907struct ftrace_page {
908 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500909 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200910 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700911};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200912
913#define ENTRIES_PER_PAGE \
914 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
915
916/* estimate from running different kernels */
917#define NR_TO_INIT 10000
918
919static struct ftrace_page *ftrace_pages_start;
920static struct ftrace_page *ftrace_pages;
921
Steven Rostedt37ad50842008-05-12 21:20:48 +0200922static struct dyn_ftrace *ftrace_free_records;
923
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400924static struct ftrace_func_entry *
925ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
926{
927 unsigned long key;
928 struct ftrace_func_entry *entry;
929 struct hlist_head *hhd;
930 struct hlist_node *n;
931
932 if (!hash->count)
933 return NULL;
934
935 if (hash->size_bits > 0)
936 key = hash_long(ip, hash->size_bits);
937 else
938 key = 0;
939
940 hhd = &hash->buckets[key];
941
942 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
943 if (entry->ip == ip)
944 return entry;
945 }
946 return NULL;
947}
948
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400949static void __add_hash_entry(struct ftrace_hash *hash,
950 struct ftrace_func_entry *entry)
951{
952 struct hlist_head *hhd;
953 unsigned long key;
954
955 if (hash->size_bits)
956 key = hash_long(entry->ip, hash->size_bits);
957 else
958 key = 0;
959
960 hhd = &hash->buckets[key];
961 hlist_add_head(&entry->hlist, hhd);
962 hash->count++;
963}
964
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400965static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
966{
967 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400968
969 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
970 if (!entry)
971 return -ENOMEM;
972
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400973 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400974 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400975
976 return 0;
977}
978
979static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400980free_hash_entry(struct ftrace_hash *hash,
981 struct ftrace_func_entry *entry)
982{
983 hlist_del(&entry->hlist);
984 kfree(entry);
985 hash->count--;
986}
987
988static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400989remove_hash_entry(struct ftrace_hash *hash,
990 struct ftrace_func_entry *entry)
991{
992 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400993 hash->count--;
994}
995
996static void ftrace_hash_clear(struct ftrace_hash *hash)
997{
998 struct hlist_head *hhd;
999 struct hlist_node *tp, *tn;
1000 struct ftrace_func_entry *entry;
1001 int size = 1 << hash->size_bits;
1002 int i;
1003
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001004 if (!hash->count)
1005 return;
1006
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001007 for (i = 0; i < size; i++) {
1008 hhd = &hash->buckets[i];
1009 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001010 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001011 }
1012 FTRACE_WARN_ON(hash->count);
1013}
1014
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001015static void free_ftrace_hash(struct ftrace_hash *hash)
1016{
1017 if (!hash || hash == EMPTY_HASH)
1018 return;
1019 ftrace_hash_clear(hash);
1020 kfree(hash->buckets);
1021 kfree(hash);
1022}
1023
1024static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1025{
1026 struct ftrace_hash *hash;
1027 int size;
1028
1029 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1030 if (!hash)
1031 return NULL;
1032
1033 size = 1 << size_bits;
1034 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1035
1036 if (!hash->buckets) {
1037 kfree(hash);
1038 return NULL;
1039 }
1040
1041 hash->size_bits = size_bits;
1042
1043 return hash;
1044}
1045
1046static struct ftrace_hash *
1047alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1048{
1049 struct ftrace_func_entry *entry;
1050 struct ftrace_hash *new_hash;
1051 struct hlist_node *tp;
1052 int size;
1053 int ret;
1054 int i;
1055
1056 new_hash = alloc_ftrace_hash(size_bits);
1057 if (!new_hash)
1058 return NULL;
1059
1060 /* Empty hash? */
1061 if (!hash || !hash->count)
1062 return new_hash;
1063
1064 size = 1 << hash->size_bits;
1065 for (i = 0; i < size; i++) {
1066 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1067 ret = add_hash_entry(new_hash, entry->ip);
1068 if (ret < 0)
1069 goto free_hash;
1070 }
1071 }
1072
1073 FTRACE_WARN_ON(new_hash->count != hash->count);
1074
1075 return new_hash;
1076
1077 free_hash:
1078 free_ftrace_hash(new_hash);
1079 return NULL;
1080}
1081
1082static int
1083ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src)
1084{
1085 struct ftrace_func_entry *entry;
1086 struct hlist_node *tp, *tn;
1087 struct hlist_head *hhd;
1088 struct ftrace_hash *hash = *dst;
1089 unsigned long key;
1090 int size = src->count;
1091 int bits = 0;
1092 int i;
1093
1094 /*
1095 * If the new source is empty, just free dst and assign it
1096 * the empty_hash.
1097 */
1098 if (!src->count) {
1099 free_ftrace_hash(*dst);
1100 *dst = EMPTY_HASH;
1101 return 0;
1102 }
1103
1104 ftrace_hash_clear(hash);
1105
1106 /*
1107 * Make the hash size about 1/2 the # found
1108 */
1109 for (size /= 2; size; size >>= 1)
1110 bits++;
1111
1112 /* Don't allocate too much */
1113 if (bits > FTRACE_HASH_MAX_BITS)
1114 bits = FTRACE_HASH_MAX_BITS;
1115
1116 /* We can't modify the empty_hash */
1117 if (hash == EMPTY_HASH) {
1118 /* Create a new hash */
1119 *dst = alloc_ftrace_hash(bits);
1120 if (!*dst) {
1121 *dst = EMPTY_HASH;
1122 return -ENOMEM;
1123 }
1124 hash = *dst;
1125 } else {
1126 size = 1 << bits;
1127
1128 /* Use the old hash, but create new buckets */
1129 hhd = kzalloc(sizeof(*hhd) * size, GFP_KERNEL);
1130 if (!hhd)
1131 return -ENOMEM;
1132
1133 kfree(hash->buckets);
1134 hash->buckets = hhd;
1135 hash->size_bits = bits;
1136 }
1137
1138 size = 1 << src->size_bits;
1139 for (i = 0; i < size; i++) {
1140 hhd = &src->buckets[i];
1141 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1142 if (bits > 0)
1143 key = hash_long(entry->ip, bits);
1144 else
1145 key = 0;
1146 remove_hash_entry(src, entry);
1147 __add_hash_entry(hash, entry);
1148 }
1149 }
1150
1151 return 0;
1152}
1153
Steven Rostedt265c8312009-02-13 12:43:56 -05001154/*
1155 * This is a double for. Do not use 'break' to break out of the loop,
1156 * you must use a goto.
1157 */
1158#define do_for_each_ftrace_rec(pg, rec) \
1159 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1160 int _____i; \
1161 for (_____i = 0; _____i < pg->index; _____i++) { \
1162 rec = &pg->records[_____i];
1163
1164#define while_for_each_ftrace_rec() \
1165 } \
1166 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301167
Steven Rostedted926f92011-05-03 13:25:24 -04001168static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1169 int filter_hash,
1170 bool inc)
1171{
1172 struct ftrace_hash *hash;
1173 struct ftrace_hash *other_hash;
1174 struct ftrace_page *pg;
1175 struct dyn_ftrace *rec;
1176 int count = 0;
1177 int all = 0;
1178
1179 /* Only update if the ops has been registered */
1180 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1181 return;
1182
1183 /*
1184 * In the filter_hash case:
1185 * If the count is zero, we update all records.
1186 * Otherwise we just update the items in the hash.
1187 *
1188 * In the notrace_hash case:
1189 * We enable the update in the hash.
1190 * As disabling notrace means enabling the tracing,
1191 * and enabling notrace means disabling, the inc variable
1192 * gets inversed.
1193 */
1194 if (filter_hash) {
1195 hash = ops->filter_hash;
1196 other_hash = ops->notrace_hash;
1197 if (!hash->count)
1198 all = 1;
1199 } else {
1200 inc = !inc;
1201 hash = ops->notrace_hash;
1202 other_hash = ops->filter_hash;
1203 /*
1204 * If the notrace hash has no items,
1205 * then there's nothing to do.
1206 */
1207 if (!hash->count)
1208 return;
1209 }
1210
1211 do_for_each_ftrace_rec(pg, rec) {
1212 int in_other_hash = 0;
1213 int in_hash = 0;
1214 int match = 0;
1215
1216 if (all) {
1217 /*
1218 * Only the filter_hash affects all records.
1219 * Update if the record is not in the notrace hash.
1220 */
1221 if (!ftrace_lookup_ip(other_hash, rec->ip))
1222 match = 1;
1223 } else {
1224 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1225 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1226
1227 /*
1228 *
1229 */
1230 if (filter_hash && in_hash && !in_other_hash)
1231 match = 1;
1232 else if (!filter_hash && in_hash &&
1233 (in_other_hash || !other_hash->count))
1234 match = 1;
1235 }
1236 if (!match)
1237 continue;
1238
1239 if (inc) {
1240 rec->flags++;
1241 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1242 return;
1243 } else {
1244 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1245 return;
1246 rec->flags--;
1247 }
1248 count++;
1249 /* Shortcut, if we handled all records, we are done. */
1250 if (!all && count == hash->count)
1251 return;
1252 } while_for_each_ftrace_rec();
1253}
1254
1255static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1256 int filter_hash)
1257{
1258 __ftrace_hash_rec_update(ops, filter_hash, 0);
1259}
1260
1261static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1262 int filter_hash)
1263{
1264 __ftrace_hash_rec_update(ops, filter_hash, 1);
1265}
1266
Ingo Molnare309b412008-05-12 21:20:51 +02001267static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad50842008-05-12 21:20:48 +02001268{
Lai Jiangshanee000b72009-03-24 13:38:06 +08001269 rec->freelist = ftrace_free_records;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001270 ftrace_free_records = rec;
1271 rec->flags |= FTRACE_FL_FREE;
1272}
1273
Ingo Molnare309b412008-05-12 21:20:51 +02001274static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001275{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001276 struct dyn_ftrace *rec;
1277
1278 /* First check for freed records */
1279 if (ftrace_free_records) {
1280 rec = ftrace_free_records;
1281
Steven Rostedt37ad50842008-05-12 21:20:48 +02001282 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt6912896e2008-10-23 09:33:03 -04001283 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad50842008-05-12 21:20:48 +02001284 ftrace_free_records = NULL;
1285 return NULL;
1286 }
1287
Lai Jiangshanee000b72009-03-24 13:38:06 +08001288 ftrace_free_records = rec->freelist;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001289 memset(rec, 0, sizeof(*rec));
1290 return rec;
1291 }
1292
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001293 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001294 if (!ftrace_pages->next) {
1295 /* allocate another page */
1296 ftrace_pages->next =
1297 (void *)get_zeroed_page(GFP_KERNEL);
1298 if (!ftrace_pages->next)
1299 return NULL;
1300 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001301 ftrace_pages = ftrace_pages->next;
1302 }
1303
1304 return &ftrace_pages->records[ftrace_pages->index++];
1305}
1306
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001307static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001308ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001309{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001310 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001311
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001312 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001313 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001314
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001315 rec = ftrace_alloc_dyn_node(ip);
1316 if (!rec)
1317 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001318
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001319 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001320 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001321 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001322
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001323 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001324}
1325
Steven Rostedt05736a42008-09-22 14:55:47 -07001326static void print_ip_ins(const char *fmt, unsigned char *p)
1327{
1328 int i;
1329
1330 printk(KERN_CONT "%s", fmt);
1331
1332 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1333 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1334}
1335
Steven Rostedt31e88902008-11-14 16:21:19 -08001336static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001337{
1338 switch (failed) {
1339 case -EFAULT:
1340 FTRACE_WARN_ON_ONCE(1);
1341 pr_info("ftrace faulted on modifying ");
1342 print_ip_sym(ip);
1343 break;
1344 case -EINVAL:
1345 FTRACE_WARN_ON_ONCE(1);
1346 pr_info("ftrace failed to modify ");
1347 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001348 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001349 printk(KERN_CONT "\n");
1350 break;
1351 case -EPERM:
1352 FTRACE_WARN_ON_ONCE(1);
1353 pr_info("ftrace faulted on writing ");
1354 print_ip_sym(ip);
1355 break;
1356 default:
1357 FTRACE_WARN_ON_ONCE(1);
1358 pr_info("ftrace faulted on unknown error ");
1359 print_ip_sym(ip);
1360 }
1361}
1362
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001363
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001364/* Return 1 if the address range is reserved for ftrace */
1365int ftrace_text_reserved(void *start, void *end)
1366{
1367 struct dyn_ftrace *rec;
1368 struct ftrace_page *pg;
1369
1370 do_for_each_ftrace_rec(pg, rec) {
1371 if (rec->ip <= (unsigned long)end &&
1372 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1373 return 1;
1374 } while_for_each_ftrace_rec();
1375 return 0;
1376}
1377
1378
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301379static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001380__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001381{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001382 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001383 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001384
Shaohua Lif0001202009-01-09 11:29:42 +08001385 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001386
Steven Rostedt982c3502008-11-15 16:31:41 -05001387 /*
Steven Rostedted926f92011-05-03 13:25:24 -04001388 * If we are enabling tracing:
Steven Rostedt982c3502008-11-15 16:31:41 -05001389 *
Steven Rostedted926f92011-05-03 13:25:24 -04001390 * If the record has a ref count, then we need to enable it
1391 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001392 *
Steven Rostedted926f92011-05-03 13:25:24 -04001393 * Otherwise we make sure its disabled.
1394 *
1395 * If we are disabling tracing, then disable all records that
1396 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001397 */
Steven Rostedted926f92011-05-03 13:25:24 -04001398 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1399 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001400
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001401 /* If the state of this record hasn't changed, then do nothing */
1402 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1403 return 0;
1404
1405 if (flag) {
1406 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001407 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001408 }
1409
1410 rec->flags &= ~FTRACE_FL_ENABLED;
1411 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001412}
1413
1414static void ftrace_replace_code(int enable)
1415{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001416 struct dyn_ftrace *rec;
1417 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001418 int failed;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001419
Steven Rostedt45a4a232011-04-21 23:16:46 -04001420 if (unlikely(ftrace_disabled))
1421 return;
1422
Steven Rostedt265c8312009-02-13 12:43:56 -05001423 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001424 /* Skip over free records */
1425 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05001426 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001427
Steven Rostedt265c8312009-02-13 12:43:56 -05001428 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001429 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001430 ftrace_bug(failed, rec->ip);
1431 /* Stop processing */
1432 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001433 }
1434 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001435}
1436
Ingo Molnare309b412008-05-12 21:20:51 +02001437static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001438ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001439{
1440 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001441 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001442
1443 ip = rec->ip;
1444
Steven Rostedt45a4a232011-04-21 23:16:46 -04001445 if (unlikely(ftrace_disabled))
1446 return 0;
1447
Shaohua Li25aac9d2009-01-09 11:29:40 +08001448 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001449 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001450 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301451 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001452 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301453 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001454}
1455
Steven Rostedt000ab692009-02-17 13:35:06 -05001456/*
1457 * archs can override this function if they must do something
1458 * before the modifying code is performed.
1459 */
1460int __weak ftrace_arch_code_modify_prepare(void)
1461{
1462 return 0;
1463}
1464
1465/*
1466 * archs can override this function if they must do something
1467 * after the modifying code is performed.
1468 */
1469int __weak ftrace_arch_code_modify_post_process(void)
1470{
1471 return 0;
1472}
1473
Ingo Molnare309b412008-05-12 21:20:51 +02001474static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001475{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001476 int *command = data;
1477
Steven Rostedta3583242008-11-11 15:01:42 -05001478 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001479 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001480 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001481 ftrace_replace_code(0);
1482
1483 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1484 ftrace_update_ftrace_func(ftrace_trace_function);
1485
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001486 if (*command & FTRACE_START_FUNC_RET)
1487 ftrace_enable_ftrace_graph_caller();
1488 else if (*command & FTRACE_STOP_FUNC_RET)
1489 ftrace_disable_ftrace_graph_caller();
1490
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001491 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001492}
1493
Ingo Molnare309b412008-05-12 21:20:51 +02001494static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001495{
Steven Rostedt000ab692009-02-17 13:35:06 -05001496 int ret;
1497
1498 ret = ftrace_arch_code_modify_prepare();
1499 FTRACE_WARN_ON(ret);
1500 if (ret)
1501 return;
1502
Rusty Russell784e2d72008-07-28 12:16:31 -05001503 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001504
1505 ret = ftrace_arch_code_modify_post_process();
1506 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001507}
1508
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001509static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001510static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001511
1512static void ftrace_startup_enable(int command)
1513{
1514 if (saved_ftrace_func != ftrace_trace_function) {
1515 saved_ftrace_func = ftrace_trace_function;
1516 command |= FTRACE_UPDATE_TRACE_FUNC;
1517 }
1518
1519 if (!command || !ftrace_enabled)
1520 return;
1521
1522 ftrace_run_update_code(command);
1523}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001524
Steven Rostedtbd69c302011-05-03 21:55:54 -04001525static void ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001526{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001527 if (unlikely(ftrace_disabled))
1528 return;
1529
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001530 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001531 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001532
Steven Rostedted926f92011-05-03 13:25:24 -04001533 ops->flags |= FTRACE_OPS_FL_ENABLED;
1534 if (ftrace_start_up == 1)
1535 ftrace_hash_rec_enable(ops, 1);
1536
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001537 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001538}
1539
Steven Rostedtbd69c302011-05-03 21:55:54 -04001540static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001541{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001542 if (unlikely(ftrace_disabled))
1543 return;
1544
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001545 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001546 /*
1547 * Just warn in case of unbalance, no need to kill ftrace, it's not
1548 * critical but the ftrace_call callers may be never nopped again after
1549 * further ftrace uses.
1550 */
1551 WARN_ON_ONCE(ftrace_start_up < 0);
1552
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001553 if (!ftrace_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04001554 ftrace_hash_rec_disable(ops, 1);
1555
1556 if (!ftrace_start_up) {
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001557 command |= FTRACE_DISABLE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04001558 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
1559 }
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001560
1561 if (saved_ftrace_func != ftrace_trace_function) {
1562 saved_ftrace_func = ftrace_trace_function;
1563 command |= FTRACE_UPDATE_TRACE_FUNC;
1564 }
1565
1566 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001567 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001568
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001569 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001570}
1571
Ingo Molnare309b412008-05-12 21:20:51 +02001572static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001573{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001574 if (unlikely(ftrace_disabled))
1575 return;
1576
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001577 /* Force update next time */
1578 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001579 /* ftrace_start_up is true if we want ftrace running */
1580 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001581 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001582}
1583
Ingo Molnare309b412008-05-12 21:20:51 +02001584static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001585{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001586 if (unlikely(ftrace_disabled))
1587 return;
1588
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001589 /* ftrace_start_up is true if ftrace is running */
1590 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001591 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001592}
1593
Steven Rostedt3d083392008-05-12 21:20:42 +02001594static cycle_t ftrace_update_time;
1595static unsigned long ftrace_update_cnt;
1596unsigned long ftrace_update_tot_cnt;
1597
Steven Rostedt31e88902008-11-14 16:21:19 -08001598static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001599{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001600 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301601 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001602
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001603 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001604 ftrace_update_cnt = 0;
1605
Lai Jiangshane94142a2009-03-13 17:51:27 +08001606 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301607
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001608 /* If something went wrong, bail without enabling anything */
1609 if (unlikely(ftrace_disabled))
1610 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001611
Lai Jiangshane94142a2009-03-13 17:51:27 +08001612 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001613 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001614 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301615
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001616 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001617 * Do the initial record conversion from mcount jump
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001618 * to the NOP instructions.
1619 */
1620 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001621 ftrace_free_rec(p);
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001622 /* Game over */
1623 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001624 }
1625
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001626 ftrace_update_cnt++;
1627
1628 /*
1629 * If the tracing is enabled, go ahead and enable the record.
1630 *
1631 * The reason not to enable the record immediatelly is the
1632 * inherent check of ftrace_make_nop/ftrace_make_call for
1633 * correct previous instructions. Making first the NOP
1634 * conversion puts the module to the correct state, thus
1635 * passing the ftrace_make_call check.
1636 */
1637 if (ftrace_start_up) {
1638 int failed = __ftrace_replace_code(p, 1);
1639 if (failed) {
1640 ftrace_bug(failed, p->ip);
1641 ftrace_free_rec(p);
1642 }
1643 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001644 }
1645
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001646 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001647 ftrace_update_time = stop - start;
1648 ftrace_update_tot_cnt += ftrace_update_cnt;
1649
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001650 return 0;
1651}
1652
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001653static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001654{
1655 struct ftrace_page *pg;
1656 int cnt;
1657 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001658
1659 /* allocate a few pages */
1660 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1661 if (!ftrace_pages_start)
1662 return -1;
1663
1664 /*
1665 * Allocate a few more pages.
1666 *
1667 * TODO: have some parser search vmlinux before
1668 * final linking to find all calls to ftrace.
1669 * Then we can:
1670 * a) know how many pages to allocate.
1671 * and/or
1672 * b) set up the table then.
1673 *
1674 * The dynamic code is still necessary for
1675 * modules.
1676 */
1677
1678 pg = ftrace_pages = ftrace_pages_start;
1679
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001680 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001681 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001682 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001683
1684 for (i = 0; i < cnt; i++) {
1685 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1686
1687 /* If we fail, we'll try later anyway */
1688 if (!pg->next)
1689 break;
1690
1691 pg = pg->next;
1692 }
1693
1694 return 0;
1695}
1696
Steven Rostedt5072c592008-05-12 21:20:43 +02001697enum {
1698 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001699 FTRACE_ITER_NOTRACE = (1 << 1),
Steven Rostedt3499e462011-04-21 22:59:12 -04001700 FTRACE_ITER_PRINTALL = (1 << 2),
1701 FTRACE_ITER_HASH = (1 << 3),
Steven Rostedt647bcd02011-05-03 14:39:21 -04001702 FTRACE_ITER_ENABLED = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02001703};
1704
1705#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1706
1707struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001708 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001709 loff_t func_pos;
1710 struct ftrace_page *pg;
1711 struct dyn_ftrace *func;
1712 struct ftrace_func_probe *probe;
1713 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001714 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001715 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001716 int hidx;
1717 int idx;
1718 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02001719};
1720
Ingo Molnare309b412008-05-12 21:20:51 +02001721static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001722t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001723{
1724 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001725 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001726 struct hlist_head *hhd;
1727
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001728 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001729 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001730
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001731 if (iter->probe)
1732 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001733 retry:
1734 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1735 return NULL;
1736
1737 hhd = &ftrace_func_hash[iter->hidx];
1738
1739 if (hlist_empty(hhd)) {
1740 iter->hidx++;
1741 hnd = NULL;
1742 goto retry;
1743 }
1744
1745 if (!hnd)
1746 hnd = hhd->first;
1747 else {
1748 hnd = hnd->next;
1749 if (!hnd) {
1750 iter->hidx++;
1751 goto retry;
1752 }
1753 }
1754
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001755 if (WARN_ON_ONCE(!hnd))
1756 return NULL;
1757
1758 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1759
1760 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001761}
1762
1763static void *t_hash_start(struct seq_file *m, loff_t *pos)
1764{
1765 struct ftrace_iterator *iter = m->private;
1766 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001767 loff_t l;
1768
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001769 if (iter->func_pos > *pos)
1770 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001771
Li Zefand82d6242009-06-24 09:54:54 +08001772 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001773 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001774 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08001775 if (!p)
1776 break;
1777 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001778 if (!p)
1779 return NULL;
1780
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001781 /* Only set this if we have an item */
1782 iter->flags |= FTRACE_ITER_HASH;
1783
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001784 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001785}
1786
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001787static int
1788t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001789{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001790 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001791
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001792 rec = iter->probe;
1793 if (WARN_ON_ONCE(!rec))
1794 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001795
Steven Rostedt809dcf22009-02-16 23:06:01 -05001796 if (rec->ops->print)
1797 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1798
Steven Rostedtb375a112009-09-17 00:05:58 -04001799 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001800
1801 if (rec->data)
1802 seq_printf(m, ":%p", rec->data);
1803 seq_putc(m, '\n');
1804
1805 return 0;
1806}
1807
1808static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001809t_next(struct seq_file *m, void *v, loff_t *pos)
1810{
1811 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04001812 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02001813 struct dyn_ftrace *rec = NULL;
1814
Steven Rostedt45a4a232011-04-21 23:16:46 -04001815 if (unlikely(ftrace_disabled))
1816 return NULL;
1817
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001818 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001819 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001820
Steven Rostedt5072c592008-05-12 21:20:43 +02001821 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01001822 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02001823
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001824 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001825 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001826
Steven Rostedt5072c592008-05-12 21:20:43 +02001827 retry:
1828 if (iter->idx >= iter->pg->index) {
1829 if (iter->pg->next) {
1830 iter->pg = iter->pg->next;
1831 iter->idx = 0;
1832 goto retry;
1833 }
1834 } else {
1835 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001836 if ((rec->flags & FTRACE_FL_FREE) ||
1837
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001838 ((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04001839 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001840
Steven Rostedt41c52c02008-05-22 11:46:33 -04001841 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04001842 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
1843
1844 ((iter->flags & FTRACE_ITER_ENABLED) &&
1845 !(rec->flags & ~FTRACE_FL_MASK))) {
1846
Steven Rostedt5072c592008-05-12 21:20:43 +02001847 rec = NULL;
1848 goto retry;
1849 }
1850 }
1851
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001852 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001853 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001854
1855 iter->func = rec;
1856
1857 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02001858}
1859
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001860static void reset_iter_read(struct ftrace_iterator *iter)
1861{
1862 iter->pos = 0;
1863 iter->func_pos = 0;
1864 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02001865}
1866
1867static void *t_start(struct seq_file *m, loff_t *pos)
1868{
1869 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04001870 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02001871 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001872 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001873
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001874 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04001875
1876 if (unlikely(ftrace_disabled))
1877 return NULL;
1878
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001879 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001880 * If an lseek was done, then reset and start from beginning.
1881 */
1882 if (*pos < iter->pos)
1883 reset_iter_read(iter);
1884
1885 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001886 * For set_ftrace_filter reading, if we have the filter
1887 * off, we can short cut and just print out that all
1888 * functions are enabled.
1889 */
Steven Rostedtf45948e2011-05-02 12:29:25 -04001890 if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001891 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001892 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001893 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07001894 /* reset in case of seek/pread */
1895 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001896 return iter;
1897 }
1898
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001899 if (iter->flags & FTRACE_ITER_HASH)
1900 return t_hash_start(m, pos);
1901
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001902 /*
1903 * Unfortunately, we need to restart at ftrace_pages_start
1904 * every time we let go of the ftrace_mutex. This is because
1905 * those pointers can change without the lock.
1906 */
Li Zefan694ce0a2009-06-24 09:54:19 +08001907 iter->pg = ftrace_pages_start;
1908 iter->idx = 0;
1909 for (l = 0; l <= *pos; ) {
1910 p = t_next(m, p, &l);
1911 if (!p)
1912 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001913 }
walimis5821e1b2008-11-15 15:19:06 +08001914
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001915 if (!p) {
1916 if (iter->flags & FTRACE_ITER_FILTER)
1917 return t_hash_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001918
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001919 return NULL;
1920 }
1921
1922 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02001923}
1924
1925static void t_stop(struct seq_file *m, void *p)
1926{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001927 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001928}
1929
1930static int t_show(struct seq_file *m, void *v)
1931{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001932 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001933 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02001934
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001935 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001936 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001937
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001938 if (iter->flags & FTRACE_ITER_PRINTALL) {
1939 seq_printf(m, "#### all functions enabled ####\n");
1940 return 0;
1941 }
1942
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001943 rec = iter->func;
1944
Steven Rostedt5072c592008-05-12 21:20:43 +02001945 if (!rec)
1946 return 0;
1947
Steven Rostedt647bcd02011-05-03 14:39:21 -04001948 seq_printf(m, "%ps", (void *)rec->ip);
1949 if (iter->flags & FTRACE_ITER_ENABLED)
1950 seq_printf(m, " (%ld)",
1951 rec->flags & ~FTRACE_FL_MASK);
1952 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02001953
1954 return 0;
1955}
1956
James Morris88e9d342009-09-22 16:43:43 -07001957static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001958 .start = t_start,
1959 .next = t_next,
1960 .stop = t_stop,
1961 .show = t_show,
1962};
1963
Ingo Molnare309b412008-05-12 21:20:51 +02001964static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001965ftrace_avail_open(struct inode *inode, struct file *file)
1966{
1967 struct ftrace_iterator *iter;
1968 int ret;
1969
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001970 if (unlikely(ftrace_disabled))
1971 return -ENODEV;
1972
Steven Rostedt5072c592008-05-12 21:20:43 +02001973 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1974 if (!iter)
1975 return -ENOMEM;
1976
1977 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001978
1979 ret = seq_open(file, &show_ftrace_seq_ops);
1980 if (!ret) {
1981 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001982
Steven Rostedt5072c592008-05-12 21:20:43 +02001983 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001984 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001985 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001986 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001987
1988 return ret;
1989}
1990
Steven Rostedt647bcd02011-05-03 14:39:21 -04001991static int
1992ftrace_enabled_open(struct inode *inode, struct file *file)
1993{
1994 struct ftrace_iterator *iter;
1995 int ret;
1996
1997 if (unlikely(ftrace_disabled))
1998 return -ENODEV;
1999
2000 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2001 if (!iter)
2002 return -ENOMEM;
2003
2004 iter->pg = ftrace_pages_start;
2005 iter->flags = FTRACE_ITER_ENABLED;
2006
2007 ret = seq_open(file, &show_ftrace_seq_ops);
2008 if (!ret) {
2009 struct seq_file *m = file->private_data;
2010
2011 m->private = iter;
2012 } else {
2013 kfree(iter);
2014 }
2015
2016 return ret;
2017}
2018
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002019static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002020{
Steven Rostedt52baf112009-02-14 01:15:39 -05002021 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002022 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002023 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002024}
2025
Ingo Molnare309b412008-05-12 21:20:51 +02002026static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002027ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002028 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002029{
2030 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002031 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002032 int ret = 0;
2033
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002034 if (unlikely(ftrace_disabled))
2035 return -ENODEV;
2036
Steven Rostedt5072c592008-05-12 21:20:43 +02002037 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2038 if (!iter)
2039 return -ENOMEM;
2040
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002041 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2042 kfree(iter);
2043 return -ENOMEM;
2044 }
2045
Steven Rostedtf45948e2011-05-02 12:29:25 -04002046 if (flag & FTRACE_ITER_NOTRACE)
2047 hash = ops->notrace_hash;
2048 else
2049 hash = ops->filter_hash;
2050
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002051 iter->ops = ops;
2052 iter->flags = flag;
2053
2054 if (file->f_mode & FMODE_WRITE) {
2055 mutex_lock(&ftrace_lock);
2056 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2057 mutex_unlock(&ftrace_lock);
2058
2059 if (!iter->hash) {
2060 trace_parser_put(&iter->parser);
2061 kfree(iter);
2062 return -ENOMEM;
2063 }
2064 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002065
Steven Rostedt41c52c02008-05-22 11:46:33 -04002066 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002067
Steven Rostedt5072c592008-05-12 21:20:43 +02002068 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002069 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002070 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002071
2072 if (file->f_mode & FMODE_READ) {
2073 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002074
2075 ret = seq_open(file, &show_ftrace_seq_ops);
2076 if (!ret) {
2077 struct seq_file *m = file->private_data;
2078 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002079 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002080 /* Failed */
2081 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002082 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002083 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002084 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002085 } else
2086 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002087 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002088
2089 return ret;
2090}
2091
Steven Rostedt41c52c02008-05-22 11:46:33 -04002092static int
2093ftrace_filter_open(struct inode *inode, struct file *file)
2094{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002095 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002096 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002097}
2098
2099static int
2100ftrace_notrace_open(struct inode *inode, struct file *file)
2101{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002102 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002103 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002104}
2105
Ingo Molnare309b412008-05-12 21:20:51 +02002106static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002107ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02002108{
2109 loff_t ret;
2110
2111 if (file->f_mode & FMODE_READ)
2112 ret = seq_lseek(file, offset, origin);
2113 else
2114 file->f_pos = ret = 1;
2115
2116 return ret;
2117}
2118
Steven Rostedt64e7c442009-02-13 17:08:48 -05002119static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002120{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002121 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002122 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002123
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002124 switch (type) {
2125 case MATCH_FULL:
2126 if (strcmp(str, regex) == 0)
2127 matched = 1;
2128 break;
2129 case MATCH_FRONT_ONLY:
2130 if (strncmp(str, regex, len) == 0)
2131 matched = 1;
2132 break;
2133 case MATCH_MIDDLE_ONLY:
2134 if (strstr(str, regex))
2135 matched = 1;
2136 break;
2137 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002138 slen = strlen(str);
2139 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002140 matched = 1;
2141 break;
2142 }
2143
2144 return matched;
2145}
2146
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002147static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002148enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002149{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002150 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002151 int ret = 0;
2152
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002153 entry = ftrace_lookup_ip(hash, rec->ip);
2154 if (not) {
2155 /* Do nothing if it doesn't exist */
2156 if (!entry)
2157 return 0;
2158
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002159 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002160 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002161 /* Do nothing if it exists */
2162 if (entry)
2163 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002164
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002165 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002166 }
2167 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002168}
2169
Steven Rostedt64e7c442009-02-13 17:08:48 -05002170static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002171ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2172 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002173{
2174 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002175 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002176
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002177 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2178
2179 if (mod) {
2180 /* module lookup requires matching the module */
2181 if (!modname || strcmp(modname, mod))
2182 return 0;
2183
2184 /* blank search means to match all funcs in the mod */
2185 if (!len)
2186 return 1;
2187 }
2188
Steven Rostedt64e7c442009-02-13 17:08:48 -05002189 return ftrace_match(str, regex, len, type);
2190}
2191
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002192static int
2193match_records(struct ftrace_hash *hash, char *buff,
2194 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002195{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002196 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002197 struct ftrace_page *pg;
2198 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002199 int type = MATCH_FULL;
2200 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002201 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002202 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002203
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002204 if (len) {
2205 type = filter_parse_regex(buff, len, &search, &not);
2206 search_len = strlen(search);
2207 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002208
Steven Rostedt52baf112009-02-14 01:15:39 -05002209 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002210
2211 if (unlikely(ftrace_disabled))
2212 goto out_unlock;
2213
Steven Rostedt265c8312009-02-13 12:43:56 -05002214 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02002215
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002216 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002217 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002218 if (ret < 0) {
2219 found = ret;
2220 goto out_unlock;
2221 }
Li Zefan311d16d2009-12-08 11:15:11 +08002222 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002223 }
2224 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002225 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002226 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002227
2228 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002229}
2230
Steven Rostedt64e7c442009-02-13 17:08:48 -05002231static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002232ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002233{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002234 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002235}
2236
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002237static int
2238ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002239{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002240 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002241
Steven Rostedt64e7c442009-02-13 17:08:48 -05002242 /* blank or '*' mean the same */
2243 if (strcmp(buff, "*") == 0)
2244 buff[0] = 0;
2245
2246 /* handle the case of 'dont filter this module' */
2247 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2248 buff[0] = 0;
2249 not = 1;
2250 }
2251
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002252 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002253}
2254
Steven Rostedtf6180772009-02-14 00:40:25 -05002255/*
2256 * We register the module command as a template to show others how
2257 * to register the a command as well.
2258 */
2259
2260static int
2261ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
2262{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002263 struct ftrace_ops *ops = &global_ops;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002264 struct ftrace_hash *hash;
Steven Rostedtf6180772009-02-14 00:40:25 -05002265 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002266 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002267
2268 /*
2269 * cmd == 'mod' because we only registered this func
2270 * for the 'mod' ftrace_func_command.
2271 * But if you register one func with multiple commands,
2272 * you can tell which command was used by the cmd
2273 * parameter.
2274 */
2275
2276 /* we must have a module name */
2277 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002278 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002279
2280 mod = strsep(&param, ":");
2281 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002282 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002283
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002284 if (enable)
Steven Rostedtf45948e2011-05-02 12:29:25 -04002285 hash = ops->filter_hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002286 else
Steven Rostedtf45948e2011-05-02 12:29:25 -04002287 hash = ops->notrace_hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002288
2289 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002290 if (!ret)
2291 ret = -EINVAL;
2292 if (ret < 0)
2293 return ret;
2294
2295 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002296}
2297
2298static struct ftrace_func_command ftrace_mod_cmd = {
2299 .name = "mod",
2300 .func = ftrace_mod_callback,
2301};
2302
2303static int __init ftrace_mod_cmd_init(void)
2304{
2305 return register_ftrace_command(&ftrace_mod_cmd);
2306}
2307device_initcall(ftrace_mod_cmd_init);
2308
Steven Rostedt59df055f2009-02-14 15:29:06 -05002309static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002310function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002311{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002312 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002313 struct hlist_head *hhd;
2314 struct hlist_node *n;
2315 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002316
2317 key = hash_long(ip, FTRACE_HASH_BITS);
2318
2319 hhd = &ftrace_func_hash[key];
2320
2321 if (hlist_empty(hhd))
2322 return;
2323
2324 /*
2325 * Disable preemption for these calls to prevent a RCU grace
2326 * period. This syncs the hash iteration and freeing of items
2327 * on the hash. rcu_read_lock is too dangerous here.
2328 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002329 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002330 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2331 if (entry->ip == ip)
2332 entry->ops->func(ip, parent_ip, &entry->data);
2333 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002334 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002335}
2336
Steven Rostedtb6887d72009-02-17 12:32:04 -05002337static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002338{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002339 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002340};
2341
Steven Rostedtb6887d72009-02-17 12:32:04 -05002342static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002343
Steven Rostedtb6887d72009-02-17 12:32:04 -05002344static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002345{
2346 int i;
2347
Steven Rostedtb6887d72009-02-17 12:32:04 -05002348 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002349 return;
2350
2351 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2352 struct hlist_head *hhd = &ftrace_func_hash[i];
2353 if (hhd->first)
2354 break;
2355 }
2356 /* Nothing registered? */
2357 if (i == FTRACE_FUNC_HASHSIZE)
2358 return;
2359
Steven Rostedtb6887d72009-02-17 12:32:04 -05002360 __register_ftrace_function(&trace_probe_ops);
Steven Rostedtbd69c302011-05-03 21:55:54 -04002361 ftrace_startup(&global_ops, 0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05002362 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002363}
2364
Steven Rostedtb6887d72009-02-17 12:32:04 -05002365static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002366{
2367 int i;
2368
Steven Rostedtb6887d72009-02-17 12:32:04 -05002369 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002370 return;
2371
2372 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2373 struct hlist_head *hhd = &ftrace_func_hash[i];
2374 if (hhd->first)
2375 return;
2376 }
2377
2378 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002379 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedtbd69c302011-05-03 21:55:54 -04002380 ftrace_shutdown(&global_ops, 0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05002381 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002382}
2383
2384
2385static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2386{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002387 struct ftrace_func_probe *entry =
2388 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002389
2390 if (entry->ops->free)
2391 entry->ops->free(&entry->data);
2392 kfree(entry);
2393}
2394
2395
2396int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002397register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002398 void *data)
2399{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002400 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002401 struct ftrace_page *pg;
2402 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002403 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002404 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002405 int count = 0;
2406 char *search;
2407
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002408 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002409 len = strlen(search);
2410
Steven Rostedtb6887d72009-02-17 12:32:04 -05002411 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002412 if (WARN_ON(not))
2413 return -EINVAL;
2414
2415 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002416
Steven Rostedt45a4a232011-04-21 23:16:46 -04002417 if (unlikely(ftrace_disabled))
2418 goto out_unlock;
2419
Steven Rostedt59df055f2009-02-14 15:29:06 -05002420 do_for_each_ftrace_rec(pg, rec) {
2421
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002422 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002423 continue;
2424
2425 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2426 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002427 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002428 if (!count)
2429 count = -ENOMEM;
2430 goto out_unlock;
2431 }
2432
2433 count++;
2434
2435 entry->data = data;
2436
2437 /*
2438 * The caller might want to do something special
2439 * for each function we find. We call the callback
2440 * to give the caller an opportunity to do so.
2441 */
2442 if (ops->callback) {
2443 if (ops->callback(rec->ip, &entry->data) < 0) {
2444 /* caller does not like this func */
2445 kfree(entry);
2446 continue;
2447 }
2448 }
2449
2450 entry->ops = ops;
2451 entry->ip = rec->ip;
2452
2453 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2454 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2455
2456 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002457 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002458
2459 out_unlock:
2460 mutex_unlock(&ftrace_lock);
2461
2462 return count;
2463}
2464
2465enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002466 PROBE_TEST_FUNC = 1,
2467 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002468};
2469
2470static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002471__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002472 void *data, int flags)
2473{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002474 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002475 struct hlist_node *n, *tmp;
2476 char str[KSYM_SYMBOL_LEN];
2477 int type = MATCH_FULL;
2478 int i, len = 0;
2479 char *search;
2480
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002481 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002482 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002483 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002484 int not;
2485
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002486 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002487 len = strlen(search);
2488
Steven Rostedtb6887d72009-02-17 12:32:04 -05002489 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002490 if (WARN_ON(not))
2491 return;
2492 }
2493
2494 mutex_lock(&ftrace_lock);
2495 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2496 struct hlist_head *hhd = &ftrace_func_hash[i];
2497
2498 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2499
2500 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002501 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002502 continue;
2503
Steven Rostedtb6887d72009-02-17 12:32:04 -05002504 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002505 continue;
2506
2507 /* do this last, since it is the most expensive */
2508 if (glob) {
2509 kallsyms_lookup(entry->ip, NULL, NULL,
2510 NULL, str);
2511 if (!ftrace_match(str, glob, len, type))
2512 continue;
2513 }
2514
2515 hlist_del(&entry->node);
2516 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2517 }
2518 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002519 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002520 mutex_unlock(&ftrace_lock);
2521}
2522
2523void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002524unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002525 void *data)
2526{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002527 __unregister_ftrace_function_probe(glob, ops, data,
2528 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002529}
2530
2531void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002532unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002533{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002534 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002535}
2536
Steven Rostedtb6887d72009-02-17 12:32:04 -05002537void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002538{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002539 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002540}
2541
Steven Rostedtf6180772009-02-14 00:40:25 -05002542static LIST_HEAD(ftrace_commands);
2543static DEFINE_MUTEX(ftrace_cmd_mutex);
2544
2545int register_ftrace_command(struct ftrace_func_command *cmd)
2546{
2547 struct ftrace_func_command *p;
2548 int ret = 0;
2549
2550 mutex_lock(&ftrace_cmd_mutex);
2551 list_for_each_entry(p, &ftrace_commands, list) {
2552 if (strcmp(cmd->name, p->name) == 0) {
2553 ret = -EBUSY;
2554 goto out_unlock;
2555 }
2556 }
2557 list_add(&cmd->list, &ftrace_commands);
2558 out_unlock:
2559 mutex_unlock(&ftrace_cmd_mutex);
2560
2561 return ret;
2562}
2563
2564int unregister_ftrace_command(struct ftrace_func_command *cmd)
2565{
2566 struct ftrace_func_command *p, *n;
2567 int ret = -ENODEV;
2568
2569 mutex_lock(&ftrace_cmd_mutex);
2570 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2571 if (strcmp(cmd->name, p->name) == 0) {
2572 ret = 0;
2573 list_del_init(&p->list);
2574 goto out_unlock;
2575 }
2576 }
2577 out_unlock:
2578 mutex_unlock(&ftrace_cmd_mutex);
2579
2580 return ret;
2581}
2582
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002583static int ftrace_process_regex(struct ftrace_hash *hash,
2584 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002585{
Steven Rostedtf6180772009-02-14 00:40:25 -05002586 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002587 struct ftrace_func_command *p;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002588 int ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002589
2590 func = strsep(&next, ":");
2591
2592 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002593 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002594 if (!ret)
2595 ret = -EINVAL;
2596 if (ret < 0)
2597 return ret;
2598 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002599 }
2600
Steven Rostedtf6180772009-02-14 00:40:25 -05002601 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002602
2603 command = strsep(&next, ":");
2604
Steven Rostedtf6180772009-02-14 00:40:25 -05002605 mutex_lock(&ftrace_cmd_mutex);
2606 list_for_each_entry(p, &ftrace_commands, list) {
2607 if (strcmp(p->name, command) == 0) {
2608 ret = p->func(func, command, next, enable);
2609 goto out_unlock;
2610 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002611 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002612 out_unlock:
2613 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002614
Steven Rostedtf6180772009-02-14 00:40:25 -05002615 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002616}
2617
Ingo Molnare309b412008-05-12 21:20:51 +02002618static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002619ftrace_regex_write(struct file *file, const char __user *ubuf,
2620 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002621{
2622 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002623 struct trace_parser *parser;
2624 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002625
Li Zefan4ba79782009-09-22 13:52:20 +08002626 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002627 return 0;
2628
Steven Rostedt41c52c02008-05-22 11:46:33 -04002629 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002630
Steven Rostedt45a4a232011-04-21 23:16:46 -04002631 ret = -ENODEV;
2632 if (unlikely(ftrace_disabled))
2633 goto out_unlock;
2634
Steven Rostedt5072c592008-05-12 21:20:43 +02002635 if (file->f_mode & FMODE_READ) {
2636 struct seq_file *m = file->private_data;
2637 iter = m->private;
2638 } else
2639 iter = file->private_data;
2640
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002641 parser = &iter->parser;
2642 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002643
Li Zefan4ba79782009-09-22 13:52:20 +08002644 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002645 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002646 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002647 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002648 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002649 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002650 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002651 }
2652
Steven Rostedt5072c592008-05-12 21:20:43 +02002653 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002654out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002655 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002656
Steven Rostedt5072c592008-05-12 21:20:43 +02002657 return ret;
2658}
2659
Steven Rostedt41c52c02008-05-22 11:46:33 -04002660static ssize_t
2661ftrace_filter_write(struct file *file, const char __user *ubuf,
2662 size_t cnt, loff_t *ppos)
2663{
2664 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2665}
2666
2667static ssize_t
2668ftrace_notrace_write(struct file *file, const char __user *ubuf,
2669 size_t cnt, loff_t *ppos)
2670{
2671 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2672}
2673
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002674static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002675ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2676 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04002677{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002678 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002679 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002680 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002681
Steven Rostedt41c52c02008-05-22 11:46:33 -04002682 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002683 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002684
Steven Rostedtf45948e2011-05-02 12:29:25 -04002685 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002686 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002687 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002688 orig_hash = &ops->notrace_hash;
2689
2690 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
2691 if (!hash)
2692 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002693
Steven Rostedt41c52c02008-05-22 11:46:33 -04002694 mutex_lock(&ftrace_regex_lock);
2695 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002696 ftrace_filter_reset(hash);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002697 if (buf)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002698 ftrace_match_records(hash, buf, len);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002699
2700 mutex_lock(&ftrace_lock);
2701 ret = ftrace_hash_move(orig_hash, hash);
2702 mutex_unlock(&ftrace_lock);
2703
Steven Rostedt41c52c02008-05-22 11:46:33 -04002704 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002705
2706 free_ftrace_hash(hash);
2707 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002708}
2709
Steven Rostedt77a2b372008-05-12 21:20:45 +02002710/**
2711 * ftrace_set_filter - set a function to filter on in ftrace
2712 * @buf - the string that holds the function filter text.
2713 * @len - the length of the string.
2714 * @reset - non zero to reset all filters before applying this filter.
2715 *
2716 * Filters denote which functions should be enabled when tracing is enabled.
2717 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2718 */
Ingo Molnare309b412008-05-12 21:20:51 +02002719void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002720{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002721 ftrace_set_regex(&global_ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002722}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002723
Steven Rostedt41c52c02008-05-22 11:46:33 -04002724/**
2725 * ftrace_set_notrace - set a function to not trace in ftrace
2726 * @buf - the string that holds the function notrace text.
2727 * @len - the length of the string.
2728 * @reset - non zero to reset all filters before applying this filter.
2729 *
2730 * Notrace Filters denote which functions should not be enabled when tracing
2731 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2732 * for tracing.
2733 */
2734void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2735{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002736 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002737}
2738
Steven Rostedt2af15d62009-05-28 13:37:24 -04002739/*
2740 * command line interface to allow users to set filters on boot up.
2741 */
2742#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2743static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2744static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2745
2746static int __init set_ftrace_notrace(char *str)
2747{
2748 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2749 return 1;
2750}
2751__setup("ftrace_notrace=", set_ftrace_notrace);
2752
2753static int __init set_ftrace_filter(char *str)
2754{
2755 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2756 return 1;
2757}
2758__setup("ftrace_filter=", set_ftrace_filter);
2759
Stefan Assmann369bc182009-10-12 22:17:21 +02002760#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002761static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002762static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2763
Stefan Assmann369bc182009-10-12 22:17:21 +02002764static int __init set_graph_function(char *str)
2765{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002766 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002767 return 1;
2768}
2769__setup("ftrace_graph_filter=", set_graph_function);
2770
2771static void __init set_ftrace_early_graph(char *buf)
2772{
2773 int ret;
2774 char *func;
2775
2776 while (buf) {
2777 func = strsep(&buf, ",");
2778 /* we allow only one expression at a time */
2779 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2780 func);
2781 if (ret)
2782 printk(KERN_DEBUG "ftrace: function %s not "
2783 "traceable\n", func);
2784 }
2785}
2786#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2787
Steven Rostedtf45948e2011-05-02 12:29:25 -04002788static void __init
2789set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04002790{
2791 char *func;
2792
2793 while (buf) {
2794 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04002795 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04002796 }
2797}
2798
2799static void __init set_ftrace_early_filters(void)
2800{
2801 if (ftrace_filter_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04002802 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04002803 if (ftrace_notrace_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04002804 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002805#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2806 if (ftrace_graph_buf[0])
2807 set_ftrace_early_graph(ftrace_graph_buf);
2808#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002809}
2810
Ingo Molnare309b412008-05-12 21:20:51 +02002811static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002812ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002813{
2814 struct seq_file *m = (struct seq_file *)file->private_data;
2815 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002816 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002817 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04002818 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002819 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02002820
Steven Rostedt41c52c02008-05-22 11:46:33 -04002821 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002822 if (file->f_mode & FMODE_READ) {
2823 iter = m->private;
2824
2825 seq_release(inode, file);
2826 } else
2827 iter = file->private_data;
2828
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002829 parser = &iter->parser;
2830 if (trace_parser_loaded(parser)) {
2831 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002832 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02002833 }
2834
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002835 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002836
Steven Rostedt058e2972011-04-29 22:35:33 -04002837 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04002838 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
2839
2840 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002841 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04002842 else
2843 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002844
Steven Rostedt058e2972011-04-29 22:35:33 -04002845 mutex_lock(&ftrace_lock);
Steven Rostedted926f92011-05-03 13:25:24 -04002846 /*
2847 * Remove the current set, update the hash and add
2848 * them back.
2849 */
2850 ftrace_hash_rec_disable(iter->ops, filter_hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002851 ret = ftrace_hash_move(orig_hash, iter->hash);
Steven Rostedted926f92011-05-03 13:25:24 -04002852 if (!ret) {
2853 ftrace_hash_rec_enable(iter->ops, filter_hash);
2854 if (iter->ops->flags & FTRACE_OPS_FL_ENABLED
2855 && ftrace_enabled)
2856 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2857 }
Steven Rostedt058e2972011-04-29 22:35:33 -04002858 mutex_unlock(&ftrace_lock);
2859 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002860 free_ftrace_hash(iter->hash);
2861 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04002862
Steven Rostedt41c52c02008-05-22 11:46:33 -04002863 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002864 return 0;
2865}
2866
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002867static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002868 .open = ftrace_avail_open,
2869 .read = seq_read,
2870 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002871 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002872};
2873
Steven Rostedt647bcd02011-05-03 14:39:21 -04002874static const struct file_operations ftrace_enabled_fops = {
2875 .open = ftrace_enabled_open,
2876 .read = seq_read,
2877 .llseek = seq_lseek,
2878 .release = seq_release_private,
2879};
2880
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002881static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002882 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002883 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002884 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002885 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002886 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02002887};
2888
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002889static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002890 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002891 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002892 .write = ftrace_notrace_write,
2893 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002894 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002895};
2896
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002897#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2898
2899static DEFINE_MUTEX(graph_lock);
2900
2901int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002902int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002903unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2904
2905static void *
Li Zefan85951842009-06-24 09:54:00 +08002906__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002907{
Li Zefan85951842009-06-24 09:54:00 +08002908 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002909 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002910 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002911}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002912
Li Zefan85951842009-06-24 09:54:00 +08002913static void *
2914g_next(struct seq_file *m, void *v, loff_t *pos)
2915{
2916 (*pos)++;
2917 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002918}
2919
2920static void *g_start(struct seq_file *m, loff_t *pos)
2921{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002922 mutex_lock(&graph_lock);
2923
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002924 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002925 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002926 return (void *)1;
2927
Li Zefan85951842009-06-24 09:54:00 +08002928 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002929}
2930
2931static void g_stop(struct seq_file *m, void *p)
2932{
2933 mutex_unlock(&graph_lock);
2934}
2935
2936static int g_show(struct seq_file *m, void *v)
2937{
2938 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002939
2940 if (!ptr)
2941 return 0;
2942
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002943 if (ptr == (unsigned long *)1) {
2944 seq_printf(m, "#### all functions enabled ####\n");
2945 return 0;
2946 }
2947
Steven Rostedtb375a112009-09-17 00:05:58 -04002948 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002949
2950 return 0;
2951}
2952
James Morris88e9d342009-09-22 16:43:43 -07002953static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002954 .start = g_start,
2955 .next = g_next,
2956 .stop = g_stop,
2957 .show = g_show,
2958};
2959
2960static int
2961ftrace_graph_open(struct inode *inode, struct file *file)
2962{
2963 int ret = 0;
2964
2965 if (unlikely(ftrace_disabled))
2966 return -ENODEV;
2967
2968 mutex_lock(&graph_lock);
2969 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002970 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002971 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002972 ftrace_graph_count = 0;
2973 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2974 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002975 mutex_unlock(&graph_lock);
2976
Li Zefana4ec5e02009-09-18 14:06:28 +08002977 if (file->f_mode & FMODE_READ)
2978 ret = seq_open(file, &ftrace_graph_seq_ops);
2979
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002980 return ret;
2981}
2982
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002983static int
Li Zefan87827112009-07-23 11:29:11 +08002984ftrace_graph_release(struct inode *inode, struct file *file)
2985{
2986 if (file->f_mode & FMODE_READ)
2987 seq_release(inode, file);
2988 return 0;
2989}
2990
2991static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002992ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002993{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002994 struct dyn_ftrace *rec;
2995 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002996 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002997 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002998 int type, not;
2999 char *search;
3000 bool exists;
3001 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003002
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003003 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003004 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003005 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3006 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003007
3008 search_len = strlen(search);
3009
Steven Rostedt52baf112009-02-14 01:15:39 -05003010 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003011
3012 if (unlikely(ftrace_disabled)) {
3013 mutex_unlock(&ftrace_lock);
3014 return -ENODEV;
3015 }
3016
Steven Rostedt265c8312009-02-13 12:43:56 -05003017 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003018
Steven Rostedt45a4a232011-04-21 23:16:46 -04003019 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05003020 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003021
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003022 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003023 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003024 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003025 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003026 if (array[i] == rec->ip) {
3027 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003028 break;
3029 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003030 }
3031
3032 if (!not) {
3033 fail = 0;
3034 if (!exists) {
3035 array[(*idx)++] = rec->ip;
3036 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3037 goto out;
3038 }
3039 } else {
3040 if (exists) {
3041 array[i] = array[--(*idx)];
3042 array[*idx] = 0;
3043 fail = 0;
3044 }
3045 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003046 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003047 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003048out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003049 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003050
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003051 if (fail)
3052 return -EINVAL;
3053
3054 ftrace_graph_filter_enabled = 1;
3055 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003056}
3057
3058static ssize_t
3059ftrace_graph_write(struct file *file, const char __user *ubuf,
3060 size_t cnt, loff_t *ppos)
3061{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003062 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003063 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003064
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003065 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003066 return 0;
3067
3068 mutex_lock(&graph_lock);
3069
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003070 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3071 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003072 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003073 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003074
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003075 read = trace_get_user(&parser, ubuf, cnt, ppos);
3076
Li Zefan4ba79782009-09-22 13:52:20 +08003077 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003078 parser.buffer[parser.idx] = 0;
3079
3080 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003081 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003082 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003083 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003084 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003085 }
3086
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003087 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003088
3089out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003090 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003091out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003092 mutex_unlock(&graph_lock);
3093
3094 return ret;
3095}
3096
3097static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003098 .open = ftrace_graph_open,
3099 .read = seq_read,
3100 .write = ftrace_graph_write,
3101 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003102 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003103};
3104#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3105
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003106static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003107{
Steven Rostedt5072c592008-05-12 21:20:43 +02003108
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003109 trace_create_file("available_filter_functions", 0444,
3110 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003111
Steven Rostedt647bcd02011-05-03 14:39:21 -04003112 trace_create_file("enabled_functions", 0444,
3113 d_tracer, NULL, &ftrace_enabled_fops);
3114
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003115 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3116 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003117
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003118 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003119 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003120
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003121#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003122 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003123 NULL,
3124 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003125#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3126
Steven Rostedt5072c592008-05-12 21:20:43 +02003127 return 0;
3128}
3129
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003130static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003131 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003132 unsigned long *end)
3133{
3134 unsigned long *p;
3135 unsigned long addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003136
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003137 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003138 p = start;
3139 while (p < end) {
3140 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003141 /*
3142 * Some architecture linkers will pad between
3143 * the different mcount_loc sections of different
3144 * object files to satisfy alignments.
3145 * Skip any NULL pointers.
3146 */
3147 if (!addr)
3148 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003149 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003150 }
3151
Steven Rostedt31e88902008-11-14 16:21:19 -08003152 ftrace_update_code(mod);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003153 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003154
3155 return 0;
3156}
3157
Steven Rostedt93eb6772009-04-15 13:24:06 -04003158#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003159void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003160{
3161 struct dyn_ftrace *rec;
3162 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003163
Steven Rostedt93eb6772009-04-15 13:24:06 -04003164 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003165
3166 if (ftrace_disabled)
3167 goto out_unlock;
3168
Steven Rostedt93eb6772009-04-15 13:24:06 -04003169 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003170 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003171 /*
3172 * rec->ip is changed in ftrace_free_rec()
3173 * It should not between s and e if record was freed.
3174 */
3175 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
3176 ftrace_free_rec(rec);
3177 }
3178 } while_for_each_ftrace_rec();
Steven Rostedt45a4a232011-04-21 23:16:46 -04003179 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003180 mutex_unlock(&ftrace_lock);
3181}
3182
3183static void ftrace_init_module(struct module *mod,
3184 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003185{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003186 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003187 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003188 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003189}
3190
Steven Rostedt93eb6772009-04-15 13:24:06 -04003191static int ftrace_module_notify(struct notifier_block *self,
3192 unsigned long val, void *data)
3193{
3194 struct module *mod = data;
3195
3196 switch (val) {
3197 case MODULE_STATE_COMING:
3198 ftrace_init_module(mod, mod->ftrace_callsites,
3199 mod->ftrace_callsites +
3200 mod->num_ftrace_callsites);
3201 break;
3202 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003203 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003204 break;
3205 }
3206
3207 return 0;
3208}
3209#else
3210static int ftrace_module_notify(struct notifier_block *self,
3211 unsigned long val, void *data)
3212{
3213 return 0;
3214}
3215#endif /* CONFIG_MODULES */
3216
3217struct notifier_block ftrace_module_nb = {
3218 .notifier_call = ftrace_module_notify,
3219 .priority = 0,
3220};
3221
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003222extern unsigned long __start_mcount_loc[];
3223extern unsigned long __stop_mcount_loc[];
3224
3225void __init ftrace_init(void)
3226{
3227 unsigned long count, addr, flags;
3228 int ret;
3229
3230 /* Keep the ftrace pointer to the stub */
3231 addr = (unsigned long)ftrace_stub;
3232
3233 local_irq_save(flags);
3234 ftrace_dyn_arch_init(&addr);
3235 local_irq_restore(flags);
3236
3237 /* ftrace_dyn_arch_init places the return code in addr */
3238 if (addr)
3239 goto failed;
3240
3241 count = __stop_mcount_loc - __start_mcount_loc;
3242
3243 ret = ftrace_dyn_table_alloc(count);
3244 if (ret)
3245 goto failed;
3246
3247 last_ftrace_enabled = ftrace_enabled = 1;
3248
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003249 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003250 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003251 __stop_mcount_loc);
3252
Steven Rostedt93eb6772009-04-15 13:24:06 -04003253 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003254 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003255 pr_warning("Failed to register trace ftrace module notifier\n");
3256
Steven Rostedt2af15d62009-05-28 13:37:24 -04003257 set_ftrace_early_filters();
3258
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003259 return;
3260 failed:
3261 ftrace_disabled = 1;
3262}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003263
Steven Rostedt3d083392008-05-12 21:20:42 +02003264#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003265
Steven Rostedtbd69c302011-05-03 21:55:54 -04003266struct ftrace_ops global_ops = {
3267 .func = ftrace_stub,
3268};
3269
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003270static int __init ftrace_nodyn_init(void)
3271{
3272 ftrace_enabled = 1;
3273 return 0;
3274}
3275device_initcall(ftrace_nodyn_init);
3276
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003277static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3278static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003279/* Keep as macros so we do not need to define the commands */
Steven Rostedtbd69c302011-05-03 21:55:54 -04003280# define ftrace_startup(ops, command) do { } while (0)
3281# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003282# define ftrace_startup_sysctl() do { } while (0)
3283# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02003284#endif /* CONFIG_DYNAMIC_FTRACE */
3285
Steven Rostedte32d8952008-12-04 00:26:41 -05003286static void clear_ftrace_swapper(void)
3287{
3288 struct task_struct *p;
3289 int cpu;
3290
3291 get_online_cpus();
3292 for_each_online_cpu(cpu) {
3293 p = idle_task(cpu);
3294 clear_tsk_trace_trace(p);
3295 }
3296 put_online_cpus();
3297}
3298
3299static void set_ftrace_swapper(void)
3300{
3301 struct task_struct *p;
3302 int cpu;
3303
3304 get_online_cpus();
3305 for_each_online_cpu(cpu) {
3306 p = idle_task(cpu);
3307 set_tsk_trace_trace(p);
3308 }
3309 put_online_cpus();
3310}
3311
3312static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003313{
3314 struct task_struct *p;
3315
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003316 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05003317 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05003318 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05003319 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003320 rcu_read_unlock();
3321
Steven Rostedte32d8952008-12-04 00:26:41 -05003322 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003323}
3324
Steven Rostedte32d8952008-12-04 00:26:41 -05003325static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003326{
3327 struct task_struct *p;
3328
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003329 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003330 do_each_pid_task(pid, PIDTYPE_PID, p) {
3331 set_tsk_trace_trace(p);
3332 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003333 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003334}
3335
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003336static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003337{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003338 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003339 clear_ftrace_swapper();
3340 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003341 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05003342}
3343
3344static void set_ftrace_pid_task(struct pid *pid)
3345{
3346 if (pid == ftrace_swapper_pid)
3347 set_ftrace_swapper();
3348 else
3349 set_ftrace_pid(pid);
3350}
3351
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003352static int ftrace_pid_add(int p)
3353{
3354 struct pid *pid;
3355 struct ftrace_pid *fpid;
3356 int ret = -EINVAL;
3357
3358 mutex_lock(&ftrace_lock);
3359
3360 if (!p)
3361 pid = ftrace_swapper_pid;
3362 else
3363 pid = find_get_pid(p);
3364
3365 if (!pid)
3366 goto out;
3367
3368 ret = 0;
3369
3370 list_for_each_entry(fpid, &ftrace_pids, list)
3371 if (fpid->pid == pid)
3372 goto out_put;
3373
3374 ret = -ENOMEM;
3375
3376 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3377 if (!fpid)
3378 goto out_put;
3379
3380 list_add(&fpid->list, &ftrace_pids);
3381 fpid->pid = pid;
3382
3383 set_ftrace_pid_task(pid);
3384
3385 ftrace_update_pid_func();
3386 ftrace_startup_enable(0);
3387
3388 mutex_unlock(&ftrace_lock);
3389 return 0;
3390
3391out_put:
3392 if (pid != ftrace_swapper_pid)
3393 put_pid(pid);
3394
3395out:
3396 mutex_unlock(&ftrace_lock);
3397 return ret;
3398}
3399
3400static void ftrace_pid_reset(void)
3401{
3402 struct ftrace_pid *fpid, *safe;
3403
3404 mutex_lock(&ftrace_lock);
3405 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3406 struct pid *pid = fpid->pid;
3407
3408 clear_ftrace_pid_task(pid);
3409
3410 list_del(&fpid->list);
3411 kfree(fpid);
3412 }
3413
3414 ftrace_update_pid_func();
3415 ftrace_startup_enable(0);
3416
3417 mutex_unlock(&ftrace_lock);
3418}
3419
3420static void *fpid_start(struct seq_file *m, loff_t *pos)
3421{
3422 mutex_lock(&ftrace_lock);
3423
3424 if (list_empty(&ftrace_pids) && (!*pos))
3425 return (void *) 1;
3426
3427 return seq_list_start(&ftrace_pids, *pos);
3428}
3429
3430static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3431{
3432 if (v == (void *)1)
3433 return NULL;
3434
3435 return seq_list_next(v, &ftrace_pids, pos);
3436}
3437
3438static void fpid_stop(struct seq_file *m, void *p)
3439{
3440 mutex_unlock(&ftrace_lock);
3441}
3442
3443static int fpid_show(struct seq_file *m, void *v)
3444{
3445 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3446
3447 if (v == (void *)1) {
3448 seq_printf(m, "no pid\n");
3449 return 0;
3450 }
3451
3452 if (fpid->pid == ftrace_swapper_pid)
3453 seq_printf(m, "swapper tasks\n");
3454 else
3455 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3456
3457 return 0;
3458}
3459
3460static const struct seq_operations ftrace_pid_sops = {
3461 .start = fpid_start,
3462 .next = fpid_next,
3463 .stop = fpid_stop,
3464 .show = fpid_show,
3465};
3466
3467static int
3468ftrace_pid_open(struct inode *inode, struct file *file)
3469{
3470 int ret = 0;
3471
3472 if ((file->f_mode & FMODE_WRITE) &&
3473 (file->f_flags & O_TRUNC))
3474 ftrace_pid_reset();
3475
3476 if (file->f_mode & FMODE_READ)
3477 ret = seq_open(file, &ftrace_pid_sops);
3478
3479 return ret;
3480}
3481
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003482static ssize_t
3483ftrace_pid_write(struct file *filp, const char __user *ubuf,
3484 size_t cnt, loff_t *ppos)
3485{
Ingo Molnar457dc922009-11-23 11:03:28 +01003486 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003487 long val;
3488 int ret;
3489
3490 if (cnt >= sizeof(buf))
3491 return -EINVAL;
3492
3493 if (copy_from_user(&buf, ubuf, cnt))
3494 return -EFAULT;
3495
3496 buf[cnt] = 0;
3497
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003498 /*
3499 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3500 * to clean the filter quietly.
3501 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003502 tmp = strstrip(buf);
3503 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003504 return 1;
3505
Ingo Molnar457dc922009-11-23 11:03:28 +01003506 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003507 if (ret < 0)
3508 return ret;
3509
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003510 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003511
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003512 return ret ? ret : cnt;
3513}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003514
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003515static int
3516ftrace_pid_release(struct inode *inode, struct file *file)
3517{
3518 if (file->f_mode & FMODE_READ)
3519 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003520
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003521 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003522}
3523
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003524static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003525 .open = ftrace_pid_open,
3526 .write = ftrace_pid_write,
3527 .read = seq_read,
3528 .llseek = seq_lseek,
3529 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003530};
3531
3532static __init int ftrace_init_debugfs(void)
3533{
3534 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003535
3536 d_tracer = tracing_init_dentry();
3537 if (!d_tracer)
3538 return 0;
3539
3540 ftrace_init_dyn_debugfs(d_tracer);
3541
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003542 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3543 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003544
3545 ftrace_profile_debugfs(d_tracer);
3546
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003547 return 0;
3548}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003549fs_initcall(ftrace_init_debugfs);
3550
Steven Rostedt3d083392008-05-12 21:20:42 +02003551/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003552 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003553 *
3554 * This function should be used by panic code. It stops ftrace
3555 * but in a not so nice way. If you need to simply kill ftrace
3556 * from a non-atomic section, use ftrace_kill.
3557 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003558void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003559{
3560 ftrace_disabled = 1;
3561 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003562 clear_ftrace_function();
3563}
3564
3565/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003566 * register_ftrace_function - register a function for profiling
3567 * @ops - ops structure that holds the function for profiling.
3568 *
3569 * Register a function to be called by all functions in the
3570 * kernel.
3571 *
3572 * Note: @ops->func and all the functions it calls must be labeled
3573 * with "notrace", otherwise it will go into a
3574 * recursive loop.
3575 */
3576int register_ftrace_function(struct ftrace_ops *ops)
3577{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003578 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003579
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003580 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003581
Steven Rostedt45a4a232011-04-21 23:16:46 -04003582 if (unlikely(ftrace_disabled))
3583 goto out_unlock;
3584
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003585 ret = __register_ftrace_function(ops);
Steven Rostedtbd69c302011-05-03 21:55:54 -04003586 ftrace_startup(&global_ops, 0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003587
Steven Rostedt45a4a232011-04-21 23:16:46 -04003588 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003589 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003590 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003591}
3592
3593/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003594 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003595 * @ops - ops structure that holds the function to unregister
3596 *
3597 * Unregister a function that was added to be called by ftrace profiling.
3598 */
3599int unregister_ftrace_function(struct ftrace_ops *ops)
3600{
3601 int ret;
3602
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003603 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003604 ret = __unregister_ftrace_function(ops);
Steven Rostedtbd69c302011-05-03 21:55:54 -04003605 ftrace_shutdown(&global_ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003606 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003607
3608 return ret;
3609}
3610
Ingo Molnare309b412008-05-12 21:20:51 +02003611int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003612ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003613 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003614 loff_t *ppos)
3615{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003616 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003617
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003618 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003619
Steven Rostedt45a4a232011-04-21 23:16:46 -04003620 if (unlikely(ftrace_disabled))
3621 goto out;
3622
3623 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003624
Li Zefana32c7762009-06-26 16:55:51 +08003625 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003626 goto out;
3627
Li Zefana32c7762009-06-26 16:55:51 +08003628 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003629
3630 if (ftrace_enabled) {
3631
3632 ftrace_startup_sysctl();
3633
3634 /* we are starting ftrace again */
3635 if (ftrace_list != &ftrace_list_end) {
3636 if (ftrace_list->next == &ftrace_list_end)
3637 ftrace_trace_function = ftrace_list->func;
3638 else
3639 ftrace_trace_function = ftrace_list_func;
3640 }
3641
3642 } else {
3643 /* stopping ftrace calls (just send to ftrace_stub) */
3644 ftrace_trace_function = ftrace_stub;
3645
3646 ftrace_shutdown_sysctl();
3647 }
3648
3649 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003650 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003651 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003652}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003653
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003654#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003655
Steven Rostedt597af812009-04-03 15:24:12 -04003656static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003657static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003658
Steven Rostedte49dc192008-12-02 23:50:05 -05003659int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3660{
3661 return 0;
3662}
3663
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003664/* The callbacks that hook a function */
3665trace_func_graph_ret_t ftrace_graph_return =
3666 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003667trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003668
3669/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3670static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3671{
3672 int i;
3673 int ret = 0;
3674 unsigned long flags;
3675 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3676 struct task_struct *g, *t;
3677
3678 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3679 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3680 * sizeof(struct ftrace_ret_stack),
3681 GFP_KERNEL);
3682 if (!ret_stack_list[i]) {
3683 start = 0;
3684 end = i;
3685 ret = -ENOMEM;
3686 goto free;
3687 }
3688 }
3689
3690 read_lock_irqsave(&tasklist_lock, flags);
3691 do_each_thread(g, t) {
3692 if (start == end) {
3693 ret = -EAGAIN;
3694 goto unlock;
3695 }
3696
3697 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003698 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003699 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003700 t->curr_ret_stack = -1;
3701 /* Make sure the tasks see the -1 first: */
3702 smp_wmb();
3703 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003704 }
3705 } while_each_thread(g, t);
3706
3707unlock:
3708 read_unlock_irqrestore(&tasklist_lock, flags);
3709free:
3710 for (i = start; i < end; i++)
3711 kfree(ret_stack_list[i]);
3712 return ret;
3713}
3714
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003715static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04003716ftrace_graph_probe_sched_switch(void *ignore,
3717 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003718{
3719 unsigned long long timestamp;
3720 int index;
3721
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003722 /*
3723 * Does the user want to count the time a function was asleep.
3724 * If so, do not update the time stamps.
3725 */
3726 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3727 return;
3728
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003729 timestamp = trace_clock_local();
3730
3731 prev->ftrace_timestamp = timestamp;
3732
3733 /* only process tasks that we timestamped */
3734 if (!next->ftrace_timestamp)
3735 return;
3736
3737 /*
3738 * Update all the counters in next to make up for the
3739 * time next was sleeping.
3740 */
3741 timestamp -= next->ftrace_timestamp;
3742
3743 for (index = next->curr_ret_stack; index >= 0; index--)
3744 next->ret_stack[index].calltime += timestamp;
3745}
3746
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003747/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003748static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003749{
3750 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003751 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003752
3753 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3754 sizeof(struct ftrace_ret_stack *),
3755 GFP_KERNEL);
3756
3757 if (!ret_stack_list)
3758 return -ENOMEM;
3759
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003760 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003761 for_each_online_cpu(cpu) {
3762 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05003763 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04003764 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003765
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003766 do {
3767 ret = alloc_retstack_tasklist(ret_stack_list);
3768 } while (ret == -EAGAIN);
3769
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003770 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04003771 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003772 if (ret)
3773 pr_info("ftrace_graph: Couldn't activate tracepoint"
3774 " probe to kernel_sched_switch\n");
3775 }
3776
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003777 kfree(ret_stack_list);
3778 return ret;
3779}
3780
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003781/*
3782 * Hibernation protection.
3783 * The state of the current task is too much unstable during
3784 * suspend/restore to disk. We want to protect against that.
3785 */
3786static int
3787ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3788 void *unused)
3789{
3790 switch (state) {
3791 case PM_HIBERNATION_PREPARE:
3792 pause_graph_tracing();
3793 break;
3794
3795 case PM_POST_HIBERNATION:
3796 unpause_graph_tracing();
3797 break;
3798 }
3799 return NOTIFY_DONE;
3800}
3801
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003802int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3803 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003804{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003805 int ret = 0;
3806
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003807 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003808
Steven Rostedt05ce5812009-03-24 00:18:31 -04003809 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003810 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003811 ret = -EBUSY;
3812 goto out;
3813 }
3814
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003815 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3816 register_pm_notifier(&ftrace_suspend_notifier);
3817
Steven Rostedt597af812009-04-03 15:24:12 -04003818 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003819 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003820 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003821 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003822 goto out;
3823 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003824
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003825 ftrace_graph_return = retfunc;
3826 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003827
Steven Rostedtbd69c302011-05-03 21:55:54 -04003828 ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003829
3830out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003831 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003832 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003833}
3834
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003835void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003836{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003837 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003838
Steven Rostedt597af812009-04-03 15:24:12 -04003839 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003840 goto out;
3841
Steven Rostedt597af812009-04-03 15:24:12 -04003842 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003843 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003844 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04003845 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003846 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04003847 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003848
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003849 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003850 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003851}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003852
Steven Rostedt868baf02011-02-10 21:26:13 -05003853static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
3854
3855static void
3856graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
3857{
3858 atomic_set(&t->tracing_graph_pause, 0);
3859 atomic_set(&t->trace_overrun, 0);
3860 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003861 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05003862 smp_wmb();
3863 t->ret_stack = ret_stack;
3864}
3865
3866/*
3867 * Allocate a return stack for the idle task. May be the first
3868 * time through, or it may be done by CPU hotplug online.
3869 */
3870void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
3871{
3872 t->curr_ret_stack = -1;
3873 /*
3874 * The idle task has no parent, it either has its own
3875 * stack or no stack at all.
3876 */
3877 if (t->ret_stack)
3878 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
3879
3880 if (ftrace_graph_active) {
3881 struct ftrace_ret_stack *ret_stack;
3882
3883 ret_stack = per_cpu(idle_ret_stack, cpu);
3884 if (!ret_stack) {
3885 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3886 * sizeof(struct ftrace_ret_stack),
3887 GFP_KERNEL);
3888 if (!ret_stack)
3889 return;
3890 per_cpu(idle_ret_stack, cpu) = ret_stack;
3891 }
3892 graph_init_task(t, ret_stack);
3893 }
3894}
3895
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003896/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003897void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003898{
Steven Rostedt84047e32009-06-02 16:51:55 -04003899 /* Make sure we do not use the parent ret_stack */
3900 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05003901 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04003902
Steven Rostedt597af812009-04-03 15:24:12 -04003903 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003904 struct ftrace_ret_stack *ret_stack;
3905
3906 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003907 * sizeof(struct ftrace_ret_stack),
3908 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003909 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003910 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05003911 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04003912 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003913}
3914
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003915void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003916{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003917 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3918
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003919 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003920 /* NULL must become visible to IRQs before we free it: */
3921 barrier();
3922
3923 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003924}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003925
3926void ftrace_graph_stop(void)
3927{
3928 ftrace_stop();
3929}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003930#endif