blob: 04c002a491fb507c78f217da5627f6f33f25f34f [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 Rostedt5072c592008-05-12 21:20:43 +0200886static int ftrace_filtered;
887
Lai Jiangshane94142a2009-03-13 17:51:27 +0800888static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200889
Steven Rostedt41c52c02008-05-22 11:46:33 -0400890static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200891
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200892struct ftrace_page {
893 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500894 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200895 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700896};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200897
898#define ENTRIES_PER_PAGE \
899 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
900
901/* estimate from running different kernels */
902#define NR_TO_INIT 10000
903
904static struct ftrace_page *ftrace_pages_start;
905static struct ftrace_page *ftrace_pages;
906
Steven Rostedt37ad50842008-05-12 21:20:48 +0200907static struct dyn_ftrace *ftrace_free_records;
908
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400909static struct ftrace_func_entry *
910ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
911{
912 unsigned long key;
913 struct ftrace_func_entry *entry;
914 struct hlist_head *hhd;
915 struct hlist_node *n;
916
917 if (!hash->count)
918 return NULL;
919
920 if (hash->size_bits > 0)
921 key = hash_long(ip, hash->size_bits);
922 else
923 key = 0;
924
925 hhd = &hash->buckets[key];
926
927 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
928 if (entry->ip == ip)
929 return entry;
930 }
931 return NULL;
932}
933
934static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
935{
936 struct ftrace_func_entry *entry;
937 struct hlist_head *hhd;
938 unsigned long key;
939
940 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
941 if (!entry)
942 return -ENOMEM;
943
944 if (hash->size_bits)
945 key = hash_long(ip, hash->size_bits);
946 else
947 key = 0;
948
949 entry->ip = ip;
950 hhd = &hash->buckets[key];
951 hlist_add_head(&entry->hlist, hhd);
952 hash->count++;
953
954 return 0;
955}
956
957static void
958remove_hash_entry(struct ftrace_hash *hash,
959 struct ftrace_func_entry *entry)
960{
961 hlist_del(&entry->hlist);
962 kfree(entry);
963 hash->count--;
964}
965
966static void ftrace_hash_clear(struct ftrace_hash *hash)
967{
968 struct hlist_head *hhd;
969 struct hlist_node *tp, *tn;
970 struct ftrace_func_entry *entry;
971 int size = 1 << hash->size_bits;
972 int i;
973
974 for (i = 0; i < size; i++) {
975 hhd = &hash->buckets[i];
976 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
977 remove_hash_entry(hash, entry);
978 }
979 FTRACE_WARN_ON(hash->count);
980}
981
Steven Rostedt265c8312009-02-13 12:43:56 -0500982/*
983 * This is a double for. Do not use 'break' to break out of the loop,
984 * you must use a goto.
985 */
986#define do_for_each_ftrace_rec(pg, rec) \
987 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
988 int _____i; \
989 for (_____i = 0; _____i < pg->index; _____i++) { \
990 rec = &pg->records[_____i];
991
992#define while_for_each_ftrace_rec() \
993 } \
994 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530995
Ingo Molnare309b412008-05-12 21:20:51 +0200996static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad50842008-05-12 21:20:48 +0200997{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800998 rec->freelist = ftrace_free_records;
Steven Rostedt37ad50842008-05-12 21:20:48 +0200999 ftrace_free_records = rec;
1000 rec->flags |= FTRACE_FL_FREE;
1001}
1002
Ingo Molnare309b412008-05-12 21:20:51 +02001003static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001004{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001005 struct dyn_ftrace *rec;
1006
1007 /* First check for freed records */
1008 if (ftrace_free_records) {
1009 rec = ftrace_free_records;
1010
Steven Rostedt37ad50842008-05-12 21:20:48 +02001011 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt6912896e2008-10-23 09:33:03 -04001012 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad50842008-05-12 21:20:48 +02001013 ftrace_free_records = NULL;
1014 return NULL;
1015 }
1016
Lai Jiangshanee000b72009-03-24 13:38:06 +08001017 ftrace_free_records = rec->freelist;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001018 memset(rec, 0, sizeof(*rec));
1019 return rec;
1020 }
1021
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001022 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001023 if (!ftrace_pages->next) {
1024 /* allocate another page */
1025 ftrace_pages->next =
1026 (void *)get_zeroed_page(GFP_KERNEL);
1027 if (!ftrace_pages->next)
1028 return NULL;
1029 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001030 ftrace_pages = ftrace_pages->next;
1031 }
1032
1033 return &ftrace_pages->records[ftrace_pages->index++];
1034}
1035
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001036static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001037ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001038{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001039 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001040
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001041 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001042 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001043
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001044 rec = ftrace_alloc_dyn_node(ip);
1045 if (!rec)
1046 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001047
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001048 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001049 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001050 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001051
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001052 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001053}
1054
Steven Rostedt05736a42008-09-22 14:55:47 -07001055static void print_ip_ins(const char *fmt, unsigned char *p)
1056{
1057 int i;
1058
1059 printk(KERN_CONT "%s", fmt);
1060
1061 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1062 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1063}
1064
Steven Rostedt31e88902008-11-14 16:21:19 -08001065static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001066{
1067 switch (failed) {
1068 case -EFAULT:
1069 FTRACE_WARN_ON_ONCE(1);
1070 pr_info("ftrace faulted on modifying ");
1071 print_ip_sym(ip);
1072 break;
1073 case -EINVAL:
1074 FTRACE_WARN_ON_ONCE(1);
1075 pr_info("ftrace failed to modify ");
1076 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001077 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001078 printk(KERN_CONT "\n");
1079 break;
1080 case -EPERM:
1081 FTRACE_WARN_ON_ONCE(1);
1082 pr_info("ftrace faulted on writing ");
1083 print_ip_sym(ip);
1084 break;
1085 default:
1086 FTRACE_WARN_ON_ONCE(1);
1087 pr_info("ftrace faulted on unknown error ");
1088 print_ip_sym(ip);
1089 }
1090}
1091
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001092
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001093/* Return 1 if the address range is reserved for ftrace */
1094int ftrace_text_reserved(void *start, void *end)
1095{
1096 struct dyn_ftrace *rec;
1097 struct ftrace_page *pg;
1098
1099 do_for_each_ftrace_rec(pg, rec) {
1100 if (rec->ip <= (unsigned long)end &&
1101 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1102 return 1;
1103 } while_for_each_ftrace_rec();
1104 return 0;
1105}
1106
1107
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301108static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001109__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001110{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001111 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001112 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001113
Shaohua Lif0001202009-01-09 11:29:42 +08001114 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001115
Steven Rostedt982c3502008-11-15 16:31:41 -05001116 /*
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001117 * If this record is not to be traced or we want to disable it,
1118 * then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001119 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001120 * If we want to enable it and filtering is off, then enable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001121 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001122 * If we want to enable it and filtering is on, enable it only if
1123 * it's filtered
Steven Rostedt982c3502008-11-15 16:31:41 -05001124 */
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001125 if (enable && !ftrace_lookup_ip(&notrace_hash, rec->ip)) {
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001126 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1127 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001128 }
1129
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001130 /* If the state of this record hasn't changed, then do nothing */
1131 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1132 return 0;
1133
1134 if (flag) {
1135 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001136 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001137 }
1138
1139 rec->flags &= ~FTRACE_FL_ENABLED;
1140 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001141}
1142
1143static void ftrace_replace_code(int enable)
1144{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001145 struct dyn_ftrace *rec;
1146 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001147 int failed;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001148
Steven Rostedt45a4a232011-04-21 23:16:46 -04001149 if (unlikely(ftrace_disabled))
1150 return;
1151
Steven Rostedt265c8312009-02-13 12:43:56 -05001152 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001153 /* Skip over free records */
1154 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05001155 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001156
Steven Rostedt265c8312009-02-13 12:43:56 -05001157 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001158 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001159 ftrace_bug(failed, rec->ip);
1160 /* Stop processing */
1161 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001162 }
1163 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001164}
1165
Ingo Molnare309b412008-05-12 21:20:51 +02001166static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001167ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001168{
1169 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001170 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001171
1172 ip = rec->ip;
1173
Steven Rostedt45a4a232011-04-21 23:16:46 -04001174 if (unlikely(ftrace_disabled))
1175 return 0;
1176
Shaohua Li25aac9d2009-01-09 11:29:40 +08001177 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001178 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001179 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301180 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001181 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301182 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001183}
1184
Steven Rostedt000ab692009-02-17 13:35:06 -05001185/*
1186 * archs can override this function if they must do something
1187 * before the modifying code is performed.
1188 */
1189int __weak ftrace_arch_code_modify_prepare(void)
1190{
1191 return 0;
1192}
1193
1194/*
1195 * archs can override this function if they must do something
1196 * after the modifying code is performed.
1197 */
1198int __weak ftrace_arch_code_modify_post_process(void)
1199{
1200 return 0;
1201}
1202
Ingo Molnare309b412008-05-12 21:20:51 +02001203static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001204{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001205 int *command = data;
1206
Steven Rostedta3583242008-11-11 15:01:42 -05001207 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001208 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001209 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001210 ftrace_replace_code(0);
1211
1212 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1213 ftrace_update_ftrace_func(ftrace_trace_function);
1214
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001215 if (*command & FTRACE_START_FUNC_RET)
1216 ftrace_enable_ftrace_graph_caller();
1217 else if (*command & FTRACE_STOP_FUNC_RET)
1218 ftrace_disable_ftrace_graph_caller();
1219
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001220 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001221}
1222
Ingo Molnare309b412008-05-12 21:20:51 +02001223static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001224{
Steven Rostedt000ab692009-02-17 13:35:06 -05001225 int ret;
1226
1227 ret = ftrace_arch_code_modify_prepare();
1228 FTRACE_WARN_ON(ret);
1229 if (ret)
1230 return;
1231
Rusty Russell784e2d72008-07-28 12:16:31 -05001232 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001233
1234 ret = ftrace_arch_code_modify_post_process();
1235 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001236}
1237
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001238static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001239static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001240
1241static void ftrace_startup_enable(int command)
1242{
1243 if (saved_ftrace_func != ftrace_trace_function) {
1244 saved_ftrace_func = ftrace_trace_function;
1245 command |= FTRACE_UPDATE_TRACE_FUNC;
1246 }
1247
1248 if (!command || !ftrace_enabled)
1249 return;
1250
1251 ftrace_run_update_code(command);
1252}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001253
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001254static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001255{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001256 if (unlikely(ftrace_disabled))
1257 return;
1258
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001259 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001260 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001261
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001262 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001263}
1264
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001265static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001266{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001267 if (unlikely(ftrace_disabled))
1268 return;
1269
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001270 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001271 /*
1272 * Just warn in case of unbalance, no need to kill ftrace, it's not
1273 * critical but the ftrace_call callers may be never nopped again after
1274 * further ftrace uses.
1275 */
1276 WARN_ON_ONCE(ftrace_start_up < 0);
1277
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001278 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001279 command |= FTRACE_DISABLE_CALLS;
1280
1281 if (saved_ftrace_func != ftrace_trace_function) {
1282 saved_ftrace_func = ftrace_trace_function;
1283 command |= FTRACE_UPDATE_TRACE_FUNC;
1284 }
1285
1286 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001287 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001288
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001289 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001290}
1291
Ingo Molnare309b412008-05-12 21:20:51 +02001292static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001293{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001294 if (unlikely(ftrace_disabled))
1295 return;
1296
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001297 /* Force update next time */
1298 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001299 /* ftrace_start_up is true if we want ftrace running */
1300 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001301 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001302}
1303
Ingo Molnare309b412008-05-12 21:20:51 +02001304static void ftrace_shutdown_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 Rostedt60a7ecf2008-11-05 16:05:44 -05001309 /* ftrace_start_up is true if ftrace is running */
1310 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001311 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001312}
1313
Steven Rostedt3d083392008-05-12 21:20:42 +02001314static cycle_t ftrace_update_time;
1315static unsigned long ftrace_update_cnt;
1316unsigned long ftrace_update_tot_cnt;
1317
Steven Rostedt31e88902008-11-14 16:21:19 -08001318static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001319{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001320 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301321 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001322
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001323 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001324 ftrace_update_cnt = 0;
1325
Lai Jiangshane94142a2009-03-13 17:51:27 +08001326 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301327
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001328 /* If something went wrong, bail without enabling anything */
1329 if (unlikely(ftrace_disabled))
1330 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001331
Lai Jiangshane94142a2009-03-13 17:51:27 +08001332 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001333 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001334 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301335
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001336 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001337 * Do the initial record conversion from mcount jump
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001338 * to the NOP instructions.
1339 */
1340 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001341 ftrace_free_rec(p);
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001342 /* Game over */
1343 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001344 }
1345
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001346 ftrace_update_cnt++;
1347
1348 /*
1349 * If the tracing is enabled, go ahead and enable the record.
1350 *
1351 * The reason not to enable the record immediatelly is the
1352 * inherent check of ftrace_make_nop/ftrace_make_call for
1353 * correct previous instructions. Making first the NOP
1354 * conversion puts the module to the correct state, thus
1355 * passing the ftrace_make_call check.
1356 */
1357 if (ftrace_start_up) {
1358 int failed = __ftrace_replace_code(p, 1);
1359 if (failed) {
1360 ftrace_bug(failed, p->ip);
1361 ftrace_free_rec(p);
1362 }
1363 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001364 }
1365
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001366 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001367 ftrace_update_time = stop - start;
1368 ftrace_update_tot_cnt += ftrace_update_cnt;
1369
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001370 return 0;
1371}
1372
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001373static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001374{
1375 struct ftrace_page *pg;
1376 int cnt;
1377 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001378
1379 /* allocate a few pages */
1380 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1381 if (!ftrace_pages_start)
1382 return -1;
1383
1384 /*
1385 * Allocate a few more pages.
1386 *
1387 * TODO: have some parser search vmlinux before
1388 * final linking to find all calls to ftrace.
1389 * Then we can:
1390 * a) know how many pages to allocate.
1391 * and/or
1392 * b) set up the table then.
1393 *
1394 * The dynamic code is still necessary for
1395 * modules.
1396 */
1397
1398 pg = ftrace_pages = ftrace_pages_start;
1399
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001400 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001401 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001402 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001403
1404 for (i = 0; i < cnt; i++) {
1405 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1406
1407 /* If we fail, we'll try later anyway */
1408 if (!pg->next)
1409 break;
1410
1411 pg = pg->next;
1412 }
1413
1414 return 0;
1415}
1416
Steven Rostedt5072c592008-05-12 21:20:43 +02001417enum {
1418 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001419 FTRACE_ITER_NOTRACE = (1 << 1),
Steven Rostedt3499e462011-04-21 22:59:12 -04001420 FTRACE_ITER_PRINTALL = (1 << 2),
1421 FTRACE_ITER_HASH = (1 << 3),
Steven Rostedt5072c592008-05-12 21:20:43 +02001422};
1423
1424#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1425
1426struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001427 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001428 loff_t func_pos;
1429 struct ftrace_page *pg;
1430 struct dyn_ftrace *func;
1431 struct ftrace_func_probe *probe;
1432 struct trace_parser parser;
1433 int hidx;
1434 int idx;
1435 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02001436};
1437
Ingo Molnare309b412008-05-12 21:20:51 +02001438static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001439t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001440{
1441 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001442 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001443 struct hlist_head *hhd;
1444
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001445 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001446 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001447
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001448 if (iter->probe)
1449 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001450 retry:
1451 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1452 return NULL;
1453
1454 hhd = &ftrace_func_hash[iter->hidx];
1455
1456 if (hlist_empty(hhd)) {
1457 iter->hidx++;
1458 hnd = NULL;
1459 goto retry;
1460 }
1461
1462 if (!hnd)
1463 hnd = hhd->first;
1464 else {
1465 hnd = hnd->next;
1466 if (!hnd) {
1467 iter->hidx++;
1468 goto retry;
1469 }
1470 }
1471
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001472 if (WARN_ON_ONCE(!hnd))
1473 return NULL;
1474
1475 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1476
1477 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001478}
1479
1480static void *t_hash_start(struct seq_file *m, loff_t *pos)
1481{
1482 struct ftrace_iterator *iter = m->private;
1483 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001484 loff_t l;
1485
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001486 if (iter->func_pos > *pos)
1487 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001488
Li Zefand82d6242009-06-24 09:54:54 +08001489 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001490 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001491 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08001492 if (!p)
1493 break;
1494 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001495 if (!p)
1496 return NULL;
1497
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001498 /* Only set this if we have an item */
1499 iter->flags |= FTRACE_ITER_HASH;
1500
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001501 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001502}
1503
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001504static int
1505t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001506{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001507 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001508
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001509 rec = iter->probe;
1510 if (WARN_ON_ONCE(!rec))
1511 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001512
Steven Rostedt809dcf22009-02-16 23:06:01 -05001513 if (rec->ops->print)
1514 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1515
Steven Rostedtb375a112009-09-17 00:05:58 -04001516 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001517
1518 if (rec->data)
1519 seq_printf(m, ":%p", rec->data);
1520 seq_putc(m, '\n');
1521
1522 return 0;
1523}
1524
1525static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001526t_next(struct seq_file *m, void *v, loff_t *pos)
1527{
1528 struct ftrace_iterator *iter = m->private;
1529 struct dyn_ftrace *rec = NULL;
1530
Steven Rostedt45a4a232011-04-21 23:16:46 -04001531 if (unlikely(ftrace_disabled))
1532 return NULL;
1533
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001534 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001535 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001536
Steven Rostedt5072c592008-05-12 21:20:43 +02001537 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01001538 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02001539
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001540 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001541 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001542
Steven Rostedt5072c592008-05-12 21:20:43 +02001543 retry:
1544 if (iter->idx >= iter->pg->index) {
1545 if (iter->pg->next) {
1546 iter->pg = iter->pg->next;
1547 iter->idx = 0;
1548 goto retry;
1549 }
1550 } else {
1551 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001552 if ((rec->flags & FTRACE_FL_FREE) ||
1553
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001554 ((iter->flags & FTRACE_ITER_FILTER) &&
1555 !(rec->flags & FTRACE_FL_FILTER)) ||
1556
Steven Rostedt41c52c02008-05-22 11:46:33 -04001557 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001558 !ftrace_lookup_ip(&notrace_hash, rec->ip))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001559 rec = NULL;
1560 goto retry;
1561 }
1562 }
1563
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001564 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001565 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001566
1567 iter->func = rec;
1568
1569 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02001570}
1571
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001572static void reset_iter_read(struct ftrace_iterator *iter)
1573{
1574 iter->pos = 0;
1575 iter->func_pos = 0;
1576 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02001577}
1578
1579static void *t_start(struct seq_file *m, loff_t *pos)
1580{
1581 struct ftrace_iterator *iter = m->private;
1582 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001583 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001584
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001585 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04001586
1587 if (unlikely(ftrace_disabled))
1588 return NULL;
1589
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001590 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001591 * If an lseek was done, then reset and start from beginning.
1592 */
1593 if (*pos < iter->pos)
1594 reset_iter_read(iter);
1595
1596 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001597 * For set_ftrace_filter reading, if we have the filter
1598 * off, we can short cut and just print out that all
1599 * functions are enabled.
1600 */
1601 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1602 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001603 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001604 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07001605 /* reset in case of seek/pread */
1606 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001607 return iter;
1608 }
1609
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001610 if (iter->flags & FTRACE_ITER_HASH)
1611 return t_hash_start(m, pos);
1612
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001613 /*
1614 * Unfortunately, we need to restart at ftrace_pages_start
1615 * every time we let go of the ftrace_mutex. This is because
1616 * those pointers can change without the lock.
1617 */
Li Zefan694ce0a2009-06-24 09:54:19 +08001618 iter->pg = ftrace_pages_start;
1619 iter->idx = 0;
1620 for (l = 0; l <= *pos; ) {
1621 p = t_next(m, p, &l);
1622 if (!p)
1623 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001624 }
walimis5821e1b2008-11-15 15:19:06 +08001625
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001626 if (!p) {
1627 if (iter->flags & FTRACE_ITER_FILTER)
1628 return t_hash_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001629
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001630 return NULL;
1631 }
1632
1633 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02001634}
1635
1636static void t_stop(struct seq_file *m, void *p)
1637{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001638 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001639}
1640
1641static int t_show(struct seq_file *m, void *v)
1642{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001643 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001644 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02001645
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001646 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001647 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001648
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001649 if (iter->flags & FTRACE_ITER_PRINTALL) {
1650 seq_printf(m, "#### all functions enabled ####\n");
1651 return 0;
1652 }
1653
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001654 rec = iter->func;
1655
Steven Rostedt5072c592008-05-12 21:20:43 +02001656 if (!rec)
1657 return 0;
1658
Steven Rostedtb375a112009-09-17 00:05:58 -04001659 seq_printf(m, "%ps\n", (void *)rec->ip);
Steven Rostedt5072c592008-05-12 21:20:43 +02001660
1661 return 0;
1662}
1663
James Morris88e9d342009-09-22 16:43:43 -07001664static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001665 .start = t_start,
1666 .next = t_next,
1667 .stop = t_stop,
1668 .show = t_show,
1669};
1670
Ingo Molnare309b412008-05-12 21:20:51 +02001671static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001672ftrace_avail_open(struct inode *inode, struct file *file)
1673{
1674 struct ftrace_iterator *iter;
1675 int ret;
1676
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001677 if (unlikely(ftrace_disabled))
1678 return -ENODEV;
1679
Steven Rostedt5072c592008-05-12 21:20:43 +02001680 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1681 if (!iter)
1682 return -ENOMEM;
1683
1684 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001685
1686 ret = seq_open(file, &show_ftrace_seq_ops);
1687 if (!ret) {
1688 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001689
Steven Rostedt5072c592008-05-12 21:20:43 +02001690 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001691 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001692 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001693 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001694
1695 return ret;
1696}
1697
Steven Rostedt41c52c02008-05-22 11:46:33 -04001698static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001699{
1700 struct ftrace_page *pg;
1701 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02001702
Steven Rostedt52baf112009-02-14 01:15:39 -05001703 mutex_lock(&ftrace_lock);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001704 if (enable) {
Steven Rostedt41c52c02008-05-22 11:46:33 -04001705 ftrace_filtered = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001706 do_for_each_ftrace_rec(pg, rec) {
1707 rec->flags &= ~FTRACE_FL_FILTER;
1708 } while_for_each_ftrace_rec();
1709 } else
1710 ftrace_hash_clear(&notrace_hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05001711 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001712}
1713
Ingo Molnare309b412008-05-12 21:20:51 +02001714static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001715ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001716{
1717 struct ftrace_iterator *iter;
1718 int ret = 0;
1719
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001720 if (unlikely(ftrace_disabled))
1721 return -ENODEV;
1722
Steven Rostedt5072c592008-05-12 21:20:43 +02001723 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1724 if (!iter)
1725 return -ENOMEM;
1726
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001727 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1728 kfree(iter);
1729 return -ENOMEM;
1730 }
1731
Steven Rostedt41c52c02008-05-22 11:46:33 -04001732 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001733 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04001734 (file->f_flags & O_TRUNC))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001735 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001736
1737 if (file->f_mode & FMODE_READ) {
1738 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001739 iter->flags = enable ? FTRACE_ITER_FILTER :
1740 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001741
1742 ret = seq_open(file, &show_ftrace_seq_ops);
1743 if (!ret) {
1744 struct seq_file *m = file->private_data;
1745 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08001746 } else {
1747 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02001748 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08001749 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001750 } else
1751 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001752 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001753
1754 return ret;
1755}
1756
Steven Rostedt41c52c02008-05-22 11:46:33 -04001757static int
1758ftrace_filter_open(struct inode *inode, struct file *file)
1759{
1760 return ftrace_regex_open(inode, file, 1);
1761}
1762
1763static int
1764ftrace_notrace_open(struct inode *inode, struct file *file)
1765{
1766 return ftrace_regex_open(inode, file, 0);
1767}
1768
Ingo Molnare309b412008-05-12 21:20:51 +02001769static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001770ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001771{
1772 loff_t ret;
1773
1774 if (file->f_mode & FMODE_READ)
1775 ret = seq_lseek(file, offset, origin);
1776 else
1777 file->f_pos = ret = 1;
1778
1779 return ret;
1780}
1781
Steven Rostedt64e7c442009-02-13 17:08:48 -05001782static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001783{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001784 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08001785 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001786
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001787 switch (type) {
1788 case MATCH_FULL:
1789 if (strcmp(str, regex) == 0)
1790 matched = 1;
1791 break;
1792 case MATCH_FRONT_ONLY:
1793 if (strncmp(str, regex, len) == 0)
1794 matched = 1;
1795 break;
1796 case MATCH_MIDDLE_ONLY:
1797 if (strstr(str, regex))
1798 matched = 1;
1799 break;
1800 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08001801 slen = strlen(str);
1802 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001803 matched = 1;
1804 break;
1805 }
1806
1807 return matched;
1808}
1809
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001810static int
1811update_record(struct dyn_ftrace *rec, int enable, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04001812{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001813 struct ftrace_func_entry *entry;
1814 struct ftrace_hash *hash = &notrace_hash;
1815 int ret = 0;
1816
1817 if (enable) {
1818 if (not)
1819 rec->flags &= ~FTRACE_FL_FILTER;
1820 else
1821 rec->flags |= FTRACE_FL_FILTER;
1822 } else {
1823 if (not) {
1824 /* Do nothing if it doesn't exist */
1825 entry = ftrace_lookup_ip(hash, rec->ip);
1826 if (!entry)
1827 return 0;
1828
1829 remove_hash_entry(hash, entry);
1830 } else {
1831 /* Do nothing if it exists */
1832 entry = ftrace_lookup_ip(hash, rec->ip);
1833 if (entry)
1834 return 0;
1835
1836 ret = add_hash_entry(hash, rec->ip);
1837 }
1838 }
1839 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04001840}
1841
Steven Rostedt64e7c442009-02-13 17:08:48 -05001842static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001843ftrace_match_record(struct dyn_ftrace *rec, char *mod,
1844 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001845{
1846 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001847 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001848
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001849 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1850
1851 if (mod) {
1852 /* module lookup requires matching the module */
1853 if (!modname || strcmp(modname, mod))
1854 return 0;
1855
1856 /* blank search means to match all funcs in the mod */
1857 if (!len)
1858 return 1;
1859 }
1860
Steven Rostedt64e7c442009-02-13 17:08:48 -05001861 return ftrace_match(str, regex, len, type);
1862}
1863
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001864static int match_records(char *buff, int len, char *mod, int enable, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001865{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001866 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001867 struct ftrace_page *pg;
1868 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001869 int type = MATCH_FULL;
1870 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08001871 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001872 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001873
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001874 if (len) {
1875 type = filter_parse_regex(buff, len, &search, &not);
1876 search_len = strlen(search);
1877 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001878
Steven Rostedt52baf112009-02-14 01:15:39 -05001879 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001880
1881 if (unlikely(ftrace_disabled))
1882 goto out_unlock;
1883
Steven Rostedt265c8312009-02-13 12:43:56 -05001884 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001885
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001886 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001887 ret = update_record(rec, enable, not);
1888 if (ret < 0) {
1889 found = ret;
1890 goto out_unlock;
1891 }
Li Zefan311d16d2009-12-08 11:15:11 +08001892 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001893 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001894 /*
1895 * Only enable filtering if we have a function that
1896 * is filtered on.
1897 */
1898 if (enable && (rec->flags & FTRACE_FL_FILTER))
1899 ftrace_filtered = 1;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001900
Steven Rostedt265c8312009-02-13 12:43:56 -05001901 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001902 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05001903 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001904
1905 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02001906}
1907
Steven Rostedt64e7c442009-02-13 17:08:48 -05001908static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001909ftrace_match_records(char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001910{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001911 return match_records(buff, len, NULL, enable, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001912}
1913
Li Zefan311d16d2009-12-08 11:15:11 +08001914static int ftrace_match_module_records(char *buff, char *mod, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001915{
Steven Rostedt64e7c442009-02-13 17:08:48 -05001916 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001917
Steven Rostedt64e7c442009-02-13 17:08:48 -05001918 /* blank or '*' mean the same */
1919 if (strcmp(buff, "*") == 0)
1920 buff[0] = 0;
1921
1922 /* handle the case of 'dont filter this module' */
1923 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1924 buff[0] = 0;
1925 not = 1;
1926 }
1927
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001928 return match_records(buff, strlen(buff), mod, enable, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001929}
1930
Steven Rostedtf6180772009-02-14 00:40:25 -05001931/*
1932 * We register the module command as a template to show others how
1933 * to register the a command as well.
1934 */
1935
1936static int
1937ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1938{
1939 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001940 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05001941
1942 /*
1943 * cmd == 'mod' because we only registered this func
1944 * for the 'mod' ftrace_func_command.
1945 * But if you register one func with multiple commands,
1946 * you can tell which command was used by the cmd
1947 * parameter.
1948 */
1949
1950 /* we must have a module name */
1951 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001952 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05001953
1954 mod = strsep(&param, ":");
1955 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001956 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05001957
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001958 ret = ftrace_match_module_records(func, mod, enable);
1959 if (!ret)
1960 ret = -EINVAL;
1961 if (ret < 0)
1962 return ret;
1963
1964 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05001965}
1966
1967static struct ftrace_func_command ftrace_mod_cmd = {
1968 .name = "mod",
1969 .func = ftrace_mod_callback,
1970};
1971
1972static int __init ftrace_mod_cmd_init(void)
1973{
1974 return register_ftrace_command(&ftrace_mod_cmd);
1975}
1976device_initcall(ftrace_mod_cmd_init);
1977
Steven Rostedt59df055f2009-02-14 15:29:06 -05001978static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001979function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001980{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001981 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001982 struct hlist_head *hhd;
1983 struct hlist_node *n;
1984 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001985
1986 key = hash_long(ip, FTRACE_HASH_BITS);
1987
1988 hhd = &ftrace_func_hash[key];
1989
1990 if (hlist_empty(hhd))
1991 return;
1992
1993 /*
1994 * Disable preemption for these calls to prevent a RCU grace
1995 * period. This syncs the hash iteration and freeing of items
1996 * on the hash. rcu_read_lock is too dangerous here.
1997 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04001998 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05001999 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2000 if (entry->ip == ip)
2001 entry->ops->func(ip, parent_ip, &entry->data);
2002 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002003 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002004}
2005
Steven Rostedtb6887d72009-02-17 12:32:04 -05002006static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002007{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002008 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002009};
2010
Steven Rostedtb6887d72009-02-17 12:32:04 -05002011static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002012
Steven Rostedtb6887d72009-02-17 12:32:04 -05002013static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002014{
2015 int i;
2016
Steven Rostedtb6887d72009-02-17 12:32:04 -05002017 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002018 return;
2019
2020 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2021 struct hlist_head *hhd = &ftrace_func_hash[i];
2022 if (hhd->first)
2023 break;
2024 }
2025 /* Nothing registered? */
2026 if (i == FTRACE_FUNC_HASHSIZE)
2027 return;
2028
Steven Rostedtb6887d72009-02-17 12:32:04 -05002029 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002030 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05002031 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002032}
2033
Steven Rostedtb6887d72009-02-17 12:32:04 -05002034static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002035{
2036 int i;
2037
Steven Rostedtb6887d72009-02-17 12:32:04 -05002038 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002039 return;
2040
2041 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2042 struct hlist_head *hhd = &ftrace_func_hash[i];
2043 if (hhd->first)
2044 return;
2045 }
2046
2047 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002048 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002049 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05002050 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002051}
2052
2053
2054static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2055{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002056 struct ftrace_func_probe *entry =
2057 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002058
2059 if (entry->ops->free)
2060 entry->ops->free(&entry->data);
2061 kfree(entry);
2062}
2063
2064
2065int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002066register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002067 void *data)
2068{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002069 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002070 struct ftrace_page *pg;
2071 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002072 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002073 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002074 int count = 0;
2075 char *search;
2076
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002077 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002078 len = strlen(search);
2079
Steven Rostedtb6887d72009-02-17 12:32:04 -05002080 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002081 if (WARN_ON(not))
2082 return -EINVAL;
2083
2084 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002085
Steven Rostedt45a4a232011-04-21 23:16:46 -04002086 if (unlikely(ftrace_disabled))
2087 goto out_unlock;
2088
Steven Rostedt59df055f2009-02-14 15:29:06 -05002089 do_for_each_ftrace_rec(pg, rec) {
2090
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002091 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002092 continue;
2093
2094 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2095 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002096 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002097 if (!count)
2098 count = -ENOMEM;
2099 goto out_unlock;
2100 }
2101
2102 count++;
2103
2104 entry->data = data;
2105
2106 /*
2107 * The caller might want to do something special
2108 * for each function we find. We call the callback
2109 * to give the caller an opportunity to do so.
2110 */
2111 if (ops->callback) {
2112 if (ops->callback(rec->ip, &entry->data) < 0) {
2113 /* caller does not like this func */
2114 kfree(entry);
2115 continue;
2116 }
2117 }
2118
2119 entry->ops = ops;
2120 entry->ip = rec->ip;
2121
2122 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2123 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2124
2125 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002126 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002127
2128 out_unlock:
2129 mutex_unlock(&ftrace_lock);
2130
2131 return count;
2132}
2133
2134enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002135 PROBE_TEST_FUNC = 1,
2136 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002137};
2138
2139static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002140__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002141 void *data, int flags)
2142{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002143 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002144 struct hlist_node *n, *tmp;
2145 char str[KSYM_SYMBOL_LEN];
2146 int type = MATCH_FULL;
2147 int i, len = 0;
2148 char *search;
2149
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002150 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002151 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002152 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002153 int not;
2154
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002155 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002156 len = strlen(search);
2157
Steven Rostedtb6887d72009-02-17 12:32:04 -05002158 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002159 if (WARN_ON(not))
2160 return;
2161 }
2162
2163 mutex_lock(&ftrace_lock);
2164 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2165 struct hlist_head *hhd = &ftrace_func_hash[i];
2166
2167 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2168
2169 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002170 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002171 continue;
2172
Steven Rostedtb6887d72009-02-17 12:32:04 -05002173 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002174 continue;
2175
2176 /* do this last, since it is the most expensive */
2177 if (glob) {
2178 kallsyms_lookup(entry->ip, NULL, NULL,
2179 NULL, str);
2180 if (!ftrace_match(str, glob, len, type))
2181 continue;
2182 }
2183
2184 hlist_del(&entry->node);
2185 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2186 }
2187 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002188 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002189 mutex_unlock(&ftrace_lock);
2190}
2191
2192void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002193unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002194 void *data)
2195{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002196 __unregister_ftrace_function_probe(glob, ops, data,
2197 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002198}
2199
2200void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002201unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002202{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002203 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002204}
2205
Steven Rostedtb6887d72009-02-17 12:32:04 -05002206void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002207{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002208 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002209}
2210
Steven Rostedtf6180772009-02-14 00:40:25 -05002211static LIST_HEAD(ftrace_commands);
2212static DEFINE_MUTEX(ftrace_cmd_mutex);
2213
2214int register_ftrace_command(struct ftrace_func_command *cmd)
2215{
2216 struct ftrace_func_command *p;
2217 int ret = 0;
2218
2219 mutex_lock(&ftrace_cmd_mutex);
2220 list_for_each_entry(p, &ftrace_commands, list) {
2221 if (strcmp(cmd->name, p->name) == 0) {
2222 ret = -EBUSY;
2223 goto out_unlock;
2224 }
2225 }
2226 list_add(&cmd->list, &ftrace_commands);
2227 out_unlock:
2228 mutex_unlock(&ftrace_cmd_mutex);
2229
2230 return ret;
2231}
2232
2233int unregister_ftrace_command(struct ftrace_func_command *cmd)
2234{
2235 struct ftrace_func_command *p, *n;
2236 int ret = -ENODEV;
2237
2238 mutex_lock(&ftrace_cmd_mutex);
2239 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2240 if (strcmp(cmd->name, p->name) == 0) {
2241 ret = 0;
2242 list_del_init(&p->list);
2243 goto out_unlock;
2244 }
2245 }
2246 out_unlock:
2247 mutex_unlock(&ftrace_cmd_mutex);
2248
2249 return ret;
2250}
2251
Steven Rostedt64e7c442009-02-13 17:08:48 -05002252static int ftrace_process_regex(char *buff, int len, int enable)
2253{
Steven Rostedtf6180772009-02-14 00:40:25 -05002254 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002255 struct ftrace_func_command *p;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002256 int ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002257
2258 func = strsep(&next, ":");
2259
2260 if (!next) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002261 ret = ftrace_match_records(func, len, enable);
2262 if (!ret)
2263 ret = -EINVAL;
2264 if (ret < 0)
2265 return ret;
2266 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002267 }
2268
Steven Rostedtf6180772009-02-14 00:40:25 -05002269 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002270
2271 command = strsep(&next, ":");
2272
Steven Rostedtf6180772009-02-14 00:40:25 -05002273 mutex_lock(&ftrace_cmd_mutex);
2274 list_for_each_entry(p, &ftrace_commands, list) {
2275 if (strcmp(p->name, command) == 0) {
2276 ret = p->func(func, command, next, enable);
2277 goto out_unlock;
2278 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002279 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002280 out_unlock:
2281 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002282
Steven Rostedtf6180772009-02-14 00:40:25 -05002283 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002284}
2285
Ingo Molnare309b412008-05-12 21:20:51 +02002286static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002287ftrace_regex_write(struct file *file, const char __user *ubuf,
2288 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002289{
2290 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002291 struct trace_parser *parser;
2292 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002293
Li Zefan4ba79782009-09-22 13:52:20 +08002294 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002295 return 0;
2296
Steven Rostedt41c52c02008-05-22 11:46:33 -04002297 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002298
Steven Rostedt45a4a232011-04-21 23:16:46 -04002299 ret = -ENODEV;
2300 if (unlikely(ftrace_disabled))
2301 goto out_unlock;
2302
Steven Rostedt5072c592008-05-12 21:20:43 +02002303 if (file->f_mode & FMODE_READ) {
2304 struct seq_file *m = file->private_data;
2305 iter = m->private;
2306 } else
2307 iter = file->private_data;
2308
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002309 parser = &iter->parser;
2310 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002311
Li Zefan4ba79782009-09-22 13:52:20 +08002312 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002313 !trace_parser_cont(parser)) {
2314 ret = ftrace_process_regex(parser->buffer,
2315 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002316 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002317 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002318 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002319 }
2320
Steven Rostedt5072c592008-05-12 21:20:43 +02002321 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002322out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002323 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002324
Steven Rostedt5072c592008-05-12 21:20:43 +02002325 return ret;
2326}
2327
Steven Rostedt41c52c02008-05-22 11:46:33 -04002328static ssize_t
2329ftrace_filter_write(struct file *file, const char __user *ubuf,
2330 size_t cnt, loff_t *ppos)
2331{
2332 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2333}
2334
2335static ssize_t
2336ftrace_notrace_write(struct file *file, const char __user *ubuf,
2337 size_t cnt, loff_t *ppos)
2338{
2339 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2340}
2341
2342static void
2343ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2344{
2345 if (unlikely(ftrace_disabled))
2346 return;
2347
2348 mutex_lock(&ftrace_regex_lock);
2349 if (reset)
2350 ftrace_filter_reset(enable);
2351 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002352 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002353 mutex_unlock(&ftrace_regex_lock);
2354}
2355
Steven Rostedt77a2b372008-05-12 21:20:45 +02002356/**
2357 * ftrace_set_filter - set a function to filter on in ftrace
2358 * @buf - the string that holds the function filter text.
2359 * @len - the length of the string.
2360 * @reset - non zero to reset all filters before applying this filter.
2361 *
2362 * Filters denote which functions should be enabled when tracing is enabled.
2363 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2364 */
Ingo Molnare309b412008-05-12 21:20:51 +02002365void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002366{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002367 ftrace_set_regex(buf, len, reset, 1);
2368}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002369
Steven Rostedt41c52c02008-05-22 11:46:33 -04002370/**
2371 * ftrace_set_notrace - set a function to not trace in ftrace
2372 * @buf - the string that holds the function notrace text.
2373 * @len - the length of the string.
2374 * @reset - non zero to reset all filters before applying this filter.
2375 *
2376 * Notrace Filters denote which functions should not be enabled when tracing
2377 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2378 * for tracing.
2379 */
2380void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2381{
2382 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002383}
2384
Steven Rostedt2af15d62009-05-28 13:37:24 -04002385/*
2386 * command line interface to allow users to set filters on boot up.
2387 */
2388#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2389static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2390static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2391
2392static int __init set_ftrace_notrace(char *str)
2393{
2394 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2395 return 1;
2396}
2397__setup("ftrace_notrace=", set_ftrace_notrace);
2398
2399static int __init set_ftrace_filter(char *str)
2400{
2401 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2402 return 1;
2403}
2404__setup("ftrace_filter=", set_ftrace_filter);
2405
Stefan Assmann369bc182009-10-12 22:17:21 +02002406#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002407static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002408static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2409
Stefan Assmann369bc182009-10-12 22:17:21 +02002410static int __init set_graph_function(char *str)
2411{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002412 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002413 return 1;
2414}
2415__setup("ftrace_graph_filter=", set_graph_function);
2416
2417static void __init set_ftrace_early_graph(char *buf)
2418{
2419 int ret;
2420 char *func;
2421
2422 while (buf) {
2423 func = strsep(&buf, ",");
2424 /* we allow only one expression at a time */
2425 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2426 func);
2427 if (ret)
2428 printk(KERN_DEBUG "ftrace: function %s not "
2429 "traceable\n", func);
2430 }
2431}
2432#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2433
Steven Rostedt2af15d62009-05-28 13:37:24 -04002434static void __init set_ftrace_early_filter(char *buf, int enable)
2435{
2436 char *func;
2437
2438 while (buf) {
2439 func = strsep(&buf, ",");
2440 ftrace_set_regex(func, strlen(func), 0, enable);
2441 }
2442}
2443
2444static void __init set_ftrace_early_filters(void)
2445{
2446 if (ftrace_filter_buf[0])
2447 set_ftrace_early_filter(ftrace_filter_buf, 1);
2448 if (ftrace_notrace_buf[0])
2449 set_ftrace_early_filter(ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002450#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2451 if (ftrace_graph_buf[0])
2452 set_ftrace_early_graph(ftrace_graph_buf);
2453#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002454}
2455
Ingo Molnare309b412008-05-12 21:20:51 +02002456static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002457ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002458{
2459 struct seq_file *m = (struct seq_file *)file->private_data;
2460 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002461 struct trace_parser *parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02002462
Steven Rostedt41c52c02008-05-22 11:46:33 -04002463 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002464 if (file->f_mode & FMODE_READ) {
2465 iter = m->private;
2466
2467 seq_release(inode, file);
2468 } else
2469 iter = file->private_data;
2470
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002471 parser = &iter->parser;
2472 if (trace_parser_loaded(parser)) {
2473 parser->buffer[parser->idx] = 0;
2474 ftrace_match_records(parser->buffer, parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002475 }
2476
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002477 trace_parser_put(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002478 kfree(iter);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002479
Steven Rostedt058e2972011-04-29 22:35:33 -04002480 if (file->f_mode & FMODE_WRITE) {
2481 mutex_lock(&ftrace_lock);
2482 if (ftrace_start_up && ftrace_enabled)
2483 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2484 mutex_unlock(&ftrace_lock);
2485 }
2486
Steven Rostedt41c52c02008-05-22 11:46:33 -04002487 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002488 return 0;
2489}
2490
Steven Rostedt41c52c02008-05-22 11:46:33 -04002491static int
2492ftrace_filter_release(struct inode *inode, struct file *file)
2493{
2494 return ftrace_regex_release(inode, file, 1);
2495}
2496
2497static int
2498ftrace_notrace_release(struct inode *inode, struct file *file)
2499{
2500 return ftrace_regex_release(inode, file, 0);
2501}
2502
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002503static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002504 .open = ftrace_avail_open,
2505 .read = seq_read,
2506 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002507 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002508};
2509
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002510static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002511 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002512 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002513 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002514 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002515 .release = ftrace_filter_release,
2516};
2517
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002518static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002519 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002520 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002521 .write = ftrace_notrace_write,
2522 .llseek = ftrace_regex_lseek,
2523 .release = ftrace_notrace_release,
2524};
2525
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002526#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2527
2528static DEFINE_MUTEX(graph_lock);
2529
2530int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002531int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002532unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2533
2534static void *
Li Zefan85951842009-06-24 09:54:00 +08002535__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002536{
Li Zefan85951842009-06-24 09:54:00 +08002537 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002538 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002539 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002540}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002541
Li Zefan85951842009-06-24 09:54:00 +08002542static void *
2543g_next(struct seq_file *m, void *v, loff_t *pos)
2544{
2545 (*pos)++;
2546 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002547}
2548
2549static void *g_start(struct seq_file *m, loff_t *pos)
2550{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002551 mutex_lock(&graph_lock);
2552
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002553 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002554 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002555 return (void *)1;
2556
Li Zefan85951842009-06-24 09:54:00 +08002557 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002558}
2559
2560static void g_stop(struct seq_file *m, void *p)
2561{
2562 mutex_unlock(&graph_lock);
2563}
2564
2565static int g_show(struct seq_file *m, void *v)
2566{
2567 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002568
2569 if (!ptr)
2570 return 0;
2571
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002572 if (ptr == (unsigned long *)1) {
2573 seq_printf(m, "#### all functions enabled ####\n");
2574 return 0;
2575 }
2576
Steven Rostedtb375a112009-09-17 00:05:58 -04002577 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002578
2579 return 0;
2580}
2581
James Morris88e9d342009-09-22 16:43:43 -07002582static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002583 .start = g_start,
2584 .next = g_next,
2585 .stop = g_stop,
2586 .show = g_show,
2587};
2588
2589static int
2590ftrace_graph_open(struct inode *inode, struct file *file)
2591{
2592 int ret = 0;
2593
2594 if (unlikely(ftrace_disabled))
2595 return -ENODEV;
2596
2597 mutex_lock(&graph_lock);
2598 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002599 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002600 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002601 ftrace_graph_count = 0;
2602 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2603 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002604 mutex_unlock(&graph_lock);
2605
Li Zefana4ec5e02009-09-18 14:06:28 +08002606 if (file->f_mode & FMODE_READ)
2607 ret = seq_open(file, &ftrace_graph_seq_ops);
2608
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002609 return ret;
2610}
2611
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002612static int
Li Zefan87827112009-07-23 11:29:11 +08002613ftrace_graph_release(struct inode *inode, struct file *file)
2614{
2615 if (file->f_mode & FMODE_READ)
2616 seq_release(inode, file);
2617 return 0;
2618}
2619
2620static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002621ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002622{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002623 struct dyn_ftrace *rec;
2624 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002625 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002626 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002627 int type, not;
2628 char *search;
2629 bool exists;
2630 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002631
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002632 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002633 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002634 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2635 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002636
2637 search_len = strlen(search);
2638
Steven Rostedt52baf112009-02-14 01:15:39 -05002639 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002640
2641 if (unlikely(ftrace_disabled)) {
2642 mutex_unlock(&ftrace_lock);
2643 return -ENODEV;
2644 }
2645
Steven Rostedt265c8312009-02-13 12:43:56 -05002646 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002647
Steven Rostedt45a4a232011-04-21 23:16:46 -04002648 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05002649 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002650
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002651 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002652 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002653 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002654 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002655 if (array[i] == rec->ip) {
2656 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002657 break;
2658 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002659 }
2660
2661 if (!not) {
2662 fail = 0;
2663 if (!exists) {
2664 array[(*idx)++] = rec->ip;
2665 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2666 goto out;
2667 }
2668 } else {
2669 if (exists) {
2670 array[i] = array[--(*idx)];
2671 array[*idx] = 0;
2672 fail = 0;
2673 }
2674 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002675 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002676 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002677out:
Steven Rostedt52baf112009-02-14 01:15:39 -05002678 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002679
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002680 if (fail)
2681 return -EINVAL;
2682
2683 ftrace_graph_filter_enabled = 1;
2684 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002685}
2686
2687static ssize_t
2688ftrace_graph_write(struct file *file, const char __user *ubuf,
2689 size_t cnt, loff_t *ppos)
2690{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002691 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08002692 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002693
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002694 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002695 return 0;
2696
2697 mutex_lock(&graph_lock);
2698
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002699 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2700 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08002701 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002702 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002703
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002704 read = trace_get_user(&parser, ubuf, cnt, ppos);
2705
Li Zefan4ba79782009-09-22 13:52:20 +08002706 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002707 parser.buffer[parser.idx] = 0;
2708
2709 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08002710 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002711 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002712 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08002713 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002714 }
2715
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002716 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08002717
2718out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002719 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08002720out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002721 mutex_unlock(&graph_lock);
2722
2723 return ret;
2724}
2725
2726static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08002727 .open = ftrace_graph_open,
2728 .read = seq_read,
2729 .write = ftrace_graph_write,
2730 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002731 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002732};
2733#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2734
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002735static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002736{
Steven Rostedt5072c592008-05-12 21:20:43 +02002737
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002738 trace_create_file("available_filter_functions", 0444,
2739 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002740
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002741 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2742 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002743
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002744 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002745 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002746
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002747#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002748 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002749 NULL,
2750 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002751#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2752
Steven Rostedt5072c592008-05-12 21:20:43 +02002753 return 0;
2754}
2755
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002756static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08002757 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002758 unsigned long *end)
2759{
2760 unsigned long *p;
2761 unsigned long addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002762
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002763 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002764 p = start;
2765 while (p < end) {
2766 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002767 /*
2768 * Some architecture linkers will pad between
2769 * the different mcount_loc sections of different
2770 * object files to satisfy alignments.
2771 * Skip any NULL pointers.
2772 */
2773 if (!addr)
2774 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002775 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002776 }
2777
Steven Rostedt31e88902008-11-14 16:21:19 -08002778 ftrace_update_code(mod);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002779 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002780
2781 return 0;
2782}
2783
Steven Rostedt93eb6772009-04-15 13:24:06 -04002784#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002785void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002786{
2787 struct dyn_ftrace *rec;
2788 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04002789
Steven Rostedt93eb6772009-04-15 13:24:06 -04002790 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002791
2792 if (ftrace_disabled)
2793 goto out_unlock;
2794
Steven Rostedt93eb6772009-04-15 13:24:06 -04002795 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002796 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04002797 /*
2798 * rec->ip is changed in ftrace_free_rec()
2799 * It should not between s and e if record was freed.
2800 */
2801 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2802 ftrace_free_rec(rec);
2803 }
2804 } while_for_each_ftrace_rec();
Steven Rostedt45a4a232011-04-21 23:16:46 -04002805 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04002806 mutex_unlock(&ftrace_lock);
2807}
2808
2809static void ftrace_init_module(struct module *mod,
2810 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002811{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002812 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002813 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002814 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002815}
2816
Steven Rostedt93eb6772009-04-15 13:24:06 -04002817static int ftrace_module_notify(struct notifier_block *self,
2818 unsigned long val, void *data)
2819{
2820 struct module *mod = data;
2821
2822 switch (val) {
2823 case MODULE_STATE_COMING:
2824 ftrace_init_module(mod, mod->ftrace_callsites,
2825 mod->ftrace_callsites +
2826 mod->num_ftrace_callsites);
2827 break;
2828 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002829 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04002830 break;
2831 }
2832
2833 return 0;
2834}
2835#else
2836static int ftrace_module_notify(struct notifier_block *self,
2837 unsigned long val, void *data)
2838{
2839 return 0;
2840}
2841#endif /* CONFIG_MODULES */
2842
2843struct notifier_block ftrace_module_nb = {
2844 .notifier_call = ftrace_module_notify,
2845 .priority = 0,
2846};
2847
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002848extern unsigned long __start_mcount_loc[];
2849extern unsigned long __stop_mcount_loc[];
2850
2851void __init ftrace_init(void)
2852{
2853 unsigned long count, addr, flags;
2854 int ret;
2855
2856 /* Keep the ftrace pointer to the stub */
2857 addr = (unsigned long)ftrace_stub;
2858
2859 local_irq_save(flags);
2860 ftrace_dyn_arch_init(&addr);
2861 local_irq_restore(flags);
2862
2863 /* ftrace_dyn_arch_init places the return code in addr */
2864 if (addr)
2865 goto failed;
2866
2867 count = __stop_mcount_loc - __start_mcount_loc;
2868
2869 ret = ftrace_dyn_table_alloc(count);
2870 if (ret)
2871 goto failed;
2872
2873 last_ftrace_enabled = ftrace_enabled = 1;
2874
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002875 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08002876 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002877 __stop_mcount_loc);
2878
Steven Rostedt93eb6772009-04-15 13:24:06 -04002879 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002880 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002881 pr_warning("Failed to register trace ftrace module notifier\n");
2882
Steven Rostedt2af15d62009-05-28 13:37:24 -04002883 set_ftrace_early_filters();
2884
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002885 return;
2886 failed:
2887 ftrace_disabled = 1;
2888}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002889
Steven Rostedt3d083392008-05-12 21:20:42 +02002890#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002891
2892static int __init ftrace_nodyn_init(void)
2893{
2894 ftrace_enabled = 1;
2895 return 0;
2896}
2897device_initcall(ftrace_nodyn_init);
2898
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002899static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2900static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002901/* Keep as macros so we do not need to define the commands */
2902# define ftrace_startup(command) do { } while (0)
2903# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002904# define ftrace_startup_sysctl() do { } while (0)
2905# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002906#endif /* CONFIG_DYNAMIC_FTRACE */
2907
Steven Rostedte32d8952008-12-04 00:26:41 -05002908static void clear_ftrace_swapper(void)
2909{
2910 struct task_struct *p;
2911 int cpu;
2912
2913 get_online_cpus();
2914 for_each_online_cpu(cpu) {
2915 p = idle_task(cpu);
2916 clear_tsk_trace_trace(p);
2917 }
2918 put_online_cpus();
2919}
2920
2921static void set_ftrace_swapper(void)
2922{
2923 struct task_struct *p;
2924 int cpu;
2925
2926 get_online_cpus();
2927 for_each_online_cpu(cpu) {
2928 p = idle_task(cpu);
2929 set_tsk_trace_trace(p);
2930 }
2931 put_online_cpus();
2932}
2933
2934static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002935{
2936 struct task_struct *p;
2937
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002938 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002939 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002940 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002941 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002942 rcu_read_unlock();
2943
Steven Rostedte32d8952008-12-04 00:26:41 -05002944 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002945}
2946
Steven Rostedte32d8952008-12-04 00:26:41 -05002947static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002948{
2949 struct task_struct *p;
2950
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002951 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002952 do_each_pid_task(pid, PIDTYPE_PID, p) {
2953 set_tsk_trace_trace(p);
2954 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002955 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002956}
2957
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002958static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002959{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002960 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002961 clear_ftrace_swapper();
2962 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002963 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05002964}
2965
2966static void set_ftrace_pid_task(struct pid *pid)
2967{
2968 if (pid == ftrace_swapper_pid)
2969 set_ftrace_swapper();
2970 else
2971 set_ftrace_pid(pid);
2972}
2973
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002974static int ftrace_pid_add(int p)
2975{
2976 struct pid *pid;
2977 struct ftrace_pid *fpid;
2978 int ret = -EINVAL;
2979
2980 mutex_lock(&ftrace_lock);
2981
2982 if (!p)
2983 pid = ftrace_swapper_pid;
2984 else
2985 pid = find_get_pid(p);
2986
2987 if (!pid)
2988 goto out;
2989
2990 ret = 0;
2991
2992 list_for_each_entry(fpid, &ftrace_pids, list)
2993 if (fpid->pid == pid)
2994 goto out_put;
2995
2996 ret = -ENOMEM;
2997
2998 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2999 if (!fpid)
3000 goto out_put;
3001
3002 list_add(&fpid->list, &ftrace_pids);
3003 fpid->pid = pid;
3004
3005 set_ftrace_pid_task(pid);
3006
3007 ftrace_update_pid_func();
3008 ftrace_startup_enable(0);
3009
3010 mutex_unlock(&ftrace_lock);
3011 return 0;
3012
3013out_put:
3014 if (pid != ftrace_swapper_pid)
3015 put_pid(pid);
3016
3017out:
3018 mutex_unlock(&ftrace_lock);
3019 return ret;
3020}
3021
3022static void ftrace_pid_reset(void)
3023{
3024 struct ftrace_pid *fpid, *safe;
3025
3026 mutex_lock(&ftrace_lock);
3027 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3028 struct pid *pid = fpid->pid;
3029
3030 clear_ftrace_pid_task(pid);
3031
3032 list_del(&fpid->list);
3033 kfree(fpid);
3034 }
3035
3036 ftrace_update_pid_func();
3037 ftrace_startup_enable(0);
3038
3039 mutex_unlock(&ftrace_lock);
3040}
3041
3042static void *fpid_start(struct seq_file *m, loff_t *pos)
3043{
3044 mutex_lock(&ftrace_lock);
3045
3046 if (list_empty(&ftrace_pids) && (!*pos))
3047 return (void *) 1;
3048
3049 return seq_list_start(&ftrace_pids, *pos);
3050}
3051
3052static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3053{
3054 if (v == (void *)1)
3055 return NULL;
3056
3057 return seq_list_next(v, &ftrace_pids, pos);
3058}
3059
3060static void fpid_stop(struct seq_file *m, void *p)
3061{
3062 mutex_unlock(&ftrace_lock);
3063}
3064
3065static int fpid_show(struct seq_file *m, void *v)
3066{
3067 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3068
3069 if (v == (void *)1) {
3070 seq_printf(m, "no pid\n");
3071 return 0;
3072 }
3073
3074 if (fpid->pid == ftrace_swapper_pid)
3075 seq_printf(m, "swapper tasks\n");
3076 else
3077 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3078
3079 return 0;
3080}
3081
3082static const struct seq_operations ftrace_pid_sops = {
3083 .start = fpid_start,
3084 .next = fpid_next,
3085 .stop = fpid_stop,
3086 .show = fpid_show,
3087};
3088
3089static int
3090ftrace_pid_open(struct inode *inode, struct file *file)
3091{
3092 int ret = 0;
3093
3094 if ((file->f_mode & FMODE_WRITE) &&
3095 (file->f_flags & O_TRUNC))
3096 ftrace_pid_reset();
3097
3098 if (file->f_mode & FMODE_READ)
3099 ret = seq_open(file, &ftrace_pid_sops);
3100
3101 return ret;
3102}
3103
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003104static ssize_t
3105ftrace_pid_write(struct file *filp, const char __user *ubuf,
3106 size_t cnt, loff_t *ppos)
3107{
Ingo Molnar457dc922009-11-23 11:03:28 +01003108 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003109 long val;
3110 int ret;
3111
3112 if (cnt >= sizeof(buf))
3113 return -EINVAL;
3114
3115 if (copy_from_user(&buf, ubuf, cnt))
3116 return -EFAULT;
3117
3118 buf[cnt] = 0;
3119
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003120 /*
3121 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3122 * to clean the filter quietly.
3123 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003124 tmp = strstrip(buf);
3125 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003126 return 1;
3127
Ingo Molnar457dc922009-11-23 11:03:28 +01003128 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003129 if (ret < 0)
3130 return ret;
3131
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003132 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003133
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003134 return ret ? ret : cnt;
3135}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003136
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003137static int
3138ftrace_pid_release(struct inode *inode, struct file *file)
3139{
3140 if (file->f_mode & FMODE_READ)
3141 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003142
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003143 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003144}
3145
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003146static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003147 .open = ftrace_pid_open,
3148 .write = ftrace_pid_write,
3149 .read = seq_read,
3150 .llseek = seq_lseek,
3151 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003152};
3153
3154static __init int ftrace_init_debugfs(void)
3155{
3156 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003157
3158 d_tracer = tracing_init_dentry();
3159 if (!d_tracer)
3160 return 0;
3161
3162 ftrace_init_dyn_debugfs(d_tracer);
3163
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003164 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3165 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003166
3167 ftrace_profile_debugfs(d_tracer);
3168
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003169 return 0;
3170}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003171fs_initcall(ftrace_init_debugfs);
3172
Steven Rostedt3d083392008-05-12 21:20:42 +02003173/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003174 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003175 *
3176 * This function should be used by panic code. It stops ftrace
3177 * but in a not so nice way. If you need to simply kill ftrace
3178 * from a non-atomic section, use ftrace_kill.
3179 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003180void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003181{
3182 ftrace_disabled = 1;
3183 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003184 clear_ftrace_function();
3185}
3186
3187/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003188 * register_ftrace_function - register a function for profiling
3189 * @ops - ops structure that holds the function for profiling.
3190 *
3191 * Register a function to be called by all functions in the
3192 * kernel.
3193 *
3194 * Note: @ops->func and all the functions it calls must be labeled
3195 * with "notrace", otherwise it will go into a
3196 * recursive loop.
3197 */
3198int register_ftrace_function(struct ftrace_ops *ops)
3199{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003200 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003201
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003202 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003203
Steven Rostedt45a4a232011-04-21 23:16:46 -04003204 if (unlikely(ftrace_disabled))
3205 goto out_unlock;
3206
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003207 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003208 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003209
Steven Rostedt45a4a232011-04-21 23:16:46 -04003210 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003211 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003212 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003213}
3214
3215/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003216 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003217 * @ops - ops structure that holds the function to unregister
3218 *
3219 * Unregister a function that was added to be called by ftrace profiling.
3220 */
3221int unregister_ftrace_function(struct ftrace_ops *ops)
3222{
3223 int ret;
3224
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003225 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003226 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003227 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003228 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003229
3230 return ret;
3231}
3232
Ingo Molnare309b412008-05-12 21:20:51 +02003233int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003234ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003235 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003236 loff_t *ppos)
3237{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003238 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003239
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003240 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003241
Steven Rostedt45a4a232011-04-21 23:16:46 -04003242 if (unlikely(ftrace_disabled))
3243 goto out;
3244
3245 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003246
Li Zefana32c7762009-06-26 16:55:51 +08003247 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003248 goto out;
3249
Li Zefana32c7762009-06-26 16:55:51 +08003250 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003251
3252 if (ftrace_enabled) {
3253
3254 ftrace_startup_sysctl();
3255
3256 /* we are starting ftrace again */
3257 if (ftrace_list != &ftrace_list_end) {
3258 if (ftrace_list->next == &ftrace_list_end)
3259 ftrace_trace_function = ftrace_list->func;
3260 else
3261 ftrace_trace_function = ftrace_list_func;
3262 }
3263
3264 } else {
3265 /* stopping ftrace calls (just send to ftrace_stub) */
3266 ftrace_trace_function = ftrace_stub;
3267
3268 ftrace_shutdown_sysctl();
3269 }
3270
3271 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003272 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003273 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003274}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003275
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003276#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003277
Steven Rostedt597af812009-04-03 15:24:12 -04003278static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003279static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003280
Steven Rostedte49dc192008-12-02 23:50:05 -05003281int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3282{
3283 return 0;
3284}
3285
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003286/* The callbacks that hook a function */
3287trace_func_graph_ret_t ftrace_graph_return =
3288 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003289trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003290
3291/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3292static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3293{
3294 int i;
3295 int ret = 0;
3296 unsigned long flags;
3297 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3298 struct task_struct *g, *t;
3299
3300 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3301 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3302 * sizeof(struct ftrace_ret_stack),
3303 GFP_KERNEL);
3304 if (!ret_stack_list[i]) {
3305 start = 0;
3306 end = i;
3307 ret = -ENOMEM;
3308 goto free;
3309 }
3310 }
3311
3312 read_lock_irqsave(&tasklist_lock, flags);
3313 do_each_thread(g, t) {
3314 if (start == end) {
3315 ret = -EAGAIN;
3316 goto unlock;
3317 }
3318
3319 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003320 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003321 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003322 t->curr_ret_stack = -1;
3323 /* Make sure the tasks see the -1 first: */
3324 smp_wmb();
3325 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003326 }
3327 } while_each_thread(g, t);
3328
3329unlock:
3330 read_unlock_irqrestore(&tasklist_lock, flags);
3331free:
3332 for (i = start; i < end; i++)
3333 kfree(ret_stack_list[i]);
3334 return ret;
3335}
3336
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003337static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04003338ftrace_graph_probe_sched_switch(void *ignore,
3339 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003340{
3341 unsigned long long timestamp;
3342 int index;
3343
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003344 /*
3345 * Does the user want to count the time a function was asleep.
3346 * If so, do not update the time stamps.
3347 */
3348 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3349 return;
3350
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003351 timestamp = trace_clock_local();
3352
3353 prev->ftrace_timestamp = timestamp;
3354
3355 /* only process tasks that we timestamped */
3356 if (!next->ftrace_timestamp)
3357 return;
3358
3359 /*
3360 * Update all the counters in next to make up for the
3361 * time next was sleeping.
3362 */
3363 timestamp -= next->ftrace_timestamp;
3364
3365 for (index = next->curr_ret_stack; index >= 0; index--)
3366 next->ret_stack[index].calltime += timestamp;
3367}
3368
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003369/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003370static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003371{
3372 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003373 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003374
3375 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3376 sizeof(struct ftrace_ret_stack *),
3377 GFP_KERNEL);
3378
3379 if (!ret_stack_list)
3380 return -ENOMEM;
3381
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003382 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003383 for_each_online_cpu(cpu) {
3384 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05003385 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04003386 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003387
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003388 do {
3389 ret = alloc_retstack_tasklist(ret_stack_list);
3390 } while (ret == -EAGAIN);
3391
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003392 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04003393 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003394 if (ret)
3395 pr_info("ftrace_graph: Couldn't activate tracepoint"
3396 " probe to kernel_sched_switch\n");
3397 }
3398
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003399 kfree(ret_stack_list);
3400 return ret;
3401}
3402
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003403/*
3404 * Hibernation protection.
3405 * The state of the current task is too much unstable during
3406 * suspend/restore to disk. We want to protect against that.
3407 */
3408static int
3409ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3410 void *unused)
3411{
3412 switch (state) {
3413 case PM_HIBERNATION_PREPARE:
3414 pause_graph_tracing();
3415 break;
3416
3417 case PM_POST_HIBERNATION:
3418 unpause_graph_tracing();
3419 break;
3420 }
3421 return NOTIFY_DONE;
3422}
3423
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003424int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3425 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003426{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003427 int ret = 0;
3428
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003429 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003430
Steven Rostedt05ce5812009-03-24 00:18:31 -04003431 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003432 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003433 ret = -EBUSY;
3434 goto out;
3435 }
3436
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003437 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3438 register_pm_notifier(&ftrace_suspend_notifier);
3439
Steven Rostedt597af812009-04-03 15:24:12 -04003440 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003441 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003442 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003443 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003444 goto out;
3445 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003446
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003447 ftrace_graph_return = retfunc;
3448 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003449
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003450 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003451
3452out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003453 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003454 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003455}
3456
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003457void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003458{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003459 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003460
Steven Rostedt597af812009-04-03 15:24:12 -04003461 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003462 goto out;
3463
Steven Rostedt597af812009-04-03 15:24:12 -04003464 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003465 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003466 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003467 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003468 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04003469 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003470
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003471 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003472 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003473}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003474
Steven Rostedt868baf02011-02-10 21:26:13 -05003475static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
3476
3477static void
3478graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
3479{
3480 atomic_set(&t->tracing_graph_pause, 0);
3481 atomic_set(&t->trace_overrun, 0);
3482 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003483 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05003484 smp_wmb();
3485 t->ret_stack = ret_stack;
3486}
3487
3488/*
3489 * Allocate a return stack for the idle task. May be the first
3490 * time through, or it may be done by CPU hotplug online.
3491 */
3492void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
3493{
3494 t->curr_ret_stack = -1;
3495 /*
3496 * The idle task has no parent, it either has its own
3497 * stack or no stack at all.
3498 */
3499 if (t->ret_stack)
3500 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
3501
3502 if (ftrace_graph_active) {
3503 struct ftrace_ret_stack *ret_stack;
3504
3505 ret_stack = per_cpu(idle_ret_stack, cpu);
3506 if (!ret_stack) {
3507 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3508 * sizeof(struct ftrace_ret_stack),
3509 GFP_KERNEL);
3510 if (!ret_stack)
3511 return;
3512 per_cpu(idle_ret_stack, cpu) = ret_stack;
3513 }
3514 graph_init_task(t, ret_stack);
3515 }
3516}
3517
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003518/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003519void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003520{
Steven Rostedt84047e32009-06-02 16:51:55 -04003521 /* Make sure we do not use the parent ret_stack */
3522 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05003523 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04003524
Steven Rostedt597af812009-04-03 15:24:12 -04003525 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003526 struct ftrace_ret_stack *ret_stack;
3527
3528 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003529 * sizeof(struct ftrace_ret_stack),
3530 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003531 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003532 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05003533 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04003534 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003535}
3536
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003537void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003538{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003539 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3540
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003541 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003542 /* NULL must become visible to IRQs before we free it: */
3543 barrier();
3544
3545 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003546}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003547
3548void ftrace_graph_stop(void)
3549{
3550 ftrace_stop();
3551}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003552#endif