blob: 222eca4c302290e82c2fdae2e33500c4c3ef7901 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
14 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010025#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020026#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020028#include <linux/ctype.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020029#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050030#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080031#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020032
Steven Rostedtad8d75f2009-04-14 19:39:12 -040033#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040034
Abhishek Sagar395a59d2008-06-21 23:47:27 +053035#include <asm/ftrace.h>
Steven Rostedt2af15d62009-05-28 13:37:24 -040036#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053037
Steven Rostedt0706f1c2009-03-23 23:12:58 -040038#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040039#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020040
Steven Rostedt6912896e2008-10-23 09:33:03 -040041#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040042 ({ \
43 int ___r = cond; \
44 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040045 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040046 ___r; \
47 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040048
49#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040050 ({ \
51 int ___r = cond; \
52 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040053 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040054 ___r; \
55 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040056
Steven Rostedt8fc0c702009-02-16 15:28:00 -050057/* hash bits for specific function selection */
58#define FTRACE_HASH_BITS 7
59#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedtb448c4e2011-04-29 15:12:32 -040060#define FTRACE_HASH_MAX_BITS 10
Steven Rostedt8fc0c702009-02-16 15:28:00 -050061
Steven Rostedt4eebcc82008-05-12 21:20:48 +020062/* ftrace_enabled is a method to turn ftrace on or off */
63int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020064static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020065
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050066/* Quick disabling of function tracer. */
67int function_trace_stop;
68
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040069/* List for set_ftrace_pid's pids. */
70LIST_HEAD(ftrace_pids);
71struct ftrace_pid {
72 struct list_head list;
73 struct pid *pid;
74};
75
Steven Rostedt4eebcc82008-05-12 21:20:48 +020076/*
77 * ftrace_disabled is set when an anomaly is discovered.
78 * ftrace_disabled is much stronger than ftrace_enabled.
79 */
80static int ftrace_disabled __read_mostly;
81
Steven Rostedt52baf112009-02-14 01:15:39 -050082static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020083
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020084static struct ftrace_ops ftrace_list_end __read_mostly =
85{
Steven Rostedtfb9fb012009-03-25 13:26:41 -040086 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020087};
88
89static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
90ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050091ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050092ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020093
Paul E. McKenney3f379b02010-03-05 15:03:25 -080094/*
95 * Traverse the ftrace_list, invoking all entries. The reason that we
96 * can use rcu_dereference_raw() is that elements removed from this list
97 * are simply leaked, so there is no need to interact with a grace-period
98 * mechanism. The rcu_dereference_raw() calls are needed to handle
99 * concurrent insertions into the ftrace_list.
100 *
101 * Silly Alpha and silly pointer-speculation compiler optimizations!
102 */
Ingo Molnarf2252932008-05-22 10:37:48 +0200103static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200104{
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800105 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200106
107 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200108 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800109 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200110 };
111}
112
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500113static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
114{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500115 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500116 return;
117
118 ftrace_pid_function(ip, parent_ip);
119}
120
121static void set_ftrace_pid_function(ftrace_func_t func)
122{
123 /* do not set ftrace_pid_function to itself! */
124 if (func != ftrace_pid_func)
125 ftrace_pid_function = func;
126}
127
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200128/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200129 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200130 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200131 * This NULLs the ftrace function and in essence stops
132 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200133 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200134void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200135{
Steven Rostedt3d083392008-05-12 21:20:42 +0200136 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500137 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500138 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200139}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200140
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500141#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
142/*
143 * For those archs that do not test ftrace_trace_stop in their
144 * mcount call site, we need to do it from C.
145 */
146static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
147{
148 if (function_trace_stop)
149 return;
150
151 __ftrace_trace_function(ip, parent_ip);
152}
153#endif
154
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400155static void update_ftrace_function(void)
156{
157 ftrace_func_t func;
158
159 /*
160 * If there's only one function registered, then call that
161 * function directly. Otherwise, we need to iterate over the
162 * registered callers.
163 */
164 if (ftrace_list == &ftrace_list_end ||
165 ftrace_list->next == &ftrace_list_end)
166 func = ftrace_list->func;
167 else
168 func = ftrace_list_func;
169
170 /* If we filter on pids, update to use the pid function */
171 if (!list_empty(&ftrace_pids)) {
172 set_ftrace_pid_function(func);
173 func = ftrace_pid_func;
174 }
175#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
176 ftrace_trace_function = func;
177#else
178 __ftrace_trace_function = func;
179 ftrace_trace_function = ftrace_test_stop_func;
180#endif
181}
182
Ingo Molnare309b412008-05-12 21:20:51 +0200183static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200184{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200185 ops->next = ftrace_list;
186 /*
187 * We are entering ops into the ftrace_list but another
188 * CPU might be walking that list. We need to make sure
189 * the ops->next pointer is valid before another CPU sees
190 * the ops pointer included into the ftrace_list.
191 */
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800192 rcu_assign_pointer(ftrace_list, ops);
Steven Rostedt3d083392008-05-12 21:20:42 +0200193
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400194 if (ftrace_enabled)
195 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200196
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200197 return 0;
198}
199
Ingo Molnare309b412008-05-12 21:20:51 +0200200static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200201{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200202 struct ftrace_ops **p;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200203
204 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200205 * If we are removing the last function, then simply point
206 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200207 */
208 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
209 ftrace_trace_function = ftrace_stub;
210 ftrace_list = &ftrace_list_end;
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500211 return 0;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200212 }
213
214 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
215 if (*p == ops)
216 break;
217
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500218 if (*p != ops)
219 return -1;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200220
221 *p = (*p)->next;
222
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400223 if (ftrace_enabled)
224 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200225
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500226 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200227}
228
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500229static void ftrace_update_pid_func(void)
230{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400231 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500232 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900233 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500234
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400235 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500236}
237
Steven Rostedt493762f2009-03-23 17:12:36 -0400238#ifdef CONFIG_FUNCTION_PROFILER
239struct ftrace_profile {
240 struct hlist_node node;
241 unsigned long ip;
242 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400243#ifdef CONFIG_FUNCTION_GRAPH_TRACER
244 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400245 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400246#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400247};
248
249struct ftrace_profile_page {
250 struct ftrace_profile_page *next;
251 unsigned long index;
252 struct ftrace_profile records[];
253};
254
Steven Rostedtcafb1682009-03-24 20:50:39 -0400255struct ftrace_profile_stat {
256 atomic_t disabled;
257 struct hlist_head *hash;
258 struct ftrace_profile_page *pages;
259 struct ftrace_profile_page *start;
260 struct tracer_stat stat;
261};
262
Steven Rostedt493762f2009-03-23 17:12:36 -0400263#define PROFILE_RECORDS_SIZE \
264 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
265
266#define PROFILES_PER_PAGE \
267 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
268
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400269static int ftrace_profile_bits __read_mostly;
270static int ftrace_profile_enabled __read_mostly;
271
272/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400273static DEFINE_MUTEX(ftrace_profile_lock);
274
Steven Rostedtcafb1682009-03-24 20:50:39 -0400275static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400276
277#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
278
Steven Rostedt493762f2009-03-23 17:12:36 -0400279static void *
280function_stat_next(void *v, int idx)
281{
282 struct ftrace_profile *rec = v;
283 struct ftrace_profile_page *pg;
284
285 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
286
287 again:
Li Zefan0296e422009-06-26 11:15:37 +0800288 if (idx != 0)
289 rec++;
290
Steven Rostedt493762f2009-03-23 17:12:36 -0400291 if ((void *)rec >= (void *)&pg->records[pg->index]) {
292 pg = pg->next;
293 if (!pg)
294 return NULL;
295 rec = &pg->records[0];
296 if (!rec->counter)
297 goto again;
298 }
299
300 return rec;
301}
302
303static void *function_stat_start(struct tracer_stat *trace)
304{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400305 struct ftrace_profile_stat *stat =
306 container_of(trace, struct ftrace_profile_stat, stat);
307
308 if (!stat || !stat->start)
309 return NULL;
310
311 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400312}
313
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400314#ifdef CONFIG_FUNCTION_GRAPH_TRACER
315/* function graph compares on total time */
316static int function_stat_cmp(void *p1, void *p2)
317{
318 struct ftrace_profile *a = p1;
319 struct ftrace_profile *b = p2;
320
321 if (a->time < b->time)
322 return -1;
323 if (a->time > b->time)
324 return 1;
325 else
326 return 0;
327}
328#else
329/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400330static int function_stat_cmp(void *p1, void *p2)
331{
332 struct ftrace_profile *a = p1;
333 struct ftrace_profile *b = p2;
334
335 if (a->counter < b->counter)
336 return -1;
337 if (a->counter > b->counter)
338 return 1;
339 else
340 return 0;
341}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400342#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400343
344static int function_stat_headers(struct seq_file *m)
345{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400346#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400347 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400348 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400349 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400350 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400351#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400352 seq_printf(m, " Function Hit\n"
353 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400354#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400355 return 0;
356}
357
358static int function_stat_show(struct seq_file *m, void *v)
359{
360 struct ftrace_profile *rec = v;
361 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800362 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400363#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400364 static struct trace_seq s;
365 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400366 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400367#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800368 mutex_lock(&ftrace_profile_lock);
369
370 /* we raced with function_profile_reset() */
371 if (unlikely(rec->counter == 0)) {
372 ret = -EBUSY;
373 goto out;
374 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400375
376 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400377 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400378
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400379#ifdef CONFIG_FUNCTION_GRAPH_TRACER
380 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400381 avg = rec->time;
382 do_div(avg, rec->counter);
383
Chase Douglase330b3b2010-04-26 14:02:05 -0400384 /* Sample standard deviation (s^2) */
385 if (rec->counter <= 1)
386 stddev = 0;
387 else {
388 stddev = rec->time_squared - rec->counter * avg * avg;
389 /*
390 * Divide only 1000 for ns^2 -> us^2 conversion.
391 * trace_print_graph_duration will divide 1000 again.
392 */
393 do_div(stddev, (rec->counter - 1) * 1000);
394 }
395
Steven Rostedt34886c82009-03-25 21:00:47 -0400396 trace_seq_init(&s);
397 trace_print_graph_duration(rec->time, &s);
398 trace_seq_puts(&s, " ");
399 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400400 trace_seq_puts(&s, " ");
401 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400402 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400403#endif
404 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800405out:
406 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400407
Li Zefan3aaba202010-08-23 16:50:12 +0800408 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400409}
410
Steven Rostedtcafb1682009-03-24 20:50:39 -0400411static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400412{
413 struct ftrace_profile_page *pg;
414
Steven Rostedtcafb1682009-03-24 20:50:39 -0400415 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400416
417 while (pg) {
418 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
419 pg->index = 0;
420 pg = pg->next;
421 }
422
Steven Rostedtcafb1682009-03-24 20:50:39 -0400423 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400424 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
425}
426
Steven Rostedtcafb1682009-03-24 20:50:39 -0400427int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400428{
429 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400430 int functions;
431 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400432 int i;
433
434 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400435 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400436 return 0;
437
Steven Rostedtcafb1682009-03-24 20:50:39 -0400438 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
439 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400440 return -ENOMEM;
441
Steven Rostedt318e0a72009-03-25 20:06:34 -0400442#ifdef CONFIG_DYNAMIC_FTRACE
443 functions = ftrace_update_tot_cnt;
444#else
445 /*
446 * We do not know the number of functions that exist because
447 * dynamic tracing is what counts them. With past experience
448 * we have around 20K functions. That should be more than enough.
449 * It is highly unlikely we will execute every function in
450 * the kernel.
451 */
452 functions = 20000;
453#endif
454
Steven Rostedtcafb1682009-03-24 20:50:39 -0400455 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400456
Steven Rostedt318e0a72009-03-25 20:06:34 -0400457 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
458
459 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400460 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400461 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400462 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400463 pg = pg->next;
464 }
465
466 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400467
468 out_free:
469 pg = stat->start;
470 while (pg) {
471 unsigned long tmp = (unsigned long)pg;
472
473 pg = pg->next;
474 free_page(tmp);
475 }
476
477 free_page((unsigned long)stat->pages);
478 stat->pages = NULL;
479 stat->start = NULL;
480
481 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400482}
483
Steven Rostedtcafb1682009-03-24 20:50:39 -0400484static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400485{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400486 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400487 int size;
488
Steven Rostedtcafb1682009-03-24 20:50:39 -0400489 stat = &per_cpu(ftrace_profile_stats, cpu);
490
491 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400492 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400493 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400494 return 0;
495 }
496
497 /*
498 * We are profiling all functions, but usually only a few thousand
499 * functions are hit. We'll make a hash of 1024 items.
500 */
501 size = FTRACE_PROFILE_HASH_SIZE;
502
Steven Rostedtcafb1682009-03-24 20:50:39 -0400503 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400504
Steven Rostedtcafb1682009-03-24 20:50:39 -0400505 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400506 return -ENOMEM;
507
Steven Rostedtcafb1682009-03-24 20:50:39 -0400508 if (!ftrace_profile_bits) {
509 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400510
Steven Rostedtcafb1682009-03-24 20:50:39 -0400511 for (; size; size >>= 1)
512 ftrace_profile_bits++;
513 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400514
Steven Rostedt318e0a72009-03-25 20:06:34 -0400515 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400516 if (ftrace_profile_pages_init(stat) < 0) {
517 kfree(stat->hash);
518 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400519 return -ENOMEM;
520 }
521
522 return 0;
523}
524
Steven Rostedtcafb1682009-03-24 20:50:39 -0400525static int ftrace_profile_init(void)
526{
527 int cpu;
528 int ret = 0;
529
530 for_each_online_cpu(cpu) {
531 ret = ftrace_profile_init_cpu(cpu);
532 if (ret)
533 break;
534 }
535
536 return ret;
537}
538
Steven Rostedt493762f2009-03-23 17:12:36 -0400539/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400540static struct ftrace_profile *
541ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400542{
543 struct ftrace_profile *rec;
544 struct hlist_head *hhd;
545 struct hlist_node *n;
546 unsigned long key;
547
548 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400549 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400550
551 if (hlist_empty(hhd))
552 return NULL;
553
554 hlist_for_each_entry_rcu(rec, n, hhd, node) {
555 if (rec->ip == ip)
556 return rec;
557 }
558
559 return NULL;
560}
561
Steven Rostedtcafb1682009-03-24 20:50:39 -0400562static void ftrace_add_profile(struct ftrace_profile_stat *stat,
563 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400564{
565 unsigned long key;
566
567 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400568 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400569}
570
Steven Rostedt318e0a72009-03-25 20:06:34 -0400571/*
572 * The memory is already allocated, this simply finds a new record to use.
573 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400574static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400575ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400576{
577 struct ftrace_profile *rec = NULL;
578
Steven Rostedt318e0a72009-03-25 20:06:34 -0400579 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400580 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400581 goto out;
582
Steven Rostedt493762f2009-03-23 17:12:36 -0400583 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400584 * Try to find the function again since an NMI
585 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400586 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400587 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400588 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400589 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400590
Steven Rostedtcafb1682009-03-24 20:50:39 -0400591 if (stat->pages->index == PROFILES_PER_PAGE) {
592 if (!stat->pages->next)
593 goto out;
594 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400595 }
596
Steven Rostedtcafb1682009-03-24 20:50:39 -0400597 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400598 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400599 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400600
Steven Rostedt493762f2009-03-23 17:12:36 -0400601 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400602 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400603
604 return rec;
605}
606
Steven Rostedt493762f2009-03-23 17:12:36 -0400607static void
608function_profile_call(unsigned long ip, unsigned long parent_ip)
609{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400610 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400611 struct ftrace_profile *rec;
612 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400613
614 if (!ftrace_profile_enabled)
615 return;
616
Steven Rostedt493762f2009-03-23 17:12:36 -0400617 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400618
619 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400620 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400621 goto out;
622
623 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400624 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400625 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400626 if (!rec)
627 goto out;
628 }
629
630 rec->counter++;
631 out:
632 local_irq_restore(flags);
633}
634
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400635#ifdef CONFIG_FUNCTION_GRAPH_TRACER
636static int profile_graph_entry(struct ftrace_graph_ent *trace)
637{
638 function_profile_call(trace->func, 0);
639 return 1;
640}
641
642static void profile_graph_return(struct ftrace_graph_ret *trace)
643{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400644 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400645 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400646 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400647 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400648
649 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400650 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400651 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400652 goto out;
653
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400654 /* If the calltime was zero'd ignore it */
655 if (!trace->calltime)
656 goto out;
657
Steven Rostedta2a16d62009-03-24 23:17:58 -0400658 calltime = trace->rettime - trace->calltime;
659
660 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
661 int index;
662
663 index = trace->depth;
664
665 /* Append this call time to the parent time to subtract */
666 if (index)
667 current->ret_stack[index - 1].subtime += calltime;
668
669 if (current->ret_stack[index].subtime < calltime)
670 calltime -= current->ret_stack[index].subtime;
671 else
672 calltime = 0;
673 }
674
Steven Rostedtcafb1682009-03-24 20:50:39 -0400675 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400676 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400677 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400678 rec->time_squared += calltime * calltime;
679 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400680
Steven Rostedtcafb1682009-03-24 20:50:39 -0400681 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400682 local_irq_restore(flags);
683}
684
685static int register_ftrace_profiler(void)
686{
687 return register_ftrace_graph(&profile_graph_return,
688 &profile_graph_entry);
689}
690
691static void unregister_ftrace_profiler(void)
692{
693 unregister_ftrace_graph();
694}
695#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400696static struct ftrace_ops ftrace_profile_ops __read_mostly =
697{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400698 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400699};
700
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400701static int register_ftrace_profiler(void)
702{
703 return register_ftrace_function(&ftrace_profile_ops);
704}
705
706static void unregister_ftrace_profiler(void)
707{
708 unregister_ftrace_function(&ftrace_profile_ops);
709}
710#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
711
Steven Rostedt493762f2009-03-23 17:12:36 -0400712static ssize_t
713ftrace_profile_write(struct file *filp, const char __user *ubuf,
714 size_t cnt, loff_t *ppos)
715{
716 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400717 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400718 int ret;
719
720 if (cnt >= sizeof(buf))
721 return -EINVAL;
722
723 if (copy_from_user(&buf, ubuf, cnt))
724 return -EFAULT;
725
726 buf[cnt] = 0;
727
728 ret = strict_strtoul(buf, 10, &val);
729 if (ret < 0)
730 return ret;
731
732 val = !!val;
733
734 mutex_lock(&ftrace_profile_lock);
735 if (ftrace_profile_enabled ^ val) {
736 if (val) {
737 ret = ftrace_profile_init();
738 if (ret < 0) {
739 cnt = ret;
740 goto out;
741 }
742
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400743 ret = register_ftrace_profiler();
744 if (ret < 0) {
745 cnt = ret;
746 goto out;
747 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400748 ftrace_profile_enabled = 1;
749 } else {
750 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400751 /*
752 * unregister_ftrace_profiler calls stop_machine
753 * so this acts like an synchronize_sched.
754 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400755 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400756 }
757 }
758 out:
759 mutex_unlock(&ftrace_profile_lock);
760
Jiri Olsacf8517c2009-10-23 19:36:16 -0400761 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400762
763 return cnt;
764}
765
766static ssize_t
767ftrace_profile_read(struct file *filp, char __user *ubuf,
768 size_t cnt, loff_t *ppos)
769{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400770 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400771 int r;
772
773 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
774 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
775}
776
777static const struct file_operations ftrace_profile_fops = {
778 .open = tracing_open_generic,
779 .read = ftrace_profile_read,
780 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200781 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400782};
783
Steven Rostedtcafb1682009-03-24 20:50:39 -0400784/* used to initialize the real stat files */
785static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400786 .name = "functions",
787 .stat_start = function_stat_start,
788 .stat_next = function_stat_next,
789 .stat_cmp = function_stat_cmp,
790 .stat_headers = function_stat_headers,
791 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400792};
793
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400794static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400795{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400796 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400797 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400798 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400799 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400800 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400801
Steven Rostedtcafb1682009-03-24 20:50:39 -0400802 for_each_possible_cpu(cpu) {
803 stat = &per_cpu(ftrace_profile_stats, cpu);
804
805 /* allocate enough for function name + cpu number */
806 name = kmalloc(32, GFP_KERNEL);
807 if (!name) {
808 /*
809 * The files created are permanent, if something happens
810 * we still do not free memory.
811 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400812 WARN(1,
813 "Could not allocate stat file for cpu %d\n",
814 cpu);
815 return;
816 }
817 stat->stat = function_stats;
818 snprintf(name, 32, "function%d", cpu);
819 stat->stat.name = name;
820 ret = register_stat_tracer(&stat->stat);
821 if (ret) {
822 WARN(1,
823 "Could not register function stat for cpu %d\n",
824 cpu);
825 kfree(name);
826 return;
827 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400828 }
829
830 entry = debugfs_create_file("function_profile_enabled", 0644,
831 d_tracer, NULL, &ftrace_profile_fops);
832 if (!entry)
833 pr_warning("Could not create debugfs "
834 "'function_profile_enabled' entry\n");
835}
836
837#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400838static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400839{
840}
841#endif /* CONFIG_FUNCTION_PROFILER */
842
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100843static struct pid * const ftrace_swapper_pid = &init_struct_pid;
844
Steven Rostedt3d083392008-05-12 21:20:42 +0200845#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100846
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400847#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400848# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400849#endif
850
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500851static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
852
Steven Rostedtb6887d72009-02-17 12:32:04 -0500853struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500854 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500855 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500856 unsigned long flags;
857 unsigned long ip;
858 void *data;
859 struct rcu_head rcu;
860};
861
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200862enum {
863 FTRACE_ENABLE_CALLS = (1 << 0),
864 FTRACE_DISABLE_CALLS = (1 << 1),
865 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
Steven Rostedt79e406d2010-09-14 22:19:46 -0400866 FTRACE_START_FUNC_RET = (1 << 3),
867 FTRACE_STOP_FUNC_RET = (1 << 4),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200868};
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400869struct ftrace_func_entry {
870 struct hlist_node hlist;
871 unsigned long ip;
872};
873
874struct ftrace_hash {
875 unsigned long size_bits;
876 struct hlist_head *buckets;
877 unsigned long count;
878};
879
880static struct hlist_head notrace_buckets[1 << FTRACE_HASH_MAX_BITS];
881static struct ftrace_hash notrace_hash = {
882 .size_bits = FTRACE_HASH_MAX_BITS,
883 .buckets = notrace_buckets,
884};
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200885
Steven Rostedt1cf41dd72011-04-29 20:59:51 -0400886static struct hlist_head filter_buckets[1 << FTRACE_HASH_MAX_BITS];
887static struct ftrace_hash filter_hash = {
888 .size_bits = FTRACE_HASH_MAX_BITS,
889 .buckets = filter_buckets,
890};
Steven Rostedt5072c592008-05-12 21:20:43 +0200891
Lai Jiangshane94142a2009-03-13 17:51:27 +0800892static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200893
Steven Rostedt41c52c02008-05-22 11:46:33 -0400894static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200895
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200896struct ftrace_page {
897 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500898 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200899 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700900};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200901
902#define ENTRIES_PER_PAGE \
903 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
904
905/* estimate from running different kernels */
906#define NR_TO_INIT 10000
907
908static struct ftrace_page *ftrace_pages_start;
909static struct ftrace_page *ftrace_pages;
910
Steven Rostedt37ad50842008-05-12 21:20:48 +0200911static struct dyn_ftrace *ftrace_free_records;
912
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400913static struct ftrace_func_entry *
914ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
915{
916 unsigned long key;
917 struct ftrace_func_entry *entry;
918 struct hlist_head *hhd;
919 struct hlist_node *n;
920
921 if (!hash->count)
922 return NULL;
923
924 if (hash->size_bits > 0)
925 key = hash_long(ip, hash->size_bits);
926 else
927 key = 0;
928
929 hhd = &hash->buckets[key];
930
931 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
932 if (entry->ip == ip)
933 return entry;
934 }
935 return NULL;
936}
937
938static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
939{
940 struct ftrace_func_entry *entry;
941 struct hlist_head *hhd;
942 unsigned long key;
943
944 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
945 if (!entry)
946 return -ENOMEM;
947
948 if (hash->size_bits)
949 key = hash_long(ip, hash->size_bits);
950 else
951 key = 0;
952
953 entry->ip = ip;
954 hhd = &hash->buckets[key];
955 hlist_add_head(&entry->hlist, hhd);
956 hash->count++;
957
958 return 0;
959}
960
961static void
962remove_hash_entry(struct ftrace_hash *hash,
963 struct ftrace_func_entry *entry)
964{
965 hlist_del(&entry->hlist);
966 kfree(entry);
967 hash->count--;
968}
969
970static void ftrace_hash_clear(struct ftrace_hash *hash)
971{
972 struct hlist_head *hhd;
973 struct hlist_node *tp, *tn;
974 struct ftrace_func_entry *entry;
975 int size = 1 << hash->size_bits;
976 int i;
977
978 for (i = 0; i < size; i++) {
979 hhd = &hash->buckets[i];
980 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
981 remove_hash_entry(hash, entry);
982 }
983 FTRACE_WARN_ON(hash->count);
984}
985
Steven Rostedt265c8312009-02-13 12:43:56 -0500986/*
987 * This is a double for. Do not use 'break' to break out of the loop,
988 * you must use a goto.
989 */
990#define do_for_each_ftrace_rec(pg, rec) \
991 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
992 int _____i; \
993 for (_____i = 0; _____i < pg->index; _____i++) { \
994 rec = &pg->records[_____i];
995
996#define while_for_each_ftrace_rec() \
997 } \
998 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530999
Ingo Molnare309b412008-05-12 21:20:51 +02001000static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad50842008-05-12 21:20:48 +02001001{
Lai Jiangshanee000b72009-03-24 13:38:06 +08001002 rec->freelist = ftrace_free_records;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001003 ftrace_free_records = rec;
1004 rec->flags |= FTRACE_FL_FREE;
1005}
1006
Ingo Molnare309b412008-05-12 21:20:51 +02001007static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001008{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001009 struct dyn_ftrace *rec;
1010
1011 /* First check for freed records */
1012 if (ftrace_free_records) {
1013 rec = ftrace_free_records;
1014
Steven Rostedt37ad50842008-05-12 21:20:48 +02001015 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt6912896e2008-10-23 09:33:03 -04001016 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad50842008-05-12 21:20:48 +02001017 ftrace_free_records = NULL;
1018 return NULL;
1019 }
1020
Lai Jiangshanee000b72009-03-24 13:38:06 +08001021 ftrace_free_records = rec->freelist;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001022 memset(rec, 0, sizeof(*rec));
1023 return rec;
1024 }
1025
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001026 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001027 if (!ftrace_pages->next) {
1028 /* allocate another page */
1029 ftrace_pages->next =
1030 (void *)get_zeroed_page(GFP_KERNEL);
1031 if (!ftrace_pages->next)
1032 return NULL;
1033 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001034 ftrace_pages = ftrace_pages->next;
1035 }
1036
1037 return &ftrace_pages->records[ftrace_pages->index++];
1038}
1039
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001040static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001041ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001042{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001043 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001044
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001045 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001046 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001047
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001048 rec = ftrace_alloc_dyn_node(ip);
1049 if (!rec)
1050 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001051
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001052 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001053 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001054 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001055
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001056 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001057}
1058
Steven Rostedt05736a42008-09-22 14:55:47 -07001059static void print_ip_ins(const char *fmt, unsigned char *p)
1060{
1061 int i;
1062
1063 printk(KERN_CONT "%s", fmt);
1064
1065 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1066 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1067}
1068
Steven Rostedt31e88902008-11-14 16:21:19 -08001069static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001070{
1071 switch (failed) {
1072 case -EFAULT:
1073 FTRACE_WARN_ON_ONCE(1);
1074 pr_info("ftrace faulted on modifying ");
1075 print_ip_sym(ip);
1076 break;
1077 case -EINVAL:
1078 FTRACE_WARN_ON_ONCE(1);
1079 pr_info("ftrace failed to modify ");
1080 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001081 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001082 printk(KERN_CONT "\n");
1083 break;
1084 case -EPERM:
1085 FTRACE_WARN_ON_ONCE(1);
1086 pr_info("ftrace faulted on writing ");
1087 print_ip_sym(ip);
1088 break;
1089 default:
1090 FTRACE_WARN_ON_ONCE(1);
1091 pr_info("ftrace faulted on unknown error ");
1092 print_ip_sym(ip);
1093 }
1094}
1095
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001096
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001097/* Return 1 if the address range is reserved for ftrace */
1098int ftrace_text_reserved(void *start, void *end)
1099{
1100 struct dyn_ftrace *rec;
1101 struct ftrace_page *pg;
1102
1103 do_for_each_ftrace_rec(pg, rec) {
1104 if (rec->ip <= (unsigned long)end &&
1105 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1106 return 1;
1107 } while_for_each_ftrace_rec();
1108 return 0;
1109}
1110
1111
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301112static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001113__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001114{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001115 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001116 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001117
Shaohua Lif0001202009-01-09 11:29:42 +08001118 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001119
Steven Rostedt982c3502008-11-15 16:31:41 -05001120 /*
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001121 * If this record is not to be traced or we want to disable it,
1122 * then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001123 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001124 * If we want to enable it and filtering is off, then enable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001125 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001126 * If we want to enable it and filtering is on, enable it only if
1127 * it's filtered
Steven Rostedt982c3502008-11-15 16:31:41 -05001128 */
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001129 if (enable && !ftrace_lookup_ip(&notrace_hash, rec->ip)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001130 if (!filter_hash.count || ftrace_lookup_ip(&filter_hash, rec->ip))
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001131 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001132 }
1133
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001134 /* If the state of this record hasn't changed, then do nothing */
1135 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1136 return 0;
1137
1138 if (flag) {
1139 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001140 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001141 }
1142
1143 rec->flags &= ~FTRACE_FL_ENABLED;
1144 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001145}
1146
1147static void ftrace_replace_code(int enable)
1148{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001149 struct dyn_ftrace *rec;
1150 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001151 int failed;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001152
Steven Rostedt45a4a232011-04-21 23:16:46 -04001153 if (unlikely(ftrace_disabled))
1154 return;
1155
Steven Rostedt265c8312009-02-13 12:43:56 -05001156 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001157 /* Skip over free records */
1158 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05001159 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001160
Steven Rostedt265c8312009-02-13 12:43:56 -05001161 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001162 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001163 ftrace_bug(failed, rec->ip);
1164 /* Stop processing */
1165 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001166 }
1167 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001168}
1169
Ingo Molnare309b412008-05-12 21:20:51 +02001170static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001171ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001172{
1173 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001174 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001175
1176 ip = rec->ip;
1177
Steven Rostedt45a4a232011-04-21 23:16:46 -04001178 if (unlikely(ftrace_disabled))
1179 return 0;
1180
Shaohua Li25aac9d2009-01-09 11:29:40 +08001181 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001182 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001183 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301184 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001185 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301186 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001187}
1188
Steven Rostedt000ab692009-02-17 13:35:06 -05001189/*
1190 * archs can override this function if they must do something
1191 * before the modifying code is performed.
1192 */
1193int __weak ftrace_arch_code_modify_prepare(void)
1194{
1195 return 0;
1196}
1197
1198/*
1199 * archs can override this function if they must do something
1200 * after the modifying code is performed.
1201 */
1202int __weak ftrace_arch_code_modify_post_process(void)
1203{
1204 return 0;
1205}
1206
Ingo Molnare309b412008-05-12 21:20:51 +02001207static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001208{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001209 int *command = data;
1210
Steven Rostedta3583242008-11-11 15:01:42 -05001211 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001212 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001213 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001214 ftrace_replace_code(0);
1215
1216 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1217 ftrace_update_ftrace_func(ftrace_trace_function);
1218
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001219 if (*command & FTRACE_START_FUNC_RET)
1220 ftrace_enable_ftrace_graph_caller();
1221 else if (*command & FTRACE_STOP_FUNC_RET)
1222 ftrace_disable_ftrace_graph_caller();
1223
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001224 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001225}
1226
Ingo Molnare309b412008-05-12 21:20:51 +02001227static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001228{
Steven Rostedt000ab692009-02-17 13:35:06 -05001229 int ret;
1230
1231 ret = ftrace_arch_code_modify_prepare();
1232 FTRACE_WARN_ON(ret);
1233 if (ret)
1234 return;
1235
Rusty Russell784e2d72008-07-28 12:16:31 -05001236 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001237
1238 ret = ftrace_arch_code_modify_post_process();
1239 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001240}
1241
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001242static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001243static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001244
1245static void ftrace_startup_enable(int command)
1246{
1247 if (saved_ftrace_func != ftrace_trace_function) {
1248 saved_ftrace_func = ftrace_trace_function;
1249 command |= FTRACE_UPDATE_TRACE_FUNC;
1250 }
1251
1252 if (!command || !ftrace_enabled)
1253 return;
1254
1255 ftrace_run_update_code(command);
1256}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001257
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001258static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001259{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001260 if (unlikely(ftrace_disabled))
1261 return;
1262
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001263 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001264 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001265
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001266 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001267}
1268
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001269static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001270{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001271 if (unlikely(ftrace_disabled))
1272 return;
1273
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001274 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001275 /*
1276 * Just warn in case of unbalance, no need to kill ftrace, it's not
1277 * critical but the ftrace_call callers may be never nopped again after
1278 * further ftrace uses.
1279 */
1280 WARN_ON_ONCE(ftrace_start_up < 0);
1281
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001282 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001283 command |= FTRACE_DISABLE_CALLS;
1284
1285 if (saved_ftrace_func != ftrace_trace_function) {
1286 saved_ftrace_func = ftrace_trace_function;
1287 command |= FTRACE_UPDATE_TRACE_FUNC;
1288 }
1289
1290 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001291 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001292
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001293 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001294}
1295
Ingo Molnare309b412008-05-12 21:20:51 +02001296static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001297{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001298 if (unlikely(ftrace_disabled))
1299 return;
1300
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001301 /* Force update next time */
1302 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001303 /* ftrace_start_up is true if we want ftrace running */
1304 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001305 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001306}
1307
Ingo Molnare309b412008-05-12 21:20:51 +02001308static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001309{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001310 if (unlikely(ftrace_disabled))
1311 return;
1312
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001313 /* ftrace_start_up is true if ftrace is running */
1314 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001315 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001316}
1317
Steven Rostedt3d083392008-05-12 21:20:42 +02001318static cycle_t ftrace_update_time;
1319static unsigned long ftrace_update_cnt;
1320unsigned long ftrace_update_tot_cnt;
1321
Steven Rostedt31e88902008-11-14 16:21:19 -08001322static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001323{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001324 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301325 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001326
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001327 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001328 ftrace_update_cnt = 0;
1329
Lai Jiangshane94142a2009-03-13 17:51:27 +08001330 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301331
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001332 /* If something went wrong, bail without enabling anything */
1333 if (unlikely(ftrace_disabled))
1334 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001335
Lai Jiangshane94142a2009-03-13 17:51:27 +08001336 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001337 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001338 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301339
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001340 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001341 * Do the initial record conversion from mcount jump
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001342 * to the NOP instructions.
1343 */
1344 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001345 ftrace_free_rec(p);
Steven Rostedtd2c8c3e2011-04-25 14:32:42 -04001346 /* Game over */
1347 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001348 }
1349
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001350 ftrace_update_cnt++;
1351
1352 /*
1353 * If the tracing is enabled, go ahead and enable the record.
1354 *
1355 * The reason not to enable the record immediatelly is the
1356 * inherent check of ftrace_make_nop/ftrace_make_call for
1357 * correct previous instructions. Making first the NOP
1358 * conversion puts the module to the correct state, thus
1359 * passing the ftrace_make_call check.
1360 */
1361 if (ftrace_start_up) {
1362 int failed = __ftrace_replace_code(p, 1);
1363 if (failed) {
1364 ftrace_bug(failed, p->ip);
1365 ftrace_free_rec(p);
1366 }
1367 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001368 }
1369
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001370 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001371 ftrace_update_time = stop - start;
1372 ftrace_update_tot_cnt += ftrace_update_cnt;
1373
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001374 return 0;
1375}
1376
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001377static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001378{
1379 struct ftrace_page *pg;
1380 int cnt;
1381 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001382
1383 /* allocate a few pages */
1384 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1385 if (!ftrace_pages_start)
1386 return -1;
1387
1388 /*
1389 * Allocate a few more pages.
1390 *
1391 * TODO: have some parser search vmlinux before
1392 * final linking to find all calls to ftrace.
1393 * Then we can:
1394 * a) know how many pages to allocate.
1395 * and/or
1396 * b) set up the table then.
1397 *
1398 * The dynamic code is still necessary for
1399 * modules.
1400 */
1401
1402 pg = ftrace_pages = ftrace_pages_start;
1403
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001404 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001405 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001406 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001407
1408 for (i = 0; i < cnt; i++) {
1409 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1410
1411 /* If we fail, we'll try later anyway */
1412 if (!pg->next)
1413 break;
1414
1415 pg = pg->next;
1416 }
1417
1418 return 0;
1419}
1420
Steven Rostedt5072c592008-05-12 21:20:43 +02001421enum {
1422 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001423 FTRACE_ITER_NOTRACE = (1 << 1),
Steven Rostedt3499e462011-04-21 22:59:12 -04001424 FTRACE_ITER_PRINTALL = (1 << 2),
1425 FTRACE_ITER_HASH = (1 << 3),
Steven Rostedt5072c592008-05-12 21:20:43 +02001426};
1427
1428#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1429
1430struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001431 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001432 loff_t func_pos;
1433 struct ftrace_page *pg;
1434 struct dyn_ftrace *func;
1435 struct ftrace_func_probe *probe;
1436 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001437 struct ftrace_hash *hash;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001438 int hidx;
1439 int idx;
1440 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02001441};
1442
Ingo Molnare309b412008-05-12 21:20:51 +02001443static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001444t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001445{
1446 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001447 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001448 struct hlist_head *hhd;
1449
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001450 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001451 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001452
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001453 if (iter->probe)
1454 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001455 retry:
1456 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1457 return NULL;
1458
1459 hhd = &ftrace_func_hash[iter->hidx];
1460
1461 if (hlist_empty(hhd)) {
1462 iter->hidx++;
1463 hnd = NULL;
1464 goto retry;
1465 }
1466
1467 if (!hnd)
1468 hnd = hhd->first;
1469 else {
1470 hnd = hnd->next;
1471 if (!hnd) {
1472 iter->hidx++;
1473 goto retry;
1474 }
1475 }
1476
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001477 if (WARN_ON_ONCE(!hnd))
1478 return NULL;
1479
1480 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1481
1482 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001483}
1484
1485static void *t_hash_start(struct seq_file *m, loff_t *pos)
1486{
1487 struct ftrace_iterator *iter = m->private;
1488 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001489 loff_t l;
1490
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001491 if (iter->func_pos > *pos)
1492 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001493
Li Zefand82d6242009-06-24 09:54:54 +08001494 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04001495 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001496 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08001497 if (!p)
1498 break;
1499 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001500 if (!p)
1501 return NULL;
1502
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001503 /* Only set this if we have an item */
1504 iter->flags |= FTRACE_ITER_HASH;
1505
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001506 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001507}
1508
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001509static int
1510t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001511{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001512 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001513
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001514 rec = iter->probe;
1515 if (WARN_ON_ONCE(!rec))
1516 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001517
Steven Rostedt809dcf22009-02-16 23:06:01 -05001518 if (rec->ops->print)
1519 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1520
Steven Rostedtb375a112009-09-17 00:05:58 -04001521 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001522
1523 if (rec->data)
1524 seq_printf(m, ":%p", rec->data);
1525 seq_putc(m, '\n');
1526
1527 return 0;
1528}
1529
1530static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001531t_next(struct seq_file *m, void *v, loff_t *pos)
1532{
1533 struct ftrace_iterator *iter = m->private;
1534 struct dyn_ftrace *rec = NULL;
1535
Steven Rostedt45a4a232011-04-21 23:16:46 -04001536 if (unlikely(ftrace_disabled))
1537 return NULL;
1538
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001539 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001540 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001541
Steven Rostedt5072c592008-05-12 21:20:43 +02001542 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01001543 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02001544
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001545 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001546 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001547
Steven Rostedt5072c592008-05-12 21:20:43 +02001548 retry:
1549 if (iter->idx >= iter->pg->index) {
1550 if (iter->pg->next) {
1551 iter->pg = iter->pg->next;
1552 iter->idx = 0;
1553 goto retry;
1554 }
1555 } else {
1556 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001557 if ((rec->flags & FTRACE_FL_FREE) ||
1558
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001559 ((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001560 !(ftrace_lookup_ip(&filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001561
Steven Rostedt41c52c02008-05-22 11:46:33 -04001562 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001563 !ftrace_lookup_ip(&notrace_hash, rec->ip))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001564 rec = NULL;
1565 goto retry;
1566 }
1567 }
1568
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001569 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04001570 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001571
1572 iter->func = rec;
1573
1574 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02001575}
1576
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001577static void reset_iter_read(struct ftrace_iterator *iter)
1578{
1579 iter->pos = 0;
1580 iter->func_pos = 0;
1581 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02001582}
1583
1584static void *t_start(struct seq_file *m, loff_t *pos)
1585{
1586 struct ftrace_iterator *iter = m->private;
1587 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001588 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001589
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001590 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04001591
1592 if (unlikely(ftrace_disabled))
1593 return NULL;
1594
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001595 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001596 * If an lseek was done, then reset and start from beginning.
1597 */
1598 if (*pos < iter->pos)
1599 reset_iter_read(iter);
1600
1601 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001602 * For set_ftrace_filter reading, if we have the filter
1603 * off, we can short cut and just print out that all
1604 * functions are enabled.
1605 */
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001606 if (iter->flags & FTRACE_ITER_FILTER && !filter_hash.count) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001607 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001608 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001609 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07001610 /* reset in case of seek/pread */
1611 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001612 return iter;
1613 }
1614
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001615 if (iter->flags & FTRACE_ITER_HASH)
1616 return t_hash_start(m, pos);
1617
Steven Rostedt98c4fd02010-09-10 11:47:43 -04001618 /*
1619 * Unfortunately, we need to restart at ftrace_pages_start
1620 * every time we let go of the ftrace_mutex. This is because
1621 * those pointers can change without the lock.
1622 */
Li Zefan694ce0a2009-06-24 09:54:19 +08001623 iter->pg = ftrace_pages_start;
1624 iter->idx = 0;
1625 for (l = 0; l <= *pos; ) {
1626 p = t_next(m, p, &l);
1627 if (!p)
1628 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001629 }
walimis5821e1b2008-11-15 15:19:06 +08001630
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001631 if (!p) {
1632 if (iter->flags & FTRACE_ITER_FILTER)
1633 return t_hash_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001634
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001635 return NULL;
1636 }
1637
1638 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02001639}
1640
1641static void t_stop(struct seq_file *m, void *p)
1642{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001643 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001644}
1645
1646static int t_show(struct seq_file *m, void *v)
1647{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001648 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001649 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02001650
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001651 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001652 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001653
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001654 if (iter->flags & FTRACE_ITER_PRINTALL) {
1655 seq_printf(m, "#### all functions enabled ####\n");
1656 return 0;
1657 }
1658
Steven Rostedt4aeb6962010-09-09 10:00:28 -04001659 rec = iter->func;
1660
Steven Rostedt5072c592008-05-12 21:20:43 +02001661 if (!rec)
1662 return 0;
1663
Steven Rostedtb375a112009-09-17 00:05:58 -04001664 seq_printf(m, "%ps\n", (void *)rec->ip);
Steven Rostedt5072c592008-05-12 21:20:43 +02001665
1666 return 0;
1667}
1668
James Morris88e9d342009-09-22 16:43:43 -07001669static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001670 .start = t_start,
1671 .next = t_next,
1672 .stop = t_stop,
1673 .show = t_show,
1674};
1675
Ingo Molnare309b412008-05-12 21:20:51 +02001676static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001677ftrace_avail_open(struct inode *inode, struct file *file)
1678{
1679 struct ftrace_iterator *iter;
1680 int ret;
1681
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001682 if (unlikely(ftrace_disabled))
1683 return -ENODEV;
1684
Steven Rostedt5072c592008-05-12 21:20:43 +02001685 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1686 if (!iter)
1687 return -ENOMEM;
1688
1689 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001690
1691 ret = seq_open(file, &show_ftrace_seq_ops);
1692 if (!ret) {
1693 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001694
Steven Rostedt5072c592008-05-12 21:20:43 +02001695 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001696 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001697 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001698 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001699
1700 return ret;
1701}
1702
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001703static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001704{
Steven Rostedt52baf112009-02-14 01:15:39 -05001705 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001706 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05001707 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001708}
1709
Ingo Molnare309b412008-05-12 21:20:51 +02001710static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001711ftrace_regex_open(struct ftrace_hash *hash, int flag,
1712 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02001713{
1714 struct ftrace_iterator *iter;
1715 int ret = 0;
1716
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001717 if (unlikely(ftrace_disabled))
1718 return -ENODEV;
1719
Steven Rostedt5072c592008-05-12 21:20:43 +02001720 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1721 if (!iter)
1722 return -ENOMEM;
1723
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001724 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1725 kfree(iter);
1726 return -ENOMEM;
1727 }
1728
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001729 iter->hash = hash;
1730
Steven Rostedt41c52c02008-05-22 11:46:33 -04001731 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001732 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04001733 (file->f_flags & O_TRUNC))
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001734 ftrace_filter_reset(hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02001735
1736 if (file->f_mode & FMODE_READ) {
1737 iter->pg = ftrace_pages_start;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001738 iter->flags = flag;
Steven Rostedt5072c592008-05-12 21:20:43 +02001739
1740 ret = seq_open(file, &show_ftrace_seq_ops);
1741 if (!ret) {
1742 struct seq_file *m = file->private_data;
1743 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08001744 } else {
1745 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02001746 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08001747 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001748 } else
1749 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001750 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001751
1752 return ret;
1753}
1754
Steven Rostedt41c52c02008-05-22 11:46:33 -04001755static int
1756ftrace_filter_open(struct inode *inode, struct file *file)
1757{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001758 return ftrace_regex_open(&filter_hash, FTRACE_ITER_FILTER,
1759 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001760}
1761
1762static int
1763ftrace_notrace_open(struct inode *inode, struct file *file)
1764{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001765 return ftrace_regex_open(&notrace_hash, FTRACE_ITER_NOTRACE,
1766 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001767}
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
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001811enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04001812{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001813 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001814 int ret = 0;
1815
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001816 entry = ftrace_lookup_ip(hash, rec->ip);
1817 if (not) {
1818 /* Do nothing if it doesn't exist */
1819 if (!entry)
1820 return 0;
1821
1822 remove_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001823 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001824 /* Do nothing if it exists */
1825 if (entry)
1826 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001827
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001828 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001829 }
1830 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04001831}
1832
Steven Rostedt64e7c442009-02-13 17:08:48 -05001833static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001834ftrace_match_record(struct dyn_ftrace *rec, char *mod,
1835 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001836{
1837 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001838 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001839
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001840 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1841
1842 if (mod) {
1843 /* module lookup requires matching the module */
1844 if (!modname || strcmp(modname, mod))
1845 return 0;
1846
1847 /* blank search means to match all funcs in the mod */
1848 if (!len)
1849 return 1;
1850 }
1851
Steven Rostedt64e7c442009-02-13 17:08:48 -05001852 return ftrace_match(str, regex, len, type);
1853}
1854
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001855static int
1856match_records(struct ftrace_hash *hash, char *buff,
1857 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001858{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001859 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001860 struct ftrace_page *pg;
1861 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001862 int type = MATCH_FULL;
1863 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08001864 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001865 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001866
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001867 if (len) {
1868 type = filter_parse_regex(buff, len, &search, &not);
1869 search_len = strlen(search);
1870 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001871
Steven Rostedt52baf112009-02-14 01:15:39 -05001872 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001873
1874 if (unlikely(ftrace_disabled))
1875 goto out_unlock;
1876
Steven Rostedt265c8312009-02-13 12:43:56 -05001877 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001878
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001879 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001880 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001881 if (ret < 0) {
1882 found = ret;
1883 goto out_unlock;
1884 }
Li Zefan311d16d2009-12-08 11:15:11 +08001885 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001886 }
1887 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04001888 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05001889 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001890
1891 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02001892}
1893
Steven Rostedt64e7c442009-02-13 17:08:48 -05001894static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001895ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001896{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001897 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001898}
1899
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001900static int
1901ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001902{
Steven Rostedt64e7c442009-02-13 17:08:48 -05001903 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001904
Steven Rostedt64e7c442009-02-13 17:08:48 -05001905 /* blank or '*' mean the same */
1906 if (strcmp(buff, "*") == 0)
1907 buff[0] = 0;
1908
1909 /* handle the case of 'dont filter this module' */
1910 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1911 buff[0] = 0;
1912 not = 1;
1913 }
1914
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001915 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001916}
1917
Steven Rostedtf6180772009-02-14 00:40:25 -05001918/*
1919 * We register the module command as a template to show others how
1920 * to register the a command as well.
1921 */
1922
1923static int
1924ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1925{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001926 struct ftrace_hash *hash;
Steven Rostedtf6180772009-02-14 00:40:25 -05001927 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001928 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05001929
1930 /*
1931 * cmd == 'mod' because we only registered this func
1932 * for the 'mod' ftrace_func_command.
1933 * But if you register one func with multiple commands,
1934 * you can tell which command was used by the cmd
1935 * parameter.
1936 */
1937
1938 /* we must have a module name */
1939 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001940 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05001941
1942 mod = strsep(&param, ":");
1943 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001944 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05001945
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04001946 if (enable)
1947 hash = &filter_hash;
1948 else
1949 hash = &notrace_hash;
1950
1951 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001952 if (!ret)
1953 ret = -EINVAL;
1954 if (ret < 0)
1955 return ret;
1956
1957 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05001958}
1959
1960static struct ftrace_func_command ftrace_mod_cmd = {
1961 .name = "mod",
1962 .func = ftrace_mod_callback,
1963};
1964
1965static int __init ftrace_mod_cmd_init(void)
1966{
1967 return register_ftrace_command(&ftrace_mod_cmd);
1968}
1969device_initcall(ftrace_mod_cmd_init);
1970
Steven Rostedt59df055f2009-02-14 15:29:06 -05001971static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001972function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001973{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001974 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001975 struct hlist_head *hhd;
1976 struct hlist_node *n;
1977 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001978
1979 key = hash_long(ip, FTRACE_HASH_BITS);
1980
1981 hhd = &ftrace_func_hash[key];
1982
1983 if (hlist_empty(hhd))
1984 return;
1985
1986 /*
1987 * Disable preemption for these calls to prevent a RCU grace
1988 * period. This syncs the hash iteration and freeing of items
1989 * on the hash. rcu_read_lock is too dangerous here.
1990 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04001991 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05001992 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1993 if (entry->ip == ip)
1994 entry->ops->func(ip, parent_ip, &entry->data);
1995 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04001996 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05001997}
1998
Steven Rostedtb6887d72009-02-17 12:32:04 -05001999static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002000{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002001 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002002};
2003
Steven Rostedtb6887d72009-02-17 12:32:04 -05002004static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002005
Steven Rostedtb6887d72009-02-17 12:32:04 -05002006static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002007{
2008 int i;
2009
Steven Rostedtb6887d72009-02-17 12:32:04 -05002010 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002011 return;
2012
2013 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2014 struct hlist_head *hhd = &ftrace_func_hash[i];
2015 if (hhd->first)
2016 break;
2017 }
2018 /* Nothing registered? */
2019 if (i == FTRACE_FUNC_HASHSIZE)
2020 return;
2021
Steven Rostedtb6887d72009-02-17 12:32:04 -05002022 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002023 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05002024 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002025}
2026
Steven Rostedtb6887d72009-02-17 12:32:04 -05002027static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002028{
2029 int i;
2030
Steven Rostedtb6887d72009-02-17 12:32:04 -05002031 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002032 return;
2033
2034 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2035 struct hlist_head *hhd = &ftrace_func_hash[i];
2036 if (hhd->first)
2037 return;
2038 }
2039
2040 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002041 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002042 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05002043 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002044}
2045
2046
2047static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2048{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002049 struct ftrace_func_probe *entry =
2050 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002051
2052 if (entry->ops->free)
2053 entry->ops->free(&entry->data);
2054 kfree(entry);
2055}
2056
2057
2058int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002059register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002060 void *data)
2061{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002062 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002063 struct ftrace_page *pg;
2064 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002065 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002066 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002067 int count = 0;
2068 char *search;
2069
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002070 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002071 len = strlen(search);
2072
Steven Rostedtb6887d72009-02-17 12:32:04 -05002073 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002074 if (WARN_ON(not))
2075 return -EINVAL;
2076
2077 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002078
Steven Rostedt45a4a232011-04-21 23:16:46 -04002079 if (unlikely(ftrace_disabled))
2080 goto out_unlock;
2081
Steven Rostedt59df055f2009-02-14 15:29:06 -05002082 do_for_each_ftrace_rec(pg, rec) {
2083
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002084 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002085 continue;
2086
2087 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2088 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002089 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002090 if (!count)
2091 count = -ENOMEM;
2092 goto out_unlock;
2093 }
2094
2095 count++;
2096
2097 entry->data = data;
2098
2099 /*
2100 * The caller might want to do something special
2101 * for each function we find. We call the callback
2102 * to give the caller an opportunity to do so.
2103 */
2104 if (ops->callback) {
2105 if (ops->callback(rec->ip, &entry->data) < 0) {
2106 /* caller does not like this func */
2107 kfree(entry);
2108 continue;
2109 }
2110 }
2111
2112 entry->ops = ops;
2113 entry->ip = rec->ip;
2114
2115 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2116 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2117
2118 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002119 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002120
2121 out_unlock:
2122 mutex_unlock(&ftrace_lock);
2123
2124 return count;
2125}
2126
2127enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002128 PROBE_TEST_FUNC = 1,
2129 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002130};
2131
2132static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002133__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002134 void *data, int flags)
2135{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002136 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002137 struct hlist_node *n, *tmp;
2138 char str[KSYM_SYMBOL_LEN];
2139 int type = MATCH_FULL;
2140 int i, len = 0;
2141 char *search;
2142
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002143 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002144 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002145 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002146 int not;
2147
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002148 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002149 len = strlen(search);
2150
Steven Rostedtb6887d72009-02-17 12:32:04 -05002151 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002152 if (WARN_ON(not))
2153 return;
2154 }
2155
2156 mutex_lock(&ftrace_lock);
2157 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2158 struct hlist_head *hhd = &ftrace_func_hash[i];
2159
2160 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2161
2162 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002163 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002164 continue;
2165
Steven Rostedtb6887d72009-02-17 12:32:04 -05002166 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002167 continue;
2168
2169 /* do this last, since it is the most expensive */
2170 if (glob) {
2171 kallsyms_lookup(entry->ip, NULL, NULL,
2172 NULL, str);
2173 if (!ftrace_match(str, glob, len, type))
2174 continue;
2175 }
2176
2177 hlist_del(&entry->node);
2178 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2179 }
2180 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002181 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002182 mutex_unlock(&ftrace_lock);
2183}
2184
2185void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002186unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002187 void *data)
2188{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002189 __unregister_ftrace_function_probe(glob, ops, data,
2190 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002191}
2192
2193void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002194unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002195{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002196 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002197}
2198
Steven Rostedtb6887d72009-02-17 12:32:04 -05002199void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002200{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002201 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002202}
2203
Steven Rostedtf6180772009-02-14 00:40:25 -05002204static LIST_HEAD(ftrace_commands);
2205static DEFINE_MUTEX(ftrace_cmd_mutex);
2206
2207int register_ftrace_command(struct ftrace_func_command *cmd)
2208{
2209 struct ftrace_func_command *p;
2210 int ret = 0;
2211
2212 mutex_lock(&ftrace_cmd_mutex);
2213 list_for_each_entry(p, &ftrace_commands, list) {
2214 if (strcmp(cmd->name, p->name) == 0) {
2215 ret = -EBUSY;
2216 goto out_unlock;
2217 }
2218 }
2219 list_add(&cmd->list, &ftrace_commands);
2220 out_unlock:
2221 mutex_unlock(&ftrace_cmd_mutex);
2222
2223 return ret;
2224}
2225
2226int unregister_ftrace_command(struct ftrace_func_command *cmd)
2227{
2228 struct ftrace_func_command *p, *n;
2229 int ret = -ENODEV;
2230
2231 mutex_lock(&ftrace_cmd_mutex);
2232 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2233 if (strcmp(cmd->name, p->name) == 0) {
2234 ret = 0;
2235 list_del_init(&p->list);
2236 goto out_unlock;
2237 }
2238 }
2239 out_unlock:
2240 mutex_unlock(&ftrace_cmd_mutex);
2241
2242 return ret;
2243}
2244
Steven Rostedt64e7c442009-02-13 17:08:48 -05002245static int ftrace_process_regex(char *buff, int len, int enable)
2246{
Steven Rostedtf6180772009-02-14 00:40:25 -05002247 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002248 struct ftrace_func_command *p;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002249 struct ftrace_hash *hash;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002250 int ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002251
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002252 if (enable)
2253 hash = &filter_hash;
2254 else
2255 hash = &notrace_hash;
2256
Steven Rostedt64e7c442009-02-13 17:08:48 -05002257 func = strsep(&next, ":");
2258
2259 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002260 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002261 if (!ret)
2262 ret = -EINVAL;
2263 if (ret < 0)
2264 return ret;
2265 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002266 }
2267
Steven Rostedtf6180772009-02-14 00:40:25 -05002268 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002269
2270 command = strsep(&next, ":");
2271
Steven Rostedtf6180772009-02-14 00:40:25 -05002272 mutex_lock(&ftrace_cmd_mutex);
2273 list_for_each_entry(p, &ftrace_commands, list) {
2274 if (strcmp(p->name, command) == 0) {
2275 ret = p->func(func, command, next, enable);
2276 goto out_unlock;
2277 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002278 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002279 out_unlock:
2280 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002281
Steven Rostedtf6180772009-02-14 00:40:25 -05002282 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002283}
2284
Ingo Molnare309b412008-05-12 21:20:51 +02002285static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002286ftrace_regex_write(struct file *file, const char __user *ubuf,
2287 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002288{
2289 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002290 struct trace_parser *parser;
2291 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002292
Li Zefan4ba79782009-09-22 13:52:20 +08002293 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002294 return 0;
2295
Steven Rostedt41c52c02008-05-22 11:46:33 -04002296 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002297
Steven Rostedt45a4a232011-04-21 23:16:46 -04002298 ret = -ENODEV;
2299 if (unlikely(ftrace_disabled))
2300 goto out_unlock;
2301
Steven Rostedt5072c592008-05-12 21:20:43 +02002302 if (file->f_mode & FMODE_READ) {
2303 struct seq_file *m = file->private_data;
2304 iter = m->private;
2305 } else
2306 iter = file->private_data;
2307
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002308 parser = &iter->parser;
2309 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002310
Li Zefan4ba79782009-09-22 13:52:20 +08002311 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002312 !trace_parser_cont(parser)) {
2313 ret = ftrace_process_regex(parser->buffer,
2314 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002315 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002316 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002317 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002318 }
2319
Steven Rostedt5072c592008-05-12 21:20:43 +02002320 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002321out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002322 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002323
Steven Rostedt5072c592008-05-12 21:20:43 +02002324 return ret;
2325}
2326
Steven Rostedt41c52c02008-05-22 11:46:33 -04002327static ssize_t
2328ftrace_filter_write(struct file *file, const char __user *ubuf,
2329 size_t cnt, loff_t *ppos)
2330{
2331 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2332}
2333
2334static ssize_t
2335ftrace_notrace_write(struct file *file, const char __user *ubuf,
2336 size_t cnt, loff_t *ppos)
2337{
2338 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2339}
2340
2341static void
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002342ftrace_set_regex(struct ftrace_hash *hash, unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04002343{
2344 if (unlikely(ftrace_disabled))
2345 return;
2346
2347 mutex_lock(&ftrace_regex_lock);
2348 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002349 ftrace_filter_reset(hash);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002350 if (buf)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002351 ftrace_match_records(hash, buf, len);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002352 mutex_unlock(&ftrace_regex_lock);
2353}
2354
Steven Rostedt77a2b372008-05-12 21:20:45 +02002355/**
2356 * ftrace_set_filter - set a function to filter on in ftrace
2357 * @buf - the string that holds the function filter text.
2358 * @len - the length of the string.
2359 * @reset - non zero to reset all filters before applying this filter.
2360 *
2361 * Filters denote which functions should be enabled when tracing is enabled.
2362 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2363 */
Ingo Molnare309b412008-05-12 21:20:51 +02002364void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002365{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002366 ftrace_set_regex(&filter_hash, buf, len, reset);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002367}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002368
Steven Rostedt41c52c02008-05-22 11:46:33 -04002369/**
2370 * ftrace_set_notrace - set a function to not trace in ftrace
2371 * @buf - the string that holds the function notrace text.
2372 * @len - the length of the string.
2373 * @reset - non zero to reset all filters before applying this filter.
2374 *
2375 * Notrace Filters denote which functions should not be enabled when tracing
2376 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2377 * for tracing.
2378 */
2379void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2380{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002381 ftrace_set_regex(&notrace_hash, buf, len, reset);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002382}
2383
Steven Rostedt2af15d62009-05-28 13:37:24 -04002384/*
2385 * command line interface to allow users to set filters on boot up.
2386 */
2387#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2388static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2389static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2390
2391static int __init set_ftrace_notrace(char *str)
2392{
2393 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2394 return 1;
2395}
2396__setup("ftrace_notrace=", set_ftrace_notrace);
2397
2398static int __init set_ftrace_filter(char *str)
2399{
2400 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2401 return 1;
2402}
2403__setup("ftrace_filter=", set_ftrace_filter);
2404
Stefan Assmann369bc182009-10-12 22:17:21 +02002405#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002406static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002407static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2408
Stefan Assmann369bc182009-10-12 22:17:21 +02002409static int __init set_graph_function(char *str)
2410{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002411 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002412 return 1;
2413}
2414__setup("ftrace_graph_filter=", set_graph_function);
2415
2416static void __init set_ftrace_early_graph(char *buf)
2417{
2418 int ret;
2419 char *func;
2420
2421 while (buf) {
2422 func = strsep(&buf, ",");
2423 /* we allow only one expression at a time */
2424 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2425 func);
2426 if (ret)
2427 printk(KERN_DEBUG "ftrace: function %s not "
2428 "traceable\n", func);
2429 }
2430}
2431#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2432
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002433static void __init set_ftrace_early_filter(struct ftrace_hash *hash, char *buf)
Steven Rostedt2af15d62009-05-28 13:37:24 -04002434{
2435 char *func;
2436
2437 while (buf) {
2438 func = strsep(&buf, ",");
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002439 ftrace_set_regex(hash, func, strlen(func), 0);
Steven Rostedt2af15d62009-05-28 13:37:24 -04002440 }
2441}
2442
2443static void __init set_ftrace_early_filters(void)
2444{
2445 if (ftrace_filter_buf[0])
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002446 set_ftrace_early_filter(&filter_hash, ftrace_filter_buf);
Steven Rostedt2af15d62009-05-28 13:37:24 -04002447 if (ftrace_notrace_buf[0])
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002448 set_ftrace_early_filter(&notrace_hash, ftrace_notrace_buf);
Stefan Assmann369bc182009-10-12 22:17:21 +02002449#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2450 if (ftrace_graph_buf[0])
2451 set_ftrace_early_graph(ftrace_graph_buf);
2452#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002453}
2454
Ingo Molnare309b412008-05-12 21:20:51 +02002455static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002456ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002457{
2458 struct seq_file *m = (struct seq_file *)file->private_data;
2459 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002460 struct trace_parser *parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02002461
Steven Rostedt41c52c02008-05-22 11:46:33 -04002462 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002463 if (file->f_mode & FMODE_READ) {
2464 iter = m->private;
2465
2466 seq_release(inode, file);
2467 } else
2468 iter = file->private_data;
2469
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002470 parser = &iter->parser;
2471 if (trace_parser_loaded(parser)) {
2472 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002473 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02002474 }
2475
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002476 trace_parser_put(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002477 kfree(iter);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002478
Steven Rostedt058e2972011-04-29 22:35:33 -04002479 if (file->f_mode & FMODE_WRITE) {
2480 mutex_lock(&ftrace_lock);
2481 if (ftrace_start_up && ftrace_enabled)
2482 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2483 mutex_unlock(&ftrace_lock);
2484 }
2485
Steven Rostedt41c52c02008-05-22 11:46:33 -04002486 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002487 return 0;
2488}
2489
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002490static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002491 .open = ftrace_avail_open,
2492 .read = seq_read,
2493 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002494 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002495};
2496
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002497static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002498 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002499 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002500 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002501 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002502 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02002503};
2504
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002505static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002506 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002507 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002508 .write = ftrace_notrace_write,
2509 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002510 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002511};
2512
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002513#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2514
2515static DEFINE_MUTEX(graph_lock);
2516
2517int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002518int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002519unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2520
2521static void *
Li Zefan85951842009-06-24 09:54:00 +08002522__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002523{
Li Zefan85951842009-06-24 09:54:00 +08002524 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002525 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002526 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002527}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002528
Li Zefan85951842009-06-24 09:54:00 +08002529static void *
2530g_next(struct seq_file *m, void *v, loff_t *pos)
2531{
2532 (*pos)++;
2533 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002534}
2535
2536static void *g_start(struct seq_file *m, loff_t *pos)
2537{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002538 mutex_lock(&graph_lock);
2539
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002540 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002541 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002542 return (void *)1;
2543
Li Zefan85951842009-06-24 09:54:00 +08002544 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002545}
2546
2547static void g_stop(struct seq_file *m, void *p)
2548{
2549 mutex_unlock(&graph_lock);
2550}
2551
2552static int g_show(struct seq_file *m, void *v)
2553{
2554 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002555
2556 if (!ptr)
2557 return 0;
2558
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002559 if (ptr == (unsigned long *)1) {
2560 seq_printf(m, "#### all functions enabled ####\n");
2561 return 0;
2562 }
2563
Steven Rostedtb375a112009-09-17 00:05:58 -04002564 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002565
2566 return 0;
2567}
2568
James Morris88e9d342009-09-22 16:43:43 -07002569static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002570 .start = g_start,
2571 .next = g_next,
2572 .stop = g_stop,
2573 .show = g_show,
2574};
2575
2576static int
2577ftrace_graph_open(struct inode *inode, struct file *file)
2578{
2579 int ret = 0;
2580
2581 if (unlikely(ftrace_disabled))
2582 return -ENODEV;
2583
2584 mutex_lock(&graph_lock);
2585 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002586 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002587 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002588 ftrace_graph_count = 0;
2589 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2590 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002591 mutex_unlock(&graph_lock);
2592
Li Zefana4ec5e02009-09-18 14:06:28 +08002593 if (file->f_mode & FMODE_READ)
2594 ret = seq_open(file, &ftrace_graph_seq_ops);
2595
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002596 return ret;
2597}
2598
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002599static int
Li Zefan87827112009-07-23 11:29:11 +08002600ftrace_graph_release(struct inode *inode, struct file *file)
2601{
2602 if (file->f_mode & FMODE_READ)
2603 seq_release(inode, file);
2604 return 0;
2605}
2606
2607static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002608ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002609{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002610 struct dyn_ftrace *rec;
2611 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002612 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002613 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002614 int type, not;
2615 char *search;
2616 bool exists;
2617 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002618
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002619 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002620 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002621 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2622 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002623
2624 search_len = strlen(search);
2625
Steven Rostedt52baf112009-02-14 01:15:39 -05002626 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002627
2628 if (unlikely(ftrace_disabled)) {
2629 mutex_unlock(&ftrace_lock);
2630 return -ENODEV;
2631 }
2632
Steven Rostedt265c8312009-02-13 12:43:56 -05002633 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002634
Steven Rostedt45a4a232011-04-21 23:16:46 -04002635 if (rec->flags & FTRACE_FL_FREE)
Steven Rostedt265c8312009-02-13 12:43:56 -05002636 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002637
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002638 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002639 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002640 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002641 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002642 if (array[i] == rec->ip) {
2643 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002644 break;
2645 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002646 }
2647
2648 if (!not) {
2649 fail = 0;
2650 if (!exists) {
2651 array[(*idx)++] = rec->ip;
2652 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2653 goto out;
2654 }
2655 } else {
2656 if (exists) {
2657 array[i] = array[--(*idx)];
2658 array[*idx] = 0;
2659 fail = 0;
2660 }
2661 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002662 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002663 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002664out:
Steven Rostedt52baf112009-02-14 01:15:39 -05002665 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002666
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002667 if (fail)
2668 return -EINVAL;
2669
2670 ftrace_graph_filter_enabled = 1;
2671 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002672}
2673
2674static ssize_t
2675ftrace_graph_write(struct file *file, const char __user *ubuf,
2676 size_t cnt, loff_t *ppos)
2677{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002678 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08002679 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002680
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002681 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002682 return 0;
2683
2684 mutex_lock(&graph_lock);
2685
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002686 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2687 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08002688 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002689 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002690
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002691 read = trace_get_user(&parser, ubuf, cnt, ppos);
2692
Li Zefan4ba79782009-09-22 13:52:20 +08002693 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002694 parser.buffer[parser.idx] = 0;
2695
2696 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08002697 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002698 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002699 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08002700 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002701 }
2702
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002703 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08002704
2705out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002706 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08002707out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002708 mutex_unlock(&graph_lock);
2709
2710 return ret;
2711}
2712
2713static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08002714 .open = ftrace_graph_open,
2715 .read = seq_read,
2716 .write = ftrace_graph_write,
2717 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002718 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002719};
2720#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2721
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002722static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002723{
Steven Rostedt5072c592008-05-12 21:20:43 +02002724
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002725 trace_create_file("available_filter_functions", 0444,
2726 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002727
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002728 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2729 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002730
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002731 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002732 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002733
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002734#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002735 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002736 NULL,
2737 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002738#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2739
Steven Rostedt5072c592008-05-12 21:20:43 +02002740 return 0;
2741}
2742
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002743static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08002744 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002745 unsigned long *end)
2746{
2747 unsigned long *p;
2748 unsigned long addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002749
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002750 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002751 p = start;
2752 while (p < end) {
2753 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002754 /*
2755 * Some architecture linkers will pad between
2756 * the different mcount_loc sections of different
2757 * object files to satisfy alignments.
2758 * Skip any NULL pointers.
2759 */
2760 if (!addr)
2761 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002762 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002763 }
2764
Steven Rostedt31e88902008-11-14 16:21:19 -08002765 ftrace_update_code(mod);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002766 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002767
2768 return 0;
2769}
2770
Steven Rostedt93eb6772009-04-15 13:24:06 -04002771#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002772void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002773{
2774 struct dyn_ftrace *rec;
2775 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04002776
Steven Rostedt93eb6772009-04-15 13:24:06 -04002777 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002778
2779 if (ftrace_disabled)
2780 goto out_unlock;
2781
Steven Rostedt93eb6772009-04-15 13:24:06 -04002782 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002783 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04002784 /*
2785 * rec->ip is changed in ftrace_free_rec()
2786 * It should not between s and e if record was freed.
2787 */
2788 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2789 ftrace_free_rec(rec);
2790 }
2791 } while_for_each_ftrace_rec();
Steven Rostedt45a4a232011-04-21 23:16:46 -04002792 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04002793 mutex_unlock(&ftrace_lock);
2794}
2795
2796static void ftrace_init_module(struct module *mod,
2797 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002798{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002799 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002800 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002801 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002802}
2803
Steven Rostedt93eb6772009-04-15 13:24:06 -04002804static int ftrace_module_notify(struct notifier_block *self,
2805 unsigned long val, void *data)
2806{
2807 struct module *mod = data;
2808
2809 switch (val) {
2810 case MODULE_STATE_COMING:
2811 ftrace_init_module(mod, mod->ftrace_callsites,
2812 mod->ftrace_callsites +
2813 mod->num_ftrace_callsites);
2814 break;
2815 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002816 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04002817 break;
2818 }
2819
2820 return 0;
2821}
2822#else
2823static int ftrace_module_notify(struct notifier_block *self,
2824 unsigned long val, void *data)
2825{
2826 return 0;
2827}
2828#endif /* CONFIG_MODULES */
2829
2830struct notifier_block ftrace_module_nb = {
2831 .notifier_call = ftrace_module_notify,
2832 .priority = 0,
2833};
2834
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002835extern unsigned long __start_mcount_loc[];
2836extern unsigned long __stop_mcount_loc[];
2837
2838void __init ftrace_init(void)
2839{
2840 unsigned long count, addr, flags;
2841 int ret;
2842
2843 /* Keep the ftrace pointer to the stub */
2844 addr = (unsigned long)ftrace_stub;
2845
2846 local_irq_save(flags);
2847 ftrace_dyn_arch_init(&addr);
2848 local_irq_restore(flags);
2849
2850 /* ftrace_dyn_arch_init places the return code in addr */
2851 if (addr)
2852 goto failed;
2853
2854 count = __stop_mcount_loc - __start_mcount_loc;
2855
2856 ret = ftrace_dyn_table_alloc(count);
2857 if (ret)
2858 goto failed;
2859
2860 last_ftrace_enabled = ftrace_enabled = 1;
2861
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002862 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08002863 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002864 __stop_mcount_loc);
2865
Steven Rostedt93eb6772009-04-15 13:24:06 -04002866 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002867 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002868 pr_warning("Failed to register trace ftrace module notifier\n");
2869
Steven Rostedt2af15d62009-05-28 13:37:24 -04002870 set_ftrace_early_filters();
2871
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002872 return;
2873 failed:
2874 ftrace_disabled = 1;
2875}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002876
Steven Rostedt3d083392008-05-12 21:20:42 +02002877#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002878
2879static int __init ftrace_nodyn_init(void)
2880{
2881 ftrace_enabled = 1;
2882 return 0;
2883}
2884device_initcall(ftrace_nodyn_init);
2885
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002886static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2887static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002888/* Keep as macros so we do not need to define the commands */
2889# define ftrace_startup(command) do { } while (0)
2890# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002891# define ftrace_startup_sysctl() do { } while (0)
2892# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002893#endif /* CONFIG_DYNAMIC_FTRACE */
2894
Steven Rostedte32d8952008-12-04 00:26:41 -05002895static void clear_ftrace_swapper(void)
2896{
2897 struct task_struct *p;
2898 int cpu;
2899
2900 get_online_cpus();
2901 for_each_online_cpu(cpu) {
2902 p = idle_task(cpu);
2903 clear_tsk_trace_trace(p);
2904 }
2905 put_online_cpus();
2906}
2907
2908static void set_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 set_tsk_trace_trace(p);
2917 }
2918 put_online_cpus();
2919}
2920
2921static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002922{
2923 struct task_struct *p;
2924
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002925 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002926 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002927 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002928 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002929 rcu_read_unlock();
2930
Steven Rostedte32d8952008-12-04 00:26:41 -05002931 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002932}
2933
Steven Rostedte32d8952008-12-04 00:26:41 -05002934static void set_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 Rostedt978f3a42008-12-04 00:26:40 -05002939 do_each_pid_task(pid, PIDTYPE_PID, p) {
2940 set_tsk_trace_trace(p);
2941 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002942 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002943}
2944
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002945static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002946{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002947 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002948 clear_ftrace_swapper();
2949 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002950 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05002951}
2952
2953static void set_ftrace_pid_task(struct pid *pid)
2954{
2955 if (pid == ftrace_swapper_pid)
2956 set_ftrace_swapper();
2957 else
2958 set_ftrace_pid(pid);
2959}
2960
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002961static int ftrace_pid_add(int p)
2962{
2963 struct pid *pid;
2964 struct ftrace_pid *fpid;
2965 int ret = -EINVAL;
2966
2967 mutex_lock(&ftrace_lock);
2968
2969 if (!p)
2970 pid = ftrace_swapper_pid;
2971 else
2972 pid = find_get_pid(p);
2973
2974 if (!pid)
2975 goto out;
2976
2977 ret = 0;
2978
2979 list_for_each_entry(fpid, &ftrace_pids, list)
2980 if (fpid->pid == pid)
2981 goto out_put;
2982
2983 ret = -ENOMEM;
2984
2985 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2986 if (!fpid)
2987 goto out_put;
2988
2989 list_add(&fpid->list, &ftrace_pids);
2990 fpid->pid = pid;
2991
2992 set_ftrace_pid_task(pid);
2993
2994 ftrace_update_pid_func();
2995 ftrace_startup_enable(0);
2996
2997 mutex_unlock(&ftrace_lock);
2998 return 0;
2999
3000out_put:
3001 if (pid != ftrace_swapper_pid)
3002 put_pid(pid);
3003
3004out:
3005 mutex_unlock(&ftrace_lock);
3006 return ret;
3007}
3008
3009static void ftrace_pid_reset(void)
3010{
3011 struct ftrace_pid *fpid, *safe;
3012
3013 mutex_lock(&ftrace_lock);
3014 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3015 struct pid *pid = fpid->pid;
3016
3017 clear_ftrace_pid_task(pid);
3018
3019 list_del(&fpid->list);
3020 kfree(fpid);
3021 }
3022
3023 ftrace_update_pid_func();
3024 ftrace_startup_enable(0);
3025
3026 mutex_unlock(&ftrace_lock);
3027}
3028
3029static void *fpid_start(struct seq_file *m, loff_t *pos)
3030{
3031 mutex_lock(&ftrace_lock);
3032
3033 if (list_empty(&ftrace_pids) && (!*pos))
3034 return (void *) 1;
3035
3036 return seq_list_start(&ftrace_pids, *pos);
3037}
3038
3039static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3040{
3041 if (v == (void *)1)
3042 return NULL;
3043
3044 return seq_list_next(v, &ftrace_pids, pos);
3045}
3046
3047static void fpid_stop(struct seq_file *m, void *p)
3048{
3049 mutex_unlock(&ftrace_lock);
3050}
3051
3052static int fpid_show(struct seq_file *m, void *v)
3053{
3054 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3055
3056 if (v == (void *)1) {
3057 seq_printf(m, "no pid\n");
3058 return 0;
3059 }
3060
3061 if (fpid->pid == ftrace_swapper_pid)
3062 seq_printf(m, "swapper tasks\n");
3063 else
3064 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3065
3066 return 0;
3067}
3068
3069static const struct seq_operations ftrace_pid_sops = {
3070 .start = fpid_start,
3071 .next = fpid_next,
3072 .stop = fpid_stop,
3073 .show = fpid_show,
3074};
3075
3076static int
3077ftrace_pid_open(struct inode *inode, struct file *file)
3078{
3079 int ret = 0;
3080
3081 if ((file->f_mode & FMODE_WRITE) &&
3082 (file->f_flags & O_TRUNC))
3083 ftrace_pid_reset();
3084
3085 if (file->f_mode & FMODE_READ)
3086 ret = seq_open(file, &ftrace_pid_sops);
3087
3088 return ret;
3089}
3090
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003091static ssize_t
3092ftrace_pid_write(struct file *filp, const char __user *ubuf,
3093 size_t cnt, loff_t *ppos)
3094{
Ingo Molnar457dc922009-11-23 11:03:28 +01003095 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003096 long val;
3097 int ret;
3098
3099 if (cnt >= sizeof(buf))
3100 return -EINVAL;
3101
3102 if (copy_from_user(&buf, ubuf, cnt))
3103 return -EFAULT;
3104
3105 buf[cnt] = 0;
3106
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003107 /*
3108 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3109 * to clean the filter quietly.
3110 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003111 tmp = strstrip(buf);
3112 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003113 return 1;
3114
Ingo Molnar457dc922009-11-23 11:03:28 +01003115 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003116 if (ret < 0)
3117 return ret;
3118
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003119 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003120
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003121 return ret ? ret : cnt;
3122}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003123
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003124static int
3125ftrace_pid_release(struct inode *inode, struct file *file)
3126{
3127 if (file->f_mode & FMODE_READ)
3128 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003129
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003130 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003131}
3132
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003133static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003134 .open = ftrace_pid_open,
3135 .write = ftrace_pid_write,
3136 .read = seq_read,
3137 .llseek = seq_lseek,
3138 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003139};
3140
3141static __init int ftrace_init_debugfs(void)
3142{
3143 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003144
3145 d_tracer = tracing_init_dentry();
3146 if (!d_tracer)
3147 return 0;
3148
3149 ftrace_init_dyn_debugfs(d_tracer);
3150
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003151 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3152 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003153
3154 ftrace_profile_debugfs(d_tracer);
3155
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003156 return 0;
3157}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003158fs_initcall(ftrace_init_debugfs);
3159
Steven Rostedt3d083392008-05-12 21:20:42 +02003160/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003161 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003162 *
3163 * This function should be used by panic code. It stops ftrace
3164 * but in a not so nice way. If you need to simply kill ftrace
3165 * from a non-atomic section, use ftrace_kill.
3166 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003167void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003168{
3169 ftrace_disabled = 1;
3170 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003171 clear_ftrace_function();
3172}
3173
3174/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003175 * register_ftrace_function - register a function for profiling
3176 * @ops - ops structure that holds the function for profiling.
3177 *
3178 * Register a function to be called by all functions in the
3179 * kernel.
3180 *
3181 * Note: @ops->func and all the functions it calls must be labeled
3182 * with "notrace", otherwise it will go into a
3183 * recursive loop.
3184 */
3185int register_ftrace_function(struct ftrace_ops *ops)
3186{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003187 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003188
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003189 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003190
Steven Rostedt45a4a232011-04-21 23:16:46 -04003191 if (unlikely(ftrace_disabled))
3192 goto out_unlock;
3193
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003194 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003195 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003196
Steven Rostedt45a4a232011-04-21 23:16:46 -04003197 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003198 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003199 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003200}
3201
3202/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003203 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003204 * @ops - ops structure that holds the function to unregister
3205 *
3206 * Unregister a function that was added to be called by ftrace profiling.
3207 */
3208int unregister_ftrace_function(struct ftrace_ops *ops)
3209{
3210 int ret;
3211
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003212 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003213 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003214 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003215 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003216
3217 return ret;
3218}
3219
Ingo Molnare309b412008-05-12 21:20:51 +02003220int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003221ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003222 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003223 loff_t *ppos)
3224{
Steven Rostedt45a4a232011-04-21 23:16:46 -04003225 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003226
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003227 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003228
Steven Rostedt45a4a232011-04-21 23:16:46 -04003229 if (unlikely(ftrace_disabled))
3230 goto out;
3231
3232 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003233
Li Zefana32c7762009-06-26 16:55:51 +08003234 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003235 goto out;
3236
Li Zefana32c7762009-06-26 16:55:51 +08003237 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003238
3239 if (ftrace_enabled) {
3240
3241 ftrace_startup_sysctl();
3242
3243 /* we are starting ftrace again */
3244 if (ftrace_list != &ftrace_list_end) {
3245 if (ftrace_list->next == &ftrace_list_end)
3246 ftrace_trace_function = ftrace_list->func;
3247 else
3248 ftrace_trace_function = ftrace_list_func;
3249 }
3250
3251 } else {
3252 /* stopping ftrace calls (just send to ftrace_stub) */
3253 ftrace_trace_function = ftrace_stub;
3254
3255 ftrace_shutdown_sysctl();
3256 }
3257
3258 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003259 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003260 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003261}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003262
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003263#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003264
Steven Rostedt597af812009-04-03 15:24:12 -04003265static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003266static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003267
Steven Rostedte49dc192008-12-02 23:50:05 -05003268int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3269{
3270 return 0;
3271}
3272
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003273/* The callbacks that hook a function */
3274trace_func_graph_ret_t ftrace_graph_return =
3275 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003276trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003277
3278/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3279static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3280{
3281 int i;
3282 int ret = 0;
3283 unsigned long flags;
3284 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3285 struct task_struct *g, *t;
3286
3287 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3288 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3289 * sizeof(struct ftrace_ret_stack),
3290 GFP_KERNEL);
3291 if (!ret_stack_list[i]) {
3292 start = 0;
3293 end = i;
3294 ret = -ENOMEM;
3295 goto free;
3296 }
3297 }
3298
3299 read_lock_irqsave(&tasklist_lock, flags);
3300 do_each_thread(g, t) {
3301 if (start == end) {
3302 ret = -EAGAIN;
3303 goto unlock;
3304 }
3305
3306 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003307 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003308 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003309 t->curr_ret_stack = -1;
3310 /* Make sure the tasks see the -1 first: */
3311 smp_wmb();
3312 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003313 }
3314 } while_each_thread(g, t);
3315
3316unlock:
3317 read_unlock_irqrestore(&tasklist_lock, flags);
3318free:
3319 for (i = start; i < end; i++)
3320 kfree(ret_stack_list[i]);
3321 return ret;
3322}
3323
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003324static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04003325ftrace_graph_probe_sched_switch(void *ignore,
3326 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003327{
3328 unsigned long long timestamp;
3329 int index;
3330
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003331 /*
3332 * Does the user want to count the time a function was asleep.
3333 * If so, do not update the time stamps.
3334 */
3335 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3336 return;
3337
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003338 timestamp = trace_clock_local();
3339
3340 prev->ftrace_timestamp = timestamp;
3341
3342 /* only process tasks that we timestamped */
3343 if (!next->ftrace_timestamp)
3344 return;
3345
3346 /*
3347 * Update all the counters in next to make up for the
3348 * time next was sleeping.
3349 */
3350 timestamp -= next->ftrace_timestamp;
3351
3352 for (index = next->curr_ret_stack; index >= 0; index--)
3353 next->ret_stack[index].calltime += timestamp;
3354}
3355
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003356/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003357static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003358{
3359 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003360 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003361
3362 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3363 sizeof(struct ftrace_ret_stack *),
3364 GFP_KERNEL);
3365
3366 if (!ret_stack_list)
3367 return -ENOMEM;
3368
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003369 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003370 for_each_online_cpu(cpu) {
3371 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05003372 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04003373 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003374
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003375 do {
3376 ret = alloc_retstack_tasklist(ret_stack_list);
3377 } while (ret == -EAGAIN);
3378
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003379 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04003380 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003381 if (ret)
3382 pr_info("ftrace_graph: Couldn't activate tracepoint"
3383 " probe to kernel_sched_switch\n");
3384 }
3385
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003386 kfree(ret_stack_list);
3387 return ret;
3388}
3389
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003390/*
3391 * Hibernation protection.
3392 * The state of the current task is too much unstable during
3393 * suspend/restore to disk. We want to protect against that.
3394 */
3395static int
3396ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3397 void *unused)
3398{
3399 switch (state) {
3400 case PM_HIBERNATION_PREPARE:
3401 pause_graph_tracing();
3402 break;
3403
3404 case PM_POST_HIBERNATION:
3405 unpause_graph_tracing();
3406 break;
3407 }
3408 return NOTIFY_DONE;
3409}
3410
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003411int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3412 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003413{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003414 int ret = 0;
3415
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003416 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003417
Steven Rostedt05ce5812009-03-24 00:18:31 -04003418 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003419 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003420 ret = -EBUSY;
3421 goto out;
3422 }
3423
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003424 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3425 register_pm_notifier(&ftrace_suspend_notifier);
3426
Steven Rostedt597af812009-04-03 15:24:12 -04003427 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003428 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003429 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003430 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003431 goto out;
3432 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003433
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003434 ftrace_graph_return = retfunc;
3435 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003436
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003437 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003438
3439out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003440 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003441 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003442}
3443
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003444void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003445{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003446 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003447
Steven Rostedt597af812009-04-03 15:24:12 -04003448 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003449 goto out;
3450
Steven Rostedt597af812009-04-03 15:24:12 -04003451 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003452 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003453 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003454 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003455 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04003456 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003457
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003458 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003459 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003460}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003461
Steven Rostedt868baf02011-02-10 21:26:13 -05003462static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
3463
3464static void
3465graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
3466{
3467 atomic_set(&t->tracing_graph_pause, 0);
3468 atomic_set(&t->trace_overrun, 0);
3469 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003470 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05003471 smp_wmb();
3472 t->ret_stack = ret_stack;
3473}
3474
3475/*
3476 * Allocate a return stack for the idle task. May be the first
3477 * time through, or it may be done by CPU hotplug online.
3478 */
3479void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
3480{
3481 t->curr_ret_stack = -1;
3482 /*
3483 * The idle task has no parent, it either has its own
3484 * stack or no stack at all.
3485 */
3486 if (t->ret_stack)
3487 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
3488
3489 if (ftrace_graph_active) {
3490 struct ftrace_ret_stack *ret_stack;
3491
3492 ret_stack = per_cpu(idle_ret_stack, cpu);
3493 if (!ret_stack) {
3494 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3495 * sizeof(struct ftrace_ret_stack),
3496 GFP_KERNEL);
3497 if (!ret_stack)
3498 return;
3499 per_cpu(idle_ret_stack, cpu) = ret_stack;
3500 }
3501 graph_init_task(t, ret_stack);
3502 }
3503}
3504
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003505/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003506void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003507{
Steven Rostedt84047e32009-06-02 16:51:55 -04003508 /* Make sure we do not use the parent ret_stack */
3509 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05003510 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04003511
Steven Rostedt597af812009-04-03 15:24:12 -04003512 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003513 struct ftrace_ret_stack *ret_stack;
3514
3515 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003516 * sizeof(struct ftrace_ret_stack),
3517 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003518 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003519 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05003520 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04003521 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003522}
3523
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003524void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003525{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003526 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3527
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003528 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003529 /* NULL must become visible to IRQs before we free it: */
3530 barrier();
3531
3532 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003533}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003534
3535void ftrace_graph_stop(void)
3536{
3537 ftrace_stop();
3538}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003539#endif