blob: a517a6c40645dc04b7d78c1e2ee986bebf0403f6 [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 Rostedtb448c4e2011-04-29 15:12:32 -040060#define FTRACE_HASH_MAX_BITS 10
Steven Rostedt8fc0c702009-02-16 15:28:00 -050061
Steven Rostedt4eebcc82008-05-12 21:20:48 +020062/* ftrace_enabled is a method to turn ftrace on or off */
63int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020064static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020065
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050066/* Quick disabling of function tracer. */
67int function_trace_stop;
68
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040069/* List for set_ftrace_pid's pids. */
70LIST_HEAD(ftrace_pids);
71struct ftrace_pid {
72 struct list_head list;
73 struct pid *pid;
74};
75
Steven Rostedt4eebcc82008-05-12 21:20:48 +020076/*
77 * ftrace_disabled is set when an anomaly is discovered.
78 * ftrace_disabled is much stronger than ftrace_enabled.
79 */
80static int ftrace_disabled __read_mostly;
81
Steven Rostedt52baf112009-02-14 01:15:39 -050082static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020083
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020084static struct ftrace_ops ftrace_list_end __read_mostly =
85{
Steven Rostedtfb9fb012009-03-25 13:26:41 -040086 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020087};
88
89static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
90ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050091ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050092ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020093
Paul E. McKenney3f379b02010-03-05 15:03:25 -080094/*
95 * Traverse the ftrace_list, invoking all entries. The reason that we
96 * can use rcu_dereference_raw() is that elements removed from this list
97 * are simply leaked, so there is no need to interact with a grace-period
98 * mechanism. The rcu_dereference_raw() calls are needed to handle
99 * concurrent insertions into the ftrace_list.
100 *
101 * Silly Alpha and silly pointer-speculation compiler optimizations!
102 */
Ingo Molnarf2252932008-05-22 10:37:48 +0200103static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200104{
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800105 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200106
107 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200108 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800109 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200110 };
111}
112
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500113static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
114{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500115 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500116 return;
117
118 ftrace_pid_function(ip, parent_ip);
119}
120
121static void set_ftrace_pid_function(ftrace_func_t func)
122{
123 /* do not set ftrace_pid_function to itself! */
124 if (func != ftrace_pid_func)
125 ftrace_pid_function = func;
126}
127
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200128/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200129 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200130 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200131 * This NULLs the ftrace function and in essence stops
132 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200133 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200134void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200135{
Steven Rostedt3d083392008-05-12 21:20:42 +0200136 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500137 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500138 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200139}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200140
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500141#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
142/*
143 * For those archs that do not test ftrace_trace_stop in their
144 * mcount call site, we need to do it from C.
145 */
146static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
147{
148 if (function_trace_stop)
149 return;
150
151 __ftrace_trace_function(ip, parent_ip);
152}
153#endif
154
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400155static void update_ftrace_function(void)
156{
157 ftrace_func_t func;
158
159 /*
160 * If there's only one function registered, then call that
161 * function directly. Otherwise, we need to iterate over the
162 * registered callers.
163 */
164 if (ftrace_list == &ftrace_list_end ||
165 ftrace_list->next == &ftrace_list_end)
166 func = ftrace_list->func;
167 else
168 func = ftrace_list_func;
169
170 /* If we filter on pids, update to use the pid function */
171 if (!list_empty(&ftrace_pids)) {
172 set_ftrace_pid_function(func);
173 func = ftrace_pid_func;
174 }
175#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
176 ftrace_trace_function = func;
177#else
178 __ftrace_trace_function = func;
179 ftrace_trace_function = ftrace_test_stop_func;
180#endif
181}
182
Ingo Molnare309b412008-05-12 21:20:51 +0200183static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200184{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200185 ops->next = ftrace_list;
186 /*
187 * We are entering ops into the ftrace_list but another
188 * CPU might be walking that list. We need to make sure
189 * the ops->next pointer is valid before another CPU sees
190 * the ops pointer included into the ftrace_list.
191 */
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800192 rcu_assign_pointer(ftrace_list, ops);
Steven Rostedt3d083392008-05-12 21:20:42 +0200193
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400194 if (ftrace_enabled)
195 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200196
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200197 return 0;
198}
199
Ingo Molnare309b412008-05-12 21:20:51 +0200200static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200201{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200202 struct ftrace_ops **p;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200203
204 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200205 * If we are removing the last function, then simply point
206 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200207 */
208 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
209 ftrace_trace_function = ftrace_stub;
210 ftrace_list = &ftrace_list_end;
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500211 return 0;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200212 }
213
214 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
215 if (*p == ops)
216 break;
217
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500218 if (*p != ops)
219 return -1;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200220
221 *p = (*p)->next;
222
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400223 if (ftrace_enabled)
224 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200225
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500226 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200227}
228
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500229static void ftrace_update_pid_func(void)
230{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400231 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500232 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900233 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500234
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400235 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500236}
237
Steven Rostedt493762f2009-03-23 17:12:36 -0400238#ifdef CONFIG_FUNCTION_PROFILER
239struct ftrace_profile {
240 struct hlist_node node;
241 unsigned long ip;
242 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400243#ifdef CONFIG_FUNCTION_GRAPH_TRACER
244 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400245 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400246#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400247};
248
249struct ftrace_profile_page {
250 struct ftrace_profile_page *next;
251 unsigned long index;
252 struct ftrace_profile records[];
253};
254
Steven Rostedtcafb1682009-03-24 20:50:39 -0400255struct ftrace_profile_stat {
256 atomic_t disabled;
257 struct hlist_head *hash;
258 struct ftrace_profile_page *pages;
259 struct ftrace_profile_page *start;
260 struct tracer_stat stat;
261};
262
Steven Rostedt493762f2009-03-23 17:12:36 -0400263#define PROFILE_RECORDS_SIZE \
264 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
265
266#define PROFILES_PER_PAGE \
267 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
268
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400269static int ftrace_profile_bits __read_mostly;
270static int ftrace_profile_enabled __read_mostly;
271
272/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400273static DEFINE_MUTEX(ftrace_profile_lock);
274
Steven Rostedtcafb1682009-03-24 20:50:39 -0400275static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400276
277#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
278
Steven Rostedt493762f2009-03-23 17:12:36 -0400279static void *
280function_stat_next(void *v, int idx)
281{
282 struct ftrace_profile *rec = v;
283 struct ftrace_profile_page *pg;
284
285 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
286
287 again:
Li Zefan0296e422009-06-26 11:15:37 +0800288 if (idx != 0)
289 rec++;
290
Steven Rostedt493762f2009-03-23 17:12:36 -0400291 if ((void *)rec >= (void *)&pg->records[pg->index]) {
292 pg = pg->next;
293 if (!pg)
294 return NULL;
295 rec = &pg->records[0];
296 if (!rec->counter)
297 goto again;
298 }
299
300 return rec;
301}
302
303static void *function_stat_start(struct tracer_stat *trace)
304{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400305 struct ftrace_profile_stat *stat =
306 container_of(trace, struct ftrace_profile_stat, stat);
307
308 if (!stat || !stat->start)
309 return NULL;
310
311 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400312}
313
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400314#ifdef CONFIG_FUNCTION_GRAPH_TRACER
315/* function graph compares on total time */
316static int function_stat_cmp(void *p1, void *p2)
317{
318 struct ftrace_profile *a = p1;
319 struct ftrace_profile *b = p2;
320
321 if (a->time < b->time)
322 return -1;
323 if (a->time > b->time)
324 return 1;
325 else
326 return 0;
327}
328#else
329/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400330static int function_stat_cmp(void *p1, void *p2)
331{
332 struct ftrace_profile *a = p1;
333 struct ftrace_profile *b = p2;
334
335 if (a->counter < b->counter)
336 return -1;
337 if (a->counter > b->counter)
338 return 1;
339 else
340 return 0;
341}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400342#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400343
344static int function_stat_headers(struct seq_file *m)
345{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400346#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400347 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400348 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400349 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400350 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400351#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400352 seq_printf(m, " Function Hit\n"
353 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400354#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400355 return 0;
356}
357
358static int function_stat_show(struct seq_file *m, void *v)
359{
360 struct ftrace_profile *rec = v;
361 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800362 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400363#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400364 static struct trace_seq s;
365 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400366 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400367#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800368 mutex_lock(&ftrace_profile_lock);
369
370 /* we raced with function_profile_reset() */
371 if (unlikely(rec->counter == 0)) {
372 ret = -EBUSY;
373 goto out;
374 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400375
376 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400377 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400378
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400379#ifdef CONFIG_FUNCTION_GRAPH_TRACER
380 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400381 avg = rec->time;
382 do_div(avg, rec->counter);
383
Chase Douglase330b3b2010-04-26 14:02:05 -0400384 /* Sample standard deviation (s^2) */
385 if (rec->counter <= 1)
386 stddev = 0;
387 else {
388 stddev = rec->time_squared - rec->counter * avg * avg;
389 /*
390 * Divide only 1000 for ns^2 -> us^2 conversion.
391 * trace_print_graph_duration will divide 1000 again.
392 */
393 do_div(stddev, (rec->counter - 1) * 1000);
394 }
395
Steven Rostedt34886c82009-03-25 21:00:47 -0400396 trace_seq_init(&s);
397 trace_print_graph_duration(rec->time, &s);
398 trace_seq_puts(&s, " ");
399 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400400 trace_seq_puts(&s, " ");
401 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400402 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400403#endif
404 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800405out:
406 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400407
Li Zefan3aaba202010-08-23 16:50:12 +0800408 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400409}
410
Steven Rostedtcafb1682009-03-24 20:50:39 -0400411static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400412{
413 struct ftrace_profile_page *pg;
414
Steven Rostedtcafb1682009-03-24 20:50:39 -0400415 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400416
417 while (pg) {
418 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
419 pg->index = 0;
420 pg = pg->next;
421 }
422
Steven Rostedtcafb1682009-03-24 20:50:39 -0400423 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400424 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
425}
426
Steven Rostedtcafb1682009-03-24 20:50:39 -0400427int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400428{
429 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400430 int functions;
431 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400432 int i;
433
434 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400435 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400436 return 0;
437
Steven Rostedtcafb1682009-03-24 20:50:39 -0400438 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
439 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400440 return -ENOMEM;
441
Steven Rostedt318e0a72009-03-25 20:06:34 -0400442#ifdef CONFIG_DYNAMIC_FTRACE
443 functions = ftrace_update_tot_cnt;
444#else
445 /*
446 * We do not know the number of functions that exist because
447 * dynamic tracing is what counts them. With past experience
448 * we have around 20K functions. That should be more than enough.
449 * It is highly unlikely we will execute every function in
450 * the kernel.
451 */
452 functions = 20000;
453#endif
454
Steven Rostedtcafb1682009-03-24 20:50:39 -0400455 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400456
Steven Rostedt318e0a72009-03-25 20:06:34 -0400457 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
458
459 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400460 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400461 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400462 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400463 pg = pg->next;
464 }
465
466 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400467
468 out_free:
469 pg = stat->start;
470 while (pg) {
471 unsigned long tmp = (unsigned long)pg;
472
473 pg = pg->next;
474 free_page(tmp);
475 }
476
477 free_page((unsigned long)stat->pages);
478 stat->pages = NULL;
479 stat->start = NULL;
480
481 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400482}
483
Steven Rostedtcafb1682009-03-24 20:50:39 -0400484static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400485{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400486 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400487 int size;
488
Steven Rostedtcafb1682009-03-24 20:50:39 -0400489 stat = &per_cpu(ftrace_profile_stats, cpu);
490
491 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400492 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400493 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400494 return 0;
495 }
496
497 /*
498 * We are profiling all functions, but usually only a few thousand
499 * functions are hit. We'll make a hash of 1024 items.
500 */
501 size = FTRACE_PROFILE_HASH_SIZE;
502
Steven Rostedtcafb1682009-03-24 20:50:39 -0400503 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400504
Steven Rostedtcafb1682009-03-24 20:50:39 -0400505 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400506 return -ENOMEM;
507
Steven Rostedtcafb1682009-03-24 20:50:39 -0400508 if (!ftrace_profile_bits) {
509 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400510
Steven Rostedtcafb1682009-03-24 20:50:39 -0400511 for (; size; size >>= 1)
512 ftrace_profile_bits++;
513 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400514
Steven Rostedt318e0a72009-03-25 20:06:34 -0400515 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400516 if (ftrace_profile_pages_init(stat) < 0) {
517 kfree(stat->hash);
518 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400519 return -ENOMEM;
520 }
521
522 return 0;
523}
524
Steven Rostedtcafb1682009-03-24 20:50:39 -0400525static int ftrace_profile_init(void)
526{
527 int cpu;
528 int ret = 0;
529
530 for_each_online_cpu(cpu) {
531 ret = ftrace_profile_init_cpu(cpu);
532 if (ret)
533 break;
534 }
535
536 return ret;
537}
538
Steven Rostedt493762f2009-03-23 17:12:36 -0400539/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400540static struct ftrace_profile *
541ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400542{
543 struct ftrace_profile *rec;
544 struct hlist_head *hhd;
545 struct hlist_node *n;
546 unsigned long key;
547
548 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400549 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400550
551 if (hlist_empty(hhd))
552 return NULL;
553
554 hlist_for_each_entry_rcu(rec, n, hhd, node) {
555 if (rec->ip == ip)
556 return rec;
557 }
558
559 return NULL;
560}
561
Steven Rostedtcafb1682009-03-24 20:50:39 -0400562static void ftrace_add_profile(struct ftrace_profile_stat *stat,
563 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400564{
565 unsigned long key;
566
567 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400568 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400569}
570
Steven Rostedt318e0a72009-03-25 20:06:34 -0400571/*
572 * The memory is already allocated, this simply finds a new record to use.
573 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400574static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400575ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400576{
577 struct ftrace_profile *rec = NULL;
578
Steven Rostedt318e0a72009-03-25 20:06:34 -0400579 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400580 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400581 goto out;
582
Steven Rostedt493762f2009-03-23 17:12:36 -0400583 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400584 * Try to find the function again since an NMI
585 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400586 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400587 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400588 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400589 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400590
Steven Rostedtcafb1682009-03-24 20:50:39 -0400591 if (stat->pages->index == PROFILES_PER_PAGE) {
592 if (!stat->pages->next)
593 goto out;
594 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400595 }
596
Steven Rostedtcafb1682009-03-24 20:50:39 -0400597 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400598 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400599 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400600
Steven Rostedt493762f2009-03-23 17:12:36 -0400601 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400602 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400603
604 return rec;
605}
606
Steven Rostedt493762f2009-03-23 17:12:36 -0400607static void
608function_profile_call(unsigned long ip, unsigned long parent_ip)
609{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400610 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400611 struct ftrace_profile *rec;
612 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400613
614 if (!ftrace_profile_enabled)
615 return;
616
Steven Rostedt493762f2009-03-23 17:12:36 -0400617 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400618
619 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400620 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400621 goto out;
622
623 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400624 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400625 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400626 if (!rec)
627 goto out;
628 }
629
630 rec->counter++;
631 out:
632 local_irq_restore(flags);
633}
634
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400635#ifdef CONFIG_FUNCTION_GRAPH_TRACER
636static int profile_graph_entry(struct ftrace_graph_ent *trace)
637{
638 function_profile_call(trace->func, 0);
639 return 1;
640}
641
642static void profile_graph_return(struct ftrace_graph_ret *trace)
643{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400644 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400645 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400646 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400647 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400648
649 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400650 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400651 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400652 goto out;
653
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400654 /* If the calltime was zero'd ignore it */
655 if (!trace->calltime)
656 goto out;
657
Steven Rostedta2a16d62009-03-24 23:17:58 -0400658 calltime = trace->rettime - trace->calltime;
659
660 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
661 int index;
662
663 index = trace->depth;
664
665 /* Append this call time to the parent time to subtract */
666 if (index)
667 current->ret_stack[index - 1].subtime += calltime;
668
669 if (current->ret_stack[index].subtime < calltime)
670 calltime -= current->ret_stack[index].subtime;
671 else
672 calltime = 0;
673 }
674
Steven Rostedtcafb1682009-03-24 20:50:39 -0400675 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400676 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400677 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400678 rec->time_squared += calltime * calltime;
679 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400680
Steven Rostedtcafb1682009-03-24 20:50:39 -0400681 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400682 local_irq_restore(flags);
683}
684
685static int register_ftrace_profiler(void)
686{
687 return register_ftrace_graph(&profile_graph_return,
688 &profile_graph_entry);
689}
690
691static void unregister_ftrace_profiler(void)
692{
693 unregister_ftrace_graph();
694}
695#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400696static struct ftrace_ops ftrace_profile_ops __read_mostly =
697{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400698 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400699};
700
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400701static int register_ftrace_profiler(void)
702{
703 return register_ftrace_function(&ftrace_profile_ops);
704}
705
706static void unregister_ftrace_profiler(void)
707{
708 unregister_ftrace_function(&ftrace_profile_ops);
709}
710#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
711
Steven Rostedt493762f2009-03-23 17:12:36 -0400712static ssize_t
713ftrace_profile_write(struct file *filp, const char __user *ubuf,
714 size_t cnt, loff_t *ppos)
715{
716 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400717 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400718 int ret;
719
720 if (cnt >= sizeof(buf))
721 return -EINVAL;
722
723 if (copy_from_user(&buf, ubuf, cnt))
724 return -EFAULT;
725
726 buf[cnt] = 0;
727
728 ret = strict_strtoul(buf, 10, &val);
729 if (ret < 0)
730 return ret;
731
732 val = !!val;
733
734 mutex_lock(&ftrace_profile_lock);
735 if (ftrace_profile_enabled ^ val) {
736 if (val) {
737 ret = ftrace_profile_init();
738 if (ret < 0) {
739 cnt = ret;
740 goto out;
741 }
742
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400743 ret = register_ftrace_profiler();
744 if (ret < 0) {
745 cnt = ret;
746 goto out;
747 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400748 ftrace_profile_enabled = 1;
749 } else {
750 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400751 /*
752 * unregister_ftrace_profiler calls stop_machine
753 * so this acts like an synchronize_sched.
754 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400755 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400756 }
757 }
758 out:
759 mutex_unlock(&ftrace_profile_lock);
760
Jiri Olsacf8517c2009-10-23 19:36:16 -0400761 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400762
763 return cnt;
764}
765
766static ssize_t
767ftrace_profile_read(struct file *filp, char __user *ubuf,
768 size_t cnt, loff_t *ppos)
769{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400770 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400771 int r;
772
773 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
774 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
775}
776
777static const struct file_operations ftrace_profile_fops = {
778 .open = tracing_open_generic,
779 .read = ftrace_profile_read,
780 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200781 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400782};
783
Steven Rostedtcafb1682009-03-24 20:50:39 -0400784/* used to initialize the real stat files */
785static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400786 .name = "functions",
787 .stat_start = function_stat_start,
788 .stat_next = function_stat_next,
789 .stat_cmp = function_stat_cmp,
790 .stat_headers = function_stat_headers,
791 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400792};
793
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400794static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400795{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400796 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400797 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400798 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400799 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400800 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400801
Steven Rostedtcafb1682009-03-24 20:50:39 -0400802 for_each_possible_cpu(cpu) {
803 stat = &per_cpu(ftrace_profile_stats, cpu);
804
805 /* allocate enough for function name + cpu number */
806 name = kmalloc(32, GFP_KERNEL);
807 if (!name) {
808 /*
809 * The files created are permanent, if something happens
810 * we still do not free memory.
811 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400812 WARN(1,
813 "Could not allocate stat file for cpu %d\n",
814 cpu);
815 return;
816 }
817 stat->stat = function_stats;
818 snprintf(name, 32, "function%d", cpu);
819 stat->stat.name = name;
820 ret = register_stat_tracer(&stat->stat);
821 if (ret) {
822 WARN(1,
823 "Could not register function stat for cpu %d\n",
824 cpu);
825 kfree(name);
826 return;
827 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400828 }
829
830 entry = debugfs_create_file("function_profile_enabled", 0644,
831 d_tracer, NULL, &ftrace_profile_fops);
832 if (!entry)
833 pr_warning("Could not create debugfs "
834 "'function_profile_enabled' entry\n");
835}
836
837#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400838static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400839{
840}
841#endif /* CONFIG_FUNCTION_PROFILER */
842
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100843static struct pid * const ftrace_swapper_pid = &init_struct_pid;
844
Steven Rostedt3d083392008-05-12 21:20:42 +0200845#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100846
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400847#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400848# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400849#endif
850
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500851static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
852
Steven Rostedtb6887d72009-02-17 12:32:04 -0500853struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500854 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500855 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500856 unsigned long flags;
857 unsigned long ip;
858 void *data;
859 struct rcu_head rcu;
860};
861
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200862enum {
863 FTRACE_ENABLE_CALLS = (1 << 0),
864 FTRACE_DISABLE_CALLS = (1 << 1),
865 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
Steven Rostedt79e406d2010-09-14 22:19:46 -0400866 FTRACE_START_FUNC_RET = (1 << 3),
867 FTRACE_STOP_FUNC_RET = (1 << 4),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200868};
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400869struct ftrace_func_entry {
870 struct hlist_node hlist;
871 unsigned long ip;
872};
873
874struct ftrace_hash {
875 unsigned long size_bits;
876 struct hlist_head *buckets;
877 unsigned long count;
878};
879
880static struct hlist_head notrace_buckets[1 << FTRACE_HASH_MAX_BITS];
881static struct ftrace_hash notrace_hash = {
882 .size_bits = FTRACE_HASH_MAX_BITS,
883 .buckets = notrace_buckets,
884};
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200885
Steven Rostedt1cf41dd72011-04-29 20:59:51 -0400886static struct hlist_head filter_buckets[1 << FTRACE_HASH_MAX_BITS];
887static struct ftrace_hash filter_hash = {
888 .size_bits = FTRACE_HASH_MAX_BITS,
889 .buckets = filter_buckets,
890};
Steven Rostedt5072c592008-05-12 21:20:43 +0200891
Steven Rostedtf45948e2011-05-02 12:29:25 -0400892struct ftrace_ops global_ops = {
893 .func = ftrace_stub,
894 .notrace_hash = &notrace_hash,
895 .filter_hash = &filter_hash,
896};
897
Lai Jiangshane94142a2009-03-13 17:51:27 +0800898static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200899
Steven Rostedt41c52c02008-05-22 11:46:33 -0400900static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200901
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200902struct ftrace_page {
903 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500904 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200905 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700906};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200907
908#define ENTRIES_PER_PAGE \
909 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
910
911/* estimate from running different kernels */
912#define NR_TO_INIT 10000
913
914static struct ftrace_page *ftrace_pages_start;
915static struct ftrace_page *ftrace_pages;
916
Steven Rostedt37ad50842008-05-12 21:20:48 +0200917static struct dyn_ftrace *ftrace_free_records;
918
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400919static struct ftrace_func_entry *
920ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
921{
922 unsigned long key;
923 struct ftrace_func_entry *entry;
924 struct hlist_head *hhd;
925 struct hlist_node *n;
926
927 if (!hash->count)
928 return NULL;
929
930 if (hash->size_bits > 0)
931 key = hash_long(ip, hash->size_bits);
932 else
933 key = 0;
934
935 hhd = &hash->buckets[key];
936
937 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
938 if (entry->ip == ip)
939 return entry;
940 }
941 return NULL;
942}
943
944static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
945{
946 struct ftrace_func_entry *entry;
947 struct hlist_head *hhd;
948 unsigned long key;
949
950 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
951 if (!entry)
952 return -ENOMEM;
953
954 if (hash->size_bits)
955 key = hash_long(ip, hash->size_bits);
956 else
957 key = 0;
958
959 entry->ip = ip;
960 hhd = &hash->buckets[key];
961 hlist_add_head(&entry->hlist, hhd);
962 hash->count++;
963
964 return 0;
965}
966
967static void
968remove_hash_entry(struct ftrace_hash *hash,
969 struct ftrace_func_entry *entry)
970{
971 hlist_del(&entry->hlist);
972 kfree(entry);
973 hash->count--;
974}
975
976static void ftrace_hash_clear(struct ftrace_hash *hash)
977{
978 struct hlist_head *hhd;
979 struct hlist_node *tp, *tn;
980 struct ftrace_func_entry *entry;
981 int size = 1 << hash->size_bits;
982 int i;
983
984 for (i = 0; i < size; i++) {
985 hhd = &hash->buckets[i];
986 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
987 remove_hash_entry(hash, entry);
988 }
989 FTRACE_WARN_ON(hash->count);
990}
991
Steven Rostedt265c8312009-02-13 12:43:56 -0500992/*
993 * This is a double for. Do not use 'break' to break out of the loop,
994 * you must use a goto.
995 */
996#define do_for_each_ftrace_rec(pg, rec) \
997 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
998 int _____i; \
999 for (_____i = 0; _____i < pg->index; _____i++) { \
1000 rec = &pg->records[_____i];
1001
1002#define while_for_each_ftrace_rec() \
1003 } \
1004 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301005
Ingo Molnare309b412008-05-12 21:20:51 +02001006static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad50842008-05-12 21:20:48 +02001007{
Lai Jiangshanee000b72009-03-24 13:38:06 +08001008 rec->freelist = ftrace_free_records;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001009 ftrace_free_records = rec;
1010 rec->flags |= FTRACE_FL_FREE;
1011}
1012
Ingo Molnare309b412008-05-12 21:20:51 +02001013static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001014{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001015 struct dyn_ftrace *rec;
1016
1017 /* First check for freed records */
1018 if (ftrace_free_records) {
1019 rec = ftrace_free_records;
1020
Steven Rostedt37ad50842008-05-12 21:20:48 +02001021 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt6912896e2008-10-23 09:33:03 -04001022 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad50842008-05-12 21:20:48 +02001023 ftrace_free_records = NULL;
1024 return NULL;
1025 }
1026
Lai Jiangshanee000b72009-03-24 13:38:06 +08001027 ftrace_free_records = rec->freelist;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001028 memset(rec, 0, sizeof(*rec));
1029 return rec;
1030 }
1031
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001032 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001033 if (!ftrace_pages->next) {
1034 /* allocate another page */
1035 ftrace_pages->next =
1036 (void *)get_zeroed_page(GFP_KERNEL);
1037 if (!ftrace_pages->next)
1038 return NULL;
1039 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001040 ftrace_pages = ftrace_pages->next;
1041 }
1042
1043 return &ftrace_pages->records[ftrace_pages->index++];
1044}
1045
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001046static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001047ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001048{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001049 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001050
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001051 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001052 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001053
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001054 rec = ftrace_alloc_dyn_node(ip);
1055 if (!rec)
1056 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001057
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001058 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001059 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001060 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001061
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001062 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001063}
1064
Steven Rostedt05736a42008-09-22 14:55:47 -07001065static void print_ip_ins(const char *fmt, unsigned char *p)
1066{
1067 int i;
1068
1069 printk(KERN_CONT "%s", fmt);
1070
1071 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1072 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1073}
1074
Steven Rostedt31e88902008-11-14 16:21:19 -08001075static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001076{
1077 switch (failed) {
1078 case -EFAULT:
1079 FTRACE_WARN_ON_ONCE(1);
1080 pr_info("ftrace faulted on modifying ");
1081 print_ip_sym(ip);
1082 break;
1083 case -EINVAL:
1084 FTRACE_WARN_ON_ONCE(1);
1085 pr_info("ftrace failed to modify ");
1086 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001087 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001088 printk(KERN_CONT "\n");
1089 break;
1090 case -EPERM:
1091 FTRACE_WARN_ON_ONCE(1);
1092 pr_info("ftrace faulted on writing ");
1093 print_ip_sym(ip);
1094 break;
1095 default:
1096 FTRACE_WARN_ON_ONCE(1);
1097 pr_info("ftrace faulted on unknown error ");
1098 print_ip_sym(ip);
1099 }
1100}
1101
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001102
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001103/* Return 1 if the address range is reserved for ftrace */
1104int ftrace_text_reserved(void *start, void *end)
1105{
1106 struct dyn_ftrace *rec;
1107 struct ftrace_page *pg;
1108
1109 do_for_each_ftrace_rec(pg, rec) {
1110 if (rec->ip <= (unsigned long)end &&
1111 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1112 return 1;
1113 } while_for_each_ftrace_rec();
1114 return 0;
1115}
1116
1117
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301118static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001119__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001120{
Steven Rostedtf45948e2011-05-02 12:29:25 -04001121 struct ftrace_ops *ops = &global_ops;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001122 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001123 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001124
Shaohua Lif0001202009-01-09 11:29:42 +08001125 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001126
Steven Rostedt982c3502008-11-15 16:31:41 -05001127 /*
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001128 * If this record is not to be traced or we want to disable it,
1129 * then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001130 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001131 * If we want to enable it and filtering is off, then enable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001132 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001133 * If we want to enable it and filtering is on, enable it only if
1134 * it's filtered
Steven Rostedt982c3502008-11-15 16:31:41 -05001135 */
Steven Rostedtf45948e2011-05-02 12:29:25 -04001136 if (enable && !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) {
1137 if (!ops->filter_hash->count ||
1138 ftrace_lookup_ip(ops->filter_hash, rec->ip))
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001139 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001140 }
1141
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001142 /* If the state of this record hasn't changed, then do nothing */
1143 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1144 return 0;
1145
1146 if (flag) {
1147 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001148 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001149 }
1150
1151 rec->flags &= ~FTRACE_FL_ENABLED;
1152 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001153}
1154
1155static void ftrace_replace_code(int enable)
1156{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001157 struct dyn_ftrace *rec;
1158 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001159 int failed;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001160
Steven Rostedt45a4a232011-04-21 23:16:46 -04001161 if (unlikely(ftrace_disabled))
1162 return;
1163
Steven Rostedt265c8312009-02-13 12:43:56 -05001164 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001165 /* Skip over free records */
1166 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05001167 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001168
Steven Rostedt265c8312009-02-13 12:43:56 -05001169 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001170 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001171 ftrace_bug(failed, rec->ip);
1172 /* Stop processing */
1173 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001174 }
1175 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001176}
1177
Ingo Molnare309b412008-05-12 21:20:51 +02001178static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001179ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001180{
1181 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001182 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001183
1184 ip = rec->ip;
1185
Steven Rostedt45a4a232011-04-21 23:16:46 -04001186 if (unlikely(ftrace_disabled))
1187 return 0;
1188
Shaohua Li25aac9d2009-01-09 11:29:40 +08001189 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001190 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001191 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301192 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001193 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301194 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001195}
1196
Steven Rostedt000ab692009-02-17 13:35:06 -05001197/*
1198 * archs can override this function if they must do something
1199 * before the modifying code is performed.
1200 */
1201int __weak ftrace_arch_code_modify_prepare(void)
1202{
1203 return 0;
1204}
1205
1206/*
1207 * archs can override this function if they must do something
1208 * after the modifying code is performed.
1209 */
1210int __weak ftrace_arch_code_modify_post_process(void)
1211{
1212 return 0;
1213}
1214
Ingo Molnare309b412008-05-12 21:20:51 +02001215static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001216{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001217 int *command = data;
1218
Steven Rostedta3583242008-11-11 15:01:42 -05001219 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001220 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001221 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001222 ftrace_replace_code(0);
1223
1224 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1225 ftrace_update_ftrace_func(ftrace_trace_function);
1226
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001227 if (*command & FTRACE_START_FUNC_RET)
1228 ftrace_enable_ftrace_graph_caller();
1229 else if (*command & FTRACE_STOP_FUNC_RET)
1230 ftrace_disable_ftrace_graph_caller();
1231
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001232 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001233}
1234
Ingo Molnare309b412008-05-12 21:20:51 +02001235static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001236{
Steven Rostedt000ab692009-02-17 13:35:06 -05001237 int ret;
1238
1239 ret = ftrace_arch_code_modify_prepare();
1240 FTRACE_WARN_ON(ret);
1241 if (ret)
1242 return;
1243
Rusty Russell784e2d72008-07-28 12:16:31 -05001244 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001245
1246 ret = ftrace_arch_code_modify_post_process();
1247 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001248}
1249
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001250static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001251static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001252
1253static void ftrace_startup_enable(int command)
1254{
1255 if (saved_ftrace_func != ftrace_trace_function) {
1256 saved_ftrace_func = ftrace_trace_function;
1257 command |= FTRACE_UPDATE_TRACE_FUNC;
1258 }
1259
1260 if (!command || !ftrace_enabled)
1261 return;
1262
1263 ftrace_run_update_code(command);
1264}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001265
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001266static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001267{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001268 if (unlikely(ftrace_disabled))
1269 return;
1270
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001271 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001272 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001273
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001274 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001275}
1276
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001277static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001278{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001279 if (unlikely(ftrace_disabled))
1280 return;
1281
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001282 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001283 /*
1284 * Just warn in case of unbalance, no need to kill ftrace, it's not
1285 * critical but the ftrace_call callers may be never nopped again after
1286 * further ftrace uses.
1287 */
1288 WARN_ON_ONCE(ftrace_start_up < 0);
1289
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001290 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001291 command |= FTRACE_DISABLE_CALLS;
1292
1293 if (saved_ftrace_func != ftrace_trace_function) {
1294 saved_ftrace_func = ftrace_trace_function;
1295 command |= FTRACE_UPDATE_TRACE_FUNC;
1296 }
1297
1298 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001299 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001300
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001301 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001302}
1303
Ingo Molnare309b412008-05-12 21:20:51 +02001304static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001305{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001306 if (unlikely(ftrace_disabled))
1307 return;
1308
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001309 /* Force update next time */
1310 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001311 /* ftrace_start_up is true if we want ftrace running */
1312 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001313 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001314}
1315
Ingo Molnare309b412008-05-12 21:20:51 +02001316static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001317{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001318 if (unlikely(ftrace_disabled))
1319 return;
1320
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001321 /* ftrace_start_up is true if ftrace is running */
1322 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001323 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001324}
1325
Steven Rostedt3d083392008-05-12 21:20:42 +02001326static cycle_t ftrace_update_time;
1327static unsigned long ftrace_update_cnt;
1328unsigned long ftrace_update_tot_cnt;
1329
Steven Rostedt31e88902008-11-14 16:21:19 -08001330static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001331{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001332 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301333 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001334
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001335 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001336 ftrace_update_cnt = 0;
1337
Lai Jiangshane94142a2009-03-13 17:51:27 +08001338 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301339
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001340 /* If something went wrong, bail without enabling anything */
1341 if (unlikely(ftrace_disabled))
1342 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001343
Lai Jiangshane94142a2009-03-13 17:51:27 +08001344 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001345 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001346 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301347
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001348 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001349 * Do the initial record conversion from mcount jump
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001350 * to the NOP instructions.
1351 */
1352 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001353 ftrace_free_rec(p);
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001354 /* Game over */
1355 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001356 }
1357
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001358 ftrace_update_cnt++;
1359
1360 /*
1361 * If the tracing is enabled, go ahead and enable the record.
1362 *
1363 * The reason not to enable the record immediatelly is the
1364 * inherent check of ftrace_make_nop/ftrace_make_call for
1365 * correct previous instructions. Making first the NOP
1366 * conversion puts the module to the correct state, thus
1367 * passing the ftrace_make_call check.
1368 */
1369 if (ftrace_start_up) {
1370 int failed = __ftrace_replace_code(p, 1);
1371 if (failed) {
1372 ftrace_bug(failed, p->ip);
1373 ftrace_free_rec(p);
1374 }
1375 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001376 }
1377
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001378 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001379 ftrace_update_time = stop - start;
1380 ftrace_update_tot_cnt += ftrace_update_cnt;
1381
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001382 return 0;
1383}
1384
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001385static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001386{
1387 struct ftrace_page *pg;
1388 int cnt;
1389 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001390
1391 /* allocate a few pages */
1392 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1393 if (!ftrace_pages_start)
1394 return -1;
1395
1396 /*
1397 * Allocate a few more pages.
1398 *
1399 * TODO: have some parser search vmlinux before
1400 * final linking to find all calls to ftrace.
1401 * Then we can:
1402 * a) know how many pages to allocate.
1403 * and/or
1404 * b) set up the table then.
1405 *
1406 * The dynamic code is still necessary for
1407 * modules.
1408 */
1409
1410 pg = ftrace_pages = ftrace_pages_start;
1411
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001412 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001413 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001414 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001415
1416 for (i = 0; i < cnt; i++) {
1417 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1418
1419 /* If we fail, we'll try later anyway */
1420 if (!pg->next)
1421 break;
1422
1423 pg = pg->next;
1424 }
1425
1426 return 0;
1427}
1428
Steven Rostedt5072c592008-05-12 21:20:43 +02001429enum {
1430 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001431 FTRACE_ITER_NOTRACE = (1 << 1),
Steven Rostedt3499e462011-04-21 22:59:12 -04001432 FTRACE_ITER_PRINTALL = (1 << 2),
1433 FTRACE_ITER_HASH = (1 << 3),
Steven Rostedt5072c592008-05-12 21:20:43 +02001434};
1435
1436#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1437
1438struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001439 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001440 loff_t func_pos;
1441 struct ftrace_page *pg;
1442 struct dyn_ftrace *func;
1443 struct ftrace_func_probe *probe;
1444 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001445 struct ftrace_hash *hash;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001446 int hidx;
1447 int idx;
1448 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02001449};
1450
Ingo Molnare309b412008-05-12 21:20:51 +02001451static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001452t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001453{
1454 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001455 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001456 struct hlist_head *hhd;
1457
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001458 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001459 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001460
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001461 if (iter->probe)
1462 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001463 retry:
1464 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1465 return NULL;
1466
1467 hhd = &ftrace_func_hash[iter->hidx];
1468
1469 if (hlist_empty(hhd)) {
1470 iter->hidx++;
1471 hnd = NULL;
1472 goto retry;
1473 }
1474
1475 if (!hnd)
1476 hnd = hhd->first;
1477 else {
1478 hnd = hnd->next;
1479 if (!hnd) {
1480 iter->hidx++;
1481 goto retry;
1482 }
1483 }
1484
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001485 if (WARN_ON_ONCE(!hnd))
1486 return NULL;
1487
1488 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1489
1490 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001491}
1492
1493static void *t_hash_start(struct seq_file *m, loff_t *pos)
1494{
1495 struct ftrace_iterator *iter = m->private;
1496 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001497 loff_t l;
1498
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001499 if (iter->func_pos > *pos)
1500 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001501
Li Zefand82d6242009-06-24 09:54:54 +08001502 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001503 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001504 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08001505 if (!p)
1506 break;
1507 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001508 if (!p)
1509 return NULL;
1510
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001511 /* Only set this if we have an item */
1512 iter->flags |= FTRACE_ITER_HASH;
1513
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001514 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001515}
1516
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001517static int
1518t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001519{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001520 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001521
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001522 rec = iter->probe;
1523 if (WARN_ON_ONCE(!rec))
1524 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001525
Steven Rostedt809dcf22009-02-16 23:06:01 -05001526 if (rec->ops->print)
1527 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1528
Steven Rostedtb375a112009-09-17 00:05:58 -04001529 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001530
1531 if (rec->data)
1532 seq_printf(m, ":%p", rec->data);
1533 seq_putc(m, '\n');
1534
1535 return 0;
1536}
1537
1538static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001539t_next(struct seq_file *m, void *v, loff_t *pos)
1540{
1541 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04001542 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02001543 struct dyn_ftrace *rec = NULL;
1544
Steven Rostedt45a4a232011-04-21 23:16:46 -04001545 if (unlikely(ftrace_disabled))
1546 return NULL;
1547
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001548 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001549 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001550
Steven Rostedt5072c592008-05-12 21:20:43 +02001551 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01001552 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02001553
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001554 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001555 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001556
Steven Rostedt5072c592008-05-12 21:20:43 +02001557 retry:
1558 if (iter->idx >= iter->pg->index) {
1559 if (iter->pg->next) {
1560 iter->pg = iter->pg->next;
1561 iter->idx = 0;
1562 goto retry;
1563 }
1564 } else {
1565 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001566 if ((rec->flags & FTRACE_FL_FREE) ||
1567
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001568 ((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04001569 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001570
Steven Rostedt41c52c02008-05-22 11:46:33 -04001571 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04001572 !ftrace_lookup_ip(ops->notrace_hash, rec->ip))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001573 rec = NULL;
1574 goto retry;
1575 }
1576 }
1577
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001578 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001579 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001580
1581 iter->func = rec;
1582
1583 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02001584}
1585
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001586static void reset_iter_read(struct ftrace_iterator *iter)
1587{
1588 iter->pos = 0;
1589 iter->func_pos = 0;
1590 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02001591}
1592
1593static void *t_start(struct seq_file *m, loff_t *pos)
1594{
1595 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04001596 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02001597 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001598 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001599
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001600 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04001601
1602 if (unlikely(ftrace_disabled))
1603 return NULL;
1604
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001605 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001606 * If an lseek was done, then reset and start from beginning.
1607 */
1608 if (*pos < iter->pos)
1609 reset_iter_read(iter);
1610
1611 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001612 * For set_ftrace_filter reading, if we have the filter
1613 * off, we can short cut and just print out that all
1614 * functions are enabled.
1615 */
Steven Rostedtf45948e2011-05-02 12:29:25 -04001616 if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001617 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001618 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001619 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07001620 /* reset in case of seek/pread */
1621 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001622 return iter;
1623 }
1624
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001625 if (iter->flags & FTRACE_ITER_HASH)
1626 return t_hash_start(m, pos);
1627
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001628 /*
1629 * Unfortunately, we need to restart at ftrace_pages_start
1630 * every time we let go of the ftrace_mutex. This is because
1631 * those pointers can change without the lock.
1632 */
Li Zefan694ce0a2009-06-24 09:54:19 +08001633 iter->pg = ftrace_pages_start;
1634 iter->idx = 0;
1635 for (l = 0; l <= *pos; ) {
1636 p = t_next(m, p, &l);
1637 if (!p)
1638 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001639 }
walimis5821e1b2008-11-15 15:19:06 +08001640
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001641 if (!p) {
1642 if (iter->flags & FTRACE_ITER_FILTER)
1643 return t_hash_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001644
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001645 return NULL;
1646 }
1647
1648 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02001649}
1650
1651static void t_stop(struct seq_file *m, void *p)
1652{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001653 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001654}
1655
1656static int t_show(struct seq_file *m, void *v)
1657{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001658 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001659 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02001660
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001661 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001662 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001663
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001664 if (iter->flags & FTRACE_ITER_PRINTALL) {
1665 seq_printf(m, "#### all functions enabled ####\n");
1666 return 0;
1667 }
1668
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001669 rec = iter->func;
1670
Steven Rostedt5072c592008-05-12 21:20:43 +02001671 if (!rec)
1672 return 0;
1673
Steven Rostedtb375a112009-09-17 00:05:58 -04001674 seq_printf(m, "%ps\n", (void *)rec->ip);
Steven Rostedt5072c592008-05-12 21:20:43 +02001675
1676 return 0;
1677}
1678
James Morris88e9d342009-09-22 16:43:43 -07001679static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001680 .start = t_start,
1681 .next = t_next,
1682 .stop = t_stop,
1683 .show = t_show,
1684};
1685
Ingo Molnare309b412008-05-12 21:20:51 +02001686static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001687ftrace_avail_open(struct inode *inode, struct file *file)
1688{
1689 struct ftrace_iterator *iter;
1690 int ret;
1691
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001692 if (unlikely(ftrace_disabled))
1693 return -ENODEV;
1694
Steven Rostedt5072c592008-05-12 21:20:43 +02001695 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1696 if (!iter)
1697 return -ENOMEM;
1698
1699 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001700
1701 ret = seq_open(file, &show_ftrace_seq_ops);
1702 if (!ret) {
1703 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001704
Steven Rostedt5072c592008-05-12 21:20:43 +02001705 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001706 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001707 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001708 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001709
1710 return ret;
1711}
1712
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001713static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001714{
Steven Rostedt52baf112009-02-14 01:15:39 -05001715 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001716 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05001717 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001718}
1719
Ingo Molnare309b412008-05-12 21:20:51 +02001720static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04001721ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001722 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02001723{
1724 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04001725 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02001726 int ret = 0;
1727
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001728 if (unlikely(ftrace_disabled))
1729 return -ENODEV;
1730
Steven Rostedt5072c592008-05-12 21:20:43 +02001731 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1732 if (!iter)
1733 return -ENOMEM;
1734
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001735 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1736 kfree(iter);
1737 return -ENOMEM;
1738 }
1739
Steven Rostedtf45948e2011-05-02 12:29:25 -04001740 if (flag & FTRACE_ITER_NOTRACE)
1741 hash = ops->notrace_hash;
1742 else
1743 hash = ops->filter_hash;
1744
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001745 iter->hash = hash;
1746
Steven Rostedt41c52c02008-05-22 11:46:33 -04001747 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001748 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04001749 (file->f_flags & O_TRUNC))
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001750 ftrace_filter_reset(hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02001751
1752 if (file->f_mode & FMODE_READ) {
1753 iter->pg = ftrace_pages_start;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001754 iter->flags = flag;
Steven Rostedt5072c592008-05-12 21:20:43 +02001755
1756 ret = seq_open(file, &show_ftrace_seq_ops);
1757 if (!ret) {
1758 struct seq_file *m = file->private_data;
1759 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08001760 } else {
1761 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02001762 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08001763 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001764 } else
1765 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001766 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001767
1768 return ret;
1769}
1770
Steven Rostedt41c52c02008-05-22 11:46:33 -04001771static int
1772ftrace_filter_open(struct inode *inode, struct file *file)
1773{
Steven Rostedtf45948e2011-05-02 12:29:25 -04001774 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001775 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001776}
1777
1778static int
1779ftrace_notrace_open(struct inode *inode, struct file *file)
1780{
Steven Rostedtf45948e2011-05-02 12:29:25 -04001781 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001782 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001783}
1784
Ingo Molnare309b412008-05-12 21:20:51 +02001785static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001786ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001787{
1788 loff_t ret;
1789
1790 if (file->f_mode & FMODE_READ)
1791 ret = seq_lseek(file, offset, origin);
1792 else
1793 file->f_pos = ret = 1;
1794
1795 return ret;
1796}
1797
Steven Rostedt64e7c442009-02-13 17:08:48 -05001798static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001799{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001800 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08001801 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001802
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001803 switch (type) {
1804 case MATCH_FULL:
1805 if (strcmp(str, regex) == 0)
1806 matched = 1;
1807 break;
1808 case MATCH_FRONT_ONLY:
1809 if (strncmp(str, regex, len) == 0)
1810 matched = 1;
1811 break;
1812 case MATCH_MIDDLE_ONLY:
1813 if (strstr(str, regex))
1814 matched = 1;
1815 break;
1816 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08001817 slen = strlen(str);
1818 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001819 matched = 1;
1820 break;
1821 }
1822
1823 return matched;
1824}
1825
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001826static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001827enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04001828{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001829 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001830 int ret = 0;
1831
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001832 entry = ftrace_lookup_ip(hash, rec->ip);
1833 if (not) {
1834 /* Do nothing if it doesn't exist */
1835 if (!entry)
1836 return 0;
1837
1838 remove_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001839 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001840 /* Do nothing if it exists */
1841 if (entry)
1842 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001843
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001844 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001845 }
1846 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04001847}
1848
Steven Rostedt64e7c442009-02-13 17:08:48 -05001849static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001850ftrace_match_record(struct dyn_ftrace *rec, char *mod,
1851 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001852{
1853 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001854 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001855
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001856 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1857
1858 if (mod) {
1859 /* module lookup requires matching the module */
1860 if (!modname || strcmp(modname, mod))
1861 return 0;
1862
1863 /* blank search means to match all funcs in the mod */
1864 if (!len)
1865 return 1;
1866 }
1867
Steven Rostedt64e7c442009-02-13 17:08:48 -05001868 return ftrace_match(str, regex, len, type);
1869}
1870
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001871static int
1872match_records(struct ftrace_hash *hash, char *buff,
1873 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001874{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001875 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001876 struct ftrace_page *pg;
1877 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001878 int type = MATCH_FULL;
1879 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08001880 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001881 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001882
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001883 if (len) {
1884 type = filter_parse_regex(buff, len, &search, &not);
1885 search_len = strlen(search);
1886 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001887
Steven Rostedt52baf112009-02-14 01:15:39 -05001888 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001889
1890 if (unlikely(ftrace_disabled))
1891 goto out_unlock;
1892
Steven Rostedt265c8312009-02-13 12:43:56 -05001893 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001894
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001895 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001896 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001897 if (ret < 0) {
1898 found = ret;
1899 goto out_unlock;
1900 }
Li Zefan311d16d2009-12-08 11:15:11 +08001901 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001902 }
1903 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001904 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05001905 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001906
1907 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02001908}
1909
Steven Rostedt64e7c442009-02-13 17:08:48 -05001910static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001911ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001912{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001913 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001914}
1915
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001916static int
1917ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001918{
Steven Rostedt64e7c442009-02-13 17:08:48 -05001919 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001920
Steven Rostedt64e7c442009-02-13 17:08:48 -05001921 /* blank or '*' mean the same */
1922 if (strcmp(buff, "*") == 0)
1923 buff[0] = 0;
1924
1925 /* handle the case of 'dont filter this module' */
1926 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1927 buff[0] = 0;
1928 not = 1;
1929 }
1930
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001931 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001932}
1933
Steven Rostedtf6180772009-02-14 00:40:25 -05001934/*
1935 * We register the module command as a template to show others how
1936 * to register the a command as well.
1937 */
1938
1939static int
1940ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1941{
Steven Rostedtf45948e2011-05-02 12:29:25 -04001942 struct ftrace_ops *ops = &global_ops;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001943 struct ftrace_hash *hash;
Steven Rostedtf6180772009-02-14 00:40:25 -05001944 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001945 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05001946
1947 /*
1948 * cmd == 'mod' because we only registered this func
1949 * for the 'mod' ftrace_func_command.
1950 * But if you register one func with multiple commands,
1951 * you can tell which command was used by the cmd
1952 * parameter.
1953 */
1954
1955 /* we must have a module name */
1956 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001957 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05001958
1959 mod = strsep(&param, ":");
1960 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001961 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05001962
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001963 if (enable)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001964 hash = ops->filter_hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001965 else
Steven Rostedtf45948e2011-05-02 12:29:25 -04001966 hash = ops->notrace_hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001967
1968 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001969 if (!ret)
1970 ret = -EINVAL;
1971 if (ret < 0)
1972 return ret;
1973
1974 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05001975}
1976
1977static struct ftrace_func_command ftrace_mod_cmd = {
1978 .name = "mod",
1979 .func = ftrace_mod_callback,
1980};
1981
1982static int __init ftrace_mod_cmd_init(void)
1983{
1984 return register_ftrace_command(&ftrace_mod_cmd);
1985}
1986device_initcall(ftrace_mod_cmd_init);
1987
Steven Rostedt59df055f2009-02-14 15:29:06 -05001988static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001989function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001990{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001991 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001992 struct hlist_head *hhd;
1993 struct hlist_node *n;
1994 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001995
1996 key = hash_long(ip, FTRACE_HASH_BITS);
1997
1998 hhd = &ftrace_func_hash[key];
1999
2000 if (hlist_empty(hhd))
2001 return;
2002
2003 /*
2004 * Disable preemption for these calls to prevent a RCU grace
2005 * period. This syncs the hash iteration and freeing of items
2006 * on the hash. rcu_read_lock is too dangerous here.
2007 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002008 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002009 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2010 if (entry->ip == ip)
2011 entry->ops->func(ip, parent_ip, &entry->data);
2012 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002013 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002014}
2015
Steven Rostedtb6887d72009-02-17 12:32:04 -05002016static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002017{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002018 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002019};
2020
Steven Rostedtb6887d72009-02-17 12:32:04 -05002021static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002022
Steven Rostedtb6887d72009-02-17 12:32:04 -05002023static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002024{
2025 int i;
2026
Steven Rostedtb6887d72009-02-17 12:32:04 -05002027 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002028 return;
2029
2030 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2031 struct hlist_head *hhd = &ftrace_func_hash[i];
2032 if (hhd->first)
2033 break;
2034 }
2035 /* Nothing registered? */
2036 if (i == FTRACE_FUNC_HASHSIZE)
2037 return;
2038
Steven Rostedtb6887d72009-02-17 12:32:04 -05002039 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002040 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05002041 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002042}
2043
Steven Rostedtb6887d72009-02-17 12:32:04 -05002044static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002045{
2046 int i;
2047
Steven Rostedtb6887d72009-02-17 12:32:04 -05002048 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002049 return;
2050
2051 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2052 struct hlist_head *hhd = &ftrace_func_hash[i];
2053 if (hhd->first)
2054 return;
2055 }
2056
2057 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002058 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002059 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05002060 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002061}
2062
2063
2064static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2065{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002066 struct ftrace_func_probe *entry =
2067 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002068
2069 if (entry->ops->free)
2070 entry->ops->free(&entry->data);
2071 kfree(entry);
2072}
2073
2074
2075int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002076register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002077 void *data)
2078{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002079 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002080 struct ftrace_page *pg;
2081 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002082 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002083 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002084 int count = 0;
2085 char *search;
2086
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002087 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002088 len = strlen(search);
2089
Steven Rostedtb6887d72009-02-17 12:32:04 -05002090 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002091 if (WARN_ON(not))
2092 return -EINVAL;
2093
2094 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002095
Steven Rostedt45a4a232011-04-21 23:16:46 -04002096 if (unlikely(ftrace_disabled))
2097 goto out_unlock;
2098
Steven Rostedt59df055f2009-02-14 15:29:06 -05002099 do_for_each_ftrace_rec(pg, rec) {
2100
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002101 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002102 continue;
2103
2104 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2105 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002106 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002107 if (!count)
2108 count = -ENOMEM;
2109 goto out_unlock;
2110 }
2111
2112 count++;
2113
2114 entry->data = data;
2115
2116 /*
2117 * The caller might want to do something special
2118 * for each function we find. We call the callback
2119 * to give the caller an opportunity to do so.
2120 */
2121 if (ops->callback) {
2122 if (ops->callback(rec->ip, &entry->data) < 0) {
2123 /* caller does not like this func */
2124 kfree(entry);
2125 continue;
2126 }
2127 }
2128
2129 entry->ops = ops;
2130 entry->ip = rec->ip;
2131
2132 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2133 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2134
2135 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002136 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002137
2138 out_unlock:
2139 mutex_unlock(&ftrace_lock);
2140
2141 return count;
2142}
2143
2144enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002145 PROBE_TEST_FUNC = 1,
2146 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002147};
2148
2149static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002150__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002151 void *data, int flags)
2152{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002153 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002154 struct hlist_node *n, *tmp;
2155 char str[KSYM_SYMBOL_LEN];
2156 int type = MATCH_FULL;
2157 int i, len = 0;
2158 char *search;
2159
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002160 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002161 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002162 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002163 int not;
2164
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002165 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002166 len = strlen(search);
2167
Steven Rostedtb6887d72009-02-17 12:32:04 -05002168 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002169 if (WARN_ON(not))
2170 return;
2171 }
2172
2173 mutex_lock(&ftrace_lock);
2174 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2175 struct hlist_head *hhd = &ftrace_func_hash[i];
2176
2177 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2178
2179 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002180 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002181 continue;
2182
Steven Rostedtb6887d72009-02-17 12:32:04 -05002183 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002184 continue;
2185
2186 /* do this last, since it is the most expensive */
2187 if (glob) {
2188 kallsyms_lookup(entry->ip, NULL, NULL,
2189 NULL, str);
2190 if (!ftrace_match(str, glob, len, type))
2191 continue;
2192 }
2193
2194 hlist_del(&entry->node);
2195 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2196 }
2197 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002198 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002199 mutex_unlock(&ftrace_lock);
2200}
2201
2202void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002203unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002204 void *data)
2205{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002206 __unregister_ftrace_function_probe(glob, ops, data,
2207 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002208}
2209
2210void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002211unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002212{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002213 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002214}
2215
Steven Rostedtb6887d72009-02-17 12:32:04 -05002216void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002217{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002218 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002219}
2220
Steven Rostedtf6180772009-02-14 00:40:25 -05002221static LIST_HEAD(ftrace_commands);
2222static DEFINE_MUTEX(ftrace_cmd_mutex);
2223
2224int register_ftrace_command(struct ftrace_func_command *cmd)
2225{
2226 struct ftrace_func_command *p;
2227 int ret = 0;
2228
2229 mutex_lock(&ftrace_cmd_mutex);
2230 list_for_each_entry(p, &ftrace_commands, list) {
2231 if (strcmp(cmd->name, p->name) == 0) {
2232 ret = -EBUSY;
2233 goto out_unlock;
2234 }
2235 }
2236 list_add(&cmd->list, &ftrace_commands);
2237 out_unlock:
2238 mutex_unlock(&ftrace_cmd_mutex);
2239
2240 return ret;
2241}
2242
2243int unregister_ftrace_command(struct ftrace_func_command *cmd)
2244{
2245 struct ftrace_func_command *p, *n;
2246 int ret = -ENODEV;
2247
2248 mutex_lock(&ftrace_cmd_mutex);
2249 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2250 if (strcmp(cmd->name, p->name) == 0) {
2251 ret = 0;
2252 list_del_init(&p->list);
2253 goto out_unlock;
2254 }
2255 }
2256 out_unlock:
2257 mutex_unlock(&ftrace_cmd_mutex);
2258
2259 return ret;
2260}
2261
Steven Rostedt64e7c442009-02-13 17:08:48 -05002262static int ftrace_process_regex(char *buff, int len, int enable)
2263{
Steven Rostedtf6180772009-02-14 00:40:25 -05002264 char *func, *command, *next = buff;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002265 struct ftrace_ops *ops = &global_ops;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002266 struct ftrace_func_command *p;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002267 struct ftrace_hash *hash;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002268 int ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002269
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002270 if (enable)
Steven Rostedtf45948e2011-05-02 12:29:25 -04002271 hash = ops->filter_hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002272 else
Steven Rostedtf45948e2011-05-02 12:29:25 -04002273 hash = ops->notrace_hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002274
Steven Rostedt64e7c442009-02-13 17:08:48 -05002275 func = strsep(&next, ":");
2276
2277 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002278 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002279 if (!ret)
2280 ret = -EINVAL;
2281 if (ret < 0)
2282 return ret;
2283 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002284 }
2285
Steven Rostedtf6180772009-02-14 00:40:25 -05002286 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002287
2288 command = strsep(&next, ":");
2289
Steven Rostedtf6180772009-02-14 00:40:25 -05002290 mutex_lock(&ftrace_cmd_mutex);
2291 list_for_each_entry(p, &ftrace_commands, list) {
2292 if (strcmp(p->name, command) == 0) {
2293 ret = p->func(func, command, next, enable);
2294 goto out_unlock;
2295 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002296 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002297 out_unlock:
2298 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002299
Steven Rostedtf6180772009-02-14 00:40:25 -05002300 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002301}
2302
Ingo Molnare309b412008-05-12 21:20:51 +02002303static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002304ftrace_regex_write(struct file *file, const char __user *ubuf,
2305 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002306{
2307 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002308 struct trace_parser *parser;
2309 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002310
Li Zefan4ba79782009-09-22 13:52:20 +08002311 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002312 return 0;
2313
Steven Rostedt41c52c02008-05-22 11:46:33 -04002314 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002315
Steven Rostedt45a4a232011-04-21 23:16:46 -04002316 ret = -ENODEV;
2317 if (unlikely(ftrace_disabled))
2318 goto out_unlock;
2319
Steven Rostedt5072c592008-05-12 21:20:43 +02002320 if (file->f_mode & FMODE_READ) {
2321 struct seq_file *m = file->private_data;
2322 iter = m->private;
2323 } else
2324 iter = file->private_data;
2325
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002326 parser = &iter->parser;
2327 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002328
Li Zefan4ba79782009-09-22 13:52:20 +08002329 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002330 !trace_parser_cont(parser)) {
2331 ret = ftrace_process_regex(parser->buffer,
2332 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002333 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002334 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002335 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002336 }
2337
Steven Rostedt5072c592008-05-12 21:20:43 +02002338 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002339out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002340 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002341
Steven Rostedt5072c592008-05-12 21:20:43 +02002342 return ret;
2343}
2344
Steven Rostedt41c52c02008-05-22 11:46:33 -04002345static ssize_t
2346ftrace_filter_write(struct file *file, const char __user *ubuf,
2347 size_t cnt, loff_t *ppos)
2348{
2349 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2350}
2351
2352static ssize_t
2353ftrace_notrace_write(struct file *file, const char __user *ubuf,
2354 size_t cnt, loff_t *ppos)
2355{
2356 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2357}
2358
2359static void
Steven Rostedtf45948e2011-05-02 12:29:25 -04002360ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2361 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04002362{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002363 struct ftrace_hash *hash;
2364
Steven Rostedt41c52c02008-05-22 11:46:33 -04002365 if (unlikely(ftrace_disabled))
2366 return;
2367
Steven Rostedtf45948e2011-05-02 12:29:25 -04002368 if (enable)
2369 hash = ops->filter_hash;
2370 else
2371 hash = ops->notrace_hash;
2372
Steven Rostedt41c52c02008-05-22 11:46:33 -04002373 mutex_lock(&ftrace_regex_lock);
2374 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002375 ftrace_filter_reset(hash);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002376 if (buf)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002377 ftrace_match_records(hash, buf, len);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002378 mutex_unlock(&ftrace_regex_lock);
2379}
2380
Steven Rostedt77a2b372008-05-12 21:20:45 +02002381/**
2382 * ftrace_set_filter - set a function to filter on in ftrace
2383 * @buf - the string that holds the function filter text.
2384 * @len - the length of the string.
2385 * @reset - non zero to reset all filters before applying this filter.
2386 *
2387 * Filters denote which functions should be enabled when tracing is enabled.
2388 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2389 */
Ingo Molnare309b412008-05-12 21:20:51 +02002390void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002391{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002392 ftrace_set_regex(&global_ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002393}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002394
Steven Rostedt41c52c02008-05-22 11:46:33 -04002395/**
2396 * ftrace_set_notrace - set a function to not trace in ftrace
2397 * @buf - the string that holds the function notrace text.
2398 * @len - the length of the string.
2399 * @reset - non zero to reset all filters before applying this filter.
2400 *
2401 * Notrace Filters denote which functions should not be enabled when tracing
2402 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2403 * for tracing.
2404 */
2405void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2406{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002407 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002408}
2409
Steven Rostedt2af15d62009-05-28 13:37:24 -04002410/*
2411 * command line interface to allow users to set filters on boot up.
2412 */
2413#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2414static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2415static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2416
2417static int __init set_ftrace_notrace(char *str)
2418{
2419 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2420 return 1;
2421}
2422__setup("ftrace_notrace=", set_ftrace_notrace);
2423
2424static int __init set_ftrace_filter(char *str)
2425{
2426 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2427 return 1;
2428}
2429__setup("ftrace_filter=", set_ftrace_filter);
2430
Stefan Assmann369bc182009-10-12 22:17:21 +02002431#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002432static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002433static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2434
Stefan Assmann369bc182009-10-12 22:17:21 +02002435static int __init set_graph_function(char *str)
2436{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002437 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002438 return 1;
2439}
2440__setup("ftrace_graph_filter=", set_graph_function);
2441
2442static void __init set_ftrace_early_graph(char *buf)
2443{
2444 int ret;
2445 char *func;
2446
2447 while (buf) {
2448 func = strsep(&buf, ",");
2449 /* we allow only one expression at a time */
2450 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2451 func);
2452 if (ret)
2453 printk(KERN_DEBUG "ftrace: function %s not "
2454 "traceable\n", func);
2455 }
2456}
2457#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2458
Steven Rostedtf45948e2011-05-02 12:29:25 -04002459static void __init
2460set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04002461{
2462 char *func;
2463
2464 while (buf) {
2465 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04002466 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04002467 }
2468}
2469
2470static void __init set_ftrace_early_filters(void)
2471{
2472 if (ftrace_filter_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04002473 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04002474 if (ftrace_notrace_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04002475 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002476#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2477 if (ftrace_graph_buf[0])
2478 set_ftrace_early_graph(ftrace_graph_buf);
2479#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002480}
2481
Ingo Molnare309b412008-05-12 21:20:51 +02002482static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002483ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002484{
2485 struct seq_file *m = (struct seq_file *)file->private_data;
2486 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002487 struct trace_parser *parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02002488
Steven Rostedt41c52c02008-05-22 11:46:33 -04002489 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002490 if (file->f_mode & FMODE_READ) {
2491 iter = m->private;
2492
2493 seq_release(inode, file);
2494 } else
2495 iter = file->private_data;
2496
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002497 parser = &iter->parser;
2498 if (trace_parser_loaded(parser)) {
2499 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002500 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02002501 }
2502
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002503 trace_parser_put(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002504 kfree(iter);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002505
Steven Rostedt058e2972011-04-29 22:35:33 -04002506 if (file->f_mode & FMODE_WRITE) {
2507 mutex_lock(&ftrace_lock);
2508 if (ftrace_start_up && ftrace_enabled)
2509 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2510 mutex_unlock(&ftrace_lock);
2511 }
2512
Steven Rostedt41c52c02008-05-22 11:46:33 -04002513 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002514 return 0;
2515}
2516
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002517static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002518 .open = ftrace_avail_open,
2519 .read = seq_read,
2520 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002521 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002522};
2523
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002524static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002525 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002526 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002527 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002528 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002529 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02002530};
2531
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002532static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002533 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002534 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002535 .write = ftrace_notrace_write,
2536 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002537 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002538};
2539
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002540#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2541
2542static DEFINE_MUTEX(graph_lock);
2543
2544int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002545int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002546unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2547
2548static void *
Li Zefan85951842009-06-24 09:54:00 +08002549__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002550{
Li Zefan85951842009-06-24 09:54:00 +08002551 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002552 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002553 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002554}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002555
Li Zefan85951842009-06-24 09:54:00 +08002556static void *
2557g_next(struct seq_file *m, void *v, loff_t *pos)
2558{
2559 (*pos)++;
2560 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002561}
2562
2563static void *g_start(struct seq_file *m, loff_t *pos)
2564{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002565 mutex_lock(&graph_lock);
2566
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002567 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002568 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002569 return (void *)1;
2570
Li Zefan85951842009-06-24 09:54:00 +08002571 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002572}
2573
2574static void g_stop(struct seq_file *m, void *p)
2575{
2576 mutex_unlock(&graph_lock);
2577}
2578
2579static int g_show(struct seq_file *m, void *v)
2580{
2581 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002582
2583 if (!ptr)
2584 return 0;
2585
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002586 if (ptr == (unsigned long *)1) {
2587 seq_printf(m, "#### all functions enabled ####\n");
2588 return 0;
2589 }
2590
Steven Rostedtb375a112009-09-17 00:05:58 -04002591 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002592
2593 return 0;
2594}
2595
James Morris88e9d342009-09-22 16:43:43 -07002596static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002597 .start = g_start,
2598 .next = g_next,
2599 .stop = g_stop,
2600 .show = g_show,
2601};
2602
2603static int
2604ftrace_graph_open(struct inode *inode, struct file *file)
2605{
2606 int ret = 0;
2607
2608 if (unlikely(ftrace_disabled))
2609 return -ENODEV;
2610
2611 mutex_lock(&graph_lock);
2612 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002613 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002614 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002615 ftrace_graph_count = 0;
2616 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2617 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002618 mutex_unlock(&graph_lock);
2619
Li Zefana4ec5e02009-09-18 14:06:28 +08002620 if (file->f_mode & FMODE_READ)
2621 ret = seq_open(file, &ftrace_graph_seq_ops);
2622
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002623 return ret;
2624}
2625
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002626static int
Li Zefan87827112009-07-23 11:29:11 +08002627ftrace_graph_release(struct inode *inode, struct file *file)
2628{
2629 if (file->f_mode & FMODE_READ)
2630 seq_release(inode, file);
2631 return 0;
2632}
2633
2634static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002635ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002636{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002637 struct dyn_ftrace *rec;
2638 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002639 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002640 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002641 int type, not;
2642 char *search;
2643 bool exists;
2644 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002645
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002646 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002647 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002648 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2649 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002650
2651 search_len = strlen(search);
2652
Steven Rostedt52baf112009-02-14 01:15:39 -05002653 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002654
2655 if (unlikely(ftrace_disabled)) {
2656 mutex_unlock(&ftrace_lock);
2657 return -ENODEV;
2658 }
2659
Steven Rostedt265c8312009-02-13 12:43:56 -05002660 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002661
Steven Rostedt45a4a232011-04-21 23:16:46 -04002662 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05002663 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002664
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002665 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002666 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002667 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002668 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002669 if (array[i] == rec->ip) {
2670 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002671 break;
2672 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002673 }
2674
2675 if (!not) {
2676 fail = 0;
2677 if (!exists) {
2678 array[(*idx)++] = rec->ip;
2679 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2680 goto out;
2681 }
2682 } else {
2683 if (exists) {
2684 array[i] = array[--(*idx)];
2685 array[*idx] = 0;
2686 fail = 0;
2687 }
2688 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002689 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002690 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002691out:
Steven Rostedt52baf112009-02-14 01:15:39 -05002692 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002693
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002694 if (fail)
2695 return -EINVAL;
2696
2697 ftrace_graph_filter_enabled = 1;
2698 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002699}
2700
2701static ssize_t
2702ftrace_graph_write(struct file *file, const char __user *ubuf,
2703 size_t cnt, loff_t *ppos)
2704{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002705 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08002706 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002707
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002708 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002709 return 0;
2710
2711 mutex_lock(&graph_lock);
2712
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002713 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2714 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08002715 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002716 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002717
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002718 read = trace_get_user(&parser, ubuf, cnt, ppos);
2719
Li Zefan4ba79782009-09-22 13:52:20 +08002720 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002721 parser.buffer[parser.idx] = 0;
2722
2723 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08002724 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002725 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002726 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08002727 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002728 }
2729
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002730 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08002731
2732out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002733 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08002734out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002735 mutex_unlock(&graph_lock);
2736
2737 return ret;
2738}
2739
2740static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08002741 .open = ftrace_graph_open,
2742 .read = seq_read,
2743 .write = ftrace_graph_write,
2744 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002745 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002746};
2747#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2748
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002749static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002750{
Steven Rostedt5072c592008-05-12 21:20:43 +02002751
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002752 trace_create_file("available_filter_functions", 0444,
2753 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002754
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002755 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2756 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002757
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002758 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002759 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002760
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002761#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002762 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002763 NULL,
2764 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002765#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2766
Steven Rostedt5072c592008-05-12 21:20:43 +02002767 return 0;
2768}
2769
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002770static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08002771 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002772 unsigned long *end)
2773{
2774 unsigned long *p;
2775 unsigned long addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002776
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002777 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002778 p = start;
2779 while (p < end) {
2780 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002781 /*
2782 * Some architecture linkers will pad between
2783 * the different mcount_loc sections of different
2784 * object files to satisfy alignments.
2785 * Skip any NULL pointers.
2786 */
2787 if (!addr)
2788 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002789 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002790 }
2791
Steven Rostedt31e88902008-11-14 16:21:19 -08002792 ftrace_update_code(mod);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002793 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002794
2795 return 0;
2796}
2797
Steven Rostedt93eb6772009-04-15 13:24:06 -04002798#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002799void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002800{
2801 struct dyn_ftrace *rec;
2802 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04002803
Steven Rostedt93eb6772009-04-15 13:24:06 -04002804 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002805
2806 if (ftrace_disabled)
2807 goto out_unlock;
2808
Steven Rostedt93eb6772009-04-15 13:24:06 -04002809 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002810 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04002811 /*
2812 * rec->ip is changed in ftrace_free_rec()
2813 * It should not between s and e if record was freed.
2814 */
2815 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2816 ftrace_free_rec(rec);
2817 }
2818 } while_for_each_ftrace_rec();
Steven Rostedt45a4a232011-04-21 23:16:46 -04002819 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04002820 mutex_unlock(&ftrace_lock);
2821}
2822
2823static void ftrace_init_module(struct module *mod,
2824 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002825{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002826 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002827 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002828 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002829}
2830
Steven Rostedt93eb6772009-04-15 13:24:06 -04002831static int ftrace_module_notify(struct notifier_block *self,
2832 unsigned long val, void *data)
2833{
2834 struct module *mod = data;
2835
2836 switch (val) {
2837 case MODULE_STATE_COMING:
2838 ftrace_init_module(mod, mod->ftrace_callsites,
2839 mod->ftrace_callsites +
2840 mod->num_ftrace_callsites);
2841 break;
2842 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002843 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04002844 break;
2845 }
2846
2847 return 0;
2848}
2849#else
2850static int ftrace_module_notify(struct notifier_block *self,
2851 unsigned long val, void *data)
2852{
2853 return 0;
2854}
2855#endif /* CONFIG_MODULES */
2856
2857struct notifier_block ftrace_module_nb = {
2858 .notifier_call = ftrace_module_notify,
2859 .priority = 0,
2860};
2861
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002862extern unsigned long __start_mcount_loc[];
2863extern unsigned long __stop_mcount_loc[];
2864
2865void __init ftrace_init(void)
2866{
2867 unsigned long count, addr, flags;
2868 int ret;
2869
2870 /* Keep the ftrace pointer to the stub */
2871 addr = (unsigned long)ftrace_stub;
2872
2873 local_irq_save(flags);
2874 ftrace_dyn_arch_init(&addr);
2875 local_irq_restore(flags);
2876
2877 /* ftrace_dyn_arch_init places the return code in addr */
2878 if (addr)
2879 goto failed;
2880
2881 count = __stop_mcount_loc - __start_mcount_loc;
2882
2883 ret = ftrace_dyn_table_alloc(count);
2884 if (ret)
2885 goto failed;
2886
2887 last_ftrace_enabled = ftrace_enabled = 1;
2888
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002889 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08002890 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002891 __stop_mcount_loc);
2892
Steven Rostedt93eb6772009-04-15 13:24:06 -04002893 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002894 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002895 pr_warning("Failed to register trace ftrace module notifier\n");
2896
Steven Rostedt2af15d62009-05-28 13:37:24 -04002897 set_ftrace_early_filters();
2898
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002899 return;
2900 failed:
2901 ftrace_disabled = 1;
2902}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002903
Steven Rostedt3d083392008-05-12 21:20:42 +02002904#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002905
2906static int __init ftrace_nodyn_init(void)
2907{
2908 ftrace_enabled = 1;
2909 return 0;
2910}
2911device_initcall(ftrace_nodyn_init);
2912
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002913static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2914static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002915/* Keep as macros so we do not need to define the commands */
2916# define ftrace_startup(command) do { } while (0)
2917# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002918# define ftrace_startup_sysctl() do { } while (0)
2919# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002920#endif /* CONFIG_DYNAMIC_FTRACE */
2921
Steven Rostedte32d8952008-12-04 00:26:41 -05002922static void clear_ftrace_swapper(void)
2923{
2924 struct task_struct *p;
2925 int cpu;
2926
2927 get_online_cpus();
2928 for_each_online_cpu(cpu) {
2929 p = idle_task(cpu);
2930 clear_tsk_trace_trace(p);
2931 }
2932 put_online_cpus();
2933}
2934
2935static void set_ftrace_swapper(void)
2936{
2937 struct task_struct *p;
2938 int cpu;
2939
2940 get_online_cpus();
2941 for_each_online_cpu(cpu) {
2942 p = idle_task(cpu);
2943 set_tsk_trace_trace(p);
2944 }
2945 put_online_cpus();
2946}
2947
2948static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002949{
2950 struct task_struct *p;
2951
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002952 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002953 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002954 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002955 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002956 rcu_read_unlock();
2957
Steven Rostedte32d8952008-12-04 00:26:41 -05002958 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002959}
2960
Steven Rostedte32d8952008-12-04 00:26:41 -05002961static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002962{
2963 struct task_struct *p;
2964
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002965 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002966 do_each_pid_task(pid, PIDTYPE_PID, p) {
2967 set_tsk_trace_trace(p);
2968 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002969 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002970}
2971
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002972static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002973{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002974 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002975 clear_ftrace_swapper();
2976 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002977 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05002978}
2979
2980static void set_ftrace_pid_task(struct pid *pid)
2981{
2982 if (pid == ftrace_swapper_pid)
2983 set_ftrace_swapper();
2984 else
2985 set_ftrace_pid(pid);
2986}
2987
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002988static int ftrace_pid_add(int p)
2989{
2990 struct pid *pid;
2991 struct ftrace_pid *fpid;
2992 int ret = -EINVAL;
2993
2994 mutex_lock(&ftrace_lock);
2995
2996 if (!p)
2997 pid = ftrace_swapper_pid;
2998 else
2999 pid = find_get_pid(p);
3000
3001 if (!pid)
3002 goto out;
3003
3004 ret = 0;
3005
3006 list_for_each_entry(fpid, &ftrace_pids, list)
3007 if (fpid->pid == pid)
3008 goto out_put;
3009
3010 ret = -ENOMEM;
3011
3012 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3013 if (!fpid)
3014 goto out_put;
3015
3016 list_add(&fpid->list, &ftrace_pids);
3017 fpid->pid = pid;
3018
3019 set_ftrace_pid_task(pid);
3020
3021 ftrace_update_pid_func();
3022 ftrace_startup_enable(0);
3023
3024 mutex_unlock(&ftrace_lock);
3025 return 0;
3026
3027out_put:
3028 if (pid != ftrace_swapper_pid)
3029 put_pid(pid);
3030
3031out:
3032 mutex_unlock(&ftrace_lock);
3033 return ret;
3034}
3035
3036static void ftrace_pid_reset(void)
3037{
3038 struct ftrace_pid *fpid, *safe;
3039
3040 mutex_lock(&ftrace_lock);
3041 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3042 struct pid *pid = fpid->pid;
3043
3044 clear_ftrace_pid_task(pid);
3045
3046 list_del(&fpid->list);
3047 kfree(fpid);
3048 }
3049
3050 ftrace_update_pid_func();
3051 ftrace_startup_enable(0);
3052
3053 mutex_unlock(&ftrace_lock);
3054}
3055
3056static void *fpid_start(struct seq_file *m, loff_t *pos)
3057{
3058 mutex_lock(&ftrace_lock);
3059
3060 if (list_empty(&ftrace_pids) && (!*pos))
3061 return (void *) 1;
3062
3063 return seq_list_start(&ftrace_pids, *pos);
3064}
3065
3066static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3067{
3068 if (v == (void *)1)
3069 return NULL;
3070
3071 return seq_list_next(v, &ftrace_pids, pos);
3072}
3073
3074static void fpid_stop(struct seq_file *m, void *p)
3075{
3076 mutex_unlock(&ftrace_lock);
3077}
3078
3079static int fpid_show(struct seq_file *m, void *v)
3080{
3081 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3082
3083 if (v == (void *)1) {
3084 seq_printf(m, "no pid\n");
3085 return 0;
3086 }
3087
3088 if (fpid->pid == ftrace_swapper_pid)
3089 seq_printf(m, "swapper tasks\n");
3090 else
3091 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3092
3093 return 0;
3094}
3095
3096static const struct seq_operations ftrace_pid_sops = {
3097 .start = fpid_start,
3098 .next = fpid_next,
3099 .stop = fpid_stop,
3100 .show = fpid_show,
3101};
3102
3103static int
3104ftrace_pid_open(struct inode *inode, struct file *file)
3105{
3106 int ret = 0;
3107
3108 if ((file->f_mode & FMODE_WRITE) &&
3109 (file->f_flags & O_TRUNC))
3110 ftrace_pid_reset();
3111
3112 if (file->f_mode & FMODE_READ)
3113 ret = seq_open(file, &ftrace_pid_sops);
3114
3115 return ret;
3116}
3117
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003118static ssize_t
3119ftrace_pid_write(struct file *filp, const char __user *ubuf,
3120 size_t cnt, loff_t *ppos)
3121{
Ingo Molnar457dc922009-11-23 11:03:28 +01003122 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003123 long val;
3124 int ret;
3125
3126 if (cnt >= sizeof(buf))
3127 return -EINVAL;
3128
3129 if (copy_from_user(&buf, ubuf, cnt))
3130 return -EFAULT;
3131
3132 buf[cnt] = 0;
3133
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003134 /*
3135 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3136 * to clean the filter quietly.
3137 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003138 tmp = strstrip(buf);
3139 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003140 return 1;
3141
Ingo Molnar457dc922009-11-23 11:03:28 +01003142 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003143 if (ret < 0)
3144 return ret;
3145
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003146 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003147
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003148 return ret ? ret : cnt;
3149}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003150
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003151static int
3152ftrace_pid_release(struct inode *inode, struct file *file)
3153{
3154 if (file->f_mode & FMODE_READ)
3155 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003156
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003157 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003158}
3159
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003160static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003161 .open = ftrace_pid_open,
3162 .write = ftrace_pid_write,
3163 .read = seq_read,
3164 .llseek = seq_lseek,
3165 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003166};
3167
3168static __init int ftrace_init_debugfs(void)
3169{
3170 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003171
3172 d_tracer = tracing_init_dentry();
3173 if (!d_tracer)
3174 return 0;
3175
3176 ftrace_init_dyn_debugfs(d_tracer);
3177
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003178 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3179 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003180
3181 ftrace_profile_debugfs(d_tracer);
3182
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003183 return 0;
3184}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003185fs_initcall(ftrace_init_debugfs);
3186
Steven Rostedt3d083392008-05-12 21:20:42 +02003187/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003188 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003189 *
3190 * This function should be used by panic code. It stops ftrace
3191 * but in a not so nice way. If you need to simply kill ftrace
3192 * from a non-atomic section, use ftrace_kill.
3193 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003194void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003195{
3196 ftrace_disabled = 1;
3197 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003198 clear_ftrace_function();
3199}
3200
3201/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003202 * register_ftrace_function - register a function for profiling
3203 * @ops - ops structure that holds the function for profiling.
3204 *
3205 * Register a function to be called by all functions in the
3206 * kernel.
3207 *
3208 * Note: @ops->func and all the functions it calls must be labeled
3209 * with "notrace", otherwise it will go into a
3210 * recursive loop.
3211 */
3212int register_ftrace_function(struct ftrace_ops *ops)
3213{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003214 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003215
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003216 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003217
Steven Rostedt45a4a232011-04-21 23:16:46 -04003218 if (unlikely(ftrace_disabled))
3219 goto out_unlock;
3220
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003221 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003222 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003223
Steven Rostedt45a4a232011-04-21 23:16:46 -04003224 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003225 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003226 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003227}
3228
3229/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003230 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003231 * @ops - ops structure that holds the function to unregister
3232 *
3233 * Unregister a function that was added to be called by ftrace profiling.
3234 */
3235int unregister_ftrace_function(struct ftrace_ops *ops)
3236{
3237 int ret;
3238
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003239 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003240 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003241 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003242 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003243
3244 return ret;
3245}
3246
Ingo Molnare309b412008-05-12 21:20:51 +02003247int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003248ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003249 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003250 loff_t *ppos)
3251{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003252 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003253
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003254 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003255
Steven Rostedt45a4a232011-04-21 23:16:46 -04003256 if (unlikely(ftrace_disabled))
3257 goto out;
3258
3259 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003260
Li Zefana32c7762009-06-26 16:55:51 +08003261 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003262 goto out;
3263
Li Zefana32c7762009-06-26 16:55:51 +08003264 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003265
3266 if (ftrace_enabled) {
3267
3268 ftrace_startup_sysctl();
3269
3270 /* we are starting ftrace again */
3271 if (ftrace_list != &ftrace_list_end) {
3272 if (ftrace_list->next == &ftrace_list_end)
3273 ftrace_trace_function = ftrace_list->func;
3274 else
3275 ftrace_trace_function = ftrace_list_func;
3276 }
3277
3278 } else {
3279 /* stopping ftrace calls (just send to ftrace_stub) */
3280 ftrace_trace_function = ftrace_stub;
3281
3282 ftrace_shutdown_sysctl();
3283 }
3284
3285 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003286 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003287 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003288}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003289
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003290#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003291
Steven Rostedt597af812009-04-03 15:24:12 -04003292static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003293static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003294
Steven Rostedte49dc192008-12-02 23:50:05 -05003295int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3296{
3297 return 0;
3298}
3299
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003300/* The callbacks that hook a function */
3301trace_func_graph_ret_t ftrace_graph_return =
3302 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003303trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003304
3305/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3306static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3307{
3308 int i;
3309 int ret = 0;
3310 unsigned long flags;
3311 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3312 struct task_struct *g, *t;
3313
3314 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3315 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3316 * sizeof(struct ftrace_ret_stack),
3317 GFP_KERNEL);
3318 if (!ret_stack_list[i]) {
3319 start = 0;
3320 end = i;
3321 ret = -ENOMEM;
3322 goto free;
3323 }
3324 }
3325
3326 read_lock_irqsave(&tasklist_lock, flags);
3327 do_each_thread(g, t) {
3328 if (start == end) {
3329 ret = -EAGAIN;
3330 goto unlock;
3331 }
3332
3333 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003334 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003335 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003336 t->curr_ret_stack = -1;
3337 /* Make sure the tasks see the -1 first: */
3338 smp_wmb();
3339 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003340 }
3341 } while_each_thread(g, t);
3342
3343unlock:
3344 read_unlock_irqrestore(&tasklist_lock, flags);
3345free:
3346 for (i = start; i < end; i++)
3347 kfree(ret_stack_list[i]);
3348 return ret;
3349}
3350
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003351static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04003352ftrace_graph_probe_sched_switch(void *ignore,
3353 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003354{
3355 unsigned long long timestamp;
3356 int index;
3357
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003358 /*
3359 * Does the user want to count the time a function was asleep.
3360 * If so, do not update the time stamps.
3361 */
3362 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3363 return;
3364
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003365 timestamp = trace_clock_local();
3366
3367 prev->ftrace_timestamp = timestamp;
3368
3369 /* only process tasks that we timestamped */
3370 if (!next->ftrace_timestamp)
3371 return;
3372
3373 /*
3374 * Update all the counters in next to make up for the
3375 * time next was sleeping.
3376 */
3377 timestamp -= next->ftrace_timestamp;
3378
3379 for (index = next->curr_ret_stack; index >= 0; index--)
3380 next->ret_stack[index].calltime += timestamp;
3381}
3382
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003383/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003384static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003385{
3386 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003387 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003388
3389 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3390 sizeof(struct ftrace_ret_stack *),
3391 GFP_KERNEL);
3392
3393 if (!ret_stack_list)
3394 return -ENOMEM;
3395
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003396 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003397 for_each_online_cpu(cpu) {
3398 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05003399 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04003400 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003401
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003402 do {
3403 ret = alloc_retstack_tasklist(ret_stack_list);
3404 } while (ret == -EAGAIN);
3405
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003406 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04003407 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003408 if (ret)
3409 pr_info("ftrace_graph: Couldn't activate tracepoint"
3410 " probe to kernel_sched_switch\n");
3411 }
3412
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003413 kfree(ret_stack_list);
3414 return ret;
3415}
3416
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003417/*
3418 * Hibernation protection.
3419 * The state of the current task is too much unstable during
3420 * suspend/restore to disk. We want to protect against that.
3421 */
3422static int
3423ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3424 void *unused)
3425{
3426 switch (state) {
3427 case PM_HIBERNATION_PREPARE:
3428 pause_graph_tracing();
3429 break;
3430
3431 case PM_POST_HIBERNATION:
3432 unpause_graph_tracing();
3433 break;
3434 }
3435 return NOTIFY_DONE;
3436}
3437
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003438int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3439 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003440{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003441 int ret = 0;
3442
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003443 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003444
Steven Rostedt05ce5812009-03-24 00:18:31 -04003445 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003446 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003447 ret = -EBUSY;
3448 goto out;
3449 }
3450
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003451 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3452 register_pm_notifier(&ftrace_suspend_notifier);
3453
Steven Rostedt597af812009-04-03 15:24:12 -04003454 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003455 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003456 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003457 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003458 goto out;
3459 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003460
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003461 ftrace_graph_return = retfunc;
3462 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003463
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003464 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003465
3466out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003467 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003468 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003469}
3470
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003471void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003472{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003473 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003474
Steven Rostedt597af812009-04-03 15:24:12 -04003475 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003476 goto out;
3477
Steven Rostedt597af812009-04-03 15:24:12 -04003478 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003479 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003480 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003481 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003482 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04003483 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003484
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003485 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003486 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003487}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003488
Steven Rostedt868baf02011-02-10 21:26:13 -05003489static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
3490
3491static void
3492graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
3493{
3494 atomic_set(&t->tracing_graph_pause, 0);
3495 atomic_set(&t->trace_overrun, 0);
3496 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003497 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05003498 smp_wmb();
3499 t->ret_stack = ret_stack;
3500}
3501
3502/*
3503 * Allocate a return stack for the idle task. May be the first
3504 * time through, or it may be done by CPU hotplug online.
3505 */
3506void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
3507{
3508 t->curr_ret_stack = -1;
3509 /*
3510 * The idle task has no parent, it either has its own
3511 * stack or no stack at all.
3512 */
3513 if (t->ret_stack)
3514 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
3515
3516 if (ftrace_graph_active) {
3517 struct ftrace_ret_stack *ret_stack;
3518
3519 ret_stack = per_cpu(idle_ret_stack, cpu);
3520 if (!ret_stack) {
3521 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3522 * sizeof(struct ftrace_ret_stack),
3523 GFP_KERNEL);
3524 if (!ret_stack)
3525 return;
3526 per_cpu(idle_ret_stack, cpu) = ret_stack;
3527 }
3528 graph_init_task(t, ret_stack);
3529 }
3530}
3531
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003532/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003533void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003534{
Steven Rostedt84047e32009-06-02 16:51:55 -04003535 /* Make sure we do not use the parent ret_stack */
3536 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05003537 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04003538
Steven Rostedt597af812009-04-03 15:24:12 -04003539 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003540 struct ftrace_ret_stack *ret_stack;
3541
3542 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003543 * sizeof(struct ftrace_ret_stack),
3544 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003545 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003546 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05003547 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04003548 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003549}
3550
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003551void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003552{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003553 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3554
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003555 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003556 /* NULL must become visible to IRQs before we free it: */
3557 barrier();
3558
3559 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003560}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003561
3562void ftrace_graph_stop(void)
3563{
3564 ftrace_stop();
3565}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003566#endif