blob: dcd3a814d39b0a258578f15ad9807b38427647a2 [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>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Steven Rostedt4eebcc82008-05-12 21:20:48 +020065/* ftrace_enabled is a method to turn ftrace on or off */
66int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020067static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020068
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050069/* Quick disabling of function tracer. */
70int function_trace_stop;
71
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040072/* List for set_ftrace_pid's pids. */
73LIST_HEAD(ftrace_pids);
74struct ftrace_pid {
75 struct list_head list;
76 struct pid *pid;
77};
78
Steven Rostedt4eebcc82008-05-12 21:20:48 +020079/*
80 * ftrace_disabled is set when an anomaly is discovered.
81 * ftrace_disabled is much stronger than ftrace_enabled.
82 */
83static int ftrace_disabled __read_mostly;
84
Steven Rostedt52baf112009-02-14 01:15:39 -050085static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020086
Paul McQuadebd38c0e2011-05-31 20:51:55 +010087static struct ftrace_ops ftrace_list_end __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -040088 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020089};
90
Steven Rostedtb8489142011-05-04 09:27:52 -040091static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
92static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020093ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt6331c282011-07-13 15:11:02 -040094static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050095ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050096ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -040097static struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020098
Steven Rostedtb8489142011-05-04 09:27:52 -040099static void
100ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
101
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800102/*
Steven Rostedtb8489142011-05-04 09:27:52 -0400103 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800104 * can use rcu_dereference_raw() is that elements removed from this list
105 * are simply leaked, so there is no need to interact with a grace-period
106 * mechanism. The rcu_dereference_raw() calls are needed to handle
Steven Rostedtb8489142011-05-04 09:27:52 -0400107 * concurrent insertions into the ftrace_global_list.
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800108 *
109 * Silly Alpha and silly pointer-speculation compiler optimizations!
110 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400111static void ftrace_global_list_func(unsigned long ip,
112 unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200113{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400114 struct ftrace_ops *op;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200115
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400116 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
117 return;
118
119 trace_recursion_set(TRACE_GLOBAL_BIT);
120 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200121 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200122 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800123 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200124 };
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400125 trace_recursion_clear(TRACE_GLOBAL_BIT);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200126}
127
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500128static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
129{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500130 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500131 return;
132
133 ftrace_pid_function(ip, parent_ip);
134}
135
136static void set_ftrace_pid_function(ftrace_func_t func)
137{
138 /* do not set ftrace_pid_function to itself! */
139 if (func != ftrace_pid_func)
140 ftrace_pid_function = func;
141}
142
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200143/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200144 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200145 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200146 * This NULLs the ftrace function and in essence stops
147 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200148 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200149void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200150{
Steven Rostedt3d083392008-05-12 21:20:42 +0200151 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500152 __ftrace_trace_function = ftrace_stub;
Steven Rostedt6331c282011-07-13 15:11:02 -0400153 __ftrace_trace_function_delay = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500154 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200155}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200156
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500157#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
158/*
159 * For those archs that do not test ftrace_trace_stop in their
160 * mcount call site, we need to do it from C.
161 */
162static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
163{
164 if (function_trace_stop)
165 return;
166
167 __ftrace_trace_function(ip, parent_ip);
168}
169#endif
170
Steven Rostedt2b499382011-05-03 22:49:52 -0400171static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400172{
173 ftrace_func_t func;
174
175 /*
176 * If there's only one function registered, then call that
177 * function directly. Otherwise, we need to iterate over the
178 * registered callers.
179 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400180 if (ftrace_global_list == &ftrace_list_end ||
181 ftrace_global_list->next == &ftrace_list_end)
182 func = ftrace_global_list->func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400183 else
Steven Rostedtb8489142011-05-04 09:27:52 -0400184 func = ftrace_global_list_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400185
186 /* If we filter on pids, update to use the pid function */
187 if (!list_empty(&ftrace_pids)) {
188 set_ftrace_pid_function(func);
189 func = ftrace_pid_func;
190 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400191
192 global_ops.func = func;
193}
194
195static void update_ftrace_function(void)
196{
197 ftrace_func_t func;
198
199 update_global_ops();
200
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400201 /*
202 * If we are at the end of the list and this ops is
203 * not dynamic, then have the mcount trampoline call
204 * the function directly
205 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400206 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400207 (ftrace_ops_list->next == &ftrace_list_end &&
208 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
Steven Rostedtb8489142011-05-04 09:27:52 -0400209 func = ftrace_ops_list->func;
210 else
211 func = ftrace_ops_list_func;
Steven Rostedt2b499382011-05-03 22:49:52 -0400212
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400213#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
214 ftrace_trace_function = func;
215#else
Steven Rostedt6331c282011-07-13 15:11:02 -0400216#ifdef CONFIG_DYNAMIC_FTRACE
217 /* do not update till all functions have been modified */
218 __ftrace_trace_function_delay = func;
219#else
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400220 __ftrace_trace_function = func;
Steven Rostedt6331c282011-07-13 15:11:02 -0400221#endif
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400222 ftrace_trace_function = ftrace_test_stop_func;
223#endif
224}
225
Steven Rostedt2b499382011-05-03 22:49:52 -0400226static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200227{
Steven Rostedt2b499382011-05-03 22:49:52 -0400228 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200229 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400230 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200231 * CPU might be walking that list. We need to make sure
232 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400233 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200234 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400235 rcu_assign_pointer(*list, ops);
236}
Steven Rostedt3d083392008-05-12 21:20:42 +0200237
Steven Rostedt2b499382011-05-03 22:49:52 -0400238static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
239{
240 struct ftrace_ops **p;
241
242 /*
243 * If we are removing the last function, then simply point
244 * to the ftrace_stub.
245 */
246 if (*list == ops && ops->next == &ftrace_list_end) {
247 *list = &ftrace_list_end;
248 return 0;
249 }
250
251 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
252 if (*p == ops)
253 break;
254
255 if (*p != ops)
256 return -1;
257
258 *p = (*p)->next;
259 return 0;
260}
261
262static int __register_ftrace_function(struct ftrace_ops *ops)
263{
264 if (ftrace_disabled)
265 return -ENODEV;
266
267 if (FTRACE_WARN_ON(ops == &global_ops))
268 return -EINVAL;
269
Steven Rostedtb8489142011-05-04 09:27:52 -0400270 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
271 return -EBUSY;
272
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400273 if (!core_kernel_data((unsigned long)ops))
274 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
275
Steven Rostedtb8489142011-05-04 09:27:52 -0400276 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
277 int first = ftrace_global_list == &ftrace_list_end;
278 add_ftrace_ops(&ftrace_global_list, ops);
279 ops->flags |= FTRACE_OPS_FL_ENABLED;
280 if (first)
281 add_ftrace_ops(&ftrace_ops_list, &global_ops);
282 } else
283 add_ftrace_ops(&ftrace_ops_list, ops);
284
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400285 if (ftrace_enabled)
286 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200287
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200288 return 0;
289}
290
Ingo Molnare309b412008-05-12 21:20:51 +0200291static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200292{
Steven Rostedt2b499382011-05-03 22:49:52 -0400293 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200294
Steven Rostedt2b499382011-05-03 22:49:52 -0400295 if (ftrace_disabled)
296 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200297
Steven Rostedtb8489142011-05-04 09:27:52 -0400298 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
299 return -EBUSY;
300
Steven Rostedt2b499382011-05-03 22:49:52 -0400301 if (FTRACE_WARN_ON(ops == &global_ops))
302 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200303
Steven Rostedtb8489142011-05-04 09:27:52 -0400304 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
305 ret = remove_ftrace_ops(&ftrace_global_list, ops);
306 if (!ret && ftrace_global_list == &ftrace_list_end)
307 ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops);
308 if (!ret)
309 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
310 } else
311 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
312
Steven Rostedt2b499382011-05-03 22:49:52 -0400313 if (ret < 0)
314 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400315
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400316 if (ftrace_enabled)
317 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200318
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400319 /*
320 * Dynamic ops may be freed, we must make sure that all
321 * callers are done before leaving this function.
322 */
323 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
324 synchronize_sched();
325
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500326 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200327}
328
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500329static void ftrace_update_pid_func(void)
330{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400331 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500332 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900333 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500334
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400335 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500336}
337
Steven Rostedt493762f2009-03-23 17:12:36 -0400338#ifdef CONFIG_FUNCTION_PROFILER
339struct ftrace_profile {
340 struct hlist_node node;
341 unsigned long ip;
342 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400343#ifdef CONFIG_FUNCTION_GRAPH_TRACER
344 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400345 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400346#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400347};
348
349struct ftrace_profile_page {
350 struct ftrace_profile_page *next;
351 unsigned long index;
352 struct ftrace_profile records[];
353};
354
Steven Rostedtcafb1682009-03-24 20:50:39 -0400355struct ftrace_profile_stat {
356 atomic_t disabled;
357 struct hlist_head *hash;
358 struct ftrace_profile_page *pages;
359 struct ftrace_profile_page *start;
360 struct tracer_stat stat;
361};
362
Steven Rostedt493762f2009-03-23 17:12:36 -0400363#define PROFILE_RECORDS_SIZE \
364 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
365
366#define PROFILES_PER_PAGE \
367 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
368
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400369static int ftrace_profile_bits __read_mostly;
370static int ftrace_profile_enabled __read_mostly;
371
372/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400373static DEFINE_MUTEX(ftrace_profile_lock);
374
Steven Rostedtcafb1682009-03-24 20:50:39 -0400375static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400376
377#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
378
Steven Rostedt493762f2009-03-23 17:12:36 -0400379static void *
380function_stat_next(void *v, int idx)
381{
382 struct ftrace_profile *rec = v;
383 struct ftrace_profile_page *pg;
384
385 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
386
387 again:
Li Zefan0296e422009-06-26 11:15:37 +0800388 if (idx != 0)
389 rec++;
390
Steven Rostedt493762f2009-03-23 17:12:36 -0400391 if ((void *)rec >= (void *)&pg->records[pg->index]) {
392 pg = pg->next;
393 if (!pg)
394 return NULL;
395 rec = &pg->records[0];
396 if (!rec->counter)
397 goto again;
398 }
399
400 return rec;
401}
402
403static void *function_stat_start(struct tracer_stat *trace)
404{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400405 struct ftrace_profile_stat *stat =
406 container_of(trace, struct ftrace_profile_stat, stat);
407
408 if (!stat || !stat->start)
409 return NULL;
410
411 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400412}
413
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400414#ifdef CONFIG_FUNCTION_GRAPH_TRACER
415/* function graph compares on total time */
416static int function_stat_cmp(void *p1, void *p2)
417{
418 struct ftrace_profile *a = p1;
419 struct ftrace_profile *b = p2;
420
421 if (a->time < b->time)
422 return -1;
423 if (a->time > b->time)
424 return 1;
425 else
426 return 0;
427}
428#else
429/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400430static int function_stat_cmp(void *p1, void *p2)
431{
432 struct ftrace_profile *a = p1;
433 struct ftrace_profile *b = p2;
434
435 if (a->counter < b->counter)
436 return -1;
437 if (a->counter > b->counter)
438 return 1;
439 else
440 return 0;
441}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400442#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400443
444static int function_stat_headers(struct seq_file *m)
445{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400446#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400447 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400448 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400449 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400450 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400451#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400452 seq_printf(m, " Function Hit\n"
453 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400454#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400455 return 0;
456}
457
458static int function_stat_show(struct seq_file *m, void *v)
459{
460 struct ftrace_profile *rec = v;
461 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800462 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400463#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400464 static struct trace_seq s;
465 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400466 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400467#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800468 mutex_lock(&ftrace_profile_lock);
469
470 /* we raced with function_profile_reset() */
471 if (unlikely(rec->counter == 0)) {
472 ret = -EBUSY;
473 goto out;
474 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400475
476 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400477 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400478
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400479#ifdef CONFIG_FUNCTION_GRAPH_TRACER
480 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400481 avg = rec->time;
482 do_div(avg, rec->counter);
483
Chase Douglase330b3b2010-04-26 14:02:05 -0400484 /* Sample standard deviation (s^2) */
485 if (rec->counter <= 1)
486 stddev = 0;
487 else {
488 stddev = rec->time_squared - rec->counter * avg * avg;
489 /*
490 * Divide only 1000 for ns^2 -> us^2 conversion.
491 * trace_print_graph_duration will divide 1000 again.
492 */
493 do_div(stddev, (rec->counter - 1) * 1000);
494 }
495
Steven Rostedt34886c82009-03-25 21:00:47 -0400496 trace_seq_init(&s);
497 trace_print_graph_duration(rec->time, &s);
498 trace_seq_puts(&s, " ");
499 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400500 trace_seq_puts(&s, " ");
501 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400502 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400503#endif
504 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800505out:
506 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400507
Li Zefan3aaba202010-08-23 16:50:12 +0800508 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400509}
510
Steven Rostedtcafb1682009-03-24 20:50:39 -0400511static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400512{
513 struct ftrace_profile_page *pg;
514
Steven Rostedtcafb1682009-03-24 20:50:39 -0400515 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400516
517 while (pg) {
518 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
519 pg->index = 0;
520 pg = pg->next;
521 }
522
Steven Rostedtcafb1682009-03-24 20:50:39 -0400523 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400524 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
525}
526
Steven Rostedtcafb1682009-03-24 20:50:39 -0400527int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400528{
529 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400530 int functions;
531 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400532 int i;
533
534 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400535 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400536 return 0;
537
Steven Rostedtcafb1682009-03-24 20:50:39 -0400538 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
539 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400540 return -ENOMEM;
541
Steven Rostedt318e0a72009-03-25 20:06:34 -0400542#ifdef CONFIG_DYNAMIC_FTRACE
543 functions = ftrace_update_tot_cnt;
544#else
545 /*
546 * We do not know the number of functions that exist because
547 * dynamic tracing is what counts them. With past experience
548 * we have around 20K functions. That should be more than enough.
549 * It is highly unlikely we will execute every function in
550 * the kernel.
551 */
552 functions = 20000;
553#endif
554
Steven Rostedtcafb1682009-03-24 20:50:39 -0400555 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400556
Steven Rostedt318e0a72009-03-25 20:06:34 -0400557 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
558
559 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400560 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400561 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400562 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400563 pg = pg->next;
564 }
565
566 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400567
568 out_free:
569 pg = stat->start;
570 while (pg) {
571 unsigned long tmp = (unsigned long)pg;
572
573 pg = pg->next;
574 free_page(tmp);
575 }
576
577 free_page((unsigned long)stat->pages);
578 stat->pages = NULL;
579 stat->start = NULL;
580
581 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400582}
583
Steven Rostedtcafb1682009-03-24 20:50:39 -0400584static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400585{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400586 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400587 int size;
588
Steven Rostedtcafb1682009-03-24 20:50:39 -0400589 stat = &per_cpu(ftrace_profile_stats, cpu);
590
591 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400592 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400593 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400594 return 0;
595 }
596
597 /*
598 * We are profiling all functions, but usually only a few thousand
599 * functions are hit. We'll make a hash of 1024 items.
600 */
601 size = FTRACE_PROFILE_HASH_SIZE;
602
Steven Rostedtcafb1682009-03-24 20:50:39 -0400603 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400604
Steven Rostedtcafb1682009-03-24 20:50:39 -0400605 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400606 return -ENOMEM;
607
Steven Rostedtcafb1682009-03-24 20:50:39 -0400608 if (!ftrace_profile_bits) {
609 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400610
Steven Rostedtcafb1682009-03-24 20:50:39 -0400611 for (; size; size >>= 1)
612 ftrace_profile_bits++;
613 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400614
Steven Rostedt318e0a72009-03-25 20:06:34 -0400615 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400616 if (ftrace_profile_pages_init(stat) < 0) {
617 kfree(stat->hash);
618 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400619 return -ENOMEM;
620 }
621
622 return 0;
623}
624
Steven Rostedtcafb1682009-03-24 20:50:39 -0400625static int ftrace_profile_init(void)
626{
627 int cpu;
628 int ret = 0;
629
630 for_each_online_cpu(cpu) {
631 ret = ftrace_profile_init_cpu(cpu);
632 if (ret)
633 break;
634 }
635
636 return ret;
637}
638
Steven Rostedt493762f2009-03-23 17:12:36 -0400639/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400640static struct ftrace_profile *
641ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400642{
643 struct ftrace_profile *rec;
644 struct hlist_head *hhd;
645 struct hlist_node *n;
646 unsigned long key;
647
648 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400649 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400650
651 if (hlist_empty(hhd))
652 return NULL;
653
654 hlist_for_each_entry_rcu(rec, n, hhd, node) {
655 if (rec->ip == ip)
656 return rec;
657 }
658
659 return NULL;
660}
661
Steven Rostedtcafb1682009-03-24 20:50:39 -0400662static void ftrace_add_profile(struct ftrace_profile_stat *stat,
663 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400664{
665 unsigned long key;
666
667 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400668 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400669}
670
Steven Rostedt318e0a72009-03-25 20:06:34 -0400671/*
672 * The memory is already allocated, this simply finds a new record to use.
673 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400674static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400675ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400676{
677 struct ftrace_profile *rec = NULL;
678
Steven Rostedt318e0a72009-03-25 20:06:34 -0400679 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400680 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400681 goto out;
682
Steven Rostedt493762f2009-03-23 17:12:36 -0400683 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400684 * Try to find the function again since an NMI
685 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400686 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400687 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400688 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400689 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400690
Steven Rostedtcafb1682009-03-24 20:50:39 -0400691 if (stat->pages->index == PROFILES_PER_PAGE) {
692 if (!stat->pages->next)
693 goto out;
694 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400695 }
696
Steven Rostedtcafb1682009-03-24 20:50:39 -0400697 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400698 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400699 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400700
Steven Rostedt493762f2009-03-23 17:12:36 -0400701 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400702 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400703
704 return rec;
705}
706
Steven Rostedt493762f2009-03-23 17:12:36 -0400707static void
708function_profile_call(unsigned long ip, unsigned long parent_ip)
709{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400710 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400711 struct ftrace_profile *rec;
712 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400713
714 if (!ftrace_profile_enabled)
715 return;
716
Steven Rostedt493762f2009-03-23 17:12:36 -0400717 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400718
719 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400720 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400721 goto out;
722
723 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400724 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400725 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400726 if (!rec)
727 goto out;
728 }
729
730 rec->counter++;
731 out:
732 local_irq_restore(flags);
733}
734
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400735#ifdef CONFIG_FUNCTION_GRAPH_TRACER
736static int profile_graph_entry(struct ftrace_graph_ent *trace)
737{
738 function_profile_call(trace->func, 0);
739 return 1;
740}
741
742static void profile_graph_return(struct ftrace_graph_ret *trace)
743{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400744 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400745 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400746 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400747 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400748
749 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400751 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400752 goto out;
753
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400754 /* If the calltime was zero'd ignore it */
755 if (!trace->calltime)
756 goto out;
757
Steven Rostedta2a16d62009-03-24 23:17:58 -0400758 calltime = trace->rettime - trace->calltime;
759
760 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
761 int index;
762
763 index = trace->depth;
764
765 /* Append this call time to the parent time to subtract */
766 if (index)
767 current->ret_stack[index - 1].subtime += calltime;
768
769 if (current->ret_stack[index].subtime < calltime)
770 calltime -= current->ret_stack[index].subtime;
771 else
772 calltime = 0;
773 }
774
Steven Rostedtcafb1682009-03-24 20:50:39 -0400775 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400776 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400777 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400778 rec->time_squared += calltime * calltime;
779 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400780
Steven Rostedtcafb1682009-03-24 20:50:39 -0400781 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400782 local_irq_restore(flags);
783}
784
785static int register_ftrace_profiler(void)
786{
787 return register_ftrace_graph(&profile_graph_return,
788 &profile_graph_entry);
789}
790
791static void unregister_ftrace_profiler(void)
792{
793 unregister_ftrace_graph();
794}
795#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100796static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400797 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400798};
799
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400800static int register_ftrace_profiler(void)
801{
802 return register_ftrace_function(&ftrace_profile_ops);
803}
804
805static void unregister_ftrace_profiler(void)
806{
807 unregister_ftrace_function(&ftrace_profile_ops);
808}
809#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
810
Steven Rostedt493762f2009-03-23 17:12:36 -0400811static ssize_t
812ftrace_profile_write(struct file *filp, const char __user *ubuf,
813 size_t cnt, loff_t *ppos)
814{
815 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400816 int ret;
817
Peter Huewe22fe9b52011-06-07 21:58:27 +0200818 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
819 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400820 return ret;
821
822 val = !!val;
823
824 mutex_lock(&ftrace_profile_lock);
825 if (ftrace_profile_enabled ^ val) {
826 if (val) {
827 ret = ftrace_profile_init();
828 if (ret < 0) {
829 cnt = ret;
830 goto out;
831 }
832
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400833 ret = register_ftrace_profiler();
834 if (ret < 0) {
835 cnt = ret;
836 goto out;
837 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400838 ftrace_profile_enabled = 1;
839 } else {
840 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400841 /*
842 * unregister_ftrace_profiler calls stop_machine
843 * so this acts like an synchronize_sched.
844 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400845 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400846 }
847 }
848 out:
849 mutex_unlock(&ftrace_profile_lock);
850
Jiri Olsacf8517c2009-10-23 19:36:16 -0400851 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400852
853 return cnt;
854}
855
856static ssize_t
857ftrace_profile_read(struct file *filp, char __user *ubuf,
858 size_t cnt, loff_t *ppos)
859{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400860 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400861 int r;
862
863 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
864 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
865}
866
867static const struct file_operations ftrace_profile_fops = {
868 .open = tracing_open_generic,
869 .read = ftrace_profile_read,
870 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200871 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400872};
873
Steven Rostedtcafb1682009-03-24 20:50:39 -0400874/* used to initialize the real stat files */
875static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400876 .name = "functions",
877 .stat_start = function_stat_start,
878 .stat_next = function_stat_next,
879 .stat_cmp = function_stat_cmp,
880 .stat_headers = function_stat_headers,
881 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400882};
883
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400884static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400885{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400886 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400887 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400888 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400889 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400890 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400891
Steven Rostedtcafb1682009-03-24 20:50:39 -0400892 for_each_possible_cpu(cpu) {
893 stat = &per_cpu(ftrace_profile_stats, cpu);
894
895 /* allocate enough for function name + cpu number */
896 name = kmalloc(32, GFP_KERNEL);
897 if (!name) {
898 /*
899 * The files created are permanent, if something happens
900 * we still do not free memory.
901 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400902 WARN(1,
903 "Could not allocate stat file for cpu %d\n",
904 cpu);
905 return;
906 }
907 stat->stat = function_stats;
908 snprintf(name, 32, "function%d", cpu);
909 stat->stat.name = name;
910 ret = register_stat_tracer(&stat->stat);
911 if (ret) {
912 WARN(1,
913 "Could not register function stat for cpu %d\n",
914 cpu);
915 kfree(name);
916 return;
917 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400918 }
919
920 entry = debugfs_create_file("function_profile_enabled", 0644,
921 d_tracer, NULL, &ftrace_profile_fops);
922 if (!entry)
923 pr_warning("Could not create debugfs "
924 "'function_profile_enabled' entry\n");
925}
926
927#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400928static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400929{
930}
931#endif /* CONFIG_FUNCTION_PROFILER */
932
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100933static struct pid * const ftrace_swapper_pid = &init_struct_pid;
934
Steven Rostedt3d083392008-05-12 21:20:42 +0200935#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100936
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400937#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400938# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400939#endif
940
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500941static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
942
Steven Rostedtb6887d72009-02-17 12:32:04 -0500943struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500944 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500945 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500946 unsigned long flags;
947 unsigned long ip;
948 void *data;
949 struct rcu_head rcu;
950};
951
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400952struct ftrace_func_entry {
953 struct hlist_node hlist;
954 unsigned long ip;
955};
956
957struct ftrace_hash {
958 unsigned long size_bits;
959 struct hlist_head *buckets;
960 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -0400961 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400962};
963
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400964/*
965 * We make these constant because no one should touch them,
966 * but they are used as the default "empty hash", to avoid allocating
967 * it all the time. These are in a read only section such that if
968 * anyone does try to modify it, it will cause an exception.
969 */
970static const struct hlist_head empty_buckets[1];
971static const struct ftrace_hash empty_hash = {
972 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400973};
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400974#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +0200975
Steven Rostedt2b499382011-05-03 22:49:52 -0400976static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -0400977 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400978 .notrace_hash = EMPTY_HASH,
979 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -0400980};
981
Steven Rostedt41c52c02008-05-22 11:46:33 -0400982static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200983
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200984struct ftrace_page {
985 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -0500986 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500987 int index;
Steven Rostedta7900872011-12-16 16:23:44 -0500988 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -0700989};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200990
Steven Rostedt85ae32a2011-12-16 16:30:31 -0500991static struct ftrace_page *ftrace_new_pgs;
992
Steven Rostedta7900872011-12-16 16:23:44 -0500993#define ENTRY_SIZE sizeof(struct dyn_ftrace)
994#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200995
996/* estimate from running different kernels */
997#define NR_TO_INIT 10000
998
999static struct ftrace_page *ftrace_pages_start;
1000static struct ftrace_page *ftrace_pages;
1001
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001002static struct ftrace_func_entry *
1003ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1004{
1005 unsigned long key;
1006 struct ftrace_func_entry *entry;
1007 struct hlist_head *hhd;
1008 struct hlist_node *n;
1009
1010 if (!hash->count)
1011 return NULL;
1012
1013 if (hash->size_bits > 0)
1014 key = hash_long(ip, hash->size_bits);
1015 else
1016 key = 0;
1017
1018 hhd = &hash->buckets[key];
1019
1020 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1021 if (entry->ip == ip)
1022 return entry;
1023 }
1024 return NULL;
1025}
1026
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001027static void __add_hash_entry(struct ftrace_hash *hash,
1028 struct ftrace_func_entry *entry)
1029{
1030 struct hlist_head *hhd;
1031 unsigned long key;
1032
1033 if (hash->size_bits)
1034 key = hash_long(entry->ip, hash->size_bits);
1035 else
1036 key = 0;
1037
1038 hhd = &hash->buckets[key];
1039 hlist_add_head(&entry->hlist, hhd);
1040 hash->count++;
1041}
1042
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001043static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1044{
1045 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001046
1047 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1048 if (!entry)
1049 return -ENOMEM;
1050
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001051 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001052 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001053
1054 return 0;
1055}
1056
1057static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001058free_hash_entry(struct ftrace_hash *hash,
1059 struct ftrace_func_entry *entry)
1060{
1061 hlist_del(&entry->hlist);
1062 kfree(entry);
1063 hash->count--;
1064}
1065
1066static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001067remove_hash_entry(struct ftrace_hash *hash,
1068 struct ftrace_func_entry *entry)
1069{
1070 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001071 hash->count--;
1072}
1073
1074static void ftrace_hash_clear(struct ftrace_hash *hash)
1075{
1076 struct hlist_head *hhd;
1077 struct hlist_node *tp, *tn;
1078 struct ftrace_func_entry *entry;
1079 int size = 1 << hash->size_bits;
1080 int i;
1081
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001082 if (!hash->count)
1083 return;
1084
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001085 for (i = 0; i < size; i++) {
1086 hhd = &hash->buckets[i];
1087 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001088 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001089 }
1090 FTRACE_WARN_ON(hash->count);
1091}
1092
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001093static void free_ftrace_hash(struct ftrace_hash *hash)
1094{
1095 if (!hash || hash == EMPTY_HASH)
1096 return;
1097 ftrace_hash_clear(hash);
1098 kfree(hash->buckets);
1099 kfree(hash);
1100}
1101
Steven Rostedt07fd5512011-05-05 18:03:47 -04001102static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1103{
1104 struct ftrace_hash *hash;
1105
1106 hash = container_of(rcu, struct ftrace_hash, rcu);
1107 free_ftrace_hash(hash);
1108}
1109
1110static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1111{
1112 if (!hash || hash == EMPTY_HASH)
1113 return;
1114 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1115}
1116
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001117static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1118{
1119 struct ftrace_hash *hash;
1120 int size;
1121
1122 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1123 if (!hash)
1124 return NULL;
1125
1126 size = 1 << size_bits;
1127 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1128
1129 if (!hash->buckets) {
1130 kfree(hash);
1131 return NULL;
1132 }
1133
1134 hash->size_bits = size_bits;
1135
1136 return hash;
1137}
1138
1139static struct ftrace_hash *
1140alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1141{
1142 struct ftrace_func_entry *entry;
1143 struct ftrace_hash *new_hash;
1144 struct hlist_node *tp;
1145 int size;
1146 int ret;
1147 int i;
1148
1149 new_hash = alloc_ftrace_hash(size_bits);
1150 if (!new_hash)
1151 return NULL;
1152
1153 /* Empty hash? */
1154 if (!hash || !hash->count)
1155 return new_hash;
1156
1157 size = 1 << hash->size_bits;
1158 for (i = 0; i < size; i++) {
1159 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1160 ret = add_hash_entry(new_hash, entry->ip);
1161 if (ret < 0)
1162 goto free_hash;
1163 }
1164 }
1165
1166 FTRACE_WARN_ON(new_hash->count != hash->count);
1167
1168 return new_hash;
1169
1170 free_hash:
1171 free_ftrace_hash(new_hash);
1172 return NULL;
1173}
1174
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001175static void
1176ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1177static void
1178ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1179
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001180static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001181ftrace_hash_move(struct ftrace_ops *ops, int enable,
1182 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001183{
1184 struct ftrace_func_entry *entry;
1185 struct hlist_node *tp, *tn;
1186 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001187 struct ftrace_hash *old_hash;
1188 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001189 unsigned long key;
1190 int size = src->count;
1191 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001192 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001193 int i;
1194
1195 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001196 * Remove the current set, update the hash and add
1197 * them back.
1198 */
1199 ftrace_hash_rec_disable(ops, enable);
1200
1201 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001202 * If the new source is empty, just free dst and assign it
1203 * the empty_hash.
1204 */
1205 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001206 free_ftrace_hash_rcu(*dst);
1207 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001208 /* still need to update the function records */
1209 ret = 0;
1210 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001211 }
1212
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001213 /*
1214 * Make the hash size about 1/2 the # found
1215 */
1216 for (size /= 2; size; size >>= 1)
1217 bits++;
1218
1219 /* Don't allocate too much */
1220 if (bits > FTRACE_HASH_MAX_BITS)
1221 bits = FTRACE_HASH_MAX_BITS;
1222
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001223 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001224 new_hash = alloc_ftrace_hash(bits);
1225 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001226 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001227
1228 size = 1 << src->size_bits;
1229 for (i = 0; i < size; i++) {
1230 hhd = &src->buckets[i];
1231 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1232 if (bits > 0)
1233 key = hash_long(entry->ip, bits);
1234 else
1235 key = 0;
1236 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001237 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001238 }
1239 }
1240
Steven Rostedt07fd5512011-05-05 18:03:47 -04001241 old_hash = *dst;
1242 rcu_assign_pointer(*dst, new_hash);
1243 free_ftrace_hash_rcu(old_hash);
1244
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001245 ret = 0;
1246 out:
1247 /*
1248 * Enable regardless of ret:
1249 * On success, we enable the new hash.
1250 * On failure, we re-enable the original hash.
1251 */
1252 ftrace_hash_rec_enable(ops, enable);
1253
1254 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001255}
1256
Steven Rostedt265c8312009-02-13 12:43:56 -05001257/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001258 * Test the hashes for this ops to see if we want to call
1259 * the ops->func or not.
1260 *
1261 * It's a match if the ip is in the ops->filter_hash or
1262 * the filter_hash does not exist or is empty,
1263 * AND
1264 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001265 *
1266 * This needs to be called with preemption disabled as
1267 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001268 */
1269static int
1270ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1271{
1272 struct ftrace_hash *filter_hash;
1273 struct ftrace_hash *notrace_hash;
1274 int ret;
1275
Steven Rostedtb8489142011-05-04 09:27:52 -04001276 filter_hash = rcu_dereference_raw(ops->filter_hash);
1277 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1278
1279 if ((!filter_hash || !filter_hash->count ||
1280 ftrace_lookup_ip(filter_hash, ip)) &&
1281 (!notrace_hash || !notrace_hash->count ||
1282 !ftrace_lookup_ip(notrace_hash, ip)))
1283 ret = 1;
1284 else
1285 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001286
1287 return ret;
1288}
1289
1290/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001291 * This is a double for. Do not use 'break' to break out of the loop,
1292 * you must use a goto.
1293 */
1294#define do_for_each_ftrace_rec(pg, rec) \
1295 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1296 int _____i; \
1297 for (_____i = 0; _____i < pg->index; _____i++) { \
1298 rec = &pg->records[_____i];
1299
1300#define while_for_each_ftrace_rec() \
1301 } \
1302 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301303
Steven Rostedt5855fea2011-12-16 19:27:42 -05001304
1305static int ftrace_cmp_recs(const void *a, const void *b)
1306{
1307 const struct dyn_ftrace *reca = a;
1308 const struct dyn_ftrace *recb = b;
1309
1310 if (reca->ip > recb->ip)
1311 return 1;
1312 if (reca->ip < recb->ip)
1313 return -1;
1314 return 0;
1315}
1316
Steven Rostedtc88fd862011-08-16 09:53:39 -04001317/**
1318 * ftrace_location - return true if the ip giving is a traced location
1319 * @ip: the instruction pointer to check
1320 *
1321 * Returns 1 if @ip given is a pointer to a ftrace location.
1322 * That is, the instruction that is either a NOP or call to
1323 * the function tracer. It checks the ftrace internal tables to
1324 * determine if the address belongs or not.
1325 */
1326int ftrace_location(unsigned long ip)
1327{
1328 struct ftrace_page *pg;
1329 struct dyn_ftrace *rec;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001330 struct dyn_ftrace key;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001331
Steven Rostedt5855fea2011-12-16 19:27:42 -05001332 key.ip = ip;
1333
1334 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1335 rec = bsearch(&key, pg->records, pg->index,
1336 sizeof(struct dyn_ftrace),
1337 ftrace_cmp_recs);
1338 if (rec)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001339 return 1;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001340 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001341
1342 return 0;
1343}
1344
Steven Rostedted926f92011-05-03 13:25:24 -04001345static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1346 int filter_hash,
1347 bool inc)
1348{
1349 struct ftrace_hash *hash;
1350 struct ftrace_hash *other_hash;
1351 struct ftrace_page *pg;
1352 struct dyn_ftrace *rec;
1353 int count = 0;
1354 int all = 0;
1355
1356 /* Only update if the ops has been registered */
1357 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1358 return;
1359
1360 /*
1361 * In the filter_hash case:
1362 * If the count is zero, we update all records.
1363 * Otherwise we just update the items in the hash.
1364 *
1365 * In the notrace_hash case:
1366 * We enable the update in the hash.
1367 * As disabling notrace means enabling the tracing,
1368 * and enabling notrace means disabling, the inc variable
1369 * gets inversed.
1370 */
1371 if (filter_hash) {
1372 hash = ops->filter_hash;
1373 other_hash = ops->notrace_hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001374 if (!hash || !hash->count)
Steven Rostedted926f92011-05-03 13:25:24 -04001375 all = 1;
1376 } else {
1377 inc = !inc;
1378 hash = ops->notrace_hash;
1379 other_hash = ops->filter_hash;
1380 /*
1381 * If the notrace hash has no items,
1382 * then there's nothing to do.
1383 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001384 if (hash && !hash->count)
Steven Rostedted926f92011-05-03 13:25:24 -04001385 return;
1386 }
1387
1388 do_for_each_ftrace_rec(pg, rec) {
1389 int in_other_hash = 0;
1390 int in_hash = 0;
1391 int match = 0;
1392
1393 if (all) {
1394 /*
1395 * Only the filter_hash affects all records.
1396 * Update if the record is not in the notrace hash.
1397 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001398 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001399 match = 1;
1400 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -04001401 in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip);
1402 in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001403
1404 /*
1405 *
1406 */
1407 if (filter_hash && in_hash && !in_other_hash)
1408 match = 1;
1409 else if (!filter_hash && in_hash &&
1410 (in_other_hash || !other_hash->count))
1411 match = 1;
1412 }
1413 if (!match)
1414 continue;
1415
1416 if (inc) {
1417 rec->flags++;
1418 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1419 return;
1420 } else {
1421 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1422 return;
1423 rec->flags--;
1424 }
1425 count++;
1426 /* Shortcut, if we handled all records, we are done. */
1427 if (!all && count == hash->count)
1428 return;
1429 } while_for_each_ftrace_rec();
1430}
1431
1432static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1433 int filter_hash)
1434{
1435 __ftrace_hash_rec_update(ops, filter_hash, 0);
1436}
1437
1438static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1439 int filter_hash)
1440{
1441 __ftrace_hash_rec_update(ops, filter_hash, 1);
1442}
1443
Ingo Molnare309b412008-05-12 21:20:51 +02001444static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001445{
Steven Rostedta7900872011-12-16 16:23:44 -05001446 if (ftrace_pages->index == ftrace_pages->size) {
1447 /* We should have allocated enough */
1448 if (WARN_ON(!ftrace_pages->next))
1449 return NULL;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001450 ftrace_pages = ftrace_pages->next;
1451 }
1452
1453 return &ftrace_pages->records[ftrace_pages->index++];
1454}
1455
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001456static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001457ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001458{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001459 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001460
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001461 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001462 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001463
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001464 rec = ftrace_alloc_dyn_node(ip);
1465 if (!rec)
1466 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001467
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001468 rec->ip = ip;
Steven Rostedt3d083392008-05-12 21:20:42 +02001469
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001470 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001471}
1472
Steven Rostedt05736a42008-09-22 14:55:47 -07001473static void print_ip_ins(const char *fmt, unsigned char *p)
1474{
1475 int i;
1476
1477 printk(KERN_CONT "%s", fmt);
1478
1479 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1480 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1481}
1482
Steven Rostedtc88fd862011-08-16 09:53:39 -04001483/**
1484 * ftrace_bug - report and shutdown function tracer
1485 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1486 * @ip: The address that failed
1487 *
1488 * The arch code that enables or disables the function tracing
1489 * can call ftrace_bug() when it has detected a problem in
1490 * modifying the code. @failed should be one of either:
1491 * EFAULT - if the problem happens on reading the @ip address
1492 * EINVAL - if what is read at @ip is not what was expected
1493 * EPERM - if the problem happens on writting to the @ip address
1494 */
1495void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001496{
1497 switch (failed) {
1498 case -EFAULT:
1499 FTRACE_WARN_ON_ONCE(1);
1500 pr_info("ftrace faulted on modifying ");
1501 print_ip_sym(ip);
1502 break;
1503 case -EINVAL:
1504 FTRACE_WARN_ON_ONCE(1);
1505 pr_info("ftrace failed to modify ");
1506 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001507 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001508 printk(KERN_CONT "\n");
1509 break;
1510 case -EPERM:
1511 FTRACE_WARN_ON_ONCE(1);
1512 pr_info("ftrace faulted on writing ");
1513 print_ip_sym(ip);
1514 break;
1515 default:
1516 FTRACE_WARN_ON_ONCE(1);
1517 pr_info("ftrace faulted on unknown error ");
1518 print_ip_sym(ip);
1519 }
1520}
1521
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001522
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001523/* Return 1 if the address range is reserved for ftrace */
1524int ftrace_text_reserved(void *start, void *end)
1525{
1526 struct dyn_ftrace *rec;
1527 struct ftrace_page *pg;
1528
1529 do_for_each_ftrace_rec(pg, rec) {
1530 if (rec->ip <= (unsigned long)end &&
1531 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1532 return 1;
1533 } while_for_each_ftrace_rec();
1534 return 0;
1535}
1536
Steven Rostedtc88fd862011-08-16 09:53:39 -04001537static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001538{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001539 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001540
Steven Rostedt982c3502008-11-15 16:31:41 -05001541 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001542 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001543 *
Steven Rostedted926f92011-05-03 13:25:24 -04001544 * If the record has a ref count, then we need to enable it
1545 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001546 *
Steven Rostedted926f92011-05-03 13:25:24 -04001547 * Otherwise we make sure its disabled.
1548 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001549 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001550 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001551 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001552 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001553 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001554
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001555 /* If the state of this record hasn't changed, then do nothing */
1556 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001557 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001558
1559 if (flag) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001560 if (update)
1561 rec->flags |= FTRACE_FL_ENABLED;
1562 return FTRACE_UPDATE_MAKE_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001563 }
1564
Steven Rostedtc88fd862011-08-16 09:53:39 -04001565 if (update)
1566 rec->flags &= ~FTRACE_FL_ENABLED;
1567
1568 return FTRACE_UPDATE_MAKE_NOP;
1569}
1570
1571/**
1572 * ftrace_update_record, set a record that now is tracing or not
1573 * @rec: the record to update
1574 * @enable: set to 1 if the record is tracing, zero to force disable
1575 *
1576 * The records that represent all functions that can be traced need
1577 * to be updated when tracing has been enabled.
1578 */
1579int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1580{
1581 return ftrace_check_record(rec, enable, 1);
1582}
1583
1584/**
1585 * ftrace_test_record, check if the record has been enabled or not
1586 * @rec: the record to test
1587 * @enable: set to 1 to check if enabled, 0 if it is disabled
1588 *
1589 * The arch code may need to test if a record is already set to
1590 * tracing to determine how to modify the function code that it
1591 * represents.
1592 */
1593int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1594{
1595 return ftrace_check_record(rec, enable, 0);
1596}
1597
1598static int
1599__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1600{
1601 unsigned long ftrace_addr;
1602 int ret;
1603
1604 ftrace_addr = (unsigned long)FTRACE_ADDR;
1605
1606 ret = ftrace_update_record(rec, enable);
1607
1608 switch (ret) {
1609 case FTRACE_UPDATE_IGNORE:
1610 return 0;
1611
1612 case FTRACE_UPDATE_MAKE_CALL:
1613 return ftrace_make_call(rec, ftrace_addr);
1614
1615 case FTRACE_UPDATE_MAKE_NOP:
1616 return ftrace_make_nop(NULL, rec, ftrace_addr);
1617 }
1618
1619 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001620}
1621
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001622static void ftrace_replace_code(int update)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001623{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001624 struct dyn_ftrace *rec;
1625 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001626 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001627
Steven Rostedt45a4a232011-04-21 23:16:46 -04001628 if (unlikely(ftrace_disabled))
1629 return;
1630
Steven Rostedt265c8312009-02-13 12:43:56 -05001631 do_for_each_ftrace_rec(pg, rec) {
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001632 failed = __ftrace_replace_code(rec, update);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001633 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001634 ftrace_bug(failed, rec->ip);
1635 /* Stop processing */
1636 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001637 }
1638 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001639}
1640
Steven Rostedtc88fd862011-08-16 09:53:39 -04001641struct ftrace_rec_iter {
1642 struct ftrace_page *pg;
1643 int index;
1644};
1645
1646/**
1647 * ftrace_rec_iter_start, start up iterating over traced functions
1648 *
1649 * Returns an iterator handle that is used to iterate over all
1650 * the records that represent address locations where functions
1651 * are traced.
1652 *
1653 * May return NULL if no records are available.
1654 */
1655struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1656{
1657 /*
1658 * We only use a single iterator.
1659 * Protected by the ftrace_lock mutex.
1660 */
1661 static struct ftrace_rec_iter ftrace_rec_iter;
1662 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1663
1664 iter->pg = ftrace_pages_start;
1665 iter->index = 0;
1666
1667 /* Could have empty pages */
1668 while (iter->pg && !iter->pg->index)
1669 iter->pg = iter->pg->next;
1670
1671 if (!iter->pg)
1672 return NULL;
1673
1674 return iter;
1675}
1676
1677/**
1678 * ftrace_rec_iter_next, get the next record to process.
1679 * @iter: The handle to the iterator.
1680 *
1681 * Returns the next iterator after the given iterator @iter.
1682 */
1683struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1684{
1685 iter->index++;
1686
1687 if (iter->index >= iter->pg->index) {
1688 iter->pg = iter->pg->next;
1689 iter->index = 0;
1690
1691 /* Could have empty pages */
1692 while (iter->pg && !iter->pg->index)
1693 iter->pg = iter->pg->next;
1694 }
1695
1696 if (!iter->pg)
1697 return NULL;
1698
1699 return iter;
1700}
1701
1702/**
1703 * ftrace_rec_iter_record, get the record at the iterator location
1704 * @iter: The current iterator location
1705 *
1706 * Returns the record that the current @iter is at.
1707 */
1708struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1709{
1710 return &iter->pg->records[iter->index];
1711}
1712
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301713static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001714ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001715{
1716 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001717 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001718
1719 ip = rec->ip;
1720
Steven Rostedt45a4a232011-04-21 23:16:46 -04001721 if (unlikely(ftrace_disabled))
1722 return 0;
1723
Shaohua Li25aac9d2009-01-09 11:29:40 +08001724 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001725 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001726 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301727 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001728 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301729 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001730}
1731
Steven Rostedt000ab692009-02-17 13:35:06 -05001732/*
1733 * archs can override this function if they must do something
1734 * before the modifying code is performed.
1735 */
1736int __weak ftrace_arch_code_modify_prepare(void)
1737{
1738 return 0;
1739}
1740
1741/*
1742 * archs can override this function if they must do something
1743 * after the modifying code is performed.
1744 */
1745int __weak ftrace_arch_code_modify_post_process(void)
1746{
1747 return 0;
1748}
1749
Ingo Molnare309b412008-05-12 21:20:51 +02001750static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001751{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001752 int *command = data;
1753
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001754 if (*command & FTRACE_UPDATE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001755 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001756 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001757 ftrace_replace_code(0);
1758
1759 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1760 ftrace_update_ftrace_func(ftrace_trace_function);
1761
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001762 if (*command & FTRACE_START_FUNC_RET)
1763 ftrace_enable_ftrace_graph_caller();
1764 else if (*command & FTRACE_STOP_FUNC_RET)
1765 ftrace_disable_ftrace_graph_caller();
1766
Steven Rostedtc88fd862011-08-16 09:53:39 -04001767 return 0;
1768}
1769
1770/**
1771 * ftrace_run_stop_machine, go back to the stop machine method
1772 * @command: The command to tell ftrace what to do
1773 *
1774 * If an arch needs to fall back to the stop machine method, the
1775 * it can call this function.
1776 */
1777void ftrace_run_stop_machine(int command)
1778{
1779 stop_machine(__ftrace_modify_code, &command, NULL);
1780}
1781
1782/**
1783 * arch_ftrace_update_code, modify the code to trace or not trace
1784 * @command: The command that needs to be done
1785 *
1786 * Archs can override this function if it does not need to
1787 * run stop_machine() to modify code.
1788 */
1789void __weak arch_ftrace_update_code(int command)
1790{
1791 ftrace_run_stop_machine(command);
1792}
1793
1794static void ftrace_run_update_code(int command)
1795{
1796 int ret;
1797
1798 ret = ftrace_arch_code_modify_prepare();
1799 FTRACE_WARN_ON(ret);
1800 if (ret)
1801 return;
1802 /*
1803 * Do not call function tracer while we update the code.
1804 * We are in stop machine.
1805 */
1806 function_trace_stop++;
1807
1808 /*
1809 * By default we use stop_machine() to modify the code.
1810 * But archs can do what ever they want as long as it
1811 * is safe. The stop_machine() is the safest, but also
1812 * produces the most overhead.
1813 */
1814 arch_ftrace_update_code(command);
1815
Steven Rostedt6331c282011-07-13 15:11:02 -04001816#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1817 /*
1818 * For archs that call ftrace_test_stop_func(), we must
1819 * wait till after we update all the function callers
1820 * before we update the callback. This keeps different
1821 * ops that record different functions from corrupting
1822 * each other.
1823 */
1824 __ftrace_trace_function = __ftrace_trace_function_delay;
1825#endif
1826 function_trace_stop--;
1827
Steven Rostedt000ab692009-02-17 13:35:06 -05001828 ret = ftrace_arch_code_modify_post_process();
1829 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001830}
1831
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001832static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001833static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001834static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001835
1836static void ftrace_startup_enable(int command)
1837{
1838 if (saved_ftrace_func != ftrace_trace_function) {
1839 saved_ftrace_func = ftrace_trace_function;
1840 command |= FTRACE_UPDATE_TRACE_FUNC;
1841 }
1842
1843 if (!command || !ftrace_enabled)
1844 return;
1845
1846 ftrace_run_update_code(command);
1847}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001848
Steven Rostedta1cd6172011-05-23 15:24:25 -04001849static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001850{
Steven Rostedtb8489142011-05-04 09:27:52 -04001851 bool hash_enable = true;
1852
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001853 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001854 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001855
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001856 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001857 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001858
Steven Rostedtb8489142011-05-04 09:27:52 -04001859 /* ops marked global share the filter hashes */
1860 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1861 ops = &global_ops;
1862 /* Don't update hash if global is already set */
1863 if (global_start_up)
1864 hash_enable = false;
1865 global_start_up++;
1866 }
1867
Steven Rostedted926f92011-05-03 13:25:24 -04001868 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001869 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001870 ftrace_hash_rec_enable(ops, 1);
1871
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001872 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001873
1874 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001875}
1876
Steven Rostedtbd69c302011-05-03 21:55:54 -04001877static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001878{
Steven Rostedtb8489142011-05-04 09:27:52 -04001879 bool hash_disable = true;
1880
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001881 if (unlikely(ftrace_disabled))
1882 return;
1883
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001884 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001885 /*
1886 * Just warn in case of unbalance, no need to kill ftrace, it's not
1887 * critical but the ftrace_call callers may be never nopped again after
1888 * further ftrace uses.
1889 */
1890 WARN_ON_ONCE(ftrace_start_up < 0);
1891
Steven Rostedtb8489142011-05-04 09:27:52 -04001892 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1893 ops = &global_ops;
1894 global_start_up--;
1895 WARN_ON_ONCE(global_start_up < 0);
1896 /* Don't update hash if global still has users */
1897 if (global_start_up) {
1898 WARN_ON_ONCE(!ftrace_start_up);
1899 hash_disable = false;
1900 }
1901 }
1902
1903 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04001904 ftrace_hash_rec_disable(ops, 1);
1905
Steven Rostedtb8489142011-05-04 09:27:52 -04001906 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04001907 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001908
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001909 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001910
1911 if (saved_ftrace_func != ftrace_trace_function) {
1912 saved_ftrace_func = ftrace_trace_function;
1913 command |= FTRACE_UPDATE_TRACE_FUNC;
1914 }
1915
1916 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001917 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001918
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001919 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001920}
1921
Ingo Molnare309b412008-05-12 21:20:51 +02001922static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001923{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001924 if (unlikely(ftrace_disabled))
1925 return;
1926
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001927 /* Force update next time */
1928 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001929 /* ftrace_start_up is true if we want ftrace running */
1930 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001931 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001932}
1933
Ingo Molnare309b412008-05-12 21:20:51 +02001934static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001935{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001936 if (unlikely(ftrace_disabled))
1937 return;
1938
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001939 /* ftrace_start_up is true if ftrace is running */
1940 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001941 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001942}
1943
Steven Rostedt3d083392008-05-12 21:20:42 +02001944static cycle_t ftrace_update_time;
1945static unsigned long ftrace_update_cnt;
1946unsigned long ftrace_update_tot_cnt;
1947
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001948static int ops_traces_mod(struct ftrace_ops *ops)
1949{
1950 struct ftrace_hash *hash;
1951
1952 hash = ops->filter_hash;
1953 return !!(!hash || !hash->count);
1954}
1955
Steven Rostedt31e88902008-11-14 16:21:19 -08001956static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001957{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001958 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001959 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301960 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001961 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001962 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001963
1964 /*
1965 * When adding a module, we need to check if tracers are
1966 * currently enabled and if they are set to trace all functions.
1967 * If they are, we need to enable the module functions as well
1968 * as update the reference counts for those function records.
1969 */
1970 if (mod) {
1971 struct ftrace_ops *ops;
1972
1973 for (ops = ftrace_ops_list;
1974 ops != &ftrace_list_end; ops = ops->next) {
1975 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
1976 ops_traces_mod(ops))
1977 ref++;
1978 }
1979 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001980
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001981 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001982 ftrace_update_cnt = 0;
1983
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001984 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301985
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001986 for (i = 0; i < pg->index; i++) {
1987 /* If something went wrong, bail without enabling anything */
1988 if (unlikely(ftrace_disabled))
1989 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001990
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001991 p = &pg->records[i];
1992 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301993
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001994 /*
1995 * Do the initial record conversion from mcount jump
1996 * to the NOP instructions.
1997 */
1998 if (!ftrace_code_disable(mod, p))
1999 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002000
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002001 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002002
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002003 /*
2004 * If the tracing is enabled, go ahead and enable the record.
2005 *
2006 * The reason not to enable the record immediatelly is the
2007 * inherent check of ftrace_make_nop/ftrace_make_call for
2008 * correct previous instructions. Making first the NOP
2009 * conversion puts the module to the correct state, thus
2010 * passing the ftrace_make_call check.
2011 */
2012 if (ftrace_start_up && ref) {
2013 int failed = __ftrace_replace_code(p, 1);
2014 if (failed)
2015 ftrace_bug(failed, p->ip);
2016 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002017 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002018 }
2019
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002020 ftrace_new_pgs = NULL;
2021
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002022 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002023 ftrace_update_time = stop - start;
2024 ftrace_update_tot_cnt += ftrace_update_cnt;
2025
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002026 return 0;
2027}
2028
Steven Rostedta7900872011-12-16 16:23:44 -05002029static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002030{
Steven Rostedta7900872011-12-16 16:23:44 -05002031 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002032 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002033
Steven Rostedta7900872011-12-16 16:23:44 -05002034 if (WARN_ON(!count))
2035 return -EINVAL;
2036
2037 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002038
2039 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002040 * We want to fill as much as possible. No more than a page
2041 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002042 */
Steven Rostedta7900872011-12-16 16:23:44 -05002043 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2044 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002045
Steven Rostedta7900872011-12-16 16:23:44 -05002046 again:
2047 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2048
2049 if (!pg->records) {
2050 /* if we can't allocate this size, try something smaller */
2051 if (!order)
2052 return -ENOMEM;
2053 order >>= 1;
2054 goto again;
2055 }
2056
2057 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2058 pg->size = cnt;
2059
2060 if (cnt > count)
2061 cnt = count;
2062
2063 return cnt;
2064}
2065
2066static struct ftrace_page *
2067ftrace_allocate_pages(unsigned long num_to_init)
2068{
2069 struct ftrace_page *start_pg;
2070 struct ftrace_page *pg;
2071 int order;
2072 int cnt;
2073
2074 if (!num_to_init)
2075 return 0;
2076
2077 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2078 if (!pg)
2079 return NULL;
2080
2081 /*
2082 * Try to allocate as much as possible in one continues
2083 * location that fills in all of the space. We want to
2084 * waste as little space as possible.
2085 */
2086 for (;;) {
2087 cnt = ftrace_allocate_records(pg, num_to_init);
2088 if (cnt < 0)
2089 goto free_pages;
2090
2091 num_to_init -= cnt;
2092 if (!num_to_init)
2093 break;
2094
2095 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2096 if (!pg->next)
2097 goto free_pages;
2098
2099 pg = pg->next;
2100 }
2101
2102 return start_pg;
2103
2104 free_pages:
2105 while (start_pg) {
2106 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2107 free_pages((unsigned long)pg->records, order);
2108 start_pg = pg->next;
2109 kfree(pg);
2110 pg = start_pg;
2111 }
2112 pr_info("ftrace: FAILED to allocate memory for functions\n");
2113 return NULL;
2114}
2115
2116static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2117{
2118 int cnt;
2119
2120 if (!num_to_init) {
2121 pr_info("ftrace: No functions to be traced?\n");
2122 return -1;
2123 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002124
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002125 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002126 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002127 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002128
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002129 return 0;
2130}
2131
Steven Rostedt5072c592008-05-12 21:20:43 +02002132enum {
2133 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002134 FTRACE_ITER_NOTRACE = (1 << 1),
Steven Rostedt3499e462011-04-21 22:59:12 -04002135 FTRACE_ITER_PRINTALL = (1 << 2),
2136 FTRACE_ITER_HASH = (1 << 3),
Steven Rostedt647bcd02011-05-03 14:39:21 -04002137 FTRACE_ITER_ENABLED = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02002138};
2139
2140#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2141
2142struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002143 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002144 loff_t func_pos;
2145 struct ftrace_page *pg;
2146 struct dyn_ftrace *func;
2147 struct ftrace_func_probe *probe;
2148 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002149 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002150 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002151 int hidx;
2152 int idx;
2153 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002154};
2155
Ingo Molnare309b412008-05-12 21:20:51 +02002156static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002157t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002158{
2159 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002160 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002161 struct hlist_head *hhd;
2162
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002163 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002164 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002165
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002166 if (iter->probe)
2167 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002168 retry:
2169 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2170 return NULL;
2171
2172 hhd = &ftrace_func_hash[iter->hidx];
2173
2174 if (hlist_empty(hhd)) {
2175 iter->hidx++;
2176 hnd = NULL;
2177 goto retry;
2178 }
2179
2180 if (!hnd)
2181 hnd = hhd->first;
2182 else {
2183 hnd = hnd->next;
2184 if (!hnd) {
2185 iter->hidx++;
2186 goto retry;
2187 }
2188 }
2189
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002190 if (WARN_ON_ONCE(!hnd))
2191 return NULL;
2192
2193 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2194
2195 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002196}
2197
2198static void *t_hash_start(struct seq_file *m, loff_t *pos)
2199{
2200 struct ftrace_iterator *iter = m->private;
2201 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002202 loff_t l;
2203
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002204 if (iter->func_pos > *pos)
2205 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002206
Li Zefand82d6242009-06-24 09:54:54 +08002207 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002208 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002209 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002210 if (!p)
2211 break;
2212 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002213 if (!p)
2214 return NULL;
2215
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002216 /* Only set this if we have an item */
2217 iter->flags |= FTRACE_ITER_HASH;
2218
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002219 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002220}
2221
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002222static int
2223t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002224{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002225 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002226
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002227 rec = iter->probe;
2228 if (WARN_ON_ONCE(!rec))
2229 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002230
Steven Rostedt809dcf22009-02-16 23:06:01 -05002231 if (rec->ops->print)
2232 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2233
Steven Rostedtb375a112009-09-17 00:05:58 -04002234 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002235
2236 if (rec->data)
2237 seq_printf(m, ":%p", rec->data);
2238 seq_putc(m, '\n');
2239
2240 return 0;
2241}
2242
2243static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002244t_next(struct seq_file *m, void *v, loff_t *pos)
2245{
2246 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002247 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002248 struct dyn_ftrace *rec = NULL;
2249
Steven Rostedt45a4a232011-04-21 23:16:46 -04002250 if (unlikely(ftrace_disabled))
2251 return NULL;
2252
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002253 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002254 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002255
Steven Rostedt5072c592008-05-12 21:20:43 +02002256 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002257 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002258
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002259 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002260 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002261
Steven Rostedt5072c592008-05-12 21:20:43 +02002262 retry:
2263 if (iter->idx >= iter->pg->index) {
2264 if (iter->pg->next) {
2265 iter->pg = iter->pg->next;
2266 iter->idx = 0;
2267 goto retry;
2268 }
2269 } else {
2270 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002271 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002272 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002273
Steven Rostedt41c52c02008-05-22 11:46:33 -04002274 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002275 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2276
2277 ((iter->flags & FTRACE_ITER_ENABLED) &&
2278 !(rec->flags & ~FTRACE_FL_MASK))) {
2279
Steven Rostedt5072c592008-05-12 21:20:43 +02002280 rec = NULL;
2281 goto retry;
2282 }
2283 }
2284
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002285 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002286 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002287
2288 iter->func = rec;
2289
2290 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002291}
2292
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002293static void reset_iter_read(struct ftrace_iterator *iter)
2294{
2295 iter->pos = 0;
2296 iter->func_pos = 0;
2297 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002298}
2299
2300static void *t_start(struct seq_file *m, loff_t *pos)
2301{
2302 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002303 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002304 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002305 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002306
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002307 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002308
2309 if (unlikely(ftrace_disabled))
2310 return NULL;
2311
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002312 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002313 * If an lseek was done, then reset and start from beginning.
2314 */
2315 if (*pos < iter->pos)
2316 reset_iter_read(iter);
2317
2318 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002319 * For set_ftrace_filter reading, if we have the filter
2320 * off, we can short cut and just print out that all
2321 * functions are enabled.
2322 */
Steven Rostedtf45948e2011-05-02 12:29:25 -04002323 if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002324 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002325 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002326 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002327 /* reset in case of seek/pread */
2328 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002329 return iter;
2330 }
2331
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002332 if (iter->flags & FTRACE_ITER_HASH)
2333 return t_hash_start(m, pos);
2334
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002335 /*
2336 * Unfortunately, we need to restart at ftrace_pages_start
2337 * every time we let go of the ftrace_mutex. This is because
2338 * those pointers can change without the lock.
2339 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002340 iter->pg = ftrace_pages_start;
2341 iter->idx = 0;
2342 for (l = 0; l <= *pos; ) {
2343 p = t_next(m, p, &l);
2344 if (!p)
2345 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002346 }
walimis5821e1b2008-11-15 15:19:06 +08002347
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002348 if (!p) {
2349 if (iter->flags & FTRACE_ITER_FILTER)
2350 return t_hash_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002351
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002352 return NULL;
2353 }
2354
2355 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002356}
2357
2358static void t_stop(struct seq_file *m, void *p)
2359{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002360 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002361}
2362
2363static int t_show(struct seq_file *m, void *v)
2364{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002365 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002366 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002367
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002368 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002369 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002370
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002371 if (iter->flags & FTRACE_ITER_PRINTALL) {
2372 seq_printf(m, "#### all functions enabled ####\n");
2373 return 0;
2374 }
2375
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002376 rec = iter->func;
2377
Steven Rostedt5072c592008-05-12 21:20:43 +02002378 if (!rec)
2379 return 0;
2380
Steven Rostedt647bcd02011-05-03 14:39:21 -04002381 seq_printf(m, "%ps", (void *)rec->ip);
2382 if (iter->flags & FTRACE_ITER_ENABLED)
2383 seq_printf(m, " (%ld)",
2384 rec->flags & ~FTRACE_FL_MASK);
2385 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002386
2387 return 0;
2388}
2389
James Morris88e9d342009-09-22 16:43:43 -07002390static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002391 .start = t_start,
2392 .next = t_next,
2393 .stop = t_stop,
2394 .show = t_show,
2395};
2396
Ingo Molnare309b412008-05-12 21:20:51 +02002397static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002398ftrace_avail_open(struct inode *inode, struct file *file)
2399{
2400 struct ftrace_iterator *iter;
2401 int ret;
2402
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002403 if (unlikely(ftrace_disabled))
2404 return -ENODEV;
2405
Steven Rostedt5072c592008-05-12 21:20:43 +02002406 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2407 if (!iter)
2408 return -ENOMEM;
2409
2410 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002411
2412 ret = seq_open(file, &show_ftrace_seq_ops);
2413 if (!ret) {
2414 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002415
Steven Rostedt5072c592008-05-12 21:20:43 +02002416 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002417 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002418 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002419 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002420
2421 return ret;
2422}
2423
Steven Rostedt647bcd02011-05-03 14:39:21 -04002424static int
2425ftrace_enabled_open(struct inode *inode, struct file *file)
2426{
2427 struct ftrace_iterator *iter;
2428 int ret;
2429
2430 if (unlikely(ftrace_disabled))
2431 return -ENODEV;
2432
2433 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2434 if (!iter)
2435 return -ENOMEM;
2436
2437 iter->pg = ftrace_pages_start;
2438 iter->flags = FTRACE_ITER_ENABLED;
2439
2440 ret = seq_open(file, &show_ftrace_seq_ops);
2441 if (!ret) {
2442 struct seq_file *m = file->private_data;
2443
2444 m->private = iter;
2445 } else {
2446 kfree(iter);
2447 }
2448
2449 return ret;
2450}
2451
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002452static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002453{
Steven Rostedt52baf112009-02-14 01:15:39 -05002454 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002455 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002456 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002457}
2458
Ingo Molnare309b412008-05-12 21:20:51 +02002459static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002460ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002461 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002462{
2463 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002464 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002465 int ret = 0;
2466
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002467 if (unlikely(ftrace_disabled))
2468 return -ENODEV;
2469
Steven Rostedt5072c592008-05-12 21:20:43 +02002470 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2471 if (!iter)
2472 return -ENOMEM;
2473
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002474 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2475 kfree(iter);
2476 return -ENOMEM;
2477 }
2478
Steven Rostedtf45948e2011-05-02 12:29:25 -04002479 if (flag & FTRACE_ITER_NOTRACE)
2480 hash = ops->notrace_hash;
2481 else
2482 hash = ops->filter_hash;
2483
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002484 iter->ops = ops;
2485 iter->flags = flag;
2486
2487 if (file->f_mode & FMODE_WRITE) {
2488 mutex_lock(&ftrace_lock);
2489 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2490 mutex_unlock(&ftrace_lock);
2491
2492 if (!iter->hash) {
2493 trace_parser_put(&iter->parser);
2494 kfree(iter);
2495 return -ENOMEM;
2496 }
2497 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002498
Steven Rostedt41c52c02008-05-22 11:46:33 -04002499 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002500
Steven Rostedt5072c592008-05-12 21:20:43 +02002501 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002502 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002503 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002504
2505 if (file->f_mode & FMODE_READ) {
2506 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002507
2508 ret = seq_open(file, &show_ftrace_seq_ops);
2509 if (!ret) {
2510 struct seq_file *m = file->private_data;
2511 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002512 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002513 /* Failed */
2514 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002515 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002516 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002517 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002518 } else
2519 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002520 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002521
2522 return ret;
2523}
2524
Steven Rostedt41c52c02008-05-22 11:46:33 -04002525static int
2526ftrace_filter_open(struct inode *inode, struct file *file)
2527{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002528 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002529 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002530}
2531
2532static int
2533ftrace_notrace_open(struct inode *inode, struct file *file)
2534{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002535 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002536 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002537}
2538
Ingo Molnare309b412008-05-12 21:20:51 +02002539static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002540ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02002541{
2542 loff_t ret;
2543
2544 if (file->f_mode & FMODE_READ)
2545 ret = seq_lseek(file, offset, origin);
2546 else
2547 file->f_pos = ret = 1;
2548
2549 return ret;
2550}
2551
Steven Rostedt64e7c442009-02-13 17:08:48 -05002552static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002553{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002554 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002555 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002556
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002557 switch (type) {
2558 case MATCH_FULL:
2559 if (strcmp(str, regex) == 0)
2560 matched = 1;
2561 break;
2562 case MATCH_FRONT_ONLY:
2563 if (strncmp(str, regex, len) == 0)
2564 matched = 1;
2565 break;
2566 case MATCH_MIDDLE_ONLY:
2567 if (strstr(str, regex))
2568 matched = 1;
2569 break;
2570 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002571 slen = strlen(str);
2572 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002573 matched = 1;
2574 break;
2575 }
2576
2577 return matched;
2578}
2579
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002580static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002581enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002582{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002583 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002584 int ret = 0;
2585
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002586 entry = ftrace_lookup_ip(hash, rec->ip);
2587 if (not) {
2588 /* Do nothing if it doesn't exist */
2589 if (!entry)
2590 return 0;
2591
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002592 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002593 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002594 /* Do nothing if it exists */
2595 if (entry)
2596 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002597
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002598 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002599 }
2600 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002601}
2602
Steven Rostedt64e7c442009-02-13 17:08:48 -05002603static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002604ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2605 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002606{
2607 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002608 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002609
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002610 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2611
2612 if (mod) {
2613 /* module lookup requires matching the module */
2614 if (!modname || strcmp(modname, mod))
2615 return 0;
2616
2617 /* blank search means to match all funcs in the mod */
2618 if (!len)
2619 return 1;
2620 }
2621
Steven Rostedt64e7c442009-02-13 17:08:48 -05002622 return ftrace_match(str, regex, len, type);
2623}
2624
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002625static int
2626match_records(struct ftrace_hash *hash, char *buff,
2627 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002628{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002629 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002630 struct ftrace_page *pg;
2631 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002632 int type = MATCH_FULL;
2633 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002634 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002635 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002636
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002637 if (len) {
2638 type = filter_parse_regex(buff, len, &search, &not);
2639 search_len = strlen(search);
2640 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002641
Steven Rostedt52baf112009-02-14 01:15:39 -05002642 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002643
2644 if (unlikely(ftrace_disabled))
2645 goto out_unlock;
2646
Steven Rostedt265c8312009-02-13 12:43:56 -05002647 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002648 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002649 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002650 if (ret < 0) {
2651 found = ret;
2652 goto out_unlock;
2653 }
Li Zefan311d16d2009-12-08 11:15:11 +08002654 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002655 }
2656 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002657 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002658 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002659
2660 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002661}
2662
Steven Rostedt64e7c442009-02-13 17:08:48 -05002663static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002664ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002665{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002666 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002667}
2668
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002669static int
2670ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002671{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002672 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002673
Steven Rostedt64e7c442009-02-13 17:08:48 -05002674 /* blank or '*' mean the same */
2675 if (strcmp(buff, "*") == 0)
2676 buff[0] = 0;
2677
2678 /* handle the case of 'dont filter this module' */
2679 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2680 buff[0] = 0;
2681 not = 1;
2682 }
2683
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002684 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002685}
2686
Steven Rostedtf6180772009-02-14 00:40:25 -05002687/*
2688 * We register the module command as a template to show others how
2689 * to register the a command as well.
2690 */
2691
2692static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002693ftrace_mod_callback(struct ftrace_hash *hash,
2694 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002695{
2696 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002697 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002698
2699 /*
2700 * cmd == 'mod' because we only registered this func
2701 * for the 'mod' ftrace_func_command.
2702 * But if you register one func with multiple commands,
2703 * you can tell which command was used by the cmd
2704 * parameter.
2705 */
2706
2707 /* we must have a module name */
2708 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002709 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002710
2711 mod = strsep(&param, ":");
2712 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002713 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002714
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002715 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002716 if (!ret)
2717 ret = -EINVAL;
2718 if (ret < 0)
2719 return ret;
2720
2721 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002722}
2723
2724static struct ftrace_func_command ftrace_mod_cmd = {
2725 .name = "mod",
2726 .func = ftrace_mod_callback,
2727};
2728
2729static int __init ftrace_mod_cmd_init(void)
2730{
2731 return register_ftrace_command(&ftrace_mod_cmd);
2732}
2733device_initcall(ftrace_mod_cmd_init);
2734
Steven Rostedt59df055f2009-02-14 15:29:06 -05002735static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002736function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002737{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002738 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002739 struct hlist_head *hhd;
2740 struct hlist_node *n;
2741 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002742
2743 key = hash_long(ip, FTRACE_HASH_BITS);
2744
2745 hhd = &ftrace_func_hash[key];
2746
2747 if (hlist_empty(hhd))
2748 return;
2749
2750 /*
2751 * Disable preemption for these calls to prevent a RCU grace
2752 * period. This syncs the hash iteration and freeing of items
2753 * on the hash. rcu_read_lock is too dangerous here.
2754 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002755 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002756 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2757 if (entry->ip == ip)
2758 entry->ops->func(ip, parent_ip, &entry->data);
2759 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002760 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002761}
2762
Steven Rostedtb6887d72009-02-17 12:32:04 -05002763static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002764{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002765 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002766};
2767
Steven Rostedtb6887d72009-02-17 12:32:04 -05002768static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002769
Steven Rostedtb6887d72009-02-17 12:32:04 -05002770static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002771{
Steven Rostedtb8489142011-05-04 09:27:52 -04002772 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002773 int i;
2774
Steven Rostedtb6887d72009-02-17 12:32:04 -05002775 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002776 return;
2777
2778 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2779 struct hlist_head *hhd = &ftrace_func_hash[i];
2780 if (hhd->first)
2781 break;
2782 }
2783 /* Nothing registered? */
2784 if (i == FTRACE_FUNC_HASHSIZE)
2785 return;
2786
Steven Rostedtb8489142011-05-04 09:27:52 -04002787 ret = __register_ftrace_function(&trace_probe_ops);
2788 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002789 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002790
Steven Rostedtb6887d72009-02-17 12:32:04 -05002791 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002792}
2793
Steven Rostedtb6887d72009-02-17 12:32:04 -05002794static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002795{
Steven Rostedtb8489142011-05-04 09:27:52 -04002796 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002797 int i;
2798
Steven Rostedtb6887d72009-02-17 12:32:04 -05002799 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002800 return;
2801
2802 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2803 struct hlist_head *hhd = &ftrace_func_hash[i];
2804 if (hhd->first)
2805 return;
2806 }
2807
2808 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002809 ret = __unregister_ftrace_function(&trace_probe_ops);
2810 if (!ret)
2811 ftrace_shutdown(&trace_probe_ops, 0);
2812
Steven Rostedtb6887d72009-02-17 12:32:04 -05002813 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002814}
2815
2816
2817static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2818{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002819 struct ftrace_func_probe *entry =
2820 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002821
2822 if (entry->ops->free)
2823 entry->ops->free(&entry->data);
2824 kfree(entry);
2825}
2826
2827
2828int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002829register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002830 void *data)
2831{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002832 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002833 struct ftrace_page *pg;
2834 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002835 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002836 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002837 int count = 0;
2838 char *search;
2839
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002840 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002841 len = strlen(search);
2842
Steven Rostedtb6887d72009-02-17 12:32:04 -05002843 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002844 if (WARN_ON(not))
2845 return -EINVAL;
2846
2847 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002848
Steven Rostedt45a4a232011-04-21 23:16:46 -04002849 if (unlikely(ftrace_disabled))
2850 goto out_unlock;
2851
Steven Rostedt59df055f2009-02-14 15:29:06 -05002852 do_for_each_ftrace_rec(pg, rec) {
2853
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002854 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002855 continue;
2856
2857 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2858 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002859 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002860 if (!count)
2861 count = -ENOMEM;
2862 goto out_unlock;
2863 }
2864
2865 count++;
2866
2867 entry->data = data;
2868
2869 /*
2870 * The caller might want to do something special
2871 * for each function we find. We call the callback
2872 * to give the caller an opportunity to do so.
2873 */
2874 if (ops->callback) {
2875 if (ops->callback(rec->ip, &entry->data) < 0) {
2876 /* caller does not like this func */
2877 kfree(entry);
2878 continue;
2879 }
2880 }
2881
2882 entry->ops = ops;
2883 entry->ip = rec->ip;
2884
2885 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2886 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2887
2888 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002889 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002890
2891 out_unlock:
2892 mutex_unlock(&ftrace_lock);
2893
2894 return count;
2895}
2896
2897enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002898 PROBE_TEST_FUNC = 1,
2899 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002900};
2901
2902static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002903__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002904 void *data, int flags)
2905{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002906 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002907 struct hlist_node *n, *tmp;
2908 char str[KSYM_SYMBOL_LEN];
2909 int type = MATCH_FULL;
2910 int i, len = 0;
2911 char *search;
2912
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002913 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002914 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002915 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002916 int not;
2917
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002918 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002919 len = strlen(search);
2920
Steven Rostedtb6887d72009-02-17 12:32:04 -05002921 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002922 if (WARN_ON(not))
2923 return;
2924 }
2925
2926 mutex_lock(&ftrace_lock);
2927 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2928 struct hlist_head *hhd = &ftrace_func_hash[i];
2929
2930 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2931
2932 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002933 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002934 continue;
2935
Steven Rostedtb6887d72009-02-17 12:32:04 -05002936 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002937 continue;
2938
2939 /* do this last, since it is the most expensive */
2940 if (glob) {
2941 kallsyms_lookup(entry->ip, NULL, NULL,
2942 NULL, str);
2943 if (!ftrace_match(str, glob, len, type))
2944 continue;
2945 }
2946
2947 hlist_del(&entry->node);
2948 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2949 }
2950 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002951 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002952 mutex_unlock(&ftrace_lock);
2953}
2954
2955void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002956unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002957 void *data)
2958{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002959 __unregister_ftrace_function_probe(glob, ops, data,
2960 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002961}
2962
2963void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002964unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002965{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002966 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002967}
2968
Steven Rostedtb6887d72009-02-17 12:32:04 -05002969void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002970{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002971 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002972}
2973
Steven Rostedtf6180772009-02-14 00:40:25 -05002974static LIST_HEAD(ftrace_commands);
2975static DEFINE_MUTEX(ftrace_cmd_mutex);
2976
2977int register_ftrace_command(struct ftrace_func_command *cmd)
2978{
2979 struct ftrace_func_command *p;
2980 int ret = 0;
2981
2982 mutex_lock(&ftrace_cmd_mutex);
2983 list_for_each_entry(p, &ftrace_commands, list) {
2984 if (strcmp(cmd->name, p->name) == 0) {
2985 ret = -EBUSY;
2986 goto out_unlock;
2987 }
2988 }
2989 list_add(&cmd->list, &ftrace_commands);
2990 out_unlock:
2991 mutex_unlock(&ftrace_cmd_mutex);
2992
2993 return ret;
2994}
2995
2996int unregister_ftrace_command(struct ftrace_func_command *cmd)
2997{
2998 struct ftrace_func_command *p, *n;
2999 int ret = -ENODEV;
3000
3001 mutex_lock(&ftrace_cmd_mutex);
3002 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3003 if (strcmp(cmd->name, p->name) == 0) {
3004 ret = 0;
3005 list_del_init(&p->list);
3006 goto out_unlock;
3007 }
3008 }
3009 out_unlock:
3010 mutex_unlock(&ftrace_cmd_mutex);
3011
3012 return ret;
3013}
3014
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003015static int ftrace_process_regex(struct ftrace_hash *hash,
3016 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003017{
Steven Rostedtf6180772009-02-14 00:40:25 -05003018 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003019 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003020 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003021
3022 func = strsep(&next, ":");
3023
3024 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003025 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003026 if (!ret)
3027 ret = -EINVAL;
3028 if (ret < 0)
3029 return ret;
3030 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003031 }
3032
Steven Rostedtf6180772009-02-14 00:40:25 -05003033 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003034
3035 command = strsep(&next, ":");
3036
Steven Rostedtf6180772009-02-14 00:40:25 -05003037 mutex_lock(&ftrace_cmd_mutex);
3038 list_for_each_entry(p, &ftrace_commands, list) {
3039 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003040 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003041 goto out_unlock;
3042 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003043 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003044 out_unlock:
3045 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003046
Steven Rostedtf6180772009-02-14 00:40:25 -05003047 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003048}
3049
Ingo Molnare309b412008-05-12 21:20:51 +02003050static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003051ftrace_regex_write(struct file *file, const char __user *ubuf,
3052 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003053{
3054 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003055 struct trace_parser *parser;
3056 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003057
Li Zefan4ba79782009-09-22 13:52:20 +08003058 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003059 return 0;
3060
Steven Rostedt41c52c02008-05-22 11:46:33 -04003061 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003062
Steven Rostedt45a4a232011-04-21 23:16:46 -04003063 ret = -ENODEV;
3064 if (unlikely(ftrace_disabled))
3065 goto out_unlock;
3066
Steven Rostedt5072c592008-05-12 21:20:43 +02003067 if (file->f_mode & FMODE_READ) {
3068 struct seq_file *m = file->private_data;
3069 iter = m->private;
3070 } else
3071 iter = file->private_data;
3072
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003073 parser = &iter->parser;
3074 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003075
Li Zefan4ba79782009-09-22 13:52:20 +08003076 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003077 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003078 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003079 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003080 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003081 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003082 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003083 }
3084
Steven Rostedt5072c592008-05-12 21:20:43 +02003085 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003086out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003087 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003088
Steven Rostedt5072c592008-05-12 21:20:43 +02003089 return ret;
3090}
3091
Steven Rostedt41c52c02008-05-22 11:46:33 -04003092static ssize_t
3093ftrace_filter_write(struct file *file, const char __user *ubuf,
3094 size_t cnt, loff_t *ppos)
3095{
3096 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3097}
3098
3099static ssize_t
3100ftrace_notrace_write(struct file *file, const char __user *ubuf,
3101 size_t cnt, loff_t *ppos)
3102{
3103 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3104}
3105
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003106static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003107ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3108 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003109{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003110 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003111 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003112 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003113
Steven Rostedt936e0742011-05-05 22:54:01 -04003114 /* All global ops uses the global ops filters */
3115 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3116 ops = &global_ops;
3117
Steven Rostedt41c52c02008-05-22 11:46:33 -04003118 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003119 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003120
Steven Rostedtf45948e2011-05-02 12:29:25 -04003121 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003122 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003123 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003124 orig_hash = &ops->notrace_hash;
3125
3126 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3127 if (!hash)
3128 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003129
Steven Rostedt41c52c02008-05-22 11:46:33 -04003130 mutex_lock(&ftrace_regex_lock);
3131 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003132 ftrace_filter_reset(hash);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003133 if (buf)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003134 ftrace_match_records(hash, buf, len);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003135
3136 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003137 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003138 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3139 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003140 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003141
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003142 mutex_unlock(&ftrace_lock);
3143
Steven Rostedt41c52c02008-05-22 11:46:33 -04003144 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003145
3146 free_ftrace_hash(hash);
3147 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003148}
3149
Steven Rostedt77a2b372008-05-12 21:20:45 +02003150/**
3151 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003152 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003153 * @buf - the string that holds the function filter text.
3154 * @len - the length of the string.
3155 * @reset - non zero to reset all filters before applying this filter.
3156 *
3157 * Filters denote which functions should be enabled when tracing is enabled.
3158 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3159 */
Steven Rostedt936e0742011-05-05 22:54:01 -04003160void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3161 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003162{
Steven Rostedt936e0742011-05-05 22:54:01 -04003163 ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003164}
Steven Rostedt936e0742011-05-05 22:54:01 -04003165EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003166
Steven Rostedt41c52c02008-05-22 11:46:33 -04003167/**
3168 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003169 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003170 * @buf - the string that holds the function notrace text.
3171 * @len - the length of the string.
3172 * @reset - non zero to reset all filters before applying this filter.
3173 *
3174 * Notrace Filters denote which functions should not be enabled when tracing
3175 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3176 * for tracing.
3177 */
Steven Rostedt936e0742011-05-05 22:54:01 -04003178void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3179 int len, int reset)
3180{
3181 ftrace_set_regex(ops, buf, len, reset, 0);
3182}
3183EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3184/**
3185 * ftrace_set_filter - set a function to filter on in ftrace
3186 * @ops - the ops to set the filter with
3187 * @buf - the string that holds the function filter text.
3188 * @len - the length of the string.
3189 * @reset - non zero to reset all filters before applying this filter.
3190 *
3191 * Filters denote which functions should be enabled when tracing is enabled.
3192 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3193 */
3194void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3195{
3196 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3197}
3198EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3199
3200/**
3201 * ftrace_set_notrace - set a function to not trace in ftrace
3202 * @ops - the ops to set the notrace filter with
3203 * @buf - the string that holds the function notrace text.
3204 * @len - the length of the string.
3205 * @reset - non zero to reset all filters before applying this filter.
3206 *
3207 * Notrace Filters denote which functions should not be enabled when tracing
3208 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3209 * for tracing.
3210 */
3211void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003212{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003213 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003214}
Steven Rostedt936e0742011-05-05 22:54:01 -04003215EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003216
Steven Rostedt2af15d62009-05-28 13:37:24 -04003217/*
3218 * command line interface to allow users to set filters on boot up.
3219 */
3220#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3221static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3222static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3223
3224static int __init set_ftrace_notrace(char *str)
3225{
3226 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3227 return 1;
3228}
3229__setup("ftrace_notrace=", set_ftrace_notrace);
3230
3231static int __init set_ftrace_filter(char *str)
3232{
3233 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3234 return 1;
3235}
3236__setup("ftrace_filter=", set_ftrace_filter);
3237
Stefan Assmann369bc182009-10-12 22:17:21 +02003238#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003239static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003240static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3241
Stefan Assmann369bc182009-10-12 22:17:21 +02003242static int __init set_graph_function(char *str)
3243{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003244 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003245 return 1;
3246}
3247__setup("ftrace_graph_filter=", set_graph_function);
3248
3249static void __init set_ftrace_early_graph(char *buf)
3250{
3251 int ret;
3252 char *func;
3253
3254 while (buf) {
3255 func = strsep(&buf, ",");
3256 /* we allow only one expression at a time */
3257 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3258 func);
3259 if (ret)
3260 printk(KERN_DEBUG "ftrace: function %s not "
3261 "traceable\n", func);
3262 }
3263}
3264#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3265
Steven Rostedtf45948e2011-05-02 12:29:25 -04003266static void __init
3267set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003268{
3269 char *func;
3270
3271 while (buf) {
3272 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003273 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003274 }
3275}
3276
3277static void __init set_ftrace_early_filters(void)
3278{
3279 if (ftrace_filter_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04003280 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003281 if (ftrace_notrace_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04003282 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003283#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3284 if (ftrace_graph_buf[0])
3285 set_ftrace_early_graph(ftrace_graph_buf);
3286#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003287}
3288
Ingo Molnare309b412008-05-12 21:20:51 +02003289static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003290ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003291{
3292 struct seq_file *m = (struct seq_file *)file->private_data;
3293 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003294 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003295 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003296 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003297 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003298
Steven Rostedt41c52c02008-05-22 11:46:33 -04003299 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003300 if (file->f_mode & FMODE_READ) {
3301 iter = m->private;
3302
3303 seq_release(inode, file);
3304 } else
3305 iter = file->private_data;
3306
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003307 parser = &iter->parser;
3308 if (trace_parser_loaded(parser)) {
3309 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003310 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003311 }
3312
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003313 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003314
Steven Rostedt058e2972011-04-29 22:35:33 -04003315 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003316 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3317
3318 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003319 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003320 else
3321 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003322
Steven Rostedt058e2972011-04-29 22:35:33 -04003323 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003324 ret = ftrace_hash_move(iter->ops, filter_hash,
3325 orig_hash, iter->hash);
3326 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3327 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003328 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003329
Steven Rostedt058e2972011-04-29 22:35:33 -04003330 mutex_unlock(&ftrace_lock);
3331 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003332 free_ftrace_hash(iter->hash);
3333 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003334
Steven Rostedt41c52c02008-05-22 11:46:33 -04003335 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003336 return 0;
3337}
3338
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003339static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003340 .open = ftrace_avail_open,
3341 .read = seq_read,
3342 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003343 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003344};
3345
Steven Rostedt647bcd02011-05-03 14:39:21 -04003346static const struct file_operations ftrace_enabled_fops = {
3347 .open = ftrace_enabled_open,
3348 .read = seq_read,
3349 .llseek = seq_lseek,
3350 .release = seq_release_private,
3351};
3352
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003353static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003354 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003355 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003356 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003357 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003358 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003359};
3360
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003361static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003362 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003363 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003364 .write = ftrace_notrace_write,
3365 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003366 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003367};
3368
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003369#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3370
3371static DEFINE_MUTEX(graph_lock);
3372
3373int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003374int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003375unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3376
3377static void *
Li Zefan85951842009-06-24 09:54:00 +08003378__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003379{
Li Zefan85951842009-06-24 09:54:00 +08003380 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003381 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003382 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003383}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003384
Li Zefan85951842009-06-24 09:54:00 +08003385static void *
3386g_next(struct seq_file *m, void *v, loff_t *pos)
3387{
3388 (*pos)++;
3389 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003390}
3391
3392static void *g_start(struct seq_file *m, loff_t *pos)
3393{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003394 mutex_lock(&graph_lock);
3395
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003396 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003397 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003398 return (void *)1;
3399
Li Zefan85951842009-06-24 09:54:00 +08003400 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003401}
3402
3403static void g_stop(struct seq_file *m, void *p)
3404{
3405 mutex_unlock(&graph_lock);
3406}
3407
3408static int g_show(struct seq_file *m, void *v)
3409{
3410 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003411
3412 if (!ptr)
3413 return 0;
3414
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003415 if (ptr == (unsigned long *)1) {
3416 seq_printf(m, "#### all functions enabled ####\n");
3417 return 0;
3418 }
3419
Steven Rostedtb375a112009-09-17 00:05:58 -04003420 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003421
3422 return 0;
3423}
3424
James Morris88e9d342009-09-22 16:43:43 -07003425static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003426 .start = g_start,
3427 .next = g_next,
3428 .stop = g_stop,
3429 .show = g_show,
3430};
3431
3432static int
3433ftrace_graph_open(struct inode *inode, struct file *file)
3434{
3435 int ret = 0;
3436
3437 if (unlikely(ftrace_disabled))
3438 return -ENODEV;
3439
3440 mutex_lock(&graph_lock);
3441 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003442 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003443 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003444 ftrace_graph_count = 0;
3445 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3446 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003447 mutex_unlock(&graph_lock);
3448
Li Zefana4ec5e02009-09-18 14:06:28 +08003449 if (file->f_mode & FMODE_READ)
3450 ret = seq_open(file, &ftrace_graph_seq_ops);
3451
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003452 return ret;
3453}
3454
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003455static int
Li Zefan87827112009-07-23 11:29:11 +08003456ftrace_graph_release(struct inode *inode, struct file *file)
3457{
3458 if (file->f_mode & FMODE_READ)
3459 seq_release(inode, file);
3460 return 0;
3461}
3462
3463static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003464ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003465{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003466 struct dyn_ftrace *rec;
3467 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003468 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003469 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003470 int type, not;
3471 char *search;
3472 bool exists;
3473 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003474
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003475 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003476 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003477 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3478 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003479
3480 search_len = strlen(search);
3481
Steven Rostedt52baf112009-02-14 01:15:39 -05003482 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003483
3484 if (unlikely(ftrace_disabled)) {
3485 mutex_unlock(&ftrace_lock);
3486 return -ENODEV;
3487 }
3488
Steven Rostedt265c8312009-02-13 12:43:56 -05003489 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003490
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003491 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003492 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003493 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003494 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003495 if (array[i] == rec->ip) {
3496 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003497 break;
3498 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003499 }
3500
3501 if (!not) {
3502 fail = 0;
3503 if (!exists) {
3504 array[(*idx)++] = rec->ip;
3505 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3506 goto out;
3507 }
3508 } else {
3509 if (exists) {
3510 array[i] = array[--(*idx)];
3511 array[*idx] = 0;
3512 fail = 0;
3513 }
3514 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003515 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003516 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003517out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003518 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003519
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003520 if (fail)
3521 return -EINVAL;
3522
3523 ftrace_graph_filter_enabled = 1;
3524 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003525}
3526
3527static ssize_t
3528ftrace_graph_write(struct file *file, const char __user *ubuf,
3529 size_t cnt, loff_t *ppos)
3530{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003531 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003532 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003533
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003534 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003535 return 0;
3536
3537 mutex_lock(&graph_lock);
3538
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003539 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3540 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003541 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003542 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003543
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003544 read = trace_get_user(&parser, ubuf, cnt, ppos);
3545
Li Zefan4ba79782009-09-22 13:52:20 +08003546 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003547 parser.buffer[parser.idx] = 0;
3548
3549 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003550 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003551 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003552 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003553 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003554 }
3555
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003556 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003557
3558out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003559 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003560out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003561 mutex_unlock(&graph_lock);
3562
3563 return ret;
3564}
3565
3566static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003567 .open = ftrace_graph_open,
3568 .read = seq_read,
3569 .write = ftrace_graph_write,
3570 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003571 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003572};
3573#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3574
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003575static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003576{
Steven Rostedt5072c592008-05-12 21:20:43 +02003577
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003578 trace_create_file("available_filter_functions", 0444,
3579 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003580
Steven Rostedt647bcd02011-05-03 14:39:21 -04003581 trace_create_file("enabled_functions", 0444,
3582 d_tracer, NULL, &ftrace_enabled_fops);
3583
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003584 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3585 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003586
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003587 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003588 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003589
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003590#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003591 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003592 NULL,
3593 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003594#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3595
Steven Rostedt5072c592008-05-12 21:20:43 +02003596 return 0;
3597}
3598
Steven Rostedt68950612011-12-16 17:06:45 -05003599static void ftrace_swap_recs(void *a, void *b, int size)
3600{
3601 struct dyn_ftrace *reca = a;
3602 struct dyn_ftrace *recb = b;
3603 struct dyn_ftrace t;
3604
3605 t = *reca;
3606 *reca = *recb;
3607 *recb = t;
3608}
3609
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003610static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003611 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003612 unsigned long *end)
3613{
Steven Rostedta7900872011-12-16 16:23:44 -05003614 struct ftrace_page *pg;
3615 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003616 unsigned long *p;
3617 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003618 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003619 int ret = -ENOMEM;
3620
3621 count = end - start;
3622
3623 if (!count)
3624 return 0;
3625
3626 pg = ftrace_allocate_pages(count);
3627 if (!pg)
3628 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003629
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003630 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003631
Steven Rostedt32082302011-12-16 14:42:37 -05003632 /*
3633 * Core and each module needs their own pages, as
3634 * modules will free them when they are removed.
3635 * Force a new page to be allocated for modules.
3636 */
Steven Rostedta7900872011-12-16 16:23:44 -05003637 if (!mod) {
3638 WARN_ON(ftrace_pages || ftrace_pages_start);
3639 /* First initialization */
3640 ftrace_pages = ftrace_pages_start = pg;
3641 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003642 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003643 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003644
Steven Rostedta7900872011-12-16 16:23:44 -05003645 if (WARN_ON(ftrace_pages->next)) {
3646 /* Hmm, we have free pages? */
3647 while (ftrace_pages->next)
3648 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003649 }
Steven Rostedta7900872011-12-16 16:23:44 -05003650
3651 ftrace_pages->next = pg;
3652 ftrace_pages = pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003653 }
3654
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003655 p = start;
3656 while (p < end) {
3657 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003658 /*
3659 * Some architecture linkers will pad between
3660 * the different mcount_loc sections of different
3661 * object files to satisfy alignments.
3662 * Skip any NULL pointers.
3663 */
3664 if (!addr)
3665 continue;
Steven Rostedta7900872011-12-16 16:23:44 -05003666 if (!ftrace_record_ip(addr))
3667 break;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003668 }
3669
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003670 /* These new locations need to be initialized */
3671 ftrace_new_pgs = pg;
3672
Steven Rostedt68950612011-12-16 17:06:45 -05003673 /* Make each individual set of pages sorted by ips */
3674 for (; pg; pg = pg->next)
3675 sort(pg->records, pg->index, sizeof(struct dyn_ftrace),
3676 ftrace_cmp_recs, ftrace_swap_recs);
3677
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003678 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003679 * We only need to disable interrupts on start up
3680 * because we are modifying code that an interrupt
3681 * may execute, and the modification is not atomic.
3682 * But for modules, nothing runs the code we modify
3683 * until we are finished with it, and there's no
3684 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003685 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003686 if (!mod)
3687 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003688 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003689 if (!mod)
3690 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003691 ret = 0;
3692 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003693 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003694
Steven Rostedta7900872011-12-16 16:23:44 -05003695 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003696}
3697
Steven Rostedt93eb6772009-04-15 13:24:06 -04003698#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003699
3700#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3701
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003702void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003703{
3704 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003705 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003706 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003707 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003708
Steven Rostedt93eb6772009-04-15 13:24:06 -04003709 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003710
3711 if (ftrace_disabled)
3712 goto out_unlock;
3713
Steven Rostedt32082302011-12-16 14:42:37 -05003714 /*
3715 * Each module has its own ftrace_pages, remove
3716 * them from the list.
3717 */
3718 last_pg = &ftrace_pages_start;
3719 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3720 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003721 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003722 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003723 * As core pages are first, the first
3724 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003725 */
Steven Rostedt32082302011-12-16 14:42:37 -05003726 if (WARN_ON(pg == ftrace_pages_start))
3727 goto out_unlock;
3728
3729 /* Check if we are deleting the last page */
3730 if (pg == ftrace_pages)
3731 ftrace_pages = next_to_ftrace_page(last_pg);
3732
3733 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003734 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3735 free_pages((unsigned long)pg->records, order);
3736 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05003737 } else
3738 last_pg = &pg->next;
3739 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003740 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003741 mutex_unlock(&ftrace_lock);
3742}
3743
3744static void ftrace_init_module(struct module *mod,
3745 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003746{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003747 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003748 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003749 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003750}
3751
Steven Rostedt93eb6772009-04-15 13:24:06 -04003752static int ftrace_module_notify(struct notifier_block *self,
3753 unsigned long val, void *data)
3754{
3755 struct module *mod = data;
3756
3757 switch (val) {
3758 case MODULE_STATE_COMING:
3759 ftrace_init_module(mod, mod->ftrace_callsites,
3760 mod->ftrace_callsites +
3761 mod->num_ftrace_callsites);
3762 break;
3763 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003764 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003765 break;
3766 }
3767
3768 return 0;
3769}
3770#else
3771static int ftrace_module_notify(struct notifier_block *self,
3772 unsigned long val, void *data)
3773{
3774 return 0;
3775}
3776#endif /* CONFIG_MODULES */
3777
3778struct notifier_block ftrace_module_nb = {
3779 .notifier_call = ftrace_module_notify,
3780 .priority = 0,
3781};
3782
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003783extern unsigned long __start_mcount_loc[];
3784extern unsigned long __stop_mcount_loc[];
3785
3786void __init ftrace_init(void)
3787{
3788 unsigned long count, addr, flags;
3789 int ret;
3790
3791 /* Keep the ftrace pointer to the stub */
3792 addr = (unsigned long)ftrace_stub;
3793
3794 local_irq_save(flags);
3795 ftrace_dyn_arch_init(&addr);
3796 local_irq_restore(flags);
3797
3798 /* ftrace_dyn_arch_init places the return code in addr */
3799 if (addr)
3800 goto failed;
3801
3802 count = __stop_mcount_loc - __start_mcount_loc;
3803
3804 ret = ftrace_dyn_table_alloc(count);
3805 if (ret)
3806 goto failed;
3807
3808 last_ftrace_enabled = ftrace_enabled = 1;
3809
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003810 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003811 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003812 __stop_mcount_loc);
3813
Steven Rostedt93eb6772009-04-15 13:24:06 -04003814 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003815 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003816 pr_warning("Failed to register trace ftrace module notifier\n");
3817
Steven Rostedt2af15d62009-05-28 13:37:24 -04003818 set_ftrace_early_filters();
3819
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003820 return;
3821 failed:
3822 ftrace_disabled = 1;
3823}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003824
Steven Rostedt3d083392008-05-12 21:20:42 +02003825#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003826
Steven Rostedt2b499382011-05-03 22:49:52 -04003827static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003828 .func = ftrace_stub,
3829};
3830
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003831static int __init ftrace_nodyn_init(void)
3832{
3833 ftrace_enabled = 1;
3834 return 0;
3835}
3836device_initcall(ftrace_nodyn_init);
3837
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003838static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3839static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003840/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003841# define ftrace_startup(ops, command) \
3842 ({ \
3843 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3844 0; \
3845 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04003846# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003847# define ftrace_startup_sysctl() do { } while (0)
3848# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003849
3850static inline int
3851ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3852{
3853 return 1;
3854}
3855
Steven Rostedt3d083392008-05-12 21:20:42 +02003856#endif /* CONFIG_DYNAMIC_FTRACE */
3857
Steven Rostedtb8489142011-05-04 09:27:52 -04003858static void
3859ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3860{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003861 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04003862
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003863 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3864 return;
3865
3866 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003867 /*
3868 * Some of the ops may be dynamically allocated,
3869 * they must be freed after a synchronize_sched().
3870 */
3871 preempt_disable_notrace();
3872 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04003873 while (op != &ftrace_list_end) {
3874 if (ftrace_ops_test(op, ip))
3875 op->func(ip, parent_ip);
3876 op = rcu_dereference_raw(op->next);
3877 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003878 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003879 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04003880}
3881
Steven Rostedte32d8952008-12-04 00:26:41 -05003882static void clear_ftrace_swapper(void)
3883{
3884 struct task_struct *p;
3885 int cpu;
3886
3887 get_online_cpus();
3888 for_each_online_cpu(cpu) {
3889 p = idle_task(cpu);
3890 clear_tsk_trace_trace(p);
3891 }
3892 put_online_cpus();
3893}
3894
3895static void set_ftrace_swapper(void)
3896{
3897 struct task_struct *p;
3898 int cpu;
3899
3900 get_online_cpus();
3901 for_each_online_cpu(cpu) {
3902 p = idle_task(cpu);
3903 set_tsk_trace_trace(p);
3904 }
3905 put_online_cpus();
3906}
3907
3908static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003909{
3910 struct task_struct *p;
3911
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003912 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05003913 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05003914 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05003915 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003916 rcu_read_unlock();
3917
Steven Rostedte32d8952008-12-04 00:26:41 -05003918 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003919}
3920
Steven Rostedte32d8952008-12-04 00:26:41 -05003921static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003922{
3923 struct task_struct *p;
3924
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003925 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003926 do_each_pid_task(pid, PIDTYPE_PID, p) {
3927 set_tsk_trace_trace(p);
3928 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003929 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003930}
3931
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003932static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003933{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003934 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003935 clear_ftrace_swapper();
3936 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003937 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05003938}
3939
3940static void set_ftrace_pid_task(struct pid *pid)
3941{
3942 if (pid == ftrace_swapper_pid)
3943 set_ftrace_swapper();
3944 else
3945 set_ftrace_pid(pid);
3946}
3947
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003948static int ftrace_pid_add(int p)
3949{
3950 struct pid *pid;
3951 struct ftrace_pid *fpid;
3952 int ret = -EINVAL;
3953
3954 mutex_lock(&ftrace_lock);
3955
3956 if (!p)
3957 pid = ftrace_swapper_pid;
3958 else
3959 pid = find_get_pid(p);
3960
3961 if (!pid)
3962 goto out;
3963
3964 ret = 0;
3965
3966 list_for_each_entry(fpid, &ftrace_pids, list)
3967 if (fpid->pid == pid)
3968 goto out_put;
3969
3970 ret = -ENOMEM;
3971
3972 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3973 if (!fpid)
3974 goto out_put;
3975
3976 list_add(&fpid->list, &ftrace_pids);
3977 fpid->pid = pid;
3978
3979 set_ftrace_pid_task(pid);
3980
3981 ftrace_update_pid_func();
3982 ftrace_startup_enable(0);
3983
3984 mutex_unlock(&ftrace_lock);
3985 return 0;
3986
3987out_put:
3988 if (pid != ftrace_swapper_pid)
3989 put_pid(pid);
3990
3991out:
3992 mutex_unlock(&ftrace_lock);
3993 return ret;
3994}
3995
3996static void ftrace_pid_reset(void)
3997{
3998 struct ftrace_pid *fpid, *safe;
3999
4000 mutex_lock(&ftrace_lock);
4001 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4002 struct pid *pid = fpid->pid;
4003
4004 clear_ftrace_pid_task(pid);
4005
4006 list_del(&fpid->list);
4007 kfree(fpid);
4008 }
4009
4010 ftrace_update_pid_func();
4011 ftrace_startup_enable(0);
4012
4013 mutex_unlock(&ftrace_lock);
4014}
4015
4016static void *fpid_start(struct seq_file *m, loff_t *pos)
4017{
4018 mutex_lock(&ftrace_lock);
4019
4020 if (list_empty(&ftrace_pids) && (!*pos))
4021 return (void *) 1;
4022
4023 return seq_list_start(&ftrace_pids, *pos);
4024}
4025
4026static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4027{
4028 if (v == (void *)1)
4029 return NULL;
4030
4031 return seq_list_next(v, &ftrace_pids, pos);
4032}
4033
4034static void fpid_stop(struct seq_file *m, void *p)
4035{
4036 mutex_unlock(&ftrace_lock);
4037}
4038
4039static int fpid_show(struct seq_file *m, void *v)
4040{
4041 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4042
4043 if (v == (void *)1) {
4044 seq_printf(m, "no pid\n");
4045 return 0;
4046 }
4047
4048 if (fpid->pid == ftrace_swapper_pid)
4049 seq_printf(m, "swapper tasks\n");
4050 else
4051 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4052
4053 return 0;
4054}
4055
4056static const struct seq_operations ftrace_pid_sops = {
4057 .start = fpid_start,
4058 .next = fpid_next,
4059 .stop = fpid_stop,
4060 .show = fpid_show,
4061};
4062
4063static int
4064ftrace_pid_open(struct inode *inode, struct file *file)
4065{
4066 int ret = 0;
4067
4068 if ((file->f_mode & FMODE_WRITE) &&
4069 (file->f_flags & O_TRUNC))
4070 ftrace_pid_reset();
4071
4072 if (file->f_mode & FMODE_READ)
4073 ret = seq_open(file, &ftrace_pid_sops);
4074
4075 return ret;
4076}
4077
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004078static ssize_t
4079ftrace_pid_write(struct file *filp, const char __user *ubuf,
4080 size_t cnt, loff_t *ppos)
4081{
Ingo Molnar457dc922009-11-23 11:03:28 +01004082 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004083 long val;
4084 int ret;
4085
4086 if (cnt >= sizeof(buf))
4087 return -EINVAL;
4088
4089 if (copy_from_user(&buf, ubuf, cnt))
4090 return -EFAULT;
4091
4092 buf[cnt] = 0;
4093
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004094 /*
4095 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4096 * to clean the filter quietly.
4097 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004098 tmp = strstrip(buf);
4099 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004100 return 1;
4101
Ingo Molnar457dc922009-11-23 11:03:28 +01004102 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004103 if (ret < 0)
4104 return ret;
4105
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004106 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004107
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004108 return ret ? ret : cnt;
4109}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004110
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004111static int
4112ftrace_pid_release(struct inode *inode, struct file *file)
4113{
4114 if (file->f_mode & FMODE_READ)
4115 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004116
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004117 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004118}
4119
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004120static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004121 .open = ftrace_pid_open,
4122 .write = ftrace_pid_write,
4123 .read = seq_read,
4124 .llseek = seq_lseek,
4125 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004126};
4127
4128static __init int ftrace_init_debugfs(void)
4129{
4130 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004131
4132 d_tracer = tracing_init_dentry();
4133 if (!d_tracer)
4134 return 0;
4135
4136 ftrace_init_dyn_debugfs(d_tracer);
4137
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004138 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4139 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004140
4141 ftrace_profile_debugfs(d_tracer);
4142
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004143 return 0;
4144}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004145fs_initcall(ftrace_init_debugfs);
4146
Steven Rostedt3d083392008-05-12 21:20:42 +02004147/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004148 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004149 *
4150 * This function should be used by panic code. It stops ftrace
4151 * but in a not so nice way. If you need to simply kill ftrace
4152 * from a non-atomic section, use ftrace_kill.
4153 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004154void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004155{
4156 ftrace_disabled = 1;
4157 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004158 clear_ftrace_function();
4159}
4160
4161/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004162 * Test if ftrace is dead or not.
4163 */
4164int ftrace_is_dead(void)
4165{
4166 return ftrace_disabled;
4167}
4168
4169/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004170 * register_ftrace_function - register a function for profiling
4171 * @ops - ops structure that holds the function for profiling.
4172 *
4173 * Register a function to be called by all functions in the
4174 * kernel.
4175 *
4176 * Note: @ops->func and all the functions it calls must be labeled
4177 * with "notrace", otherwise it will go into a
4178 * recursive loop.
4179 */
4180int register_ftrace_function(struct ftrace_ops *ops)
4181{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004182 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004183
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004184 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004185
Steven Rostedt45a4a232011-04-21 23:16:46 -04004186 if (unlikely(ftrace_disabled))
4187 goto out_unlock;
4188
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004189 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004190 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004191 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004192
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004193
Steven Rostedt45a4a232011-04-21 23:16:46 -04004194 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004195 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004196 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004197}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004198EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004199
4200/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004201 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004202 * @ops - ops structure that holds the function to unregister
4203 *
4204 * Unregister a function that was added to be called by ftrace profiling.
4205 */
4206int unregister_ftrace_function(struct ftrace_ops *ops)
4207{
4208 int ret;
4209
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004210 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004211 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004212 if (!ret)
4213 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004214 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004215
4216 return ret;
4217}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004218EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004219
Ingo Molnare309b412008-05-12 21:20:51 +02004220int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004221ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004222 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004223 loff_t *ppos)
4224{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004225 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004226
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004227 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004228
Steven Rostedt45a4a232011-04-21 23:16:46 -04004229 if (unlikely(ftrace_disabled))
4230 goto out;
4231
4232 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004233
Li Zefana32c7762009-06-26 16:55:51 +08004234 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004235 goto out;
4236
Li Zefana32c7762009-06-26 16:55:51 +08004237 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004238
4239 if (ftrace_enabled) {
4240
4241 ftrace_startup_sysctl();
4242
4243 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004244 if (ftrace_ops_list != &ftrace_list_end) {
4245 if (ftrace_ops_list->next == &ftrace_list_end)
4246 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004247 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004248 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004249 }
4250
4251 } else {
4252 /* stopping ftrace calls (just send to ftrace_stub) */
4253 ftrace_trace_function = ftrace_stub;
4254
4255 ftrace_shutdown_sysctl();
4256 }
4257
4258 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004259 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004260 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004261}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004262
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004263#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004264
Steven Rostedt597af812009-04-03 15:24:12 -04004265static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004266static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004267
Steven Rostedte49dc192008-12-02 23:50:05 -05004268int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4269{
4270 return 0;
4271}
4272
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004273/* The callbacks that hook a function */
4274trace_func_graph_ret_t ftrace_graph_return =
4275 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004276trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004277
4278/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4279static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4280{
4281 int i;
4282 int ret = 0;
4283 unsigned long flags;
4284 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4285 struct task_struct *g, *t;
4286
4287 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4288 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4289 * sizeof(struct ftrace_ret_stack),
4290 GFP_KERNEL);
4291 if (!ret_stack_list[i]) {
4292 start = 0;
4293 end = i;
4294 ret = -ENOMEM;
4295 goto free;
4296 }
4297 }
4298
4299 read_lock_irqsave(&tasklist_lock, flags);
4300 do_each_thread(g, t) {
4301 if (start == end) {
4302 ret = -EAGAIN;
4303 goto unlock;
4304 }
4305
4306 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004307 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004308 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004309 t->curr_ret_stack = -1;
4310 /* Make sure the tasks see the -1 first: */
4311 smp_wmb();
4312 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004313 }
4314 } while_each_thread(g, t);
4315
4316unlock:
4317 read_unlock_irqrestore(&tasklist_lock, flags);
4318free:
4319 for (i = start; i < end; i++)
4320 kfree(ret_stack_list[i]);
4321 return ret;
4322}
4323
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004324static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004325ftrace_graph_probe_sched_switch(void *ignore,
4326 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004327{
4328 unsigned long long timestamp;
4329 int index;
4330
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004331 /*
4332 * Does the user want to count the time a function was asleep.
4333 * If so, do not update the time stamps.
4334 */
4335 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4336 return;
4337
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004338 timestamp = trace_clock_local();
4339
4340 prev->ftrace_timestamp = timestamp;
4341
4342 /* only process tasks that we timestamped */
4343 if (!next->ftrace_timestamp)
4344 return;
4345
4346 /*
4347 * Update all the counters in next to make up for the
4348 * time next was sleeping.
4349 */
4350 timestamp -= next->ftrace_timestamp;
4351
4352 for (index = next->curr_ret_stack; index >= 0; index--)
4353 next->ret_stack[index].calltime += timestamp;
4354}
4355
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004356/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004357static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004358{
4359 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004360 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004361
4362 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4363 sizeof(struct ftrace_ret_stack *),
4364 GFP_KERNEL);
4365
4366 if (!ret_stack_list)
4367 return -ENOMEM;
4368
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004369 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004370 for_each_online_cpu(cpu) {
4371 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004372 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004373 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004374
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004375 do {
4376 ret = alloc_retstack_tasklist(ret_stack_list);
4377 } while (ret == -EAGAIN);
4378
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004379 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004380 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004381 if (ret)
4382 pr_info("ftrace_graph: Couldn't activate tracepoint"
4383 " probe to kernel_sched_switch\n");
4384 }
4385
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004386 kfree(ret_stack_list);
4387 return ret;
4388}
4389
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004390/*
4391 * Hibernation protection.
4392 * The state of the current task is too much unstable during
4393 * suspend/restore to disk. We want to protect against that.
4394 */
4395static int
4396ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4397 void *unused)
4398{
4399 switch (state) {
4400 case PM_HIBERNATION_PREPARE:
4401 pause_graph_tracing();
4402 break;
4403
4404 case PM_POST_HIBERNATION:
4405 unpause_graph_tracing();
4406 break;
4407 }
4408 return NOTIFY_DONE;
4409}
4410
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004411int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4412 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004413{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004414 int ret = 0;
4415
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004416 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004417
Steven Rostedt05ce5812009-03-24 00:18:31 -04004418 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004419 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004420 ret = -EBUSY;
4421 goto out;
4422 }
4423
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004424 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4425 register_pm_notifier(&ftrace_suspend_notifier);
4426
Steven Rostedt597af812009-04-03 15:24:12 -04004427 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004428 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004429 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004430 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004431 goto out;
4432 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004433
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004434 ftrace_graph_return = retfunc;
4435 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004436
Steven Rostedta1cd6172011-05-23 15:24:25 -04004437 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004438
4439out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004440 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004441 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004442}
4443
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004444void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004445{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004446 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004447
Steven Rostedt597af812009-04-03 15:24:12 -04004448 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004449 goto out;
4450
Steven Rostedt597af812009-04-03 15:24:12 -04004451 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004452 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004453 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004454 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004455 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004456 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004457
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004458 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004459 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004460}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004461
Steven Rostedt868baf02011-02-10 21:26:13 -05004462static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4463
4464static void
4465graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4466{
4467 atomic_set(&t->tracing_graph_pause, 0);
4468 atomic_set(&t->trace_overrun, 0);
4469 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004470 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004471 smp_wmb();
4472 t->ret_stack = ret_stack;
4473}
4474
4475/*
4476 * Allocate a return stack for the idle task. May be the first
4477 * time through, or it may be done by CPU hotplug online.
4478 */
4479void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4480{
4481 t->curr_ret_stack = -1;
4482 /*
4483 * The idle task has no parent, it either has its own
4484 * stack or no stack at all.
4485 */
4486 if (t->ret_stack)
4487 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4488
4489 if (ftrace_graph_active) {
4490 struct ftrace_ret_stack *ret_stack;
4491
4492 ret_stack = per_cpu(idle_ret_stack, cpu);
4493 if (!ret_stack) {
4494 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4495 * sizeof(struct ftrace_ret_stack),
4496 GFP_KERNEL);
4497 if (!ret_stack)
4498 return;
4499 per_cpu(idle_ret_stack, cpu) = ret_stack;
4500 }
4501 graph_init_task(t, ret_stack);
4502 }
4503}
4504
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004505/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004506void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004507{
Steven Rostedt84047e32009-06-02 16:51:55 -04004508 /* Make sure we do not use the parent ret_stack */
4509 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004510 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004511
Steven Rostedt597af812009-04-03 15:24:12 -04004512 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004513 struct ftrace_ret_stack *ret_stack;
4514
4515 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004516 * sizeof(struct ftrace_ret_stack),
4517 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004518 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004519 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004520 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004521 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004522}
4523
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004524void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004525{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004526 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4527
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004528 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004529 /* NULL must become visible to IRQs before we free it: */
4530 barrier();
4531
4532 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004533}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004534
4535void ftrace_graph_stop(void)
4536{
4537 ftrace_stop();
4538}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004539#endif