blob: e1ee07f81ca23b922f764594061757a537d094bf [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 Rostedt06a51d92011-12-19 19:07:36 -05001002static bool ftrace_hash_empty(struct ftrace_hash *hash)
1003{
1004 return !hash || !hash->count;
1005}
1006
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001007static struct ftrace_func_entry *
1008ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1009{
1010 unsigned long key;
1011 struct ftrace_func_entry *entry;
1012 struct hlist_head *hhd;
1013 struct hlist_node *n;
1014
Steven Rostedt06a51d92011-12-19 19:07:36 -05001015 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001016 return NULL;
1017
1018 if (hash->size_bits > 0)
1019 key = hash_long(ip, hash->size_bits);
1020 else
1021 key = 0;
1022
1023 hhd = &hash->buckets[key];
1024
1025 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1026 if (entry->ip == ip)
1027 return entry;
1028 }
1029 return NULL;
1030}
1031
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001032static void __add_hash_entry(struct ftrace_hash *hash,
1033 struct ftrace_func_entry *entry)
1034{
1035 struct hlist_head *hhd;
1036 unsigned long key;
1037
1038 if (hash->size_bits)
1039 key = hash_long(entry->ip, hash->size_bits);
1040 else
1041 key = 0;
1042
1043 hhd = &hash->buckets[key];
1044 hlist_add_head(&entry->hlist, hhd);
1045 hash->count++;
1046}
1047
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001048static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1049{
1050 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001051
1052 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1053 if (!entry)
1054 return -ENOMEM;
1055
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001056 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001057 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001058
1059 return 0;
1060}
1061
1062static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001063free_hash_entry(struct ftrace_hash *hash,
1064 struct ftrace_func_entry *entry)
1065{
1066 hlist_del(&entry->hlist);
1067 kfree(entry);
1068 hash->count--;
1069}
1070
1071static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001072remove_hash_entry(struct ftrace_hash *hash,
1073 struct ftrace_func_entry *entry)
1074{
1075 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001076 hash->count--;
1077}
1078
1079static void ftrace_hash_clear(struct ftrace_hash *hash)
1080{
1081 struct hlist_head *hhd;
1082 struct hlist_node *tp, *tn;
1083 struct ftrace_func_entry *entry;
1084 int size = 1 << hash->size_bits;
1085 int i;
1086
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001087 if (!hash->count)
1088 return;
1089
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001090 for (i = 0; i < size; i++) {
1091 hhd = &hash->buckets[i];
1092 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001093 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001094 }
1095 FTRACE_WARN_ON(hash->count);
1096}
1097
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001098static void free_ftrace_hash(struct ftrace_hash *hash)
1099{
1100 if (!hash || hash == EMPTY_HASH)
1101 return;
1102 ftrace_hash_clear(hash);
1103 kfree(hash->buckets);
1104 kfree(hash);
1105}
1106
Steven Rostedt07fd5512011-05-05 18:03:47 -04001107static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1108{
1109 struct ftrace_hash *hash;
1110
1111 hash = container_of(rcu, struct ftrace_hash, rcu);
1112 free_ftrace_hash(hash);
1113}
1114
1115static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1116{
1117 if (!hash || hash == EMPTY_HASH)
1118 return;
1119 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1120}
1121
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001122static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1123{
1124 struct ftrace_hash *hash;
1125 int size;
1126
1127 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1128 if (!hash)
1129 return NULL;
1130
1131 size = 1 << size_bits;
1132 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1133
1134 if (!hash->buckets) {
1135 kfree(hash);
1136 return NULL;
1137 }
1138
1139 hash->size_bits = size_bits;
1140
1141 return hash;
1142}
1143
1144static struct ftrace_hash *
1145alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1146{
1147 struct ftrace_func_entry *entry;
1148 struct ftrace_hash *new_hash;
1149 struct hlist_node *tp;
1150 int size;
1151 int ret;
1152 int i;
1153
1154 new_hash = alloc_ftrace_hash(size_bits);
1155 if (!new_hash)
1156 return NULL;
1157
1158 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001159 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001160 return new_hash;
1161
1162 size = 1 << hash->size_bits;
1163 for (i = 0; i < size; i++) {
1164 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1165 ret = add_hash_entry(new_hash, entry->ip);
1166 if (ret < 0)
1167 goto free_hash;
1168 }
1169 }
1170
1171 FTRACE_WARN_ON(new_hash->count != hash->count);
1172
1173 return new_hash;
1174
1175 free_hash:
1176 free_ftrace_hash(new_hash);
1177 return NULL;
1178}
1179
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001180static void
1181ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1182static void
1183ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1184
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001185static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001186ftrace_hash_move(struct ftrace_ops *ops, int enable,
1187 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001188{
1189 struct ftrace_func_entry *entry;
1190 struct hlist_node *tp, *tn;
1191 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001192 struct ftrace_hash *old_hash;
1193 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001194 unsigned long key;
1195 int size = src->count;
1196 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001197 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001198 int i;
1199
1200 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001201 * Remove the current set, update the hash and add
1202 * them back.
1203 */
1204 ftrace_hash_rec_disable(ops, enable);
1205
1206 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001207 * If the new source is empty, just free dst and assign it
1208 * the empty_hash.
1209 */
1210 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001211 free_ftrace_hash_rcu(*dst);
1212 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001213 /* still need to update the function records */
1214 ret = 0;
1215 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001216 }
1217
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001218 /*
1219 * Make the hash size about 1/2 the # found
1220 */
1221 for (size /= 2; size; size >>= 1)
1222 bits++;
1223
1224 /* Don't allocate too much */
1225 if (bits > FTRACE_HASH_MAX_BITS)
1226 bits = FTRACE_HASH_MAX_BITS;
1227
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001228 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001229 new_hash = alloc_ftrace_hash(bits);
1230 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001231 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001232
1233 size = 1 << src->size_bits;
1234 for (i = 0; i < size; i++) {
1235 hhd = &src->buckets[i];
1236 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1237 if (bits > 0)
1238 key = hash_long(entry->ip, bits);
1239 else
1240 key = 0;
1241 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001242 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001243 }
1244 }
1245
Steven Rostedt07fd5512011-05-05 18:03:47 -04001246 old_hash = *dst;
1247 rcu_assign_pointer(*dst, new_hash);
1248 free_ftrace_hash_rcu(old_hash);
1249
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001250 ret = 0;
1251 out:
1252 /*
1253 * Enable regardless of ret:
1254 * On success, we enable the new hash.
1255 * On failure, we re-enable the original hash.
1256 */
1257 ftrace_hash_rec_enable(ops, enable);
1258
1259 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001260}
1261
Steven Rostedt265c8312009-02-13 12:43:56 -05001262/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001263 * Test the hashes for this ops to see if we want to call
1264 * the ops->func or not.
1265 *
1266 * It's a match if the ip is in the ops->filter_hash or
1267 * the filter_hash does not exist or is empty,
1268 * AND
1269 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001270 *
1271 * This needs to be called with preemption disabled as
1272 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001273 */
1274static int
1275ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1276{
1277 struct ftrace_hash *filter_hash;
1278 struct ftrace_hash *notrace_hash;
1279 int ret;
1280
Steven Rostedtb8489142011-05-04 09:27:52 -04001281 filter_hash = rcu_dereference_raw(ops->filter_hash);
1282 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1283
Steven Rostedt06a51d92011-12-19 19:07:36 -05001284 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001285 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001286 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001287 !ftrace_lookup_ip(notrace_hash, ip)))
1288 ret = 1;
1289 else
1290 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001291
1292 return ret;
1293}
1294
1295/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001296 * This is a double for. Do not use 'break' to break out of the loop,
1297 * you must use a goto.
1298 */
1299#define do_for_each_ftrace_rec(pg, rec) \
1300 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1301 int _____i; \
1302 for (_____i = 0; _____i < pg->index; _____i++) { \
1303 rec = &pg->records[_____i];
1304
1305#define while_for_each_ftrace_rec() \
1306 } \
1307 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301308
Steven Rostedt5855fea2011-12-16 19:27:42 -05001309
1310static int ftrace_cmp_recs(const void *a, const void *b)
1311{
1312 const struct dyn_ftrace *reca = a;
1313 const struct dyn_ftrace *recb = b;
1314
1315 if (reca->ip > recb->ip)
1316 return 1;
1317 if (reca->ip < recb->ip)
1318 return -1;
1319 return 0;
1320}
1321
Steven Rostedtc88fd862011-08-16 09:53:39 -04001322/**
1323 * ftrace_location - return true if the ip giving is a traced location
1324 * @ip: the instruction pointer to check
1325 *
1326 * Returns 1 if @ip given is a pointer to a ftrace location.
1327 * That is, the instruction that is either a NOP or call to
1328 * the function tracer. It checks the ftrace internal tables to
1329 * determine if the address belongs or not.
1330 */
1331int ftrace_location(unsigned long ip)
1332{
1333 struct ftrace_page *pg;
1334 struct dyn_ftrace *rec;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001335 struct dyn_ftrace key;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001336
Steven Rostedt5855fea2011-12-16 19:27:42 -05001337 key.ip = ip;
1338
1339 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1340 rec = bsearch(&key, pg->records, pg->index,
1341 sizeof(struct dyn_ftrace),
1342 ftrace_cmp_recs);
1343 if (rec)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001344 return 1;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001345 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001346
1347 return 0;
1348}
1349
Steven Rostedted926f92011-05-03 13:25:24 -04001350static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1351 int filter_hash,
1352 bool inc)
1353{
1354 struct ftrace_hash *hash;
1355 struct ftrace_hash *other_hash;
1356 struct ftrace_page *pg;
1357 struct dyn_ftrace *rec;
1358 int count = 0;
1359 int all = 0;
1360
1361 /* Only update if the ops has been registered */
1362 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1363 return;
1364
1365 /*
1366 * In the filter_hash case:
1367 * If the count is zero, we update all records.
1368 * Otherwise we just update the items in the hash.
1369 *
1370 * In the notrace_hash case:
1371 * We enable the update in the hash.
1372 * As disabling notrace means enabling the tracing,
1373 * and enabling notrace means disabling, the inc variable
1374 * gets inversed.
1375 */
1376 if (filter_hash) {
1377 hash = ops->filter_hash;
1378 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001379 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001380 all = 1;
1381 } else {
1382 inc = !inc;
1383 hash = ops->notrace_hash;
1384 other_hash = ops->filter_hash;
1385 /*
1386 * If the notrace hash has no items,
1387 * then there's nothing to do.
1388 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001389 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001390 return;
1391 }
1392
1393 do_for_each_ftrace_rec(pg, rec) {
1394 int in_other_hash = 0;
1395 int in_hash = 0;
1396 int match = 0;
1397
1398 if (all) {
1399 /*
1400 * Only the filter_hash affects all records.
1401 * Update if the record is not in the notrace hash.
1402 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001403 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001404 match = 1;
1405 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001406 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1407 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001408
1409 /*
1410 *
1411 */
1412 if (filter_hash && in_hash && !in_other_hash)
1413 match = 1;
1414 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001415 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001416 match = 1;
1417 }
1418 if (!match)
1419 continue;
1420
1421 if (inc) {
1422 rec->flags++;
1423 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1424 return;
1425 } else {
1426 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1427 return;
1428 rec->flags--;
1429 }
1430 count++;
1431 /* Shortcut, if we handled all records, we are done. */
1432 if (!all && count == hash->count)
1433 return;
1434 } while_for_each_ftrace_rec();
1435}
1436
1437static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1438 int filter_hash)
1439{
1440 __ftrace_hash_rec_update(ops, filter_hash, 0);
1441}
1442
1443static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1444 int filter_hash)
1445{
1446 __ftrace_hash_rec_update(ops, filter_hash, 1);
1447}
1448
Ingo Molnare309b412008-05-12 21:20:51 +02001449static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001450{
Steven Rostedta7900872011-12-16 16:23:44 -05001451 if (ftrace_pages->index == ftrace_pages->size) {
1452 /* We should have allocated enough */
1453 if (WARN_ON(!ftrace_pages->next))
1454 return NULL;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001455 ftrace_pages = ftrace_pages->next;
1456 }
1457
1458 return &ftrace_pages->records[ftrace_pages->index++];
1459}
1460
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001461static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001462ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001463{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001464 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001465
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001466 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001467 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001468
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001469 rec = ftrace_alloc_dyn_node(ip);
1470 if (!rec)
1471 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001472
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001473 rec->ip = ip;
Steven Rostedt3d083392008-05-12 21:20:42 +02001474
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001475 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001476}
1477
Steven Rostedt05736a42008-09-22 14:55:47 -07001478static void print_ip_ins(const char *fmt, unsigned char *p)
1479{
1480 int i;
1481
1482 printk(KERN_CONT "%s", fmt);
1483
1484 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1485 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1486}
1487
Steven Rostedtc88fd862011-08-16 09:53:39 -04001488/**
1489 * ftrace_bug - report and shutdown function tracer
1490 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1491 * @ip: The address that failed
1492 *
1493 * The arch code that enables or disables the function tracing
1494 * can call ftrace_bug() when it has detected a problem in
1495 * modifying the code. @failed should be one of either:
1496 * EFAULT - if the problem happens on reading the @ip address
1497 * EINVAL - if what is read at @ip is not what was expected
1498 * EPERM - if the problem happens on writting to the @ip address
1499 */
1500void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001501{
1502 switch (failed) {
1503 case -EFAULT:
1504 FTRACE_WARN_ON_ONCE(1);
1505 pr_info("ftrace faulted on modifying ");
1506 print_ip_sym(ip);
1507 break;
1508 case -EINVAL:
1509 FTRACE_WARN_ON_ONCE(1);
1510 pr_info("ftrace failed to modify ");
1511 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001512 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001513 printk(KERN_CONT "\n");
1514 break;
1515 case -EPERM:
1516 FTRACE_WARN_ON_ONCE(1);
1517 pr_info("ftrace faulted on writing ");
1518 print_ip_sym(ip);
1519 break;
1520 default:
1521 FTRACE_WARN_ON_ONCE(1);
1522 pr_info("ftrace faulted on unknown error ");
1523 print_ip_sym(ip);
1524 }
1525}
1526
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001527
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001528/* Return 1 if the address range is reserved for ftrace */
1529int ftrace_text_reserved(void *start, void *end)
1530{
1531 struct dyn_ftrace *rec;
1532 struct ftrace_page *pg;
1533
1534 do_for_each_ftrace_rec(pg, rec) {
1535 if (rec->ip <= (unsigned long)end &&
1536 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1537 return 1;
1538 } while_for_each_ftrace_rec();
1539 return 0;
1540}
1541
Steven Rostedtc88fd862011-08-16 09:53:39 -04001542static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001543{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001544 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001545
Steven Rostedt982c3502008-11-15 16:31:41 -05001546 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001547 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001548 *
Steven Rostedted926f92011-05-03 13:25:24 -04001549 * If the record has a ref count, then we need to enable it
1550 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001551 *
Steven Rostedted926f92011-05-03 13:25:24 -04001552 * Otherwise we make sure its disabled.
1553 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001554 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001555 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001556 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001557 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001558 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001559
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001560 /* If the state of this record hasn't changed, then do nothing */
1561 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001562 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001563
1564 if (flag) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001565 if (update)
1566 rec->flags |= FTRACE_FL_ENABLED;
1567 return FTRACE_UPDATE_MAKE_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001568 }
1569
Steven Rostedtc88fd862011-08-16 09:53:39 -04001570 if (update)
1571 rec->flags &= ~FTRACE_FL_ENABLED;
1572
1573 return FTRACE_UPDATE_MAKE_NOP;
1574}
1575
1576/**
1577 * ftrace_update_record, set a record that now is tracing or not
1578 * @rec: the record to update
1579 * @enable: set to 1 if the record is tracing, zero to force disable
1580 *
1581 * The records that represent all functions that can be traced need
1582 * to be updated when tracing has been enabled.
1583 */
1584int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1585{
1586 return ftrace_check_record(rec, enable, 1);
1587}
1588
1589/**
1590 * ftrace_test_record, check if the record has been enabled or not
1591 * @rec: the record to test
1592 * @enable: set to 1 to check if enabled, 0 if it is disabled
1593 *
1594 * The arch code may need to test if a record is already set to
1595 * tracing to determine how to modify the function code that it
1596 * represents.
1597 */
1598int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1599{
1600 return ftrace_check_record(rec, enable, 0);
1601}
1602
1603static int
1604__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1605{
1606 unsigned long ftrace_addr;
1607 int ret;
1608
1609 ftrace_addr = (unsigned long)FTRACE_ADDR;
1610
1611 ret = ftrace_update_record(rec, enable);
1612
1613 switch (ret) {
1614 case FTRACE_UPDATE_IGNORE:
1615 return 0;
1616
1617 case FTRACE_UPDATE_MAKE_CALL:
1618 return ftrace_make_call(rec, ftrace_addr);
1619
1620 case FTRACE_UPDATE_MAKE_NOP:
1621 return ftrace_make_nop(NULL, rec, ftrace_addr);
1622 }
1623
1624 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001625}
1626
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001627static void ftrace_replace_code(int update)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001628{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001629 struct dyn_ftrace *rec;
1630 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001631 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001632
Steven Rostedt45a4a232011-04-21 23:16:46 -04001633 if (unlikely(ftrace_disabled))
1634 return;
1635
Steven Rostedt265c8312009-02-13 12:43:56 -05001636 do_for_each_ftrace_rec(pg, rec) {
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001637 failed = __ftrace_replace_code(rec, update);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001638 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001639 ftrace_bug(failed, rec->ip);
1640 /* Stop processing */
1641 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001642 }
1643 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001644}
1645
Steven Rostedtc88fd862011-08-16 09:53:39 -04001646struct ftrace_rec_iter {
1647 struct ftrace_page *pg;
1648 int index;
1649};
1650
1651/**
1652 * ftrace_rec_iter_start, start up iterating over traced functions
1653 *
1654 * Returns an iterator handle that is used to iterate over all
1655 * the records that represent address locations where functions
1656 * are traced.
1657 *
1658 * May return NULL if no records are available.
1659 */
1660struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1661{
1662 /*
1663 * We only use a single iterator.
1664 * Protected by the ftrace_lock mutex.
1665 */
1666 static struct ftrace_rec_iter ftrace_rec_iter;
1667 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1668
1669 iter->pg = ftrace_pages_start;
1670 iter->index = 0;
1671
1672 /* Could have empty pages */
1673 while (iter->pg && !iter->pg->index)
1674 iter->pg = iter->pg->next;
1675
1676 if (!iter->pg)
1677 return NULL;
1678
1679 return iter;
1680}
1681
1682/**
1683 * ftrace_rec_iter_next, get the next record to process.
1684 * @iter: The handle to the iterator.
1685 *
1686 * Returns the next iterator after the given iterator @iter.
1687 */
1688struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1689{
1690 iter->index++;
1691
1692 if (iter->index >= iter->pg->index) {
1693 iter->pg = iter->pg->next;
1694 iter->index = 0;
1695
1696 /* Could have empty pages */
1697 while (iter->pg && !iter->pg->index)
1698 iter->pg = iter->pg->next;
1699 }
1700
1701 if (!iter->pg)
1702 return NULL;
1703
1704 return iter;
1705}
1706
1707/**
1708 * ftrace_rec_iter_record, get the record at the iterator location
1709 * @iter: The current iterator location
1710 *
1711 * Returns the record that the current @iter is at.
1712 */
1713struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1714{
1715 return &iter->pg->records[iter->index];
1716}
1717
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301718static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001719ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001720{
1721 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001722 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001723
1724 ip = rec->ip;
1725
Steven Rostedt45a4a232011-04-21 23:16:46 -04001726 if (unlikely(ftrace_disabled))
1727 return 0;
1728
Shaohua Li25aac9d2009-01-09 11:29:40 +08001729 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001730 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001731 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301732 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001733 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301734 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001735}
1736
Steven Rostedt000ab692009-02-17 13:35:06 -05001737/*
1738 * archs can override this function if they must do something
1739 * before the modifying code is performed.
1740 */
1741int __weak ftrace_arch_code_modify_prepare(void)
1742{
1743 return 0;
1744}
1745
1746/*
1747 * archs can override this function if they must do something
1748 * after the modifying code is performed.
1749 */
1750int __weak ftrace_arch_code_modify_post_process(void)
1751{
1752 return 0;
1753}
1754
Ingo Molnare309b412008-05-12 21:20:51 +02001755static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001756{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001757 int *command = data;
1758
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001759 if (*command & FTRACE_UPDATE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001760 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001761 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001762 ftrace_replace_code(0);
1763
1764 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1765 ftrace_update_ftrace_func(ftrace_trace_function);
1766
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001767 if (*command & FTRACE_START_FUNC_RET)
1768 ftrace_enable_ftrace_graph_caller();
1769 else if (*command & FTRACE_STOP_FUNC_RET)
1770 ftrace_disable_ftrace_graph_caller();
1771
Steven Rostedtc88fd862011-08-16 09:53:39 -04001772 return 0;
1773}
1774
1775/**
1776 * ftrace_run_stop_machine, go back to the stop machine method
1777 * @command: The command to tell ftrace what to do
1778 *
1779 * If an arch needs to fall back to the stop machine method, the
1780 * it can call this function.
1781 */
1782void ftrace_run_stop_machine(int command)
1783{
1784 stop_machine(__ftrace_modify_code, &command, NULL);
1785}
1786
1787/**
1788 * arch_ftrace_update_code, modify the code to trace or not trace
1789 * @command: The command that needs to be done
1790 *
1791 * Archs can override this function if it does not need to
1792 * run stop_machine() to modify code.
1793 */
1794void __weak arch_ftrace_update_code(int command)
1795{
1796 ftrace_run_stop_machine(command);
1797}
1798
1799static void ftrace_run_update_code(int command)
1800{
1801 int ret;
1802
1803 ret = ftrace_arch_code_modify_prepare();
1804 FTRACE_WARN_ON(ret);
1805 if (ret)
1806 return;
1807 /*
1808 * Do not call function tracer while we update the code.
1809 * We are in stop machine.
1810 */
1811 function_trace_stop++;
1812
1813 /*
1814 * By default we use stop_machine() to modify the code.
1815 * But archs can do what ever they want as long as it
1816 * is safe. The stop_machine() is the safest, but also
1817 * produces the most overhead.
1818 */
1819 arch_ftrace_update_code(command);
1820
Steven Rostedt6331c282011-07-13 15:11:02 -04001821#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1822 /*
1823 * For archs that call ftrace_test_stop_func(), we must
1824 * wait till after we update all the function callers
1825 * before we update the callback. This keeps different
1826 * ops that record different functions from corrupting
1827 * each other.
1828 */
1829 __ftrace_trace_function = __ftrace_trace_function_delay;
1830#endif
1831 function_trace_stop--;
1832
Steven Rostedt000ab692009-02-17 13:35:06 -05001833 ret = ftrace_arch_code_modify_post_process();
1834 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001835}
1836
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001837static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001838static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001839static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001840
1841static void ftrace_startup_enable(int command)
1842{
1843 if (saved_ftrace_func != ftrace_trace_function) {
1844 saved_ftrace_func = ftrace_trace_function;
1845 command |= FTRACE_UPDATE_TRACE_FUNC;
1846 }
1847
1848 if (!command || !ftrace_enabled)
1849 return;
1850
1851 ftrace_run_update_code(command);
1852}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001853
Steven Rostedta1cd6172011-05-23 15:24:25 -04001854static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001855{
Steven Rostedtb8489142011-05-04 09:27:52 -04001856 bool hash_enable = true;
1857
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001858 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001859 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001860
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001861 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001862 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001863
Steven Rostedtb8489142011-05-04 09:27:52 -04001864 /* ops marked global share the filter hashes */
1865 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1866 ops = &global_ops;
1867 /* Don't update hash if global is already set */
1868 if (global_start_up)
1869 hash_enable = false;
1870 global_start_up++;
1871 }
1872
Steven Rostedted926f92011-05-03 13:25:24 -04001873 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001874 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001875 ftrace_hash_rec_enable(ops, 1);
1876
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001877 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001878
1879 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001880}
1881
Steven Rostedtbd69c302011-05-03 21:55:54 -04001882static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001883{
Steven Rostedtb8489142011-05-04 09:27:52 -04001884 bool hash_disable = true;
1885
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001886 if (unlikely(ftrace_disabled))
1887 return;
1888
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001889 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001890 /*
1891 * Just warn in case of unbalance, no need to kill ftrace, it's not
1892 * critical but the ftrace_call callers may be never nopped again after
1893 * further ftrace uses.
1894 */
1895 WARN_ON_ONCE(ftrace_start_up < 0);
1896
Steven Rostedtb8489142011-05-04 09:27:52 -04001897 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1898 ops = &global_ops;
1899 global_start_up--;
1900 WARN_ON_ONCE(global_start_up < 0);
1901 /* Don't update hash if global still has users */
1902 if (global_start_up) {
1903 WARN_ON_ONCE(!ftrace_start_up);
1904 hash_disable = false;
1905 }
1906 }
1907
1908 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04001909 ftrace_hash_rec_disable(ops, 1);
1910
Steven Rostedtb8489142011-05-04 09:27:52 -04001911 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04001912 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001913
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001914 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001915
1916 if (saved_ftrace_func != ftrace_trace_function) {
1917 saved_ftrace_func = ftrace_trace_function;
1918 command |= FTRACE_UPDATE_TRACE_FUNC;
1919 }
1920
1921 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001922 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001923
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001924 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001925}
1926
Ingo Molnare309b412008-05-12 21:20:51 +02001927static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001928{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001929 if (unlikely(ftrace_disabled))
1930 return;
1931
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001932 /* Force update next time */
1933 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001934 /* ftrace_start_up is true if we want ftrace running */
1935 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001936 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001937}
1938
Ingo Molnare309b412008-05-12 21:20:51 +02001939static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001940{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001941 if (unlikely(ftrace_disabled))
1942 return;
1943
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001944 /* ftrace_start_up is true if ftrace is running */
1945 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001946 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001947}
1948
Steven Rostedt3d083392008-05-12 21:20:42 +02001949static cycle_t ftrace_update_time;
1950static unsigned long ftrace_update_cnt;
1951unsigned long ftrace_update_tot_cnt;
1952
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001953static int ops_traces_mod(struct ftrace_ops *ops)
1954{
1955 struct ftrace_hash *hash;
1956
1957 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001958 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001959}
1960
Steven Rostedt31e88902008-11-14 16:21:19 -08001961static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001962{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001963 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001964 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301965 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001966 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001967 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001968
1969 /*
1970 * When adding a module, we need to check if tracers are
1971 * currently enabled and if they are set to trace all functions.
1972 * If they are, we need to enable the module functions as well
1973 * as update the reference counts for those function records.
1974 */
1975 if (mod) {
1976 struct ftrace_ops *ops;
1977
1978 for (ops = ftrace_ops_list;
1979 ops != &ftrace_list_end; ops = ops->next) {
1980 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
1981 ops_traces_mod(ops))
1982 ref++;
1983 }
1984 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001985
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001986 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001987 ftrace_update_cnt = 0;
1988
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001989 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301990
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001991 for (i = 0; i < pg->index; i++) {
1992 /* If something went wrong, bail without enabling anything */
1993 if (unlikely(ftrace_disabled))
1994 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001995
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001996 p = &pg->records[i];
1997 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301998
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001999 /*
2000 * Do the initial record conversion from mcount jump
2001 * to the NOP instructions.
2002 */
2003 if (!ftrace_code_disable(mod, p))
2004 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002005
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002006 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002007
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002008 /*
2009 * If the tracing is enabled, go ahead and enable the record.
2010 *
2011 * The reason not to enable the record immediatelly is the
2012 * inherent check of ftrace_make_nop/ftrace_make_call for
2013 * correct previous instructions. Making first the NOP
2014 * conversion puts the module to the correct state, thus
2015 * passing the ftrace_make_call check.
2016 */
2017 if (ftrace_start_up && ref) {
2018 int failed = __ftrace_replace_code(p, 1);
2019 if (failed)
2020 ftrace_bug(failed, p->ip);
2021 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002022 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002023 }
2024
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002025 ftrace_new_pgs = NULL;
2026
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002027 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002028 ftrace_update_time = stop - start;
2029 ftrace_update_tot_cnt += ftrace_update_cnt;
2030
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002031 return 0;
2032}
2033
Steven Rostedta7900872011-12-16 16:23:44 -05002034static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002035{
Steven Rostedta7900872011-12-16 16:23:44 -05002036 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002037 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002038
Steven Rostedta7900872011-12-16 16:23:44 -05002039 if (WARN_ON(!count))
2040 return -EINVAL;
2041
2042 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002043
2044 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002045 * We want to fill as much as possible. No more than a page
2046 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002047 */
Steven Rostedta7900872011-12-16 16:23:44 -05002048 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2049 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002050
Steven Rostedta7900872011-12-16 16:23:44 -05002051 again:
2052 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2053
2054 if (!pg->records) {
2055 /* if we can't allocate this size, try something smaller */
2056 if (!order)
2057 return -ENOMEM;
2058 order >>= 1;
2059 goto again;
2060 }
2061
2062 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2063 pg->size = cnt;
2064
2065 if (cnt > count)
2066 cnt = count;
2067
2068 return cnt;
2069}
2070
2071static struct ftrace_page *
2072ftrace_allocate_pages(unsigned long num_to_init)
2073{
2074 struct ftrace_page *start_pg;
2075 struct ftrace_page *pg;
2076 int order;
2077 int cnt;
2078
2079 if (!num_to_init)
2080 return 0;
2081
2082 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2083 if (!pg)
2084 return NULL;
2085
2086 /*
2087 * Try to allocate as much as possible in one continues
2088 * location that fills in all of the space. We want to
2089 * waste as little space as possible.
2090 */
2091 for (;;) {
2092 cnt = ftrace_allocate_records(pg, num_to_init);
2093 if (cnt < 0)
2094 goto free_pages;
2095
2096 num_to_init -= cnt;
2097 if (!num_to_init)
2098 break;
2099
2100 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2101 if (!pg->next)
2102 goto free_pages;
2103
2104 pg = pg->next;
2105 }
2106
2107 return start_pg;
2108
2109 free_pages:
2110 while (start_pg) {
2111 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2112 free_pages((unsigned long)pg->records, order);
2113 start_pg = pg->next;
2114 kfree(pg);
2115 pg = start_pg;
2116 }
2117 pr_info("ftrace: FAILED to allocate memory for functions\n");
2118 return NULL;
2119}
2120
2121static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2122{
2123 int cnt;
2124
2125 if (!num_to_init) {
2126 pr_info("ftrace: No functions to be traced?\n");
2127 return -1;
2128 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002129
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002130 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002131 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002132 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002133
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002134 return 0;
2135}
2136
Steven Rostedt5072c592008-05-12 21:20:43 +02002137enum {
2138 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002139 FTRACE_ITER_NOTRACE = (1 << 1),
Steven Rostedt3499e462011-04-21 22:59:12 -04002140 FTRACE_ITER_PRINTALL = (1 << 2),
2141 FTRACE_ITER_HASH = (1 << 3),
Steven Rostedt647bcd02011-05-03 14:39:21 -04002142 FTRACE_ITER_ENABLED = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02002143};
2144
2145#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2146
2147struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002148 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002149 loff_t func_pos;
2150 struct ftrace_page *pg;
2151 struct dyn_ftrace *func;
2152 struct ftrace_func_probe *probe;
2153 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002154 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002155 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002156 int hidx;
2157 int idx;
2158 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002159};
2160
Ingo Molnare309b412008-05-12 21:20:51 +02002161static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002162t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002163{
2164 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002165 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002166 struct hlist_head *hhd;
2167
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002168 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002169 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002170
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002171 if (iter->probe)
2172 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002173 retry:
2174 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2175 return NULL;
2176
2177 hhd = &ftrace_func_hash[iter->hidx];
2178
2179 if (hlist_empty(hhd)) {
2180 iter->hidx++;
2181 hnd = NULL;
2182 goto retry;
2183 }
2184
2185 if (!hnd)
2186 hnd = hhd->first;
2187 else {
2188 hnd = hnd->next;
2189 if (!hnd) {
2190 iter->hidx++;
2191 goto retry;
2192 }
2193 }
2194
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002195 if (WARN_ON_ONCE(!hnd))
2196 return NULL;
2197
2198 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2199
2200 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002201}
2202
2203static void *t_hash_start(struct seq_file *m, loff_t *pos)
2204{
2205 struct ftrace_iterator *iter = m->private;
2206 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002207 loff_t l;
2208
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002209 if (iter->func_pos > *pos)
2210 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002211
Li Zefand82d6242009-06-24 09:54:54 +08002212 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002213 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002214 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002215 if (!p)
2216 break;
2217 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002218 if (!p)
2219 return NULL;
2220
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002221 /* Only set this if we have an item */
2222 iter->flags |= FTRACE_ITER_HASH;
2223
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002224 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002225}
2226
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002227static int
2228t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002229{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002230 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002231
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002232 rec = iter->probe;
2233 if (WARN_ON_ONCE(!rec))
2234 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002235
Steven Rostedt809dcf22009-02-16 23:06:01 -05002236 if (rec->ops->print)
2237 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2238
Steven Rostedtb375a112009-09-17 00:05:58 -04002239 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002240
2241 if (rec->data)
2242 seq_printf(m, ":%p", rec->data);
2243 seq_putc(m, '\n');
2244
2245 return 0;
2246}
2247
2248static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002249t_next(struct seq_file *m, void *v, loff_t *pos)
2250{
2251 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002252 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002253 struct dyn_ftrace *rec = NULL;
2254
Steven Rostedt45a4a232011-04-21 23:16:46 -04002255 if (unlikely(ftrace_disabled))
2256 return NULL;
2257
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002258 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002259 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002260
Steven Rostedt5072c592008-05-12 21:20:43 +02002261 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002262 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002263
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002264 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002265 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002266
Steven Rostedt5072c592008-05-12 21:20:43 +02002267 retry:
2268 if (iter->idx >= iter->pg->index) {
2269 if (iter->pg->next) {
2270 iter->pg = iter->pg->next;
2271 iter->idx = 0;
2272 goto retry;
2273 }
2274 } else {
2275 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002276 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002277 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002278
Steven Rostedt41c52c02008-05-22 11:46:33 -04002279 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002280 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2281
2282 ((iter->flags & FTRACE_ITER_ENABLED) &&
2283 !(rec->flags & ~FTRACE_FL_MASK))) {
2284
Steven Rostedt5072c592008-05-12 21:20:43 +02002285 rec = NULL;
2286 goto retry;
2287 }
2288 }
2289
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002290 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002291 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002292
2293 iter->func = rec;
2294
2295 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002296}
2297
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002298static void reset_iter_read(struct ftrace_iterator *iter)
2299{
2300 iter->pos = 0;
2301 iter->func_pos = 0;
2302 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002303}
2304
2305static void *t_start(struct seq_file *m, loff_t *pos)
2306{
2307 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002308 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002309 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002310 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002311
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002312 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002313
2314 if (unlikely(ftrace_disabled))
2315 return NULL;
2316
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002317 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002318 * If an lseek was done, then reset and start from beginning.
2319 */
2320 if (*pos < iter->pos)
2321 reset_iter_read(iter);
2322
2323 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002324 * For set_ftrace_filter reading, if we have the filter
2325 * off, we can short cut and just print out that all
2326 * functions are enabled.
2327 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002328 if (iter->flags & FTRACE_ITER_FILTER &&
2329 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002330 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002331 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002332 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002333 /* reset in case of seek/pread */
2334 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002335 return iter;
2336 }
2337
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002338 if (iter->flags & FTRACE_ITER_HASH)
2339 return t_hash_start(m, pos);
2340
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002341 /*
2342 * Unfortunately, we need to restart at ftrace_pages_start
2343 * every time we let go of the ftrace_mutex. This is because
2344 * those pointers can change without the lock.
2345 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002346 iter->pg = ftrace_pages_start;
2347 iter->idx = 0;
2348 for (l = 0; l <= *pos; ) {
2349 p = t_next(m, p, &l);
2350 if (!p)
2351 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002352 }
walimis5821e1b2008-11-15 15:19:06 +08002353
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002354 if (!p) {
2355 if (iter->flags & FTRACE_ITER_FILTER)
2356 return t_hash_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002357
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002358 return NULL;
2359 }
2360
2361 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002362}
2363
2364static void t_stop(struct seq_file *m, void *p)
2365{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002366 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002367}
2368
2369static int t_show(struct seq_file *m, void *v)
2370{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002371 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002372 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002373
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002374 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002375 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002376
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002377 if (iter->flags & FTRACE_ITER_PRINTALL) {
2378 seq_printf(m, "#### all functions enabled ####\n");
2379 return 0;
2380 }
2381
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002382 rec = iter->func;
2383
Steven Rostedt5072c592008-05-12 21:20:43 +02002384 if (!rec)
2385 return 0;
2386
Steven Rostedt647bcd02011-05-03 14:39:21 -04002387 seq_printf(m, "%ps", (void *)rec->ip);
2388 if (iter->flags & FTRACE_ITER_ENABLED)
2389 seq_printf(m, " (%ld)",
2390 rec->flags & ~FTRACE_FL_MASK);
2391 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002392
2393 return 0;
2394}
2395
James Morris88e9d342009-09-22 16:43:43 -07002396static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002397 .start = t_start,
2398 .next = t_next,
2399 .stop = t_stop,
2400 .show = t_show,
2401};
2402
Ingo Molnare309b412008-05-12 21:20:51 +02002403static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002404ftrace_avail_open(struct inode *inode, struct file *file)
2405{
2406 struct ftrace_iterator *iter;
2407 int ret;
2408
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002409 if (unlikely(ftrace_disabled))
2410 return -ENODEV;
2411
Steven Rostedt5072c592008-05-12 21:20:43 +02002412 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2413 if (!iter)
2414 return -ENOMEM;
2415
2416 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002417
2418 ret = seq_open(file, &show_ftrace_seq_ops);
2419 if (!ret) {
2420 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002421
Steven Rostedt5072c592008-05-12 21:20:43 +02002422 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002423 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002424 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002425 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002426
2427 return ret;
2428}
2429
Steven Rostedt647bcd02011-05-03 14:39:21 -04002430static int
2431ftrace_enabled_open(struct inode *inode, struct file *file)
2432{
2433 struct ftrace_iterator *iter;
2434 int ret;
2435
2436 if (unlikely(ftrace_disabled))
2437 return -ENODEV;
2438
2439 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2440 if (!iter)
2441 return -ENOMEM;
2442
2443 iter->pg = ftrace_pages_start;
2444 iter->flags = FTRACE_ITER_ENABLED;
2445
2446 ret = seq_open(file, &show_ftrace_seq_ops);
2447 if (!ret) {
2448 struct seq_file *m = file->private_data;
2449
2450 m->private = iter;
2451 } else {
2452 kfree(iter);
2453 }
2454
2455 return ret;
2456}
2457
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002458static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002459{
Steven Rostedt52baf112009-02-14 01:15:39 -05002460 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002461 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002462 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002463}
2464
Ingo Molnare309b412008-05-12 21:20:51 +02002465static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002466ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002467 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002468{
2469 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002470 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002471 int ret = 0;
2472
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002473 if (unlikely(ftrace_disabled))
2474 return -ENODEV;
2475
Steven Rostedt5072c592008-05-12 21:20:43 +02002476 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2477 if (!iter)
2478 return -ENOMEM;
2479
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002480 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2481 kfree(iter);
2482 return -ENOMEM;
2483 }
2484
Steven Rostedtf45948e2011-05-02 12:29:25 -04002485 if (flag & FTRACE_ITER_NOTRACE)
2486 hash = ops->notrace_hash;
2487 else
2488 hash = ops->filter_hash;
2489
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002490 iter->ops = ops;
2491 iter->flags = flag;
2492
2493 if (file->f_mode & FMODE_WRITE) {
2494 mutex_lock(&ftrace_lock);
2495 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2496 mutex_unlock(&ftrace_lock);
2497
2498 if (!iter->hash) {
2499 trace_parser_put(&iter->parser);
2500 kfree(iter);
2501 return -ENOMEM;
2502 }
2503 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002504
Steven Rostedt41c52c02008-05-22 11:46:33 -04002505 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002506
Steven Rostedt5072c592008-05-12 21:20:43 +02002507 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002508 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002509 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002510
2511 if (file->f_mode & FMODE_READ) {
2512 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002513
2514 ret = seq_open(file, &show_ftrace_seq_ops);
2515 if (!ret) {
2516 struct seq_file *m = file->private_data;
2517 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002518 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002519 /* Failed */
2520 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002521 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002522 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002523 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002524 } else
2525 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002526 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002527
2528 return ret;
2529}
2530
Steven Rostedt41c52c02008-05-22 11:46:33 -04002531static int
2532ftrace_filter_open(struct inode *inode, struct file *file)
2533{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002534 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002535 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002536}
2537
2538static int
2539ftrace_notrace_open(struct inode *inode, struct file *file)
2540{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002541 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002542 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002543}
2544
Ingo Molnare309b412008-05-12 21:20:51 +02002545static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002546ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02002547{
2548 loff_t ret;
2549
2550 if (file->f_mode & FMODE_READ)
2551 ret = seq_lseek(file, offset, origin);
2552 else
2553 file->f_pos = ret = 1;
2554
2555 return ret;
2556}
2557
Steven Rostedt64e7c442009-02-13 17:08:48 -05002558static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002559{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002560 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002561 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002562
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002563 switch (type) {
2564 case MATCH_FULL:
2565 if (strcmp(str, regex) == 0)
2566 matched = 1;
2567 break;
2568 case MATCH_FRONT_ONLY:
2569 if (strncmp(str, regex, len) == 0)
2570 matched = 1;
2571 break;
2572 case MATCH_MIDDLE_ONLY:
2573 if (strstr(str, regex))
2574 matched = 1;
2575 break;
2576 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002577 slen = strlen(str);
2578 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002579 matched = 1;
2580 break;
2581 }
2582
2583 return matched;
2584}
2585
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002586static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002587enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002588{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002589 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002590 int ret = 0;
2591
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002592 entry = ftrace_lookup_ip(hash, rec->ip);
2593 if (not) {
2594 /* Do nothing if it doesn't exist */
2595 if (!entry)
2596 return 0;
2597
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002598 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002599 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002600 /* Do nothing if it exists */
2601 if (entry)
2602 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002603
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002604 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002605 }
2606 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002607}
2608
Steven Rostedt64e7c442009-02-13 17:08:48 -05002609static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002610ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2611 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002612{
2613 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002614 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002615
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002616 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2617
2618 if (mod) {
2619 /* module lookup requires matching the module */
2620 if (!modname || strcmp(modname, mod))
2621 return 0;
2622
2623 /* blank search means to match all funcs in the mod */
2624 if (!len)
2625 return 1;
2626 }
2627
Steven Rostedt64e7c442009-02-13 17:08:48 -05002628 return ftrace_match(str, regex, len, type);
2629}
2630
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002631static int
2632match_records(struct ftrace_hash *hash, char *buff,
2633 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002634{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002635 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002636 struct ftrace_page *pg;
2637 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002638 int type = MATCH_FULL;
2639 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002640 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002641 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002642
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002643 if (len) {
2644 type = filter_parse_regex(buff, len, &search, &not);
2645 search_len = strlen(search);
2646 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002647
Steven Rostedt52baf112009-02-14 01:15:39 -05002648 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002649
2650 if (unlikely(ftrace_disabled))
2651 goto out_unlock;
2652
Steven Rostedt265c8312009-02-13 12:43:56 -05002653 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002654 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002655 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002656 if (ret < 0) {
2657 found = ret;
2658 goto out_unlock;
2659 }
Li Zefan311d16d2009-12-08 11:15:11 +08002660 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002661 }
2662 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002663 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002664 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002665
2666 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002667}
2668
Steven Rostedt64e7c442009-02-13 17:08:48 -05002669static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002670ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002671{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002672 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002673}
2674
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002675static int
2676ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002677{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002678 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002679
Steven Rostedt64e7c442009-02-13 17:08:48 -05002680 /* blank or '*' mean the same */
2681 if (strcmp(buff, "*") == 0)
2682 buff[0] = 0;
2683
2684 /* handle the case of 'dont filter this module' */
2685 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2686 buff[0] = 0;
2687 not = 1;
2688 }
2689
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002690 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002691}
2692
Steven Rostedtf6180772009-02-14 00:40:25 -05002693/*
2694 * We register the module command as a template to show others how
2695 * to register the a command as well.
2696 */
2697
2698static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002699ftrace_mod_callback(struct ftrace_hash *hash,
2700 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002701{
2702 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002703 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002704
2705 /*
2706 * cmd == 'mod' because we only registered this func
2707 * for the 'mod' ftrace_func_command.
2708 * But if you register one func with multiple commands,
2709 * you can tell which command was used by the cmd
2710 * parameter.
2711 */
2712
2713 /* we must have a module name */
2714 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002715 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002716
2717 mod = strsep(&param, ":");
2718 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002719 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002720
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002721 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002722 if (!ret)
2723 ret = -EINVAL;
2724 if (ret < 0)
2725 return ret;
2726
2727 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002728}
2729
2730static struct ftrace_func_command ftrace_mod_cmd = {
2731 .name = "mod",
2732 .func = ftrace_mod_callback,
2733};
2734
2735static int __init ftrace_mod_cmd_init(void)
2736{
2737 return register_ftrace_command(&ftrace_mod_cmd);
2738}
2739device_initcall(ftrace_mod_cmd_init);
2740
Steven Rostedt59df055f2009-02-14 15:29:06 -05002741static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002742function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002743{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002744 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002745 struct hlist_head *hhd;
2746 struct hlist_node *n;
2747 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002748
2749 key = hash_long(ip, FTRACE_HASH_BITS);
2750
2751 hhd = &ftrace_func_hash[key];
2752
2753 if (hlist_empty(hhd))
2754 return;
2755
2756 /*
2757 * Disable preemption for these calls to prevent a RCU grace
2758 * period. This syncs the hash iteration and freeing of items
2759 * on the hash. rcu_read_lock is too dangerous here.
2760 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002761 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002762 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2763 if (entry->ip == ip)
2764 entry->ops->func(ip, parent_ip, &entry->data);
2765 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002766 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002767}
2768
Steven Rostedtb6887d72009-02-17 12:32:04 -05002769static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002770{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002771 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002772};
2773
Steven Rostedtb6887d72009-02-17 12:32:04 -05002774static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002775
Steven Rostedtb6887d72009-02-17 12:32:04 -05002776static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002777{
Steven Rostedtb8489142011-05-04 09:27:52 -04002778 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002779 int i;
2780
Steven Rostedtb6887d72009-02-17 12:32:04 -05002781 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002782 return;
2783
2784 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2785 struct hlist_head *hhd = &ftrace_func_hash[i];
2786 if (hhd->first)
2787 break;
2788 }
2789 /* Nothing registered? */
2790 if (i == FTRACE_FUNC_HASHSIZE)
2791 return;
2792
Steven Rostedtb8489142011-05-04 09:27:52 -04002793 ret = __register_ftrace_function(&trace_probe_ops);
2794 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002795 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002796
Steven Rostedtb6887d72009-02-17 12:32:04 -05002797 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002798}
2799
Steven Rostedtb6887d72009-02-17 12:32:04 -05002800static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002801{
Steven Rostedtb8489142011-05-04 09:27:52 -04002802 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002803 int i;
2804
Steven Rostedtb6887d72009-02-17 12:32:04 -05002805 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002806 return;
2807
2808 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2809 struct hlist_head *hhd = &ftrace_func_hash[i];
2810 if (hhd->first)
2811 return;
2812 }
2813
2814 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002815 ret = __unregister_ftrace_function(&trace_probe_ops);
2816 if (!ret)
2817 ftrace_shutdown(&trace_probe_ops, 0);
2818
Steven Rostedtb6887d72009-02-17 12:32:04 -05002819 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002820}
2821
2822
2823static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2824{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002825 struct ftrace_func_probe *entry =
2826 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002827
2828 if (entry->ops->free)
2829 entry->ops->free(&entry->data);
2830 kfree(entry);
2831}
2832
2833
2834int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002835register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002836 void *data)
2837{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002838 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002839 struct ftrace_page *pg;
2840 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002841 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002842 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002843 int count = 0;
2844 char *search;
2845
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002846 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002847 len = strlen(search);
2848
Steven Rostedtb6887d72009-02-17 12:32:04 -05002849 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002850 if (WARN_ON(not))
2851 return -EINVAL;
2852
2853 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002854
Steven Rostedt45a4a232011-04-21 23:16:46 -04002855 if (unlikely(ftrace_disabled))
2856 goto out_unlock;
2857
Steven Rostedt59df055f2009-02-14 15:29:06 -05002858 do_for_each_ftrace_rec(pg, rec) {
2859
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002860 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002861 continue;
2862
2863 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2864 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002865 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002866 if (!count)
2867 count = -ENOMEM;
2868 goto out_unlock;
2869 }
2870
2871 count++;
2872
2873 entry->data = data;
2874
2875 /*
2876 * The caller might want to do something special
2877 * for each function we find. We call the callback
2878 * to give the caller an opportunity to do so.
2879 */
2880 if (ops->callback) {
2881 if (ops->callback(rec->ip, &entry->data) < 0) {
2882 /* caller does not like this func */
2883 kfree(entry);
2884 continue;
2885 }
2886 }
2887
2888 entry->ops = ops;
2889 entry->ip = rec->ip;
2890
2891 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2892 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2893
2894 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002895 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002896
2897 out_unlock:
2898 mutex_unlock(&ftrace_lock);
2899
2900 return count;
2901}
2902
2903enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002904 PROBE_TEST_FUNC = 1,
2905 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002906};
2907
2908static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002909__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002910 void *data, int flags)
2911{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002912 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002913 struct hlist_node *n, *tmp;
2914 char str[KSYM_SYMBOL_LEN];
2915 int type = MATCH_FULL;
2916 int i, len = 0;
2917 char *search;
2918
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002919 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002920 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002921 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002922 int not;
2923
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002924 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002925 len = strlen(search);
2926
Steven Rostedtb6887d72009-02-17 12:32:04 -05002927 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002928 if (WARN_ON(not))
2929 return;
2930 }
2931
2932 mutex_lock(&ftrace_lock);
2933 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2934 struct hlist_head *hhd = &ftrace_func_hash[i];
2935
2936 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2937
2938 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002939 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002940 continue;
2941
Steven Rostedtb6887d72009-02-17 12:32:04 -05002942 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002943 continue;
2944
2945 /* do this last, since it is the most expensive */
2946 if (glob) {
2947 kallsyms_lookup(entry->ip, NULL, NULL,
2948 NULL, str);
2949 if (!ftrace_match(str, glob, len, type))
2950 continue;
2951 }
2952
2953 hlist_del(&entry->node);
2954 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2955 }
2956 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002957 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002958 mutex_unlock(&ftrace_lock);
2959}
2960
2961void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002962unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002963 void *data)
2964{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002965 __unregister_ftrace_function_probe(glob, ops, data,
2966 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002967}
2968
2969void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002970unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002971{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002972 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002973}
2974
Steven Rostedtb6887d72009-02-17 12:32:04 -05002975void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002976{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002977 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002978}
2979
Steven Rostedtf6180772009-02-14 00:40:25 -05002980static LIST_HEAD(ftrace_commands);
2981static DEFINE_MUTEX(ftrace_cmd_mutex);
2982
2983int register_ftrace_command(struct ftrace_func_command *cmd)
2984{
2985 struct ftrace_func_command *p;
2986 int ret = 0;
2987
2988 mutex_lock(&ftrace_cmd_mutex);
2989 list_for_each_entry(p, &ftrace_commands, list) {
2990 if (strcmp(cmd->name, p->name) == 0) {
2991 ret = -EBUSY;
2992 goto out_unlock;
2993 }
2994 }
2995 list_add(&cmd->list, &ftrace_commands);
2996 out_unlock:
2997 mutex_unlock(&ftrace_cmd_mutex);
2998
2999 return ret;
3000}
3001
3002int unregister_ftrace_command(struct ftrace_func_command *cmd)
3003{
3004 struct ftrace_func_command *p, *n;
3005 int ret = -ENODEV;
3006
3007 mutex_lock(&ftrace_cmd_mutex);
3008 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3009 if (strcmp(cmd->name, p->name) == 0) {
3010 ret = 0;
3011 list_del_init(&p->list);
3012 goto out_unlock;
3013 }
3014 }
3015 out_unlock:
3016 mutex_unlock(&ftrace_cmd_mutex);
3017
3018 return ret;
3019}
3020
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003021static int ftrace_process_regex(struct ftrace_hash *hash,
3022 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003023{
Steven Rostedtf6180772009-02-14 00:40:25 -05003024 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003025 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003026 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003027
3028 func = strsep(&next, ":");
3029
3030 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003031 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003032 if (!ret)
3033 ret = -EINVAL;
3034 if (ret < 0)
3035 return ret;
3036 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003037 }
3038
Steven Rostedtf6180772009-02-14 00:40:25 -05003039 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003040
3041 command = strsep(&next, ":");
3042
Steven Rostedtf6180772009-02-14 00:40:25 -05003043 mutex_lock(&ftrace_cmd_mutex);
3044 list_for_each_entry(p, &ftrace_commands, list) {
3045 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003046 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003047 goto out_unlock;
3048 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003049 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003050 out_unlock:
3051 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003052
Steven Rostedtf6180772009-02-14 00:40:25 -05003053 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003054}
3055
Ingo Molnare309b412008-05-12 21:20:51 +02003056static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003057ftrace_regex_write(struct file *file, const char __user *ubuf,
3058 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003059{
3060 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003061 struct trace_parser *parser;
3062 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003063
Li Zefan4ba79782009-09-22 13:52:20 +08003064 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003065 return 0;
3066
Steven Rostedt41c52c02008-05-22 11:46:33 -04003067 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003068
Steven Rostedt45a4a232011-04-21 23:16:46 -04003069 ret = -ENODEV;
3070 if (unlikely(ftrace_disabled))
3071 goto out_unlock;
3072
Steven Rostedt5072c592008-05-12 21:20:43 +02003073 if (file->f_mode & FMODE_READ) {
3074 struct seq_file *m = file->private_data;
3075 iter = m->private;
3076 } else
3077 iter = file->private_data;
3078
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003079 parser = &iter->parser;
3080 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003081
Li Zefan4ba79782009-09-22 13:52:20 +08003082 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003083 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003084 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003085 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003086 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003087 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003088 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003089 }
3090
Steven Rostedt5072c592008-05-12 21:20:43 +02003091 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003092out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003093 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003094
Steven Rostedt5072c592008-05-12 21:20:43 +02003095 return ret;
3096}
3097
Steven Rostedt41c52c02008-05-22 11:46:33 -04003098static ssize_t
3099ftrace_filter_write(struct file *file, const char __user *ubuf,
3100 size_t cnt, loff_t *ppos)
3101{
3102 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3103}
3104
3105static ssize_t
3106ftrace_notrace_write(struct file *file, const char __user *ubuf,
3107 size_t cnt, loff_t *ppos)
3108{
3109 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3110}
3111
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003112static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003113ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3114 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003115{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003116 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003117 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003118 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003119
Steven Rostedt936e0742011-05-05 22:54:01 -04003120 /* All global ops uses the global ops filters */
3121 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3122 ops = &global_ops;
3123
Steven Rostedt41c52c02008-05-22 11:46:33 -04003124 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003125 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003126
Steven Rostedtf45948e2011-05-02 12:29:25 -04003127 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003128 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003129 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003130 orig_hash = &ops->notrace_hash;
3131
3132 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3133 if (!hash)
3134 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003135
Steven Rostedt41c52c02008-05-22 11:46:33 -04003136 mutex_lock(&ftrace_regex_lock);
3137 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003138 ftrace_filter_reset(hash);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003139 if (buf)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003140 ftrace_match_records(hash, buf, len);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003141
3142 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003143 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003144 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3145 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003146 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003147
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003148 mutex_unlock(&ftrace_lock);
3149
Steven Rostedt41c52c02008-05-22 11:46:33 -04003150 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003151
3152 free_ftrace_hash(hash);
3153 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003154}
3155
Steven Rostedt77a2b372008-05-12 21:20:45 +02003156/**
3157 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003158 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003159 * @buf - the string that holds the function filter text.
3160 * @len - the length of the string.
3161 * @reset - non zero to reset all filters before applying this filter.
3162 *
3163 * Filters denote which functions should be enabled when tracing is enabled.
3164 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3165 */
Steven Rostedt936e0742011-05-05 22:54:01 -04003166void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3167 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003168{
Steven Rostedt936e0742011-05-05 22:54:01 -04003169 ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003170}
Steven Rostedt936e0742011-05-05 22:54:01 -04003171EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003172
Steven Rostedt41c52c02008-05-22 11:46:33 -04003173/**
3174 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003175 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003176 * @buf - the string that holds the function notrace text.
3177 * @len - the length of the string.
3178 * @reset - non zero to reset all filters before applying this filter.
3179 *
3180 * Notrace Filters denote which functions should not be enabled when tracing
3181 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3182 * for tracing.
3183 */
Steven Rostedt936e0742011-05-05 22:54:01 -04003184void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3185 int len, int reset)
3186{
3187 ftrace_set_regex(ops, buf, len, reset, 0);
3188}
3189EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3190/**
3191 * ftrace_set_filter - set a function to filter on in ftrace
3192 * @ops - the ops to set the filter with
3193 * @buf - the string that holds the function filter text.
3194 * @len - the length of the string.
3195 * @reset - non zero to reset all filters before applying this filter.
3196 *
3197 * Filters denote which functions should be enabled when tracing is enabled.
3198 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3199 */
3200void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3201{
3202 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3203}
3204EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3205
3206/**
3207 * ftrace_set_notrace - set a function to not trace in ftrace
3208 * @ops - the ops to set the notrace filter with
3209 * @buf - the string that holds the function notrace text.
3210 * @len - the length of the string.
3211 * @reset - non zero to reset all filters before applying this filter.
3212 *
3213 * Notrace Filters denote which functions should not be enabled when tracing
3214 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3215 * for tracing.
3216 */
3217void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003218{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003219 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003220}
Steven Rostedt936e0742011-05-05 22:54:01 -04003221EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003222
Steven Rostedt2af15d62009-05-28 13:37:24 -04003223/*
3224 * command line interface to allow users to set filters on boot up.
3225 */
3226#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3227static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3228static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3229
3230static int __init set_ftrace_notrace(char *str)
3231{
3232 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3233 return 1;
3234}
3235__setup("ftrace_notrace=", set_ftrace_notrace);
3236
3237static int __init set_ftrace_filter(char *str)
3238{
3239 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3240 return 1;
3241}
3242__setup("ftrace_filter=", set_ftrace_filter);
3243
Stefan Assmann369bc182009-10-12 22:17:21 +02003244#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003245static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003246static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3247
Stefan Assmann369bc182009-10-12 22:17:21 +02003248static int __init set_graph_function(char *str)
3249{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003250 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003251 return 1;
3252}
3253__setup("ftrace_graph_filter=", set_graph_function);
3254
3255static void __init set_ftrace_early_graph(char *buf)
3256{
3257 int ret;
3258 char *func;
3259
3260 while (buf) {
3261 func = strsep(&buf, ",");
3262 /* we allow only one expression at a time */
3263 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3264 func);
3265 if (ret)
3266 printk(KERN_DEBUG "ftrace: function %s not "
3267 "traceable\n", func);
3268 }
3269}
3270#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3271
Steven Rostedtf45948e2011-05-02 12:29:25 -04003272static void __init
3273set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003274{
3275 char *func;
3276
3277 while (buf) {
3278 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003279 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003280 }
3281}
3282
3283static void __init set_ftrace_early_filters(void)
3284{
3285 if (ftrace_filter_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04003286 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003287 if (ftrace_notrace_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04003288 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003289#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3290 if (ftrace_graph_buf[0])
3291 set_ftrace_early_graph(ftrace_graph_buf);
3292#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003293}
3294
Ingo Molnare309b412008-05-12 21:20:51 +02003295static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003296ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003297{
3298 struct seq_file *m = (struct seq_file *)file->private_data;
3299 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003300 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003301 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003302 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003303 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003304
Steven Rostedt41c52c02008-05-22 11:46:33 -04003305 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003306 if (file->f_mode & FMODE_READ) {
3307 iter = m->private;
3308
3309 seq_release(inode, file);
3310 } else
3311 iter = file->private_data;
3312
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003313 parser = &iter->parser;
3314 if (trace_parser_loaded(parser)) {
3315 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003316 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003317 }
3318
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003319 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003320
Steven Rostedt058e2972011-04-29 22:35:33 -04003321 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003322 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3323
3324 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003325 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003326 else
3327 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003328
Steven Rostedt058e2972011-04-29 22:35:33 -04003329 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003330 ret = ftrace_hash_move(iter->ops, filter_hash,
3331 orig_hash, iter->hash);
3332 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3333 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003334 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003335
Steven Rostedt058e2972011-04-29 22:35:33 -04003336 mutex_unlock(&ftrace_lock);
3337 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003338 free_ftrace_hash(iter->hash);
3339 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003340
Steven Rostedt41c52c02008-05-22 11:46:33 -04003341 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003342 return 0;
3343}
3344
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003345static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003346 .open = ftrace_avail_open,
3347 .read = seq_read,
3348 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003349 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003350};
3351
Steven Rostedt647bcd02011-05-03 14:39:21 -04003352static const struct file_operations ftrace_enabled_fops = {
3353 .open = ftrace_enabled_open,
3354 .read = seq_read,
3355 .llseek = seq_lseek,
3356 .release = seq_release_private,
3357};
3358
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003359static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003360 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003361 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003362 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003363 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003364 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003365};
3366
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003367static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003368 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003369 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003370 .write = ftrace_notrace_write,
3371 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003372 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003373};
3374
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003375#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3376
3377static DEFINE_MUTEX(graph_lock);
3378
3379int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003380int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003381unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3382
3383static void *
Li Zefan85951842009-06-24 09:54:00 +08003384__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003385{
Li Zefan85951842009-06-24 09:54:00 +08003386 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003387 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003388 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003389}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003390
Li Zefan85951842009-06-24 09:54:00 +08003391static void *
3392g_next(struct seq_file *m, void *v, loff_t *pos)
3393{
3394 (*pos)++;
3395 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003396}
3397
3398static void *g_start(struct seq_file *m, loff_t *pos)
3399{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003400 mutex_lock(&graph_lock);
3401
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003402 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003403 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003404 return (void *)1;
3405
Li Zefan85951842009-06-24 09:54:00 +08003406 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003407}
3408
3409static void g_stop(struct seq_file *m, void *p)
3410{
3411 mutex_unlock(&graph_lock);
3412}
3413
3414static int g_show(struct seq_file *m, void *v)
3415{
3416 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003417
3418 if (!ptr)
3419 return 0;
3420
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003421 if (ptr == (unsigned long *)1) {
3422 seq_printf(m, "#### all functions enabled ####\n");
3423 return 0;
3424 }
3425
Steven Rostedtb375a112009-09-17 00:05:58 -04003426 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003427
3428 return 0;
3429}
3430
James Morris88e9d342009-09-22 16:43:43 -07003431static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003432 .start = g_start,
3433 .next = g_next,
3434 .stop = g_stop,
3435 .show = g_show,
3436};
3437
3438static int
3439ftrace_graph_open(struct inode *inode, struct file *file)
3440{
3441 int ret = 0;
3442
3443 if (unlikely(ftrace_disabled))
3444 return -ENODEV;
3445
3446 mutex_lock(&graph_lock);
3447 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003448 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003449 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003450 ftrace_graph_count = 0;
3451 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3452 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003453 mutex_unlock(&graph_lock);
3454
Li Zefana4ec5e02009-09-18 14:06:28 +08003455 if (file->f_mode & FMODE_READ)
3456 ret = seq_open(file, &ftrace_graph_seq_ops);
3457
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003458 return ret;
3459}
3460
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003461static int
Li Zefan87827112009-07-23 11:29:11 +08003462ftrace_graph_release(struct inode *inode, struct file *file)
3463{
3464 if (file->f_mode & FMODE_READ)
3465 seq_release(inode, file);
3466 return 0;
3467}
3468
3469static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003470ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003471{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003472 struct dyn_ftrace *rec;
3473 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003474 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003475 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003476 int type, not;
3477 char *search;
3478 bool exists;
3479 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003480
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003481 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003482 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003483 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3484 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003485
3486 search_len = strlen(search);
3487
Steven Rostedt52baf112009-02-14 01:15:39 -05003488 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003489
3490 if (unlikely(ftrace_disabled)) {
3491 mutex_unlock(&ftrace_lock);
3492 return -ENODEV;
3493 }
3494
Steven Rostedt265c8312009-02-13 12:43:56 -05003495 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003496
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003497 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003498 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003499 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003500 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003501 if (array[i] == rec->ip) {
3502 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003503 break;
3504 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003505 }
3506
3507 if (!not) {
3508 fail = 0;
3509 if (!exists) {
3510 array[(*idx)++] = rec->ip;
3511 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3512 goto out;
3513 }
3514 } else {
3515 if (exists) {
3516 array[i] = array[--(*idx)];
3517 array[*idx] = 0;
3518 fail = 0;
3519 }
3520 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003521 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003522 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003523out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003524 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003525
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003526 if (fail)
3527 return -EINVAL;
3528
3529 ftrace_graph_filter_enabled = 1;
3530 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003531}
3532
3533static ssize_t
3534ftrace_graph_write(struct file *file, const char __user *ubuf,
3535 size_t cnt, loff_t *ppos)
3536{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003537 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003538 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003539
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003540 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003541 return 0;
3542
3543 mutex_lock(&graph_lock);
3544
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003545 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3546 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003547 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003548 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003549
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003550 read = trace_get_user(&parser, ubuf, cnt, ppos);
3551
Li Zefan4ba79782009-09-22 13:52:20 +08003552 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003553 parser.buffer[parser.idx] = 0;
3554
3555 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003556 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003557 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003558 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003559 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003560 }
3561
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003562 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003563
3564out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003565 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003566out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003567 mutex_unlock(&graph_lock);
3568
3569 return ret;
3570}
3571
3572static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003573 .open = ftrace_graph_open,
3574 .read = seq_read,
3575 .write = ftrace_graph_write,
3576 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003577 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003578};
3579#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3580
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003581static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003582{
Steven Rostedt5072c592008-05-12 21:20:43 +02003583
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003584 trace_create_file("available_filter_functions", 0444,
3585 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003586
Steven Rostedt647bcd02011-05-03 14:39:21 -04003587 trace_create_file("enabled_functions", 0444,
3588 d_tracer, NULL, &ftrace_enabled_fops);
3589
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003590 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3591 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003592
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003593 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003594 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003595
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003596#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003597 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003598 NULL,
3599 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003600#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3601
Steven Rostedt5072c592008-05-12 21:20:43 +02003602 return 0;
3603}
3604
Steven Rostedt68950612011-12-16 17:06:45 -05003605static void ftrace_swap_recs(void *a, void *b, int size)
3606{
3607 struct dyn_ftrace *reca = a;
3608 struct dyn_ftrace *recb = b;
3609 struct dyn_ftrace t;
3610
3611 t = *reca;
3612 *reca = *recb;
3613 *recb = t;
3614}
3615
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003616static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003617 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003618 unsigned long *end)
3619{
Steven Rostedta7900872011-12-16 16:23:44 -05003620 struct ftrace_page *pg;
3621 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003622 unsigned long *p;
3623 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003624 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003625 int ret = -ENOMEM;
3626
3627 count = end - start;
3628
3629 if (!count)
3630 return 0;
3631
3632 pg = ftrace_allocate_pages(count);
3633 if (!pg)
3634 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003635
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003636 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003637
Steven Rostedt32082302011-12-16 14:42:37 -05003638 /*
3639 * Core and each module needs their own pages, as
3640 * modules will free them when they are removed.
3641 * Force a new page to be allocated for modules.
3642 */
Steven Rostedta7900872011-12-16 16:23:44 -05003643 if (!mod) {
3644 WARN_ON(ftrace_pages || ftrace_pages_start);
3645 /* First initialization */
3646 ftrace_pages = ftrace_pages_start = pg;
3647 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003648 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003649 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003650
Steven Rostedta7900872011-12-16 16:23:44 -05003651 if (WARN_ON(ftrace_pages->next)) {
3652 /* Hmm, we have free pages? */
3653 while (ftrace_pages->next)
3654 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003655 }
Steven Rostedta7900872011-12-16 16:23:44 -05003656
3657 ftrace_pages->next = pg;
3658 ftrace_pages = pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003659 }
3660
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003661 p = start;
3662 while (p < end) {
3663 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003664 /*
3665 * Some architecture linkers will pad between
3666 * the different mcount_loc sections of different
3667 * object files to satisfy alignments.
3668 * Skip any NULL pointers.
3669 */
3670 if (!addr)
3671 continue;
Steven Rostedta7900872011-12-16 16:23:44 -05003672 if (!ftrace_record_ip(addr))
3673 break;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003674 }
3675
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003676 /* These new locations need to be initialized */
3677 ftrace_new_pgs = pg;
3678
Steven Rostedt68950612011-12-16 17:06:45 -05003679 /* Make each individual set of pages sorted by ips */
3680 for (; pg; pg = pg->next)
3681 sort(pg->records, pg->index, sizeof(struct dyn_ftrace),
3682 ftrace_cmp_recs, ftrace_swap_recs);
3683
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003684 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003685 * We only need to disable interrupts on start up
3686 * because we are modifying code that an interrupt
3687 * may execute, and the modification is not atomic.
3688 * But for modules, nothing runs the code we modify
3689 * until we are finished with it, and there's no
3690 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003691 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003692 if (!mod)
3693 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003694 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003695 if (!mod)
3696 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003697 ret = 0;
3698 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003699 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003700
Steven Rostedta7900872011-12-16 16:23:44 -05003701 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003702}
3703
Steven Rostedt93eb6772009-04-15 13:24:06 -04003704#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003705
3706#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3707
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003708void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003709{
3710 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003711 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003712 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003713 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003714
Steven Rostedt93eb6772009-04-15 13:24:06 -04003715 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003716
3717 if (ftrace_disabled)
3718 goto out_unlock;
3719
Steven Rostedt32082302011-12-16 14:42:37 -05003720 /*
3721 * Each module has its own ftrace_pages, remove
3722 * them from the list.
3723 */
3724 last_pg = &ftrace_pages_start;
3725 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3726 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003727 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003728 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003729 * As core pages are first, the first
3730 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003731 */
Steven Rostedt32082302011-12-16 14:42:37 -05003732 if (WARN_ON(pg == ftrace_pages_start))
3733 goto out_unlock;
3734
3735 /* Check if we are deleting the last page */
3736 if (pg == ftrace_pages)
3737 ftrace_pages = next_to_ftrace_page(last_pg);
3738
3739 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003740 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3741 free_pages((unsigned long)pg->records, order);
3742 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05003743 } else
3744 last_pg = &pg->next;
3745 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003746 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003747 mutex_unlock(&ftrace_lock);
3748}
3749
3750static void ftrace_init_module(struct module *mod,
3751 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003752{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003753 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003754 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003755 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003756}
3757
Steven Rostedt93eb6772009-04-15 13:24:06 -04003758static int ftrace_module_notify(struct notifier_block *self,
3759 unsigned long val, void *data)
3760{
3761 struct module *mod = data;
3762
3763 switch (val) {
3764 case MODULE_STATE_COMING:
3765 ftrace_init_module(mod, mod->ftrace_callsites,
3766 mod->ftrace_callsites +
3767 mod->num_ftrace_callsites);
3768 break;
3769 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003770 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003771 break;
3772 }
3773
3774 return 0;
3775}
3776#else
3777static int ftrace_module_notify(struct notifier_block *self,
3778 unsigned long val, void *data)
3779{
3780 return 0;
3781}
3782#endif /* CONFIG_MODULES */
3783
3784struct notifier_block ftrace_module_nb = {
3785 .notifier_call = ftrace_module_notify,
3786 .priority = 0,
3787};
3788
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003789extern unsigned long __start_mcount_loc[];
3790extern unsigned long __stop_mcount_loc[];
3791
3792void __init ftrace_init(void)
3793{
3794 unsigned long count, addr, flags;
3795 int ret;
3796
3797 /* Keep the ftrace pointer to the stub */
3798 addr = (unsigned long)ftrace_stub;
3799
3800 local_irq_save(flags);
3801 ftrace_dyn_arch_init(&addr);
3802 local_irq_restore(flags);
3803
3804 /* ftrace_dyn_arch_init places the return code in addr */
3805 if (addr)
3806 goto failed;
3807
3808 count = __stop_mcount_loc - __start_mcount_loc;
3809
3810 ret = ftrace_dyn_table_alloc(count);
3811 if (ret)
3812 goto failed;
3813
3814 last_ftrace_enabled = ftrace_enabled = 1;
3815
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003816 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003817 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003818 __stop_mcount_loc);
3819
Steven Rostedt93eb6772009-04-15 13:24:06 -04003820 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003821 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003822 pr_warning("Failed to register trace ftrace module notifier\n");
3823
Steven Rostedt2af15d62009-05-28 13:37:24 -04003824 set_ftrace_early_filters();
3825
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003826 return;
3827 failed:
3828 ftrace_disabled = 1;
3829}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003830
Steven Rostedt3d083392008-05-12 21:20:42 +02003831#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003832
Steven Rostedt2b499382011-05-03 22:49:52 -04003833static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003834 .func = ftrace_stub,
3835};
3836
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003837static int __init ftrace_nodyn_init(void)
3838{
3839 ftrace_enabled = 1;
3840 return 0;
3841}
3842device_initcall(ftrace_nodyn_init);
3843
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003844static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3845static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003846/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003847# define ftrace_startup(ops, command) \
3848 ({ \
3849 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3850 0; \
3851 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04003852# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003853# define ftrace_startup_sysctl() do { } while (0)
3854# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003855
3856static inline int
3857ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3858{
3859 return 1;
3860}
3861
Steven Rostedt3d083392008-05-12 21:20:42 +02003862#endif /* CONFIG_DYNAMIC_FTRACE */
3863
Steven Rostedtb8489142011-05-04 09:27:52 -04003864static void
3865ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3866{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003867 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04003868
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003869 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3870 return;
3871
3872 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003873 /*
3874 * Some of the ops may be dynamically allocated,
3875 * they must be freed after a synchronize_sched().
3876 */
3877 preempt_disable_notrace();
3878 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04003879 while (op != &ftrace_list_end) {
3880 if (ftrace_ops_test(op, ip))
3881 op->func(ip, parent_ip);
3882 op = rcu_dereference_raw(op->next);
3883 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003884 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003885 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04003886}
3887
Steven Rostedte32d8952008-12-04 00:26:41 -05003888static void clear_ftrace_swapper(void)
3889{
3890 struct task_struct *p;
3891 int cpu;
3892
3893 get_online_cpus();
3894 for_each_online_cpu(cpu) {
3895 p = idle_task(cpu);
3896 clear_tsk_trace_trace(p);
3897 }
3898 put_online_cpus();
3899}
3900
3901static void set_ftrace_swapper(void)
3902{
3903 struct task_struct *p;
3904 int cpu;
3905
3906 get_online_cpus();
3907 for_each_online_cpu(cpu) {
3908 p = idle_task(cpu);
3909 set_tsk_trace_trace(p);
3910 }
3911 put_online_cpus();
3912}
3913
3914static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003915{
3916 struct task_struct *p;
3917
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003918 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05003919 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05003920 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05003921 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003922 rcu_read_unlock();
3923
Steven Rostedte32d8952008-12-04 00:26:41 -05003924 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003925}
3926
Steven Rostedte32d8952008-12-04 00:26:41 -05003927static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003928{
3929 struct task_struct *p;
3930
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003931 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003932 do_each_pid_task(pid, PIDTYPE_PID, p) {
3933 set_tsk_trace_trace(p);
3934 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003935 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003936}
3937
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003938static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003939{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003940 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003941 clear_ftrace_swapper();
3942 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003943 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05003944}
3945
3946static void set_ftrace_pid_task(struct pid *pid)
3947{
3948 if (pid == ftrace_swapper_pid)
3949 set_ftrace_swapper();
3950 else
3951 set_ftrace_pid(pid);
3952}
3953
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003954static int ftrace_pid_add(int p)
3955{
3956 struct pid *pid;
3957 struct ftrace_pid *fpid;
3958 int ret = -EINVAL;
3959
3960 mutex_lock(&ftrace_lock);
3961
3962 if (!p)
3963 pid = ftrace_swapper_pid;
3964 else
3965 pid = find_get_pid(p);
3966
3967 if (!pid)
3968 goto out;
3969
3970 ret = 0;
3971
3972 list_for_each_entry(fpid, &ftrace_pids, list)
3973 if (fpid->pid == pid)
3974 goto out_put;
3975
3976 ret = -ENOMEM;
3977
3978 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3979 if (!fpid)
3980 goto out_put;
3981
3982 list_add(&fpid->list, &ftrace_pids);
3983 fpid->pid = pid;
3984
3985 set_ftrace_pid_task(pid);
3986
3987 ftrace_update_pid_func();
3988 ftrace_startup_enable(0);
3989
3990 mutex_unlock(&ftrace_lock);
3991 return 0;
3992
3993out_put:
3994 if (pid != ftrace_swapper_pid)
3995 put_pid(pid);
3996
3997out:
3998 mutex_unlock(&ftrace_lock);
3999 return ret;
4000}
4001
4002static void ftrace_pid_reset(void)
4003{
4004 struct ftrace_pid *fpid, *safe;
4005
4006 mutex_lock(&ftrace_lock);
4007 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4008 struct pid *pid = fpid->pid;
4009
4010 clear_ftrace_pid_task(pid);
4011
4012 list_del(&fpid->list);
4013 kfree(fpid);
4014 }
4015
4016 ftrace_update_pid_func();
4017 ftrace_startup_enable(0);
4018
4019 mutex_unlock(&ftrace_lock);
4020}
4021
4022static void *fpid_start(struct seq_file *m, loff_t *pos)
4023{
4024 mutex_lock(&ftrace_lock);
4025
4026 if (list_empty(&ftrace_pids) && (!*pos))
4027 return (void *) 1;
4028
4029 return seq_list_start(&ftrace_pids, *pos);
4030}
4031
4032static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4033{
4034 if (v == (void *)1)
4035 return NULL;
4036
4037 return seq_list_next(v, &ftrace_pids, pos);
4038}
4039
4040static void fpid_stop(struct seq_file *m, void *p)
4041{
4042 mutex_unlock(&ftrace_lock);
4043}
4044
4045static int fpid_show(struct seq_file *m, void *v)
4046{
4047 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4048
4049 if (v == (void *)1) {
4050 seq_printf(m, "no pid\n");
4051 return 0;
4052 }
4053
4054 if (fpid->pid == ftrace_swapper_pid)
4055 seq_printf(m, "swapper tasks\n");
4056 else
4057 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4058
4059 return 0;
4060}
4061
4062static const struct seq_operations ftrace_pid_sops = {
4063 .start = fpid_start,
4064 .next = fpid_next,
4065 .stop = fpid_stop,
4066 .show = fpid_show,
4067};
4068
4069static int
4070ftrace_pid_open(struct inode *inode, struct file *file)
4071{
4072 int ret = 0;
4073
4074 if ((file->f_mode & FMODE_WRITE) &&
4075 (file->f_flags & O_TRUNC))
4076 ftrace_pid_reset();
4077
4078 if (file->f_mode & FMODE_READ)
4079 ret = seq_open(file, &ftrace_pid_sops);
4080
4081 return ret;
4082}
4083
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004084static ssize_t
4085ftrace_pid_write(struct file *filp, const char __user *ubuf,
4086 size_t cnt, loff_t *ppos)
4087{
Ingo Molnar457dc922009-11-23 11:03:28 +01004088 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004089 long val;
4090 int ret;
4091
4092 if (cnt >= sizeof(buf))
4093 return -EINVAL;
4094
4095 if (copy_from_user(&buf, ubuf, cnt))
4096 return -EFAULT;
4097
4098 buf[cnt] = 0;
4099
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004100 /*
4101 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4102 * to clean the filter quietly.
4103 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004104 tmp = strstrip(buf);
4105 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004106 return 1;
4107
Ingo Molnar457dc922009-11-23 11:03:28 +01004108 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004109 if (ret < 0)
4110 return ret;
4111
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004112 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004113
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004114 return ret ? ret : cnt;
4115}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004116
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004117static int
4118ftrace_pid_release(struct inode *inode, struct file *file)
4119{
4120 if (file->f_mode & FMODE_READ)
4121 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004122
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004123 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004124}
4125
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004126static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004127 .open = ftrace_pid_open,
4128 .write = ftrace_pid_write,
4129 .read = seq_read,
4130 .llseek = seq_lseek,
4131 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004132};
4133
4134static __init int ftrace_init_debugfs(void)
4135{
4136 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004137
4138 d_tracer = tracing_init_dentry();
4139 if (!d_tracer)
4140 return 0;
4141
4142 ftrace_init_dyn_debugfs(d_tracer);
4143
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004144 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4145 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004146
4147 ftrace_profile_debugfs(d_tracer);
4148
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004149 return 0;
4150}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004151fs_initcall(ftrace_init_debugfs);
4152
Steven Rostedt3d083392008-05-12 21:20:42 +02004153/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004154 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004155 *
4156 * This function should be used by panic code. It stops ftrace
4157 * but in a not so nice way. If you need to simply kill ftrace
4158 * from a non-atomic section, use ftrace_kill.
4159 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004160void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004161{
4162 ftrace_disabled = 1;
4163 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004164 clear_ftrace_function();
4165}
4166
4167/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004168 * Test if ftrace is dead or not.
4169 */
4170int ftrace_is_dead(void)
4171{
4172 return ftrace_disabled;
4173}
4174
4175/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004176 * register_ftrace_function - register a function for profiling
4177 * @ops - ops structure that holds the function for profiling.
4178 *
4179 * Register a function to be called by all functions in the
4180 * kernel.
4181 *
4182 * Note: @ops->func and all the functions it calls must be labeled
4183 * with "notrace", otherwise it will go into a
4184 * recursive loop.
4185 */
4186int register_ftrace_function(struct ftrace_ops *ops)
4187{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004188 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004189
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004190 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004191
Steven Rostedt45a4a232011-04-21 23:16:46 -04004192 if (unlikely(ftrace_disabled))
4193 goto out_unlock;
4194
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004195 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004196 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004197 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004198
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004199
Steven Rostedt45a4a232011-04-21 23:16:46 -04004200 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004201 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004202 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004203}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004204EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004205
4206/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004207 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004208 * @ops - ops structure that holds the function to unregister
4209 *
4210 * Unregister a function that was added to be called by ftrace profiling.
4211 */
4212int unregister_ftrace_function(struct ftrace_ops *ops)
4213{
4214 int ret;
4215
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004216 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004217 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004218 if (!ret)
4219 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004220 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004221
4222 return ret;
4223}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004224EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004225
Ingo Molnare309b412008-05-12 21:20:51 +02004226int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004227ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004228 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004229 loff_t *ppos)
4230{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004231 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004232
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004233 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004234
Steven Rostedt45a4a232011-04-21 23:16:46 -04004235 if (unlikely(ftrace_disabled))
4236 goto out;
4237
4238 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004239
Li Zefana32c7762009-06-26 16:55:51 +08004240 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004241 goto out;
4242
Li Zefana32c7762009-06-26 16:55:51 +08004243 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004244
4245 if (ftrace_enabled) {
4246
4247 ftrace_startup_sysctl();
4248
4249 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004250 if (ftrace_ops_list != &ftrace_list_end) {
4251 if (ftrace_ops_list->next == &ftrace_list_end)
4252 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004253 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004254 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004255 }
4256
4257 } else {
4258 /* stopping ftrace calls (just send to ftrace_stub) */
4259 ftrace_trace_function = ftrace_stub;
4260
4261 ftrace_shutdown_sysctl();
4262 }
4263
4264 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004265 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004266 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004267}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004268
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004269#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004270
Steven Rostedt597af812009-04-03 15:24:12 -04004271static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004272static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004273
Steven Rostedte49dc192008-12-02 23:50:05 -05004274int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4275{
4276 return 0;
4277}
4278
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004279/* The callbacks that hook a function */
4280trace_func_graph_ret_t ftrace_graph_return =
4281 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004282trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004283
4284/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4285static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4286{
4287 int i;
4288 int ret = 0;
4289 unsigned long flags;
4290 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4291 struct task_struct *g, *t;
4292
4293 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4294 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4295 * sizeof(struct ftrace_ret_stack),
4296 GFP_KERNEL);
4297 if (!ret_stack_list[i]) {
4298 start = 0;
4299 end = i;
4300 ret = -ENOMEM;
4301 goto free;
4302 }
4303 }
4304
4305 read_lock_irqsave(&tasklist_lock, flags);
4306 do_each_thread(g, t) {
4307 if (start == end) {
4308 ret = -EAGAIN;
4309 goto unlock;
4310 }
4311
4312 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004313 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004314 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004315 t->curr_ret_stack = -1;
4316 /* Make sure the tasks see the -1 first: */
4317 smp_wmb();
4318 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004319 }
4320 } while_each_thread(g, t);
4321
4322unlock:
4323 read_unlock_irqrestore(&tasklist_lock, flags);
4324free:
4325 for (i = start; i < end; i++)
4326 kfree(ret_stack_list[i]);
4327 return ret;
4328}
4329
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004330static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004331ftrace_graph_probe_sched_switch(void *ignore,
4332 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004333{
4334 unsigned long long timestamp;
4335 int index;
4336
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004337 /*
4338 * Does the user want to count the time a function was asleep.
4339 * If so, do not update the time stamps.
4340 */
4341 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4342 return;
4343
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004344 timestamp = trace_clock_local();
4345
4346 prev->ftrace_timestamp = timestamp;
4347
4348 /* only process tasks that we timestamped */
4349 if (!next->ftrace_timestamp)
4350 return;
4351
4352 /*
4353 * Update all the counters in next to make up for the
4354 * time next was sleeping.
4355 */
4356 timestamp -= next->ftrace_timestamp;
4357
4358 for (index = next->curr_ret_stack; index >= 0; index--)
4359 next->ret_stack[index].calltime += timestamp;
4360}
4361
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004362/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004363static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004364{
4365 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004366 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004367
4368 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4369 sizeof(struct ftrace_ret_stack *),
4370 GFP_KERNEL);
4371
4372 if (!ret_stack_list)
4373 return -ENOMEM;
4374
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004375 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004376 for_each_online_cpu(cpu) {
4377 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004378 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004379 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004380
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004381 do {
4382 ret = alloc_retstack_tasklist(ret_stack_list);
4383 } while (ret == -EAGAIN);
4384
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004385 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004386 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004387 if (ret)
4388 pr_info("ftrace_graph: Couldn't activate tracepoint"
4389 " probe to kernel_sched_switch\n");
4390 }
4391
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004392 kfree(ret_stack_list);
4393 return ret;
4394}
4395
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004396/*
4397 * Hibernation protection.
4398 * The state of the current task is too much unstable during
4399 * suspend/restore to disk. We want to protect against that.
4400 */
4401static int
4402ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4403 void *unused)
4404{
4405 switch (state) {
4406 case PM_HIBERNATION_PREPARE:
4407 pause_graph_tracing();
4408 break;
4409
4410 case PM_POST_HIBERNATION:
4411 unpause_graph_tracing();
4412 break;
4413 }
4414 return NOTIFY_DONE;
4415}
4416
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004417int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4418 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004419{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004420 int ret = 0;
4421
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004422 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004423
Steven Rostedt05ce5812009-03-24 00:18:31 -04004424 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004425 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004426 ret = -EBUSY;
4427 goto out;
4428 }
4429
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004430 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4431 register_pm_notifier(&ftrace_suspend_notifier);
4432
Steven Rostedt597af812009-04-03 15:24:12 -04004433 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004434 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004435 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004436 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004437 goto out;
4438 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004439
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004440 ftrace_graph_return = retfunc;
4441 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004442
Steven Rostedta1cd6172011-05-23 15:24:25 -04004443 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004444
4445out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004446 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004447 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004448}
4449
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004450void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004451{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004452 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004453
Steven Rostedt597af812009-04-03 15:24:12 -04004454 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004455 goto out;
4456
Steven Rostedt597af812009-04-03 15:24:12 -04004457 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004458 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004459 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004460 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004461 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004462 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004463
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004464 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004465 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004466}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004467
Steven Rostedt868baf02011-02-10 21:26:13 -05004468static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4469
4470static void
4471graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4472{
4473 atomic_set(&t->tracing_graph_pause, 0);
4474 atomic_set(&t->trace_overrun, 0);
4475 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004476 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004477 smp_wmb();
4478 t->ret_stack = ret_stack;
4479}
4480
4481/*
4482 * Allocate a return stack for the idle task. May be the first
4483 * time through, or it may be done by CPU hotplug online.
4484 */
4485void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4486{
4487 t->curr_ret_stack = -1;
4488 /*
4489 * The idle task has no parent, it either has its own
4490 * stack or no stack at all.
4491 */
4492 if (t->ret_stack)
4493 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4494
4495 if (ftrace_graph_active) {
4496 struct ftrace_ret_stack *ret_stack;
4497
4498 ret_stack = per_cpu(idle_ret_stack, cpu);
4499 if (!ret_stack) {
4500 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4501 * sizeof(struct ftrace_ret_stack),
4502 GFP_KERNEL);
4503 if (!ret_stack)
4504 return;
4505 per_cpu(idle_ret_stack, cpu) = ret_stack;
4506 }
4507 graph_init_task(t, ret_stack);
4508 }
4509}
4510
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004511/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004512void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004513{
Steven Rostedt84047e32009-06-02 16:51:55 -04004514 /* Make sure we do not use the parent ret_stack */
4515 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004516 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004517
Steven Rostedt597af812009-04-03 15:24:12 -04004518 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004519 struct ftrace_ret_stack *ret_stack;
4520
4521 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004522 * sizeof(struct ftrace_ret_stack),
4523 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004524 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004525 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004526 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004527 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004528}
4529
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004530void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004531{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004532 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4533
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004534 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004535 /* NULL must become visible to IRQs before we free it: */
4536 barrier();
4537
4538 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004539}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004540
4541void ftrace_graph_stop(void)
4542{
4543 ftrace_stop();
4544}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004545#endif