blob: 2d6f8bcd18842311b8ef40851070e074f536eaf6 [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>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040025#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010026#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020027#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020029#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050030#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020031#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050032#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080033#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020034
Steven Rostedtad8d75f2009-04-14 19:39:12 -040035#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040036
Steven Rostedt2af15d62009-05-28 13:37:24 -040037#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053038
Steven Rostedt0706f1c2009-03-23 23:12:58 -040039#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040040#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020041
Steven Rostedt6912896e2008-10-23 09:33:03 -040042#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040043 ({ \
44 int ___r = cond; \
45 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040046 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040047 ___r; \
48 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040049
50#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040051 ({ \
52 int ___r = cond; \
53 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040054 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040055 ___r; \
56 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040057
Steven Rostedt8fc0c702009-02-16 15:28:00 -050058/* hash bits for specific function selection */
59#define FTRACE_HASH_BITS 7
60#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040061#define FTRACE_HASH_DEFAULT_BITS 10
62#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050063
Steven Rostedt4eebcc82008-05-12 21:20:48 +020064/* ftrace_enabled is a method to turn ftrace on or off */
65int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020066static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020067
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050068/* Quick disabling of function tracer. */
69int function_trace_stop;
70
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040071/* List for set_ftrace_pid's pids. */
72LIST_HEAD(ftrace_pids);
73struct ftrace_pid {
74 struct list_head list;
75 struct pid *pid;
76};
77
Steven Rostedt4eebcc82008-05-12 21:20:48 +020078/*
79 * ftrace_disabled is set when an anomaly is discovered.
80 * ftrace_disabled is much stronger than ftrace_enabled.
81 */
82static int ftrace_disabled __read_mostly;
83
Steven Rostedt52baf112009-02-14 01:15:39 -050084static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020085
Paul McQuadebd38c0e2011-05-31 20:51:55 +010086static struct ftrace_ops ftrace_list_end __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -040087 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020088};
89
Steven Rostedtb8489142011-05-04 09:27:52 -040090static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
91static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020092ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt6331c282011-07-13 15:11:02 -040093static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050094ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050095ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -040096static struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020097
Steven Rostedtb8489142011-05-04 09:27:52 -040098static void
99ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
100
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800101/*
Steven Rostedtb8489142011-05-04 09:27:52 -0400102 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800103 * can use rcu_dereference_raw() is that elements removed from this list
104 * are simply leaked, so there is no need to interact with a grace-period
105 * mechanism. The rcu_dereference_raw() calls are needed to handle
Steven Rostedtb8489142011-05-04 09:27:52 -0400106 * concurrent insertions into the ftrace_global_list.
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800107 *
108 * Silly Alpha and silly pointer-speculation compiler optimizations!
109 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400110static void ftrace_global_list_func(unsigned long ip,
111 unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200112{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400113 struct ftrace_ops *op;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200114
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400115 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
116 return;
117
118 trace_recursion_set(TRACE_GLOBAL_BIT);
119 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200120 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200121 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800122 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200123 };
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400124 trace_recursion_clear(TRACE_GLOBAL_BIT);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200125}
126
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500127static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
128{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500129 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500130 return;
131
132 ftrace_pid_function(ip, parent_ip);
133}
134
135static void set_ftrace_pid_function(ftrace_func_t func)
136{
137 /* do not set ftrace_pid_function to itself! */
138 if (func != ftrace_pid_func)
139 ftrace_pid_function = func;
140}
141
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200142/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200143 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200144 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200145 * This NULLs the ftrace function and in essence stops
146 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200147 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200148void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200149{
Steven Rostedt3d083392008-05-12 21:20:42 +0200150 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500151 __ftrace_trace_function = ftrace_stub;
Steven Rostedt6331c282011-07-13 15:11:02 -0400152 __ftrace_trace_function_delay = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500153 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200154}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200155
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500156#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
157/*
158 * For those archs that do not test ftrace_trace_stop in their
159 * mcount call site, we need to do it from C.
160 */
161static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
162{
163 if (function_trace_stop)
164 return;
165
166 __ftrace_trace_function(ip, parent_ip);
167}
168#endif
169
Steven Rostedt2b499382011-05-03 22:49:52 -0400170static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400171{
172 ftrace_func_t func;
173
174 /*
175 * If there's only one function registered, then call that
176 * function directly. Otherwise, we need to iterate over the
177 * registered callers.
178 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400179 if (ftrace_global_list == &ftrace_list_end ||
180 ftrace_global_list->next == &ftrace_list_end)
181 func = ftrace_global_list->func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400182 else
Steven Rostedtb8489142011-05-04 09:27:52 -0400183 func = ftrace_global_list_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400184
185 /* If we filter on pids, update to use the pid function */
186 if (!list_empty(&ftrace_pids)) {
187 set_ftrace_pid_function(func);
188 func = ftrace_pid_func;
189 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400190
191 global_ops.func = func;
192}
193
194static void update_ftrace_function(void)
195{
196 ftrace_func_t func;
197
198 update_global_ops();
199
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400200 /*
201 * If we are at the end of the list and this ops is
202 * not dynamic, then have the mcount trampoline call
203 * the function directly
204 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400205 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400206 (ftrace_ops_list->next == &ftrace_list_end &&
207 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
Steven Rostedtb8489142011-05-04 09:27:52 -0400208 func = ftrace_ops_list->func;
209 else
210 func = ftrace_ops_list_func;
Steven Rostedt2b499382011-05-03 22:49:52 -0400211
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400212#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
213 ftrace_trace_function = func;
214#else
Steven Rostedt6331c282011-07-13 15:11:02 -0400215#ifdef CONFIG_DYNAMIC_FTRACE
216 /* do not update till all functions have been modified */
217 __ftrace_trace_function_delay = func;
218#else
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400219 __ftrace_trace_function = func;
Steven Rostedt6331c282011-07-13 15:11:02 -0400220#endif
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400221 ftrace_trace_function = ftrace_test_stop_func;
222#endif
223}
224
Steven Rostedt2b499382011-05-03 22:49:52 -0400225static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200226{
Steven Rostedt2b499382011-05-03 22:49:52 -0400227 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200228 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400229 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200230 * CPU might be walking that list. We need to make sure
231 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400232 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200233 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400234 rcu_assign_pointer(*list, ops);
235}
Steven Rostedt3d083392008-05-12 21:20:42 +0200236
Steven Rostedt2b499382011-05-03 22:49:52 -0400237static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
238{
239 struct ftrace_ops **p;
240
241 /*
242 * If we are removing the last function, then simply point
243 * to the ftrace_stub.
244 */
245 if (*list == ops && ops->next == &ftrace_list_end) {
246 *list = &ftrace_list_end;
247 return 0;
248 }
249
250 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
251 if (*p == ops)
252 break;
253
254 if (*p != ops)
255 return -1;
256
257 *p = (*p)->next;
258 return 0;
259}
260
261static int __register_ftrace_function(struct ftrace_ops *ops)
262{
263 if (ftrace_disabled)
264 return -ENODEV;
265
266 if (FTRACE_WARN_ON(ops == &global_ops))
267 return -EINVAL;
268
Steven Rostedtb8489142011-05-04 09:27:52 -0400269 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
270 return -EBUSY;
271
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400272 if (!core_kernel_data((unsigned long)ops))
273 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
274
Steven Rostedtb8489142011-05-04 09:27:52 -0400275 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
276 int first = ftrace_global_list == &ftrace_list_end;
277 add_ftrace_ops(&ftrace_global_list, ops);
278 ops->flags |= FTRACE_OPS_FL_ENABLED;
279 if (first)
280 add_ftrace_ops(&ftrace_ops_list, &global_ops);
281 } else
282 add_ftrace_ops(&ftrace_ops_list, ops);
283
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400284 if (ftrace_enabled)
285 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200286
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200287 return 0;
288}
289
Ingo Molnare309b412008-05-12 21:20:51 +0200290static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200291{
Steven Rostedt2b499382011-05-03 22:49:52 -0400292 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200293
Steven Rostedt2b499382011-05-03 22:49:52 -0400294 if (ftrace_disabled)
295 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200296
Steven Rostedtb8489142011-05-04 09:27:52 -0400297 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
298 return -EBUSY;
299
Steven Rostedt2b499382011-05-03 22:49:52 -0400300 if (FTRACE_WARN_ON(ops == &global_ops))
301 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200302
Steven Rostedtb8489142011-05-04 09:27:52 -0400303 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
304 ret = remove_ftrace_ops(&ftrace_global_list, ops);
305 if (!ret && ftrace_global_list == &ftrace_list_end)
306 ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops);
307 if (!ret)
308 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
309 } else
310 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
311
Steven Rostedt2b499382011-05-03 22:49:52 -0400312 if (ret < 0)
313 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400314
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400315 if (ftrace_enabled)
316 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200317
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400318 /*
319 * Dynamic ops may be freed, we must make sure that all
320 * callers are done before leaving this function.
321 */
322 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
323 synchronize_sched();
324
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500325 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200326}
327
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500328static void ftrace_update_pid_func(void)
329{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400330 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500331 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900332 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500333
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400334 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500335}
336
Steven Rostedt493762f2009-03-23 17:12:36 -0400337#ifdef CONFIG_FUNCTION_PROFILER
338struct ftrace_profile {
339 struct hlist_node node;
340 unsigned long ip;
341 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400342#ifdef CONFIG_FUNCTION_GRAPH_TRACER
343 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400344 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400345#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400346};
347
348struct ftrace_profile_page {
349 struct ftrace_profile_page *next;
350 unsigned long index;
351 struct ftrace_profile records[];
352};
353
Steven Rostedtcafb1682009-03-24 20:50:39 -0400354struct ftrace_profile_stat {
355 atomic_t disabled;
356 struct hlist_head *hash;
357 struct ftrace_profile_page *pages;
358 struct ftrace_profile_page *start;
359 struct tracer_stat stat;
360};
361
Steven Rostedt493762f2009-03-23 17:12:36 -0400362#define PROFILE_RECORDS_SIZE \
363 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
364
365#define PROFILES_PER_PAGE \
366 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
367
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400368static int ftrace_profile_bits __read_mostly;
369static int ftrace_profile_enabled __read_mostly;
370
371/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400372static DEFINE_MUTEX(ftrace_profile_lock);
373
Steven Rostedtcafb1682009-03-24 20:50:39 -0400374static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400375
376#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
377
Steven Rostedt493762f2009-03-23 17:12:36 -0400378static void *
379function_stat_next(void *v, int idx)
380{
381 struct ftrace_profile *rec = v;
382 struct ftrace_profile_page *pg;
383
384 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
385
386 again:
Li Zefan0296e422009-06-26 11:15:37 +0800387 if (idx != 0)
388 rec++;
389
Steven Rostedt493762f2009-03-23 17:12:36 -0400390 if ((void *)rec >= (void *)&pg->records[pg->index]) {
391 pg = pg->next;
392 if (!pg)
393 return NULL;
394 rec = &pg->records[0];
395 if (!rec->counter)
396 goto again;
397 }
398
399 return rec;
400}
401
402static void *function_stat_start(struct tracer_stat *trace)
403{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400404 struct ftrace_profile_stat *stat =
405 container_of(trace, struct ftrace_profile_stat, stat);
406
407 if (!stat || !stat->start)
408 return NULL;
409
410 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400411}
412
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400413#ifdef CONFIG_FUNCTION_GRAPH_TRACER
414/* function graph compares on total time */
415static int function_stat_cmp(void *p1, void *p2)
416{
417 struct ftrace_profile *a = p1;
418 struct ftrace_profile *b = p2;
419
420 if (a->time < b->time)
421 return -1;
422 if (a->time > b->time)
423 return 1;
424 else
425 return 0;
426}
427#else
428/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400429static int function_stat_cmp(void *p1, void *p2)
430{
431 struct ftrace_profile *a = p1;
432 struct ftrace_profile *b = p2;
433
434 if (a->counter < b->counter)
435 return -1;
436 if (a->counter > b->counter)
437 return 1;
438 else
439 return 0;
440}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400441#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400442
443static int function_stat_headers(struct seq_file *m)
444{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400445#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400446 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400447 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400448 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400449 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400450#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400451 seq_printf(m, " Function Hit\n"
452 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400453#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400454 return 0;
455}
456
457static int function_stat_show(struct seq_file *m, void *v)
458{
459 struct ftrace_profile *rec = v;
460 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800461 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400462#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400463 static struct trace_seq s;
464 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400465 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400466#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800467 mutex_lock(&ftrace_profile_lock);
468
469 /* we raced with function_profile_reset() */
470 if (unlikely(rec->counter == 0)) {
471 ret = -EBUSY;
472 goto out;
473 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400474
475 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400476 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400477
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400478#ifdef CONFIG_FUNCTION_GRAPH_TRACER
479 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400480 avg = rec->time;
481 do_div(avg, rec->counter);
482
Chase Douglase330b3b2010-04-26 14:02:05 -0400483 /* Sample standard deviation (s^2) */
484 if (rec->counter <= 1)
485 stddev = 0;
486 else {
487 stddev = rec->time_squared - rec->counter * avg * avg;
488 /*
489 * Divide only 1000 for ns^2 -> us^2 conversion.
490 * trace_print_graph_duration will divide 1000 again.
491 */
492 do_div(stddev, (rec->counter - 1) * 1000);
493 }
494
Steven Rostedt34886c82009-03-25 21:00:47 -0400495 trace_seq_init(&s);
496 trace_print_graph_duration(rec->time, &s);
497 trace_seq_puts(&s, " ");
498 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400499 trace_seq_puts(&s, " ");
500 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400501 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400502#endif
503 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800504out:
505 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400506
Li Zefan3aaba202010-08-23 16:50:12 +0800507 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400508}
509
Steven Rostedtcafb1682009-03-24 20:50:39 -0400510static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400511{
512 struct ftrace_profile_page *pg;
513
Steven Rostedtcafb1682009-03-24 20:50:39 -0400514 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400515
516 while (pg) {
517 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
518 pg->index = 0;
519 pg = pg->next;
520 }
521
Steven Rostedtcafb1682009-03-24 20:50:39 -0400522 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400523 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
524}
525
Steven Rostedtcafb1682009-03-24 20:50:39 -0400526int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400527{
528 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400529 int functions;
530 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400531 int i;
532
533 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400534 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400535 return 0;
536
Steven Rostedtcafb1682009-03-24 20:50:39 -0400537 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
538 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400539 return -ENOMEM;
540
Steven Rostedt318e0a72009-03-25 20:06:34 -0400541#ifdef CONFIG_DYNAMIC_FTRACE
542 functions = ftrace_update_tot_cnt;
543#else
544 /*
545 * We do not know the number of functions that exist because
546 * dynamic tracing is what counts them. With past experience
547 * we have around 20K functions. That should be more than enough.
548 * It is highly unlikely we will execute every function in
549 * the kernel.
550 */
551 functions = 20000;
552#endif
553
Steven Rostedtcafb1682009-03-24 20:50:39 -0400554 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400555
Steven Rostedt318e0a72009-03-25 20:06:34 -0400556 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
557
558 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400559 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400560 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400561 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400562 pg = pg->next;
563 }
564
565 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400566
567 out_free:
568 pg = stat->start;
569 while (pg) {
570 unsigned long tmp = (unsigned long)pg;
571
572 pg = pg->next;
573 free_page(tmp);
574 }
575
576 free_page((unsigned long)stat->pages);
577 stat->pages = NULL;
578 stat->start = NULL;
579
580 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400581}
582
Steven Rostedtcafb1682009-03-24 20:50:39 -0400583static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400584{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400585 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400586 int size;
587
Steven Rostedtcafb1682009-03-24 20:50:39 -0400588 stat = &per_cpu(ftrace_profile_stats, cpu);
589
590 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400591 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400592 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400593 return 0;
594 }
595
596 /*
597 * We are profiling all functions, but usually only a few thousand
598 * functions are hit. We'll make a hash of 1024 items.
599 */
600 size = FTRACE_PROFILE_HASH_SIZE;
601
Steven Rostedtcafb1682009-03-24 20:50:39 -0400602 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400603
Steven Rostedtcafb1682009-03-24 20:50:39 -0400604 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400605 return -ENOMEM;
606
Steven Rostedtcafb1682009-03-24 20:50:39 -0400607 if (!ftrace_profile_bits) {
608 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400609
Steven Rostedtcafb1682009-03-24 20:50:39 -0400610 for (; size; size >>= 1)
611 ftrace_profile_bits++;
612 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400613
Steven Rostedt318e0a72009-03-25 20:06:34 -0400614 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400615 if (ftrace_profile_pages_init(stat) < 0) {
616 kfree(stat->hash);
617 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400618 return -ENOMEM;
619 }
620
621 return 0;
622}
623
Steven Rostedtcafb1682009-03-24 20:50:39 -0400624static int ftrace_profile_init(void)
625{
626 int cpu;
627 int ret = 0;
628
629 for_each_online_cpu(cpu) {
630 ret = ftrace_profile_init_cpu(cpu);
631 if (ret)
632 break;
633 }
634
635 return ret;
636}
637
Steven Rostedt493762f2009-03-23 17:12:36 -0400638/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400639static struct ftrace_profile *
640ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400641{
642 struct ftrace_profile *rec;
643 struct hlist_head *hhd;
644 struct hlist_node *n;
645 unsigned long key;
646
647 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400648 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400649
650 if (hlist_empty(hhd))
651 return NULL;
652
653 hlist_for_each_entry_rcu(rec, n, hhd, node) {
654 if (rec->ip == ip)
655 return rec;
656 }
657
658 return NULL;
659}
660
Steven Rostedtcafb1682009-03-24 20:50:39 -0400661static void ftrace_add_profile(struct ftrace_profile_stat *stat,
662 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400663{
664 unsigned long key;
665
666 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400667 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400668}
669
Steven Rostedt318e0a72009-03-25 20:06:34 -0400670/*
671 * The memory is already allocated, this simply finds a new record to use.
672 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400673static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400674ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400675{
676 struct ftrace_profile *rec = NULL;
677
Steven Rostedt318e0a72009-03-25 20:06:34 -0400678 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400679 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400680 goto out;
681
Steven Rostedt493762f2009-03-23 17:12:36 -0400682 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400683 * Try to find the function again since an NMI
684 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400685 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400686 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400687 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400688 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400689
Steven Rostedtcafb1682009-03-24 20:50:39 -0400690 if (stat->pages->index == PROFILES_PER_PAGE) {
691 if (!stat->pages->next)
692 goto out;
693 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400694 }
695
Steven Rostedtcafb1682009-03-24 20:50:39 -0400696 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400697 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400698 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400699
Steven Rostedt493762f2009-03-23 17:12:36 -0400700 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400701 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400702
703 return rec;
704}
705
Steven Rostedt493762f2009-03-23 17:12:36 -0400706static void
707function_profile_call(unsigned long ip, unsigned long parent_ip)
708{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400709 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400710 struct ftrace_profile *rec;
711 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400712
713 if (!ftrace_profile_enabled)
714 return;
715
Steven Rostedt493762f2009-03-23 17:12:36 -0400716 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400717
718 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400719 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400720 goto out;
721
722 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400723 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400724 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400725 if (!rec)
726 goto out;
727 }
728
729 rec->counter++;
730 out:
731 local_irq_restore(flags);
732}
733
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400734#ifdef CONFIG_FUNCTION_GRAPH_TRACER
735static int profile_graph_entry(struct ftrace_graph_ent *trace)
736{
737 function_profile_call(trace->func, 0);
738 return 1;
739}
740
741static void profile_graph_return(struct ftrace_graph_ret *trace)
742{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400743 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400744 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400745 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400746 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400747
748 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400749 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400750 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400751 goto out;
752
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400753 /* If the calltime was zero'd ignore it */
754 if (!trace->calltime)
755 goto out;
756
Steven Rostedta2a16d62009-03-24 23:17:58 -0400757 calltime = trace->rettime - trace->calltime;
758
759 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
760 int index;
761
762 index = trace->depth;
763
764 /* Append this call time to the parent time to subtract */
765 if (index)
766 current->ret_stack[index - 1].subtime += calltime;
767
768 if (current->ret_stack[index].subtime < calltime)
769 calltime -= current->ret_stack[index].subtime;
770 else
771 calltime = 0;
772 }
773
Steven Rostedtcafb1682009-03-24 20:50:39 -0400774 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400775 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400776 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400777 rec->time_squared += calltime * calltime;
778 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400779
Steven Rostedtcafb1682009-03-24 20:50:39 -0400780 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400781 local_irq_restore(flags);
782}
783
784static int register_ftrace_profiler(void)
785{
786 return register_ftrace_graph(&profile_graph_return,
787 &profile_graph_entry);
788}
789
790static void unregister_ftrace_profiler(void)
791{
792 unregister_ftrace_graph();
793}
794#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100795static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400796 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400797};
798
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400799static int register_ftrace_profiler(void)
800{
801 return register_ftrace_function(&ftrace_profile_ops);
802}
803
804static void unregister_ftrace_profiler(void)
805{
806 unregister_ftrace_function(&ftrace_profile_ops);
807}
808#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
809
Steven Rostedt493762f2009-03-23 17:12:36 -0400810static ssize_t
811ftrace_profile_write(struct file *filp, const char __user *ubuf,
812 size_t cnt, loff_t *ppos)
813{
814 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400815 int ret;
816
Peter Huewe22fe9b52011-06-07 21:58:27 +0200817 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
818 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400819 return ret;
820
821 val = !!val;
822
823 mutex_lock(&ftrace_profile_lock);
824 if (ftrace_profile_enabled ^ val) {
825 if (val) {
826 ret = ftrace_profile_init();
827 if (ret < 0) {
828 cnt = ret;
829 goto out;
830 }
831
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400832 ret = register_ftrace_profiler();
833 if (ret < 0) {
834 cnt = ret;
835 goto out;
836 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400837 ftrace_profile_enabled = 1;
838 } else {
839 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400840 /*
841 * unregister_ftrace_profiler calls stop_machine
842 * so this acts like an synchronize_sched.
843 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400844 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400845 }
846 }
847 out:
848 mutex_unlock(&ftrace_profile_lock);
849
Jiri Olsacf8517c2009-10-23 19:36:16 -0400850 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400851
852 return cnt;
853}
854
855static ssize_t
856ftrace_profile_read(struct file *filp, char __user *ubuf,
857 size_t cnt, loff_t *ppos)
858{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400859 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400860 int r;
861
862 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
863 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
864}
865
866static const struct file_operations ftrace_profile_fops = {
867 .open = tracing_open_generic,
868 .read = ftrace_profile_read,
869 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200870 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400871};
872
Steven Rostedtcafb1682009-03-24 20:50:39 -0400873/* used to initialize the real stat files */
874static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400875 .name = "functions",
876 .stat_start = function_stat_start,
877 .stat_next = function_stat_next,
878 .stat_cmp = function_stat_cmp,
879 .stat_headers = function_stat_headers,
880 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400881};
882
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400883static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400884{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400885 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400886 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400887 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400888 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400889 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400890
Steven Rostedtcafb1682009-03-24 20:50:39 -0400891 for_each_possible_cpu(cpu) {
892 stat = &per_cpu(ftrace_profile_stats, cpu);
893
894 /* allocate enough for function name + cpu number */
895 name = kmalloc(32, GFP_KERNEL);
896 if (!name) {
897 /*
898 * The files created are permanent, if something happens
899 * we still do not free memory.
900 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400901 WARN(1,
902 "Could not allocate stat file for cpu %d\n",
903 cpu);
904 return;
905 }
906 stat->stat = function_stats;
907 snprintf(name, 32, "function%d", cpu);
908 stat->stat.name = name;
909 ret = register_stat_tracer(&stat->stat);
910 if (ret) {
911 WARN(1,
912 "Could not register function stat for cpu %d\n",
913 cpu);
914 kfree(name);
915 return;
916 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400917 }
918
919 entry = debugfs_create_file("function_profile_enabled", 0644,
920 d_tracer, NULL, &ftrace_profile_fops);
921 if (!entry)
922 pr_warning("Could not create debugfs "
923 "'function_profile_enabled' entry\n");
924}
925
926#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400927static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400928{
929}
930#endif /* CONFIG_FUNCTION_PROFILER */
931
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100932static struct pid * const ftrace_swapper_pid = &init_struct_pid;
933
Steven Rostedt3d083392008-05-12 21:20:42 +0200934#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100935
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400936#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400937# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400938#endif
939
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500940static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
941
Steven Rostedtb6887d72009-02-17 12:32:04 -0500942struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500943 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500944 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500945 unsigned long flags;
946 unsigned long ip;
947 void *data;
948 struct rcu_head rcu;
949};
950
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400951struct ftrace_func_entry {
952 struct hlist_node hlist;
953 unsigned long ip;
954};
955
956struct ftrace_hash {
957 unsigned long size_bits;
958 struct hlist_head *buckets;
959 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -0400960 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400961};
962
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400963/*
964 * We make these constant because no one should touch them,
965 * but they are used as the default "empty hash", to avoid allocating
966 * it all the time. These are in a read only section such that if
967 * anyone does try to modify it, it will cause an exception.
968 */
969static const struct hlist_head empty_buckets[1];
970static const struct ftrace_hash empty_hash = {
971 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -0400972};
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400973#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +0200974
Steven Rostedt2b499382011-05-03 22:49:52 -0400975static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -0400976 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -0400977 .notrace_hash = EMPTY_HASH,
978 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -0400979};
980
Steven Rostedt41c52c02008-05-22 11:46:33 -0400981static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200982
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200983struct ftrace_page {
984 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -0500985 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500986 int index;
Steven Rostedta7900872011-12-16 16:23:44 -0500987 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -0700988};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200989
Steven Rostedt85ae32a2011-12-16 16:30:31 -0500990static struct ftrace_page *ftrace_new_pgs;
991
Steven Rostedta7900872011-12-16 16:23:44 -0500992#define ENTRY_SIZE sizeof(struct dyn_ftrace)
993#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200994
995/* estimate from running different kernels */
996#define NR_TO_INIT 10000
997
998static struct ftrace_page *ftrace_pages_start;
999static struct ftrace_page *ftrace_pages;
1000
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001001static struct ftrace_func_entry *
1002ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1003{
1004 unsigned long key;
1005 struct ftrace_func_entry *entry;
1006 struct hlist_head *hhd;
1007 struct hlist_node *n;
1008
1009 if (!hash->count)
1010 return NULL;
1011
1012 if (hash->size_bits > 0)
1013 key = hash_long(ip, hash->size_bits);
1014 else
1015 key = 0;
1016
1017 hhd = &hash->buckets[key];
1018
1019 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1020 if (entry->ip == ip)
1021 return entry;
1022 }
1023 return NULL;
1024}
1025
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001026static void __add_hash_entry(struct ftrace_hash *hash,
1027 struct ftrace_func_entry *entry)
1028{
1029 struct hlist_head *hhd;
1030 unsigned long key;
1031
1032 if (hash->size_bits)
1033 key = hash_long(entry->ip, hash->size_bits);
1034 else
1035 key = 0;
1036
1037 hhd = &hash->buckets[key];
1038 hlist_add_head(&entry->hlist, hhd);
1039 hash->count++;
1040}
1041
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001042static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1043{
1044 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001045
1046 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1047 if (!entry)
1048 return -ENOMEM;
1049
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001050 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001051 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001052
1053 return 0;
1054}
1055
1056static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001057free_hash_entry(struct ftrace_hash *hash,
1058 struct ftrace_func_entry *entry)
1059{
1060 hlist_del(&entry->hlist);
1061 kfree(entry);
1062 hash->count--;
1063}
1064
1065static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001066remove_hash_entry(struct ftrace_hash *hash,
1067 struct ftrace_func_entry *entry)
1068{
1069 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001070 hash->count--;
1071}
1072
1073static void ftrace_hash_clear(struct ftrace_hash *hash)
1074{
1075 struct hlist_head *hhd;
1076 struct hlist_node *tp, *tn;
1077 struct ftrace_func_entry *entry;
1078 int size = 1 << hash->size_bits;
1079 int i;
1080
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001081 if (!hash->count)
1082 return;
1083
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001084 for (i = 0; i < size; i++) {
1085 hhd = &hash->buckets[i];
1086 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001087 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001088 }
1089 FTRACE_WARN_ON(hash->count);
1090}
1091
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001092static void free_ftrace_hash(struct ftrace_hash *hash)
1093{
1094 if (!hash || hash == EMPTY_HASH)
1095 return;
1096 ftrace_hash_clear(hash);
1097 kfree(hash->buckets);
1098 kfree(hash);
1099}
1100
Steven Rostedt07fd5512011-05-05 18:03:47 -04001101static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1102{
1103 struct ftrace_hash *hash;
1104
1105 hash = container_of(rcu, struct ftrace_hash, rcu);
1106 free_ftrace_hash(hash);
1107}
1108
1109static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1110{
1111 if (!hash || hash == EMPTY_HASH)
1112 return;
1113 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1114}
1115
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001116static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1117{
1118 struct ftrace_hash *hash;
1119 int size;
1120
1121 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1122 if (!hash)
1123 return NULL;
1124
1125 size = 1 << size_bits;
1126 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1127
1128 if (!hash->buckets) {
1129 kfree(hash);
1130 return NULL;
1131 }
1132
1133 hash->size_bits = size_bits;
1134
1135 return hash;
1136}
1137
1138static struct ftrace_hash *
1139alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1140{
1141 struct ftrace_func_entry *entry;
1142 struct ftrace_hash *new_hash;
1143 struct hlist_node *tp;
1144 int size;
1145 int ret;
1146 int i;
1147
1148 new_hash = alloc_ftrace_hash(size_bits);
1149 if (!new_hash)
1150 return NULL;
1151
1152 /* Empty hash? */
1153 if (!hash || !hash->count)
1154 return new_hash;
1155
1156 size = 1 << hash->size_bits;
1157 for (i = 0; i < size; i++) {
1158 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1159 ret = add_hash_entry(new_hash, entry->ip);
1160 if (ret < 0)
1161 goto free_hash;
1162 }
1163 }
1164
1165 FTRACE_WARN_ON(new_hash->count != hash->count);
1166
1167 return new_hash;
1168
1169 free_hash:
1170 free_ftrace_hash(new_hash);
1171 return NULL;
1172}
1173
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001174static void
1175ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1176static void
1177ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1178
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001179static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001180ftrace_hash_move(struct ftrace_ops *ops, int enable,
1181 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001182{
1183 struct ftrace_func_entry *entry;
1184 struct hlist_node *tp, *tn;
1185 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001186 struct ftrace_hash *old_hash;
1187 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001188 unsigned long key;
1189 int size = src->count;
1190 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001191 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001192 int i;
1193
1194 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001195 * Remove the current set, update the hash and add
1196 * them back.
1197 */
1198 ftrace_hash_rec_disable(ops, enable);
1199
1200 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001201 * If the new source is empty, just free dst and assign it
1202 * the empty_hash.
1203 */
1204 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001205 free_ftrace_hash_rcu(*dst);
1206 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001207 /* still need to update the function records */
1208 ret = 0;
1209 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001210 }
1211
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001212 /*
1213 * Make the hash size about 1/2 the # found
1214 */
1215 for (size /= 2; size; size >>= 1)
1216 bits++;
1217
1218 /* Don't allocate too much */
1219 if (bits > FTRACE_HASH_MAX_BITS)
1220 bits = FTRACE_HASH_MAX_BITS;
1221
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001222 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001223 new_hash = alloc_ftrace_hash(bits);
1224 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001225 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001226
1227 size = 1 << src->size_bits;
1228 for (i = 0; i < size; i++) {
1229 hhd = &src->buckets[i];
1230 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1231 if (bits > 0)
1232 key = hash_long(entry->ip, bits);
1233 else
1234 key = 0;
1235 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001236 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001237 }
1238 }
1239
Steven Rostedt07fd5512011-05-05 18:03:47 -04001240 old_hash = *dst;
1241 rcu_assign_pointer(*dst, new_hash);
1242 free_ftrace_hash_rcu(old_hash);
1243
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001244 ret = 0;
1245 out:
1246 /*
1247 * Enable regardless of ret:
1248 * On success, we enable the new hash.
1249 * On failure, we re-enable the original hash.
1250 */
1251 ftrace_hash_rec_enable(ops, enable);
1252
1253 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001254}
1255
Steven Rostedt265c8312009-02-13 12:43:56 -05001256/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001257 * Test the hashes for this ops to see if we want to call
1258 * the ops->func or not.
1259 *
1260 * It's a match if the ip is in the ops->filter_hash or
1261 * the filter_hash does not exist or is empty,
1262 * AND
1263 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001264 *
1265 * This needs to be called with preemption disabled as
1266 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001267 */
1268static int
1269ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1270{
1271 struct ftrace_hash *filter_hash;
1272 struct ftrace_hash *notrace_hash;
1273 int ret;
1274
Steven Rostedtb8489142011-05-04 09:27:52 -04001275 filter_hash = rcu_dereference_raw(ops->filter_hash);
1276 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1277
1278 if ((!filter_hash || !filter_hash->count ||
1279 ftrace_lookup_ip(filter_hash, ip)) &&
1280 (!notrace_hash || !notrace_hash->count ||
1281 !ftrace_lookup_ip(notrace_hash, ip)))
1282 ret = 1;
1283 else
1284 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001285
1286 return ret;
1287}
1288
1289/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001290 * This is a double for. Do not use 'break' to break out of the loop,
1291 * you must use a goto.
1292 */
1293#define do_for_each_ftrace_rec(pg, rec) \
1294 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1295 int _____i; \
1296 for (_____i = 0; _____i < pg->index; _____i++) { \
1297 rec = &pg->records[_____i];
1298
1299#define while_for_each_ftrace_rec() \
1300 } \
1301 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301302
Steven Rostedtc88fd862011-08-16 09:53:39 -04001303/**
1304 * ftrace_location - return true if the ip giving is a traced location
1305 * @ip: the instruction pointer to check
1306 *
1307 * Returns 1 if @ip given is a pointer to a ftrace location.
1308 * That is, the instruction that is either a NOP or call to
1309 * the function tracer. It checks the ftrace internal tables to
1310 * determine if the address belongs or not.
1311 */
1312int ftrace_location(unsigned long ip)
1313{
1314 struct ftrace_page *pg;
1315 struct dyn_ftrace *rec;
1316
1317 do_for_each_ftrace_rec(pg, rec) {
1318 if (rec->ip == ip)
1319 return 1;
1320 } while_for_each_ftrace_rec();
1321
1322 return 0;
1323}
1324
Steven Rostedted926f92011-05-03 13:25:24 -04001325static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1326 int filter_hash,
1327 bool inc)
1328{
1329 struct ftrace_hash *hash;
1330 struct ftrace_hash *other_hash;
1331 struct ftrace_page *pg;
1332 struct dyn_ftrace *rec;
1333 int count = 0;
1334 int all = 0;
1335
1336 /* Only update if the ops has been registered */
1337 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1338 return;
1339
1340 /*
1341 * In the filter_hash case:
1342 * If the count is zero, we update all records.
1343 * Otherwise we just update the items in the hash.
1344 *
1345 * In the notrace_hash case:
1346 * We enable the update in the hash.
1347 * As disabling notrace means enabling the tracing,
1348 * and enabling notrace means disabling, the inc variable
1349 * gets inversed.
1350 */
1351 if (filter_hash) {
1352 hash = ops->filter_hash;
1353 other_hash = ops->notrace_hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001354 if (!hash || !hash->count)
Steven Rostedted926f92011-05-03 13:25:24 -04001355 all = 1;
1356 } else {
1357 inc = !inc;
1358 hash = ops->notrace_hash;
1359 other_hash = ops->filter_hash;
1360 /*
1361 * If the notrace hash has no items,
1362 * then there's nothing to do.
1363 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001364 if (hash && !hash->count)
Steven Rostedted926f92011-05-03 13:25:24 -04001365 return;
1366 }
1367
1368 do_for_each_ftrace_rec(pg, rec) {
1369 int in_other_hash = 0;
1370 int in_hash = 0;
1371 int match = 0;
1372
1373 if (all) {
1374 /*
1375 * Only the filter_hash affects all records.
1376 * Update if the record is not in the notrace hash.
1377 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001378 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001379 match = 1;
1380 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -04001381 in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip);
1382 in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001383
1384 /*
1385 *
1386 */
1387 if (filter_hash && in_hash && !in_other_hash)
1388 match = 1;
1389 else if (!filter_hash && in_hash &&
1390 (in_other_hash || !other_hash->count))
1391 match = 1;
1392 }
1393 if (!match)
1394 continue;
1395
1396 if (inc) {
1397 rec->flags++;
1398 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1399 return;
1400 } else {
1401 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1402 return;
1403 rec->flags--;
1404 }
1405 count++;
1406 /* Shortcut, if we handled all records, we are done. */
1407 if (!all && count == hash->count)
1408 return;
1409 } while_for_each_ftrace_rec();
1410}
1411
1412static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1413 int filter_hash)
1414{
1415 __ftrace_hash_rec_update(ops, filter_hash, 0);
1416}
1417
1418static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1419 int filter_hash)
1420{
1421 __ftrace_hash_rec_update(ops, filter_hash, 1);
1422}
1423
Ingo Molnare309b412008-05-12 21:20:51 +02001424static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001425{
Steven Rostedta7900872011-12-16 16:23:44 -05001426 if (ftrace_pages->index == ftrace_pages->size) {
1427 /* We should have allocated enough */
1428 if (WARN_ON(!ftrace_pages->next))
1429 return NULL;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001430 ftrace_pages = ftrace_pages->next;
1431 }
1432
1433 return &ftrace_pages->records[ftrace_pages->index++];
1434}
1435
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001436static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001437ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001438{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001439 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001440
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001441 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001442 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001443
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001444 rec = ftrace_alloc_dyn_node(ip);
1445 if (!rec)
1446 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001447
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001448 rec->ip = ip;
Steven Rostedt3d083392008-05-12 21:20:42 +02001449
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001450 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001451}
1452
Steven Rostedt05736a42008-09-22 14:55:47 -07001453static void print_ip_ins(const char *fmt, unsigned char *p)
1454{
1455 int i;
1456
1457 printk(KERN_CONT "%s", fmt);
1458
1459 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1460 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1461}
1462
Steven Rostedtc88fd862011-08-16 09:53:39 -04001463/**
1464 * ftrace_bug - report and shutdown function tracer
1465 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1466 * @ip: The address that failed
1467 *
1468 * The arch code that enables or disables the function tracing
1469 * can call ftrace_bug() when it has detected a problem in
1470 * modifying the code. @failed should be one of either:
1471 * EFAULT - if the problem happens on reading the @ip address
1472 * EINVAL - if what is read at @ip is not what was expected
1473 * EPERM - if the problem happens on writting to the @ip address
1474 */
1475void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001476{
1477 switch (failed) {
1478 case -EFAULT:
1479 FTRACE_WARN_ON_ONCE(1);
1480 pr_info("ftrace faulted on modifying ");
1481 print_ip_sym(ip);
1482 break;
1483 case -EINVAL:
1484 FTRACE_WARN_ON_ONCE(1);
1485 pr_info("ftrace failed to modify ");
1486 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001487 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001488 printk(KERN_CONT "\n");
1489 break;
1490 case -EPERM:
1491 FTRACE_WARN_ON_ONCE(1);
1492 pr_info("ftrace faulted on writing ");
1493 print_ip_sym(ip);
1494 break;
1495 default:
1496 FTRACE_WARN_ON_ONCE(1);
1497 pr_info("ftrace faulted on unknown error ");
1498 print_ip_sym(ip);
1499 }
1500}
1501
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001502
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001503/* Return 1 if the address range is reserved for ftrace */
1504int ftrace_text_reserved(void *start, void *end)
1505{
1506 struct dyn_ftrace *rec;
1507 struct ftrace_page *pg;
1508
1509 do_for_each_ftrace_rec(pg, rec) {
1510 if (rec->ip <= (unsigned long)end &&
1511 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1512 return 1;
1513 } while_for_each_ftrace_rec();
1514 return 0;
1515}
1516
Steven Rostedtc88fd862011-08-16 09:53:39 -04001517static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001518{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001519 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001520
Steven Rostedt982c3502008-11-15 16:31:41 -05001521 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001522 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001523 *
Steven Rostedted926f92011-05-03 13:25:24 -04001524 * If the record has a ref count, then we need to enable it
1525 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001526 *
Steven Rostedted926f92011-05-03 13:25:24 -04001527 * Otherwise we make sure its disabled.
1528 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001529 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001530 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001531 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001532 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001533 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001534
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001535 /* If the state of this record hasn't changed, then do nothing */
1536 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001537 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001538
1539 if (flag) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001540 if (update)
1541 rec->flags |= FTRACE_FL_ENABLED;
1542 return FTRACE_UPDATE_MAKE_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001543 }
1544
Steven Rostedtc88fd862011-08-16 09:53:39 -04001545 if (update)
1546 rec->flags &= ~FTRACE_FL_ENABLED;
1547
1548 return FTRACE_UPDATE_MAKE_NOP;
1549}
1550
1551/**
1552 * ftrace_update_record, set a record that now is tracing or not
1553 * @rec: the record to update
1554 * @enable: set to 1 if the record is tracing, zero to force disable
1555 *
1556 * The records that represent all functions that can be traced need
1557 * to be updated when tracing has been enabled.
1558 */
1559int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1560{
1561 return ftrace_check_record(rec, enable, 1);
1562}
1563
1564/**
1565 * ftrace_test_record, check if the record has been enabled or not
1566 * @rec: the record to test
1567 * @enable: set to 1 to check if enabled, 0 if it is disabled
1568 *
1569 * The arch code may need to test if a record is already set to
1570 * tracing to determine how to modify the function code that it
1571 * represents.
1572 */
1573int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1574{
1575 return ftrace_check_record(rec, enable, 0);
1576}
1577
1578static int
1579__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1580{
1581 unsigned long ftrace_addr;
1582 int ret;
1583
1584 ftrace_addr = (unsigned long)FTRACE_ADDR;
1585
1586 ret = ftrace_update_record(rec, enable);
1587
1588 switch (ret) {
1589 case FTRACE_UPDATE_IGNORE:
1590 return 0;
1591
1592 case FTRACE_UPDATE_MAKE_CALL:
1593 return ftrace_make_call(rec, ftrace_addr);
1594
1595 case FTRACE_UPDATE_MAKE_NOP:
1596 return ftrace_make_nop(NULL, rec, ftrace_addr);
1597 }
1598
1599 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001600}
1601
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001602static void ftrace_replace_code(int update)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001603{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001604 struct dyn_ftrace *rec;
1605 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001606 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001607
Steven Rostedt45a4a232011-04-21 23:16:46 -04001608 if (unlikely(ftrace_disabled))
1609 return;
1610
Steven Rostedt265c8312009-02-13 12:43:56 -05001611 do_for_each_ftrace_rec(pg, rec) {
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001612 failed = __ftrace_replace_code(rec, update);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001613 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001614 ftrace_bug(failed, rec->ip);
1615 /* Stop processing */
1616 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001617 }
1618 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001619}
1620
Steven Rostedtc88fd862011-08-16 09:53:39 -04001621struct ftrace_rec_iter {
1622 struct ftrace_page *pg;
1623 int index;
1624};
1625
1626/**
1627 * ftrace_rec_iter_start, start up iterating over traced functions
1628 *
1629 * Returns an iterator handle that is used to iterate over all
1630 * the records that represent address locations where functions
1631 * are traced.
1632 *
1633 * May return NULL if no records are available.
1634 */
1635struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1636{
1637 /*
1638 * We only use a single iterator.
1639 * Protected by the ftrace_lock mutex.
1640 */
1641 static struct ftrace_rec_iter ftrace_rec_iter;
1642 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1643
1644 iter->pg = ftrace_pages_start;
1645 iter->index = 0;
1646
1647 /* Could have empty pages */
1648 while (iter->pg && !iter->pg->index)
1649 iter->pg = iter->pg->next;
1650
1651 if (!iter->pg)
1652 return NULL;
1653
1654 return iter;
1655}
1656
1657/**
1658 * ftrace_rec_iter_next, get the next record to process.
1659 * @iter: The handle to the iterator.
1660 *
1661 * Returns the next iterator after the given iterator @iter.
1662 */
1663struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1664{
1665 iter->index++;
1666
1667 if (iter->index >= iter->pg->index) {
1668 iter->pg = iter->pg->next;
1669 iter->index = 0;
1670
1671 /* Could have empty pages */
1672 while (iter->pg && !iter->pg->index)
1673 iter->pg = iter->pg->next;
1674 }
1675
1676 if (!iter->pg)
1677 return NULL;
1678
1679 return iter;
1680}
1681
1682/**
1683 * ftrace_rec_iter_record, get the record at the iterator location
1684 * @iter: The current iterator location
1685 *
1686 * Returns the record that the current @iter is at.
1687 */
1688struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1689{
1690 return &iter->pg->records[iter->index];
1691}
1692
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301693static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001694ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001695{
1696 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001697 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001698
1699 ip = rec->ip;
1700
Steven Rostedt45a4a232011-04-21 23:16:46 -04001701 if (unlikely(ftrace_disabled))
1702 return 0;
1703
Shaohua Li25aac9d2009-01-09 11:29:40 +08001704 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001705 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001706 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301707 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001708 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301709 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001710}
1711
Steven Rostedt000ab692009-02-17 13:35:06 -05001712/*
1713 * archs can override this function if they must do something
1714 * before the modifying code is performed.
1715 */
1716int __weak ftrace_arch_code_modify_prepare(void)
1717{
1718 return 0;
1719}
1720
1721/*
1722 * archs can override this function if they must do something
1723 * after the modifying code is performed.
1724 */
1725int __weak ftrace_arch_code_modify_post_process(void)
1726{
1727 return 0;
1728}
1729
Ingo Molnare309b412008-05-12 21:20:51 +02001730static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001731{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001732 int *command = data;
1733
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001734 if (*command & FTRACE_UPDATE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001735 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001736 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001737 ftrace_replace_code(0);
1738
1739 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1740 ftrace_update_ftrace_func(ftrace_trace_function);
1741
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001742 if (*command & FTRACE_START_FUNC_RET)
1743 ftrace_enable_ftrace_graph_caller();
1744 else if (*command & FTRACE_STOP_FUNC_RET)
1745 ftrace_disable_ftrace_graph_caller();
1746
Steven Rostedtc88fd862011-08-16 09:53:39 -04001747 return 0;
1748}
1749
1750/**
1751 * ftrace_run_stop_machine, go back to the stop machine method
1752 * @command: The command to tell ftrace what to do
1753 *
1754 * If an arch needs to fall back to the stop machine method, the
1755 * it can call this function.
1756 */
1757void ftrace_run_stop_machine(int command)
1758{
1759 stop_machine(__ftrace_modify_code, &command, NULL);
1760}
1761
1762/**
1763 * arch_ftrace_update_code, modify the code to trace or not trace
1764 * @command: The command that needs to be done
1765 *
1766 * Archs can override this function if it does not need to
1767 * run stop_machine() to modify code.
1768 */
1769void __weak arch_ftrace_update_code(int command)
1770{
1771 ftrace_run_stop_machine(command);
1772}
1773
1774static void ftrace_run_update_code(int command)
1775{
1776 int ret;
1777
1778 ret = ftrace_arch_code_modify_prepare();
1779 FTRACE_WARN_ON(ret);
1780 if (ret)
1781 return;
1782 /*
1783 * Do not call function tracer while we update the code.
1784 * We are in stop machine.
1785 */
1786 function_trace_stop++;
1787
1788 /*
1789 * By default we use stop_machine() to modify the code.
1790 * But archs can do what ever they want as long as it
1791 * is safe. The stop_machine() is the safest, but also
1792 * produces the most overhead.
1793 */
1794 arch_ftrace_update_code(command);
1795
Steven Rostedt6331c282011-07-13 15:11:02 -04001796#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1797 /*
1798 * For archs that call ftrace_test_stop_func(), we must
1799 * wait till after we update all the function callers
1800 * before we update the callback. This keeps different
1801 * ops that record different functions from corrupting
1802 * each other.
1803 */
1804 __ftrace_trace_function = __ftrace_trace_function_delay;
1805#endif
1806 function_trace_stop--;
1807
Steven Rostedt000ab692009-02-17 13:35:06 -05001808 ret = ftrace_arch_code_modify_post_process();
1809 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001810}
1811
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001812static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001813static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001814static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001815
1816static void ftrace_startup_enable(int command)
1817{
1818 if (saved_ftrace_func != ftrace_trace_function) {
1819 saved_ftrace_func = ftrace_trace_function;
1820 command |= FTRACE_UPDATE_TRACE_FUNC;
1821 }
1822
1823 if (!command || !ftrace_enabled)
1824 return;
1825
1826 ftrace_run_update_code(command);
1827}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001828
Steven Rostedta1cd6172011-05-23 15:24:25 -04001829static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001830{
Steven Rostedtb8489142011-05-04 09:27:52 -04001831 bool hash_enable = true;
1832
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001833 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001834 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001835
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001836 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001837 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001838
Steven Rostedtb8489142011-05-04 09:27:52 -04001839 /* ops marked global share the filter hashes */
1840 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1841 ops = &global_ops;
1842 /* Don't update hash if global is already set */
1843 if (global_start_up)
1844 hash_enable = false;
1845 global_start_up++;
1846 }
1847
Steven Rostedted926f92011-05-03 13:25:24 -04001848 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001849 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001850 ftrace_hash_rec_enable(ops, 1);
1851
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001852 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001853
1854 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001855}
1856
Steven Rostedtbd69c302011-05-03 21:55:54 -04001857static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001858{
Steven Rostedtb8489142011-05-04 09:27:52 -04001859 bool hash_disable = true;
1860
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001861 if (unlikely(ftrace_disabled))
1862 return;
1863
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001864 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001865 /*
1866 * Just warn in case of unbalance, no need to kill ftrace, it's not
1867 * critical but the ftrace_call callers may be never nopped again after
1868 * further ftrace uses.
1869 */
1870 WARN_ON_ONCE(ftrace_start_up < 0);
1871
Steven Rostedtb8489142011-05-04 09:27:52 -04001872 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1873 ops = &global_ops;
1874 global_start_up--;
1875 WARN_ON_ONCE(global_start_up < 0);
1876 /* Don't update hash if global still has users */
1877 if (global_start_up) {
1878 WARN_ON_ONCE(!ftrace_start_up);
1879 hash_disable = false;
1880 }
1881 }
1882
1883 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04001884 ftrace_hash_rec_disable(ops, 1);
1885
Steven Rostedtb8489142011-05-04 09:27:52 -04001886 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04001887 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001888
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001889 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001890
1891 if (saved_ftrace_func != ftrace_trace_function) {
1892 saved_ftrace_func = ftrace_trace_function;
1893 command |= FTRACE_UPDATE_TRACE_FUNC;
1894 }
1895
1896 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001897 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001898
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001899 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001900}
1901
Ingo Molnare309b412008-05-12 21:20:51 +02001902static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001903{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001904 if (unlikely(ftrace_disabled))
1905 return;
1906
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001907 /* Force update next time */
1908 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001909 /* ftrace_start_up is true if we want ftrace running */
1910 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001911 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001912}
1913
Ingo Molnare309b412008-05-12 21:20:51 +02001914static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001915{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001916 if (unlikely(ftrace_disabled))
1917 return;
1918
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001919 /* ftrace_start_up is true if ftrace is running */
1920 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04001921 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001922}
1923
Steven Rostedt3d083392008-05-12 21:20:42 +02001924static cycle_t ftrace_update_time;
1925static unsigned long ftrace_update_cnt;
1926unsigned long ftrace_update_tot_cnt;
1927
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001928static int ops_traces_mod(struct ftrace_ops *ops)
1929{
1930 struct ftrace_hash *hash;
1931
1932 hash = ops->filter_hash;
1933 return !!(!hash || !hash->count);
1934}
1935
Steven Rostedt31e88902008-11-14 16:21:19 -08001936static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001937{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001938 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001939 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301940 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001941 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001942 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04001943
1944 /*
1945 * When adding a module, we need to check if tracers are
1946 * currently enabled and if they are set to trace all functions.
1947 * If they are, we need to enable the module functions as well
1948 * as update the reference counts for those function records.
1949 */
1950 if (mod) {
1951 struct ftrace_ops *ops;
1952
1953 for (ops = ftrace_ops_list;
1954 ops != &ftrace_list_end; ops = ops->next) {
1955 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
1956 ops_traces_mod(ops))
1957 ref++;
1958 }
1959 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001960
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001961 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001962 ftrace_update_cnt = 0;
1963
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001964 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301965
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001966 for (i = 0; i < pg->index; i++) {
1967 /* If something went wrong, bail without enabling anything */
1968 if (unlikely(ftrace_disabled))
1969 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001970
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001971 p = &pg->records[i];
1972 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301973
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001974 /*
1975 * Do the initial record conversion from mcount jump
1976 * to the NOP instructions.
1977 */
1978 if (!ftrace_code_disable(mod, p))
1979 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001980
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001981 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001982
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001983 /*
1984 * If the tracing is enabled, go ahead and enable the record.
1985 *
1986 * The reason not to enable the record immediatelly is the
1987 * inherent check of ftrace_make_nop/ftrace_make_call for
1988 * correct previous instructions. Making first the NOP
1989 * conversion puts the module to the correct state, thus
1990 * passing the ftrace_make_call check.
1991 */
1992 if (ftrace_start_up && ref) {
1993 int failed = __ftrace_replace_code(p, 1);
1994 if (failed)
1995 ftrace_bug(failed, p->ip);
1996 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001997 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001998 }
1999
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002000 ftrace_new_pgs = NULL;
2001
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002002 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002003 ftrace_update_time = stop - start;
2004 ftrace_update_tot_cnt += ftrace_update_cnt;
2005
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002006 return 0;
2007}
2008
Steven Rostedta7900872011-12-16 16:23:44 -05002009static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002010{
Steven Rostedta7900872011-12-16 16:23:44 -05002011 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002012 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002013
Steven Rostedta7900872011-12-16 16:23:44 -05002014 if (WARN_ON(!count))
2015 return -EINVAL;
2016
2017 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002018
2019 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002020 * We want to fill as much as possible. No more than a page
2021 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002022 */
Steven Rostedta7900872011-12-16 16:23:44 -05002023 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2024 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002025
Steven Rostedta7900872011-12-16 16:23:44 -05002026 again:
2027 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2028
2029 if (!pg->records) {
2030 /* if we can't allocate this size, try something smaller */
2031 if (!order)
2032 return -ENOMEM;
2033 order >>= 1;
2034 goto again;
2035 }
2036
2037 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2038 pg->size = cnt;
2039
2040 if (cnt > count)
2041 cnt = count;
2042
2043 return cnt;
2044}
2045
2046static struct ftrace_page *
2047ftrace_allocate_pages(unsigned long num_to_init)
2048{
2049 struct ftrace_page *start_pg;
2050 struct ftrace_page *pg;
2051 int order;
2052 int cnt;
2053
2054 if (!num_to_init)
2055 return 0;
2056
2057 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2058 if (!pg)
2059 return NULL;
2060
2061 /*
2062 * Try to allocate as much as possible in one continues
2063 * location that fills in all of the space. We want to
2064 * waste as little space as possible.
2065 */
2066 for (;;) {
2067 cnt = ftrace_allocate_records(pg, num_to_init);
2068 if (cnt < 0)
2069 goto free_pages;
2070
2071 num_to_init -= cnt;
2072 if (!num_to_init)
2073 break;
2074
2075 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2076 if (!pg->next)
2077 goto free_pages;
2078
2079 pg = pg->next;
2080 }
2081
2082 return start_pg;
2083
2084 free_pages:
2085 while (start_pg) {
2086 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2087 free_pages((unsigned long)pg->records, order);
2088 start_pg = pg->next;
2089 kfree(pg);
2090 pg = start_pg;
2091 }
2092 pr_info("ftrace: FAILED to allocate memory for functions\n");
2093 return NULL;
2094}
2095
2096static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2097{
2098 int cnt;
2099
2100 if (!num_to_init) {
2101 pr_info("ftrace: No functions to be traced?\n");
2102 return -1;
2103 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002104
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002105 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002106 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002107 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002108
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002109 return 0;
2110}
2111
Steven Rostedt5072c592008-05-12 21:20:43 +02002112enum {
2113 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002114 FTRACE_ITER_NOTRACE = (1 << 1),
Steven Rostedt3499e462011-04-21 22:59:12 -04002115 FTRACE_ITER_PRINTALL = (1 << 2),
2116 FTRACE_ITER_HASH = (1 << 3),
Steven Rostedt647bcd02011-05-03 14:39:21 -04002117 FTRACE_ITER_ENABLED = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02002118};
2119
2120#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2121
2122struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002123 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002124 loff_t func_pos;
2125 struct ftrace_page *pg;
2126 struct dyn_ftrace *func;
2127 struct ftrace_func_probe *probe;
2128 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002129 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002130 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002131 int hidx;
2132 int idx;
2133 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002134};
2135
Ingo Molnare309b412008-05-12 21:20:51 +02002136static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002137t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002138{
2139 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002140 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002141 struct hlist_head *hhd;
2142
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002143 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002144 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002145
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002146 if (iter->probe)
2147 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002148 retry:
2149 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2150 return NULL;
2151
2152 hhd = &ftrace_func_hash[iter->hidx];
2153
2154 if (hlist_empty(hhd)) {
2155 iter->hidx++;
2156 hnd = NULL;
2157 goto retry;
2158 }
2159
2160 if (!hnd)
2161 hnd = hhd->first;
2162 else {
2163 hnd = hnd->next;
2164 if (!hnd) {
2165 iter->hidx++;
2166 goto retry;
2167 }
2168 }
2169
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002170 if (WARN_ON_ONCE(!hnd))
2171 return NULL;
2172
2173 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2174
2175 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002176}
2177
2178static void *t_hash_start(struct seq_file *m, loff_t *pos)
2179{
2180 struct ftrace_iterator *iter = m->private;
2181 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002182 loff_t l;
2183
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002184 if (iter->func_pos > *pos)
2185 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002186
Li Zefand82d6242009-06-24 09:54:54 +08002187 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002188 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002189 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002190 if (!p)
2191 break;
2192 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002193 if (!p)
2194 return NULL;
2195
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002196 /* Only set this if we have an item */
2197 iter->flags |= FTRACE_ITER_HASH;
2198
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002199 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002200}
2201
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002202static int
2203t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002204{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002205 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002206
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002207 rec = iter->probe;
2208 if (WARN_ON_ONCE(!rec))
2209 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002210
Steven Rostedt809dcf22009-02-16 23:06:01 -05002211 if (rec->ops->print)
2212 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2213
Steven Rostedtb375a112009-09-17 00:05:58 -04002214 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002215
2216 if (rec->data)
2217 seq_printf(m, ":%p", rec->data);
2218 seq_putc(m, '\n');
2219
2220 return 0;
2221}
2222
2223static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002224t_next(struct seq_file *m, void *v, loff_t *pos)
2225{
2226 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002227 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002228 struct dyn_ftrace *rec = NULL;
2229
Steven Rostedt45a4a232011-04-21 23:16:46 -04002230 if (unlikely(ftrace_disabled))
2231 return NULL;
2232
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002233 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002234 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002235
Steven Rostedt5072c592008-05-12 21:20:43 +02002236 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002237 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002238
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002239 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002240 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002241
Steven Rostedt5072c592008-05-12 21:20:43 +02002242 retry:
2243 if (iter->idx >= iter->pg->index) {
2244 if (iter->pg->next) {
2245 iter->pg = iter->pg->next;
2246 iter->idx = 0;
2247 goto retry;
2248 }
2249 } else {
2250 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05002251 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002252 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05002253
Steven Rostedt41c52c02008-05-22 11:46:33 -04002254 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002255 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2256
2257 ((iter->flags & FTRACE_ITER_ENABLED) &&
2258 !(rec->flags & ~FTRACE_FL_MASK))) {
2259
Steven Rostedt5072c592008-05-12 21:20:43 +02002260 rec = NULL;
2261 goto retry;
2262 }
2263 }
2264
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002265 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002266 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002267
2268 iter->func = rec;
2269
2270 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002271}
2272
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002273static void reset_iter_read(struct ftrace_iterator *iter)
2274{
2275 iter->pos = 0;
2276 iter->func_pos = 0;
2277 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002278}
2279
2280static void *t_start(struct seq_file *m, loff_t *pos)
2281{
2282 struct ftrace_iterator *iter = m->private;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002283 struct ftrace_ops *ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002284 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002285 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002286
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002287 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002288
2289 if (unlikely(ftrace_disabled))
2290 return NULL;
2291
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002292 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002293 * If an lseek was done, then reset and start from beginning.
2294 */
2295 if (*pos < iter->pos)
2296 reset_iter_read(iter);
2297
2298 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002299 * For set_ftrace_filter reading, if we have the filter
2300 * off, we can short cut and just print out that all
2301 * functions are enabled.
2302 */
Steven Rostedtf45948e2011-05-02 12:29:25 -04002303 if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002304 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002305 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002306 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002307 /* reset in case of seek/pread */
2308 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002309 return iter;
2310 }
2311
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002312 if (iter->flags & FTRACE_ITER_HASH)
2313 return t_hash_start(m, pos);
2314
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002315 /*
2316 * Unfortunately, we need to restart at ftrace_pages_start
2317 * every time we let go of the ftrace_mutex. This is because
2318 * those pointers can change without the lock.
2319 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002320 iter->pg = ftrace_pages_start;
2321 iter->idx = 0;
2322 for (l = 0; l <= *pos; ) {
2323 p = t_next(m, p, &l);
2324 if (!p)
2325 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002326 }
walimis5821e1b2008-11-15 15:19:06 +08002327
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002328 if (!p) {
2329 if (iter->flags & FTRACE_ITER_FILTER)
2330 return t_hash_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002331
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002332 return NULL;
2333 }
2334
2335 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002336}
2337
2338static void t_stop(struct seq_file *m, void *p)
2339{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002340 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002341}
2342
2343static int t_show(struct seq_file *m, void *v)
2344{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002345 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002346 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002347
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002348 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002349 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002350
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002351 if (iter->flags & FTRACE_ITER_PRINTALL) {
2352 seq_printf(m, "#### all functions enabled ####\n");
2353 return 0;
2354 }
2355
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002356 rec = iter->func;
2357
Steven Rostedt5072c592008-05-12 21:20:43 +02002358 if (!rec)
2359 return 0;
2360
Steven Rostedt647bcd02011-05-03 14:39:21 -04002361 seq_printf(m, "%ps", (void *)rec->ip);
2362 if (iter->flags & FTRACE_ITER_ENABLED)
2363 seq_printf(m, " (%ld)",
2364 rec->flags & ~FTRACE_FL_MASK);
2365 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002366
2367 return 0;
2368}
2369
James Morris88e9d342009-09-22 16:43:43 -07002370static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002371 .start = t_start,
2372 .next = t_next,
2373 .stop = t_stop,
2374 .show = t_show,
2375};
2376
Ingo Molnare309b412008-05-12 21:20:51 +02002377static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002378ftrace_avail_open(struct inode *inode, struct file *file)
2379{
2380 struct ftrace_iterator *iter;
2381 int ret;
2382
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002383 if (unlikely(ftrace_disabled))
2384 return -ENODEV;
2385
Steven Rostedt5072c592008-05-12 21:20:43 +02002386 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2387 if (!iter)
2388 return -ENOMEM;
2389
2390 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002391
2392 ret = seq_open(file, &show_ftrace_seq_ops);
2393 if (!ret) {
2394 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002395
Steven Rostedt5072c592008-05-12 21:20:43 +02002396 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002397 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002398 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002399 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002400
2401 return ret;
2402}
2403
Steven Rostedt647bcd02011-05-03 14:39:21 -04002404static int
2405ftrace_enabled_open(struct inode *inode, struct file *file)
2406{
2407 struct ftrace_iterator *iter;
2408 int ret;
2409
2410 if (unlikely(ftrace_disabled))
2411 return -ENODEV;
2412
2413 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2414 if (!iter)
2415 return -ENOMEM;
2416
2417 iter->pg = ftrace_pages_start;
2418 iter->flags = FTRACE_ITER_ENABLED;
2419
2420 ret = seq_open(file, &show_ftrace_seq_ops);
2421 if (!ret) {
2422 struct seq_file *m = file->private_data;
2423
2424 m->private = iter;
2425 } else {
2426 kfree(iter);
2427 }
2428
2429 return ret;
2430}
2431
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002432static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002433{
Steven Rostedt52baf112009-02-14 01:15:39 -05002434 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002435 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002436 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002437}
2438
Ingo Molnare309b412008-05-12 21:20:51 +02002439static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002440ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002441 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002442{
2443 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002444 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002445 int ret = 0;
2446
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002447 if (unlikely(ftrace_disabled))
2448 return -ENODEV;
2449
Steven Rostedt5072c592008-05-12 21:20:43 +02002450 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2451 if (!iter)
2452 return -ENOMEM;
2453
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002454 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2455 kfree(iter);
2456 return -ENOMEM;
2457 }
2458
Steven Rostedtf45948e2011-05-02 12:29:25 -04002459 if (flag & FTRACE_ITER_NOTRACE)
2460 hash = ops->notrace_hash;
2461 else
2462 hash = ops->filter_hash;
2463
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002464 iter->ops = ops;
2465 iter->flags = flag;
2466
2467 if (file->f_mode & FMODE_WRITE) {
2468 mutex_lock(&ftrace_lock);
2469 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2470 mutex_unlock(&ftrace_lock);
2471
2472 if (!iter->hash) {
2473 trace_parser_put(&iter->parser);
2474 kfree(iter);
2475 return -ENOMEM;
2476 }
2477 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002478
Steven Rostedt41c52c02008-05-22 11:46:33 -04002479 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002480
Steven Rostedt5072c592008-05-12 21:20:43 +02002481 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002482 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002483 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002484
2485 if (file->f_mode & FMODE_READ) {
2486 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002487
2488 ret = seq_open(file, &show_ftrace_seq_ops);
2489 if (!ret) {
2490 struct seq_file *m = file->private_data;
2491 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002492 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002493 /* Failed */
2494 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002495 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002496 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002497 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002498 } else
2499 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002500 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002501
2502 return ret;
2503}
2504
Steven Rostedt41c52c02008-05-22 11:46:33 -04002505static int
2506ftrace_filter_open(struct inode *inode, struct file *file)
2507{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002508 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002509 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002510}
2511
2512static int
2513ftrace_notrace_open(struct inode *inode, struct file *file)
2514{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002515 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002516 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002517}
2518
Ingo Molnare309b412008-05-12 21:20:51 +02002519static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002520ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02002521{
2522 loff_t ret;
2523
2524 if (file->f_mode & FMODE_READ)
2525 ret = seq_lseek(file, offset, origin);
2526 else
2527 file->f_pos = ret = 1;
2528
2529 return ret;
2530}
2531
Steven Rostedt64e7c442009-02-13 17:08:48 -05002532static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002533{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002534 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002535 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002536
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002537 switch (type) {
2538 case MATCH_FULL:
2539 if (strcmp(str, regex) == 0)
2540 matched = 1;
2541 break;
2542 case MATCH_FRONT_ONLY:
2543 if (strncmp(str, regex, len) == 0)
2544 matched = 1;
2545 break;
2546 case MATCH_MIDDLE_ONLY:
2547 if (strstr(str, regex))
2548 matched = 1;
2549 break;
2550 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002551 slen = strlen(str);
2552 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002553 matched = 1;
2554 break;
2555 }
2556
2557 return matched;
2558}
2559
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002560static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002561enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002562{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002563 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002564 int ret = 0;
2565
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002566 entry = ftrace_lookup_ip(hash, rec->ip);
2567 if (not) {
2568 /* Do nothing if it doesn't exist */
2569 if (!entry)
2570 return 0;
2571
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002572 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002573 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002574 /* Do nothing if it exists */
2575 if (entry)
2576 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002577
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002578 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002579 }
2580 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002581}
2582
Steven Rostedt64e7c442009-02-13 17:08:48 -05002583static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002584ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2585 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002586{
2587 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002588 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002589
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002590 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2591
2592 if (mod) {
2593 /* module lookup requires matching the module */
2594 if (!modname || strcmp(modname, mod))
2595 return 0;
2596
2597 /* blank search means to match all funcs in the mod */
2598 if (!len)
2599 return 1;
2600 }
2601
Steven Rostedt64e7c442009-02-13 17:08:48 -05002602 return ftrace_match(str, regex, len, type);
2603}
2604
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002605static int
2606match_records(struct ftrace_hash *hash, char *buff,
2607 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002608{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002609 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002610 struct ftrace_page *pg;
2611 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002612 int type = MATCH_FULL;
2613 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002614 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002615 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002616
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002617 if (len) {
2618 type = filter_parse_regex(buff, len, &search, &not);
2619 search_len = strlen(search);
2620 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002621
Steven Rostedt52baf112009-02-14 01:15:39 -05002622 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002623
2624 if (unlikely(ftrace_disabled))
2625 goto out_unlock;
2626
Steven Rostedt265c8312009-02-13 12:43:56 -05002627 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002628 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002629 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002630 if (ret < 0) {
2631 found = ret;
2632 goto out_unlock;
2633 }
Li Zefan311d16d2009-12-08 11:15:11 +08002634 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002635 }
2636 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002637 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002638 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002639
2640 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002641}
2642
Steven Rostedt64e7c442009-02-13 17:08:48 -05002643static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002644ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002645{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002646 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002647}
2648
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002649static int
2650ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002651{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002652 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002653
Steven Rostedt64e7c442009-02-13 17:08:48 -05002654 /* blank or '*' mean the same */
2655 if (strcmp(buff, "*") == 0)
2656 buff[0] = 0;
2657
2658 /* handle the case of 'dont filter this module' */
2659 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2660 buff[0] = 0;
2661 not = 1;
2662 }
2663
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002664 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002665}
2666
Steven Rostedtf6180772009-02-14 00:40:25 -05002667/*
2668 * We register the module command as a template to show others how
2669 * to register the a command as well.
2670 */
2671
2672static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002673ftrace_mod_callback(struct ftrace_hash *hash,
2674 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002675{
2676 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002677 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002678
2679 /*
2680 * cmd == 'mod' because we only registered this func
2681 * for the 'mod' ftrace_func_command.
2682 * But if you register one func with multiple commands,
2683 * you can tell which command was used by the cmd
2684 * parameter.
2685 */
2686
2687 /* we must have a module name */
2688 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002689 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002690
2691 mod = strsep(&param, ":");
2692 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002693 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002694
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002695 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002696 if (!ret)
2697 ret = -EINVAL;
2698 if (ret < 0)
2699 return ret;
2700
2701 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002702}
2703
2704static struct ftrace_func_command ftrace_mod_cmd = {
2705 .name = "mod",
2706 .func = ftrace_mod_callback,
2707};
2708
2709static int __init ftrace_mod_cmd_init(void)
2710{
2711 return register_ftrace_command(&ftrace_mod_cmd);
2712}
2713device_initcall(ftrace_mod_cmd_init);
2714
Steven Rostedt59df055f2009-02-14 15:29:06 -05002715static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002716function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002717{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002718 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002719 struct hlist_head *hhd;
2720 struct hlist_node *n;
2721 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002722
2723 key = hash_long(ip, FTRACE_HASH_BITS);
2724
2725 hhd = &ftrace_func_hash[key];
2726
2727 if (hlist_empty(hhd))
2728 return;
2729
2730 /*
2731 * Disable preemption for these calls to prevent a RCU grace
2732 * period. This syncs the hash iteration and freeing of items
2733 * on the hash. rcu_read_lock is too dangerous here.
2734 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002735 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002736 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2737 if (entry->ip == ip)
2738 entry->ops->func(ip, parent_ip, &entry->data);
2739 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002740 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002741}
2742
Steven Rostedtb6887d72009-02-17 12:32:04 -05002743static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002744{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002745 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002746};
2747
Steven Rostedtb6887d72009-02-17 12:32:04 -05002748static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002749
Steven Rostedtb6887d72009-02-17 12:32:04 -05002750static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002751{
Steven Rostedtb8489142011-05-04 09:27:52 -04002752 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002753 int i;
2754
Steven Rostedtb6887d72009-02-17 12:32:04 -05002755 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002756 return;
2757
2758 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2759 struct hlist_head *hhd = &ftrace_func_hash[i];
2760 if (hhd->first)
2761 break;
2762 }
2763 /* Nothing registered? */
2764 if (i == FTRACE_FUNC_HASHSIZE)
2765 return;
2766
Steven Rostedtb8489142011-05-04 09:27:52 -04002767 ret = __register_ftrace_function(&trace_probe_ops);
2768 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002769 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002770
Steven Rostedtb6887d72009-02-17 12:32:04 -05002771 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002772}
2773
Steven Rostedtb6887d72009-02-17 12:32:04 -05002774static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002775{
Steven Rostedtb8489142011-05-04 09:27:52 -04002776 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002777 int i;
2778
Steven Rostedtb6887d72009-02-17 12:32:04 -05002779 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002780 return;
2781
2782 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2783 struct hlist_head *hhd = &ftrace_func_hash[i];
2784 if (hhd->first)
2785 return;
2786 }
2787
2788 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002789 ret = __unregister_ftrace_function(&trace_probe_ops);
2790 if (!ret)
2791 ftrace_shutdown(&trace_probe_ops, 0);
2792
Steven Rostedtb6887d72009-02-17 12:32:04 -05002793 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002794}
2795
2796
2797static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2798{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002799 struct ftrace_func_probe *entry =
2800 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002801
2802 if (entry->ops->free)
2803 entry->ops->free(&entry->data);
2804 kfree(entry);
2805}
2806
2807
2808int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002809register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002810 void *data)
2811{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002812 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002813 struct ftrace_page *pg;
2814 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002815 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002816 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002817 int count = 0;
2818 char *search;
2819
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002820 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002821 len = strlen(search);
2822
Steven Rostedtb6887d72009-02-17 12:32:04 -05002823 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002824 if (WARN_ON(not))
2825 return -EINVAL;
2826
2827 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002828
Steven Rostedt45a4a232011-04-21 23:16:46 -04002829 if (unlikely(ftrace_disabled))
2830 goto out_unlock;
2831
Steven Rostedt59df055f2009-02-14 15:29:06 -05002832 do_for_each_ftrace_rec(pg, rec) {
2833
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002834 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002835 continue;
2836
2837 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2838 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002839 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002840 if (!count)
2841 count = -ENOMEM;
2842 goto out_unlock;
2843 }
2844
2845 count++;
2846
2847 entry->data = data;
2848
2849 /*
2850 * The caller might want to do something special
2851 * for each function we find. We call the callback
2852 * to give the caller an opportunity to do so.
2853 */
2854 if (ops->callback) {
2855 if (ops->callback(rec->ip, &entry->data) < 0) {
2856 /* caller does not like this func */
2857 kfree(entry);
2858 continue;
2859 }
2860 }
2861
2862 entry->ops = ops;
2863 entry->ip = rec->ip;
2864
2865 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2866 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2867
2868 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002869 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002870
2871 out_unlock:
2872 mutex_unlock(&ftrace_lock);
2873
2874 return count;
2875}
2876
2877enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002878 PROBE_TEST_FUNC = 1,
2879 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002880};
2881
2882static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002883__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002884 void *data, int flags)
2885{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002886 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002887 struct hlist_node *n, *tmp;
2888 char str[KSYM_SYMBOL_LEN];
2889 int type = MATCH_FULL;
2890 int i, len = 0;
2891 char *search;
2892
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002893 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002894 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002895 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002896 int not;
2897
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002898 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002899 len = strlen(search);
2900
Steven Rostedtb6887d72009-02-17 12:32:04 -05002901 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002902 if (WARN_ON(not))
2903 return;
2904 }
2905
2906 mutex_lock(&ftrace_lock);
2907 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2908 struct hlist_head *hhd = &ftrace_func_hash[i];
2909
2910 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2911
2912 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002913 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002914 continue;
2915
Steven Rostedtb6887d72009-02-17 12:32:04 -05002916 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002917 continue;
2918
2919 /* do this last, since it is the most expensive */
2920 if (glob) {
2921 kallsyms_lookup(entry->ip, NULL, NULL,
2922 NULL, str);
2923 if (!ftrace_match(str, glob, len, type))
2924 continue;
2925 }
2926
2927 hlist_del(&entry->node);
2928 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2929 }
2930 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002931 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002932 mutex_unlock(&ftrace_lock);
2933}
2934
2935void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002936unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002937 void *data)
2938{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002939 __unregister_ftrace_function_probe(glob, ops, data,
2940 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002941}
2942
2943void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002944unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002945{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002946 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002947}
2948
Steven Rostedtb6887d72009-02-17 12:32:04 -05002949void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002950{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002951 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002952}
2953
Steven Rostedtf6180772009-02-14 00:40:25 -05002954static LIST_HEAD(ftrace_commands);
2955static DEFINE_MUTEX(ftrace_cmd_mutex);
2956
2957int register_ftrace_command(struct ftrace_func_command *cmd)
2958{
2959 struct ftrace_func_command *p;
2960 int ret = 0;
2961
2962 mutex_lock(&ftrace_cmd_mutex);
2963 list_for_each_entry(p, &ftrace_commands, list) {
2964 if (strcmp(cmd->name, p->name) == 0) {
2965 ret = -EBUSY;
2966 goto out_unlock;
2967 }
2968 }
2969 list_add(&cmd->list, &ftrace_commands);
2970 out_unlock:
2971 mutex_unlock(&ftrace_cmd_mutex);
2972
2973 return ret;
2974}
2975
2976int unregister_ftrace_command(struct ftrace_func_command *cmd)
2977{
2978 struct ftrace_func_command *p, *n;
2979 int ret = -ENODEV;
2980
2981 mutex_lock(&ftrace_cmd_mutex);
2982 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2983 if (strcmp(cmd->name, p->name) == 0) {
2984 ret = 0;
2985 list_del_init(&p->list);
2986 goto out_unlock;
2987 }
2988 }
2989 out_unlock:
2990 mutex_unlock(&ftrace_cmd_mutex);
2991
2992 return ret;
2993}
2994
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002995static int ftrace_process_regex(struct ftrace_hash *hash,
2996 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002997{
Steven Rostedtf6180772009-02-14 00:40:25 -05002998 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002999 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003000 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003001
3002 func = strsep(&next, ":");
3003
3004 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003005 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003006 if (!ret)
3007 ret = -EINVAL;
3008 if (ret < 0)
3009 return ret;
3010 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003011 }
3012
Steven Rostedtf6180772009-02-14 00:40:25 -05003013 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003014
3015 command = strsep(&next, ":");
3016
Steven Rostedtf6180772009-02-14 00:40:25 -05003017 mutex_lock(&ftrace_cmd_mutex);
3018 list_for_each_entry(p, &ftrace_commands, list) {
3019 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003020 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003021 goto out_unlock;
3022 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003023 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003024 out_unlock:
3025 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003026
Steven Rostedtf6180772009-02-14 00:40:25 -05003027 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003028}
3029
Ingo Molnare309b412008-05-12 21:20:51 +02003030static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003031ftrace_regex_write(struct file *file, const char __user *ubuf,
3032 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003033{
3034 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003035 struct trace_parser *parser;
3036 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003037
Li Zefan4ba79782009-09-22 13:52:20 +08003038 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003039 return 0;
3040
Steven Rostedt41c52c02008-05-22 11:46:33 -04003041 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003042
Steven Rostedt45a4a232011-04-21 23:16:46 -04003043 ret = -ENODEV;
3044 if (unlikely(ftrace_disabled))
3045 goto out_unlock;
3046
Steven Rostedt5072c592008-05-12 21:20:43 +02003047 if (file->f_mode & FMODE_READ) {
3048 struct seq_file *m = file->private_data;
3049 iter = m->private;
3050 } else
3051 iter = file->private_data;
3052
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003053 parser = &iter->parser;
3054 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003055
Li Zefan4ba79782009-09-22 13:52:20 +08003056 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003057 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003058 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003059 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003060 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003061 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003062 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003063 }
3064
Steven Rostedt5072c592008-05-12 21:20:43 +02003065 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003066out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003067 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003068
Steven Rostedt5072c592008-05-12 21:20:43 +02003069 return ret;
3070}
3071
Steven Rostedt41c52c02008-05-22 11:46:33 -04003072static ssize_t
3073ftrace_filter_write(struct file *file, const char __user *ubuf,
3074 size_t cnt, loff_t *ppos)
3075{
3076 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3077}
3078
3079static ssize_t
3080ftrace_notrace_write(struct file *file, const char __user *ubuf,
3081 size_t cnt, loff_t *ppos)
3082{
3083 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3084}
3085
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003086static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003087ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3088 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003089{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003090 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003091 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003092 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003093
Steven Rostedt936e0742011-05-05 22:54:01 -04003094 /* All global ops uses the global ops filters */
3095 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3096 ops = &global_ops;
3097
Steven Rostedt41c52c02008-05-22 11:46:33 -04003098 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003099 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003100
Steven Rostedtf45948e2011-05-02 12:29:25 -04003101 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003102 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003103 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003104 orig_hash = &ops->notrace_hash;
3105
3106 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3107 if (!hash)
3108 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003109
Steven Rostedt41c52c02008-05-22 11:46:33 -04003110 mutex_lock(&ftrace_regex_lock);
3111 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003112 ftrace_filter_reset(hash);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003113 if (buf)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003114 ftrace_match_records(hash, buf, len);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003115
3116 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003117 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003118 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3119 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003120 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003121
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003122 mutex_unlock(&ftrace_lock);
3123
Steven Rostedt41c52c02008-05-22 11:46:33 -04003124 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003125
3126 free_ftrace_hash(hash);
3127 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003128}
3129
Steven Rostedt77a2b372008-05-12 21:20:45 +02003130/**
3131 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003132 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003133 * @buf - the string that holds the function filter text.
3134 * @len - the length of the string.
3135 * @reset - non zero to reset all filters before applying this filter.
3136 *
3137 * Filters denote which functions should be enabled when tracing is enabled.
3138 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3139 */
Steven Rostedt936e0742011-05-05 22:54:01 -04003140void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3141 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003142{
Steven Rostedt936e0742011-05-05 22:54:01 -04003143 ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003144}
Steven Rostedt936e0742011-05-05 22:54:01 -04003145EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003146
Steven Rostedt41c52c02008-05-22 11:46:33 -04003147/**
3148 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003149 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003150 * @buf - the string that holds the function notrace text.
3151 * @len - the length of the string.
3152 * @reset - non zero to reset all filters before applying this filter.
3153 *
3154 * Notrace Filters denote which functions should not be enabled when tracing
3155 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3156 * for tracing.
3157 */
Steven Rostedt936e0742011-05-05 22:54:01 -04003158void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3159 int len, int reset)
3160{
3161 ftrace_set_regex(ops, buf, len, reset, 0);
3162}
3163EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3164/**
3165 * ftrace_set_filter - set a function to filter on in ftrace
3166 * @ops - the ops to set the filter with
3167 * @buf - the string that holds the function filter text.
3168 * @len - the length of the string.
3169 * @reset - non zero to reset all filters before applying this filter.
3170 *
3171 * Filters denote which functions should be enabled when tracing is enabled.
3172 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3173 */
3174void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3175{
3176 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3177}
3178EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3179
3180/**
3181 * ftrace_set_notrace - set a function to not trace in ftrace
3182 * @ops - the ops to set the notrace filter with
3183 * @buf - the string that holds the function notrace text.
3184 * @len - the length of the string.
3185 * @reset - non zero to reset all filters before applying this filter.
3186 *
3187 * Notrace Filters denote which functions should not be enabled when tracing
3188 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3189 * for tracing.
3190 */
3191void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003192{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003193 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003194}
Steven Rostedt936e0742011-05-05 22:54:01 -04003195EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003196
Steven Rostedt2af15d62009-05-28 13:37:24 -04003197/*
3198 * command line interface to allow users to set filters on boot up.
3199 */
3200#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3201static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3202static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3203
3204static int __init set_ftrace_notrace(char *str)
3205{
3206 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3207 return 1;
3208}
3209__setup("ftrace_notrace=", set_ftrace_notrace);
3210
3211static int __init set_ftrace_filter(char *str)
3212{
3213 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3214 return 1;
3215}
3216__setup("ftrace_filter=", set_ftrace_filter);
3217
Stefan Assmann369bc182009-10-12 22:17:21 +02003218#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003219static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003220static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3221
Stefan Assmann369bc182009-10-12 22:17:21 +02003222static int __init set_graph_function(char *str)
3223{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003224 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003225 return 1;
3226}
3227__setup("ftrace_graph_filter=", set_graph_function);
3228
3229static void __init set_ftrace_early_graph(char *buf)
3230{
3231 int ret;
3232 char *func;
3233
3234 while (buf) {
3235 func = strsep(&buf, ",");
3236 /* we allow only one expression at a time */
3237 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3238 func);
3239 if (ret)
3240 printk(KERN_DEBUG "ftrace: function %s not "
3241 "traceable\n", func);
3242 }
3243}
3244#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3245
Steven Rostedtf45948e2011-05-02 12:29:25 -04003246static void __init
3247set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003248{
3249 char *func;
3250
3251 while (buf) {
3252 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003253 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003254 }
3255}
3256
3257static void __init set_ftrace_early_filters(void)
3258{
3259 if (ftrace_filter_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04003260 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003261 if (ftrace_notrace_buf[0])
Steven Rostedtf45948e2011-05-02 12:29:25 -04003262 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003263#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3264 if (ftrace_graph_buf[0])
3265 set_ftrace_early_graph(ftrace_graph_buf);
3266#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003267}
3268
Ingo Molnare309b412008-05-12 21:20:51 +02003269static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003270ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003271{
3272 struct seq_file *m = (struct seq_file *)file->private_data;
3273 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003274 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003275 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003276 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003277 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003278
Steven Rostedt41c52c02008-05-22 11:46:33 -04003279 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003280 if (file->f_mode & FMODE_READ) {
3281 iter = m->private;
3282
3283 seq_release(inode, file);
3284 } else
3285 iter = file->private_data;
3286
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003287 parser = &iter->parser;
3288 if (trace_parser_loaded(parser)) {
3289 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003290 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003291 }
3292
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003293 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003294
Steven Rostedt058e2972011-04-29 22:35:33 -04003295 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003296 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3297
3298 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003299 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003300 else
3301 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003302
Steven Rostedt058e2972011-04-29 22:35:33 -04003303 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003304 ret = ftrace_hash_move(iter->ops, filter_hash,
3305 orig_hash, iter->hash);
3306 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3307 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003308 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003309
Steven Rostedt058e2972011-04-29 22:35:33 -04003310 mutex_unlock(&ftrace_lock);
3311 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003312 free_ftrace_hash(iter->hash);
3313 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003314
Steven Rostedt41c52c02008-05-22 11:46:33 -04003315 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003316 return 0;
3317}
3318
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003319static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003320 .open = ftrace_avail_open,
3321 .read = seq_read,
3322 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003323 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003324};
3325
Steven Rostedt647bcd02011-05-03 14:39:21 -04003326static const struct file_operations ftrace_enabled_fops = {
3327 .open = ftrace_enabled_open,
3328 .read = seq_read,
3329 .llseek = seq_lseek,
3330 .release = seq_release_private,
3331};
3332
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003333static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003334 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003335 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003336 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003337 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003338 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003339};
3340
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003341static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003342 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003343 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003344 .write = ftrace_notrace_write,
3345 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003346 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003347};
3348
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003349#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3350
3351static DEFINE_MUTEX(graph_lock);
3352
3353int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003354int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003355unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3356
3357static void *
Li Zefan85951842009-06-24 09:54:00 +08003358__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003359{
Li Zefan85951842009-06-24 09:54:00 +08003360 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003361 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003362 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003363}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003364
Li Zefan85951842009-06-24 09:54:00 +08003365static void *
3366g_next(struct seq_file *m, void *v, loff_t *pos)
3367{
3368 (*pos)++;
3369 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003370}
3371
3372static void *g_start(struct seq_file *m, loff_t *pos)
3373{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003374 mutex_lock(&graph_lock);
3375
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003376 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003377 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003378 return (void *)1;
3379
Li Zefan85951842009-06-24 09:54:00 +08003380 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003381}
3382
3383static void g_stop(struct seq_file *m, void *p)
3384{
3385 mutex_unlock(&graph_lock);
3386}
3387
3388static int g_show(struct seq_file *m, void *v)
3389{
3390 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003391
3392 if (!ptr)
3393 return 0;
3394
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003395 if (ptr == (unsigned long *)1) {
3396 seq_printf(m, "#### all functions enabled ####\n");
3397 return 0;
3398 }
3399
Steven Rostedtb375a112009-09-17 00:05:58 -04003400 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003401
3402 return 0;
3403}
3404
James Morris88e9d342009-09-22 16:43:43 -07003405static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003406 .start = g_start,
3407 .next = g_next,
3408 .stop = g_stop,
3409 .show = g_show,
3410};
3411
3412static int
3413ftrace_graph_open(struct inode *inode, struct file *file)
3414{
3415 int ret = 0;
3416
3417 if (unlikely(ftrace_disabled))
3418 return -ENODEV;
3419
3420 mutex_lock(&graph_lock);
3421 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003422 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003423 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003424 ftrace_graph_count = 0;
3425 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3426 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003427 mutex_unlock(&graph_lock);
3428
Li Zefana4ec5e02009-09-18 14:06:28 +08003429 if (file->f_mode & FMODE_READ)
3430 ret = seq_open(file, &ftrace_graph_seq_ops);
3431
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003432 return ret;
3433}
3434
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003435static int
Li Zefan87827112009-07-23 11:29:11 +08003436ftrace_graph_release(struct inode *inode, struct file *file)
3437{
3438 if (file->f_mode & FMODE_READ)
3439 seq_release(inode, file);
3440 return 0;
3441}
3442
3443static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003444ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003445{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003446 struct dyn_ftrace *rec;
3447 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003448 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003449 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003450 int type, not;
3451 char *search;
3452 bool exists;
3453 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003454
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003455 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003456 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003457 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3458 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003459
3460 search_len = strlen(search);
3461
Steven Rostedt52baf112009-02-14 01:15:39 -05003462 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003463
3464 if (unlikely(ftrace_disabled)) {
3465 mutex_unlock(&ftrace_lock);
3466 return -ENODEV;
3467 }
3468
Steven Rostedt265c8312009-02-13 12:43:56 -05003469 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003470
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003471 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003472 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003473 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003474 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003475 if (array[i] == rec->ip) {
3476 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003477 break;
3478 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003479 }
3480
3481 if (!not) {
3482 fail = 0;
3483 if (!exists) {
3484 array[(*idx)++] = rec->ip;
3485 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3486 goto out;
3487 }
3488 } else {
3489 if (exists) {
3490 array[i] = array[--(*idx)];
3491 array[*idx] = 0;
3492 fail = 0;
3493 }
3494 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003495 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003496 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003497out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003498 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003499
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003500 if (fail)
3501 return -EINVAL;
3502
3503 ftrace_graph_filter_enabled = 1;
3504 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003505}
3506
3507static ssize_t
3508ftrace_graph_write(struct file *file, const char __user *ubuf,
3509 size_t cnt, loff_t *ppos)
3510{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003511 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003512 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003513
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003514 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003515 return 0;
3516
3517 mutex_lock(&graph_lock);
3518
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003519 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3520 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003521 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003522 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003523
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003524 read = trace_get_user(&parser, ubuf, cnt, ppos);
3525
Li Zefan4ba79782009-09-22 13:52:20 +08003526 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003527 parser.buffer[parser.idx] = 0;
3528
3529 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003530 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003531 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003532 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003533 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003534 }
3535
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003536 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003537
3538out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003539 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003540out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003541 mutex_unlock(&graph_lock);
3542
3543 return ret;
3544}
3545
3546static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003547 .open = ftrace_graph_open,
3548 .read = seq_read,
3549 .write = ftrace_graph_write,
3550 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003551 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003552};
3553#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3554
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003555static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003556{
Steven Rostedt5072c592008-05-12 21:20:43 +02003557
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003558 trace_create_file("available_filter_functions", 0444,
3559 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003560
Steven Rostedt647bcd02011-05-03 14:39:21 -04003561 trace_create_file("enabled_functions", 0444,
3562 d_tracer, NULL, &ftrace_enabled_fops);
3563
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003564 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3565 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003566
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003567 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003568 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003569
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003570#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003571 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003572 NULL,
3573 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003574#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3575
Steven Rostedt5072c592008-05-12 21:20:43 +02003576 return 0;
3577}
3578
Steven Rostedt68950612011-12-16 17:06:45 -05003579static void ftrace_swap_recs(void *a, void *b, int size)
3580{
3581 struct dyn_ftrace *reca = a;
3582 struct dyn_ftrace *recb = b;
3583 struct dyn_ftrace t;
3584
3585 t = *reca;
3586 *reca = *recb;
3587 *recb = t;
3588}
3589
3590static int ftrace_cmp_recs(const void *a, const void *b)
3591{
3592 const struct dyn_ftrace *reca = a;
3593 const struct dyn_ftrace *recb = b;
3594
3595 if (reca->ip > recb->ip)
3596 return 1;
3597 if (reca->ip < recb->ip)
3598 return -1;
3599 return 0;
3600}
3601
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003602static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003603 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003604 unsigned long *end)
3605{
Steven Rostedta7900872011-12-16 16:23:44 -05003606 struct ftrace_page *pg;
3607 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003608 unsigned long *p;
3609 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003610 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003611 int ret = -ENOMEM;
3612
3613 count = end - start;
3614
3615 if (!count)
3616 return 0;
3617
3618 pg = ftrace_allocate_pages(count);
3619 if (!pg)
3620 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003621
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003622 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003623
Steven Rostedt320823092011-12-16 14:42:37 -05003624 /*
3625 * Core and each module needs their own pages, as
3626 * modules will free them when they are removed.
3627 * Force a new page to be allocated for modules.
3628 */
Steven Rostedta7900872011-12-16 16:23:44 -05003629 if (!mod) {
3630 WARN_ON(ftrace_pages || ftrace_pages_start);
3631 /* First initialization */
3632 ftrace_pages = ftrace_pages_start = pg;
3633 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05003634 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003635 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05003636
Steven Rostedta7900872011-12-16 16:23:44 -05003637 if (WARN_ON(ftrace_pages->next)) {
3638 /* Hmm, we have free pages? */
3639 while (ftrace_pages->next)
3640 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05003641 }
Steven Rostedta7900872011-12-16 16:23:44 -05003642
3643 ftrace_pages->next = pg;
3644 ftrace_pages = pg;
Steven Rostedt320823092011-12-16 14:42:37 -05003645 }
3646
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003647 p = start;
3648 while (p < end) {
3649 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003650 /*
3651 * Some architecture linkers will pad between
3652 * the different mcount_loc sections of different
3653 * object files to satisfy alignments.
3654 * Skip any NULL pointers.
3655 */
3656 if (!addr)
3657 continue;
Steven Rostedta7900872011-12-16 16:23:44 -05003658 if (!ftrace_record_ip(addr))
3659 break;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003660 }
3661
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003662 /* These new locations need to be initialized */
3663 ftrace_new_pgs = pg;
3664
Steven Rostedt68950612011-12-16 17:06:45 -05003665 /* Make each individual set of pages sorted by ips */
3666 for (; pg; pg = pg->next)
3667 sort(pg->records, pg->index, sizeof(struct dyn_ftrace),
3668 ftrace_cmp_recs, ftrace_swap_recs);
3669
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003670 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003671 * We only need to disable interrupts on start up
3672 * because we are modifying code that an interrupt
3673 * may execute, and the modification is not atomic.
3674 * But for modules, nothing runs the code we modify
3675 * until we are finished with it, and there's no
3676 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003677 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003678 if (!mod)
3679 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003680 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003681 if (!mod)
3682 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003683 ret = 0;
3684 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003685 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003686
Steven Rostedta7900872011-12-16 16:23:44 -05003687 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003688}
3689
Steven Rostedt93eb6772009-04-15 13:24:06 -04003690#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05003691
3692#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3693
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003694void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003695{
3696 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05003697 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003698 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003699 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003700
Steven Rostedt93eb6772009-04-15 13:24:06 -04003701 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003702
3703 if (ftrace_disabled)
3704 goto out_unlock;
3705
Steven Rostedt320823092011-12-16 14:42:37 -05003706 /*
3707 * Each module has its own ftrace_pages, remove
3708 * them from the list.
3709 */
3710 last_pg = &ftrace_pages_start;
3711 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3712 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003713 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003714 /*
Steven Rostedt320823092011-12-16 14:42:37 -05003715 * As core pages are first, the first
3716 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003717 */
Steven Rostedt320823092011-12-16 14:42:37 -05003718 if (WARN_ON(pg == ftrace_pages_start))
3719 goto out_unlock;
3720
3721 /* Check if we are deleting the last page */
3722 if (pg == ftrace_pages)
3723 ftrace_pages = next_to_ftrace_page(last_pg);
3724
3725 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003726 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3727 free_pages((unsigned long)pg->records, order);
3728 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05003729 } else
3730 last_pg = &pg->next;
3731 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003732 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003733 mutex_unlock(&ftrace_lock);
3734}
3735
3736static void ftrace_init_module(struct module *mod,
3737 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003738{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003739 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003740 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003741 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003742}
3743
Steven Rostedt93eb6772009-04-15 13:24:06 -04003744static int ftrace_module_notify(struct notifier_block *self,
3745 unsigned long val, void *data)
3746{
3747 struct module *mod = data;
3748
3749 switch (val) {
3750 case MODULE_STATE_COMING:
3751 ftrace_init_module(mod, mod->ftrace_callsites,
3752 mod->ftrace_callsites +
3753 mod->num_ftrace_callsites);
3754 break;
3755 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003756 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003757 break;
3758 }
3759
3760 return 0;
3761}
3762#else
3763static int ftrace_module_notify(struct notifier_block *self,
3764 unsigned long val, void *data)
3765{
3766 return 0;
3767}
3768#endif /* CONFIG_MODULES */
3769
3770struct notifier_block ftrace_module_nb = {
3771 .notifier_call = ftrace_module_notify,
3772 .priority = 0,
3773};
3774
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003775extern unsigned long __start_mcount_loc[];
3776extern unsigned long __stop_mcount_loc[];
3777
3778void __init ftrace_init(void)
3779{
3780 unsigned long count, addr, flags;
3781 int ret;
3782
3783 /* Keep the ftrace pointer to the stub */
3784 addr = (unsigned long)ftrace_stub;
3785
3786 local_irq_save(flags);
3787 ftrace_dyn_arch_init(&addr);
3788 local_irq_restore(flags);
3789
3790 /* ftrace_dyn_arch_init places the return code in addr */
3791 if (addr)
3792 goto failed;
3793
3794 count = __stop_mcount_loc - __start_mcount_loc;
3795
3796 ret = ftrace_dyn_table_alloc(count);
3797 if (ret)
3798 goto failed;
3799
3800 last_ftrace_enabled = ftrace_enabled = 1;
3801
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003802 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003803 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003804 __stop_mcount_loc);
3805
Steven Rostedt93eb6772009-04-15 13:24:06 -04003806 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003807 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003808 pr_warning("Failed to register trace ftrace module notifier\n");
3809
Steven Rostedt2af15d62009-05-28 13:37:24 -04003810 set_ftrace_early_filters();
3811
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003812 return;
3813 failed:
3814 ftrace_disabled = 1;
3815}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003816
Steven Rostedt3d083392008-05-12 21:20:42 +02003817#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003818
Steven Rostedt2b499382011-05-03 22:49:52 -04003819static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003820 .func = ftrace_stub,
3821};
3822
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003823static int __init ftrace_nodyn_init(void)
3824{
3825 ftrace_enabled = 1;
3826 return 0;
3827}
3828device_initcall(ftrace_nodyn_init);
3829
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003830static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3831static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003832/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003833# define ftrace_startup(ops, command) \
3834 ({ \
3835 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3836 0; \
3837 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04003838# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003839# define ftrace_startup_sysctl() do { } while (0)
3840# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003841
3842static inline int
3843ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3844{
3845 return 1;
3846}
3847
Steven Rostedt3d083392008-05-12 21:20:42 +02003848#endif /* CONFIG_DYNAMIC_FTRACE */
3849
Steven Rostedtb8489142011-05-04 09:27:52 -04003850static void
3851ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3852{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003853 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04003854
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003855 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3856 return;
3857
3858 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003859 /*
3860 * Some of the ops may be dynamically allocated,
3861 * they must be freed after a synchronize_sched().
3862 */
3863 preempt_disable_notrace();
3864 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04003865 while (op != &ftrace_list_end) {
3866 if (ftrace_ops_test(op, ip))
3867 op->func(ip, parent_ip);
3868 op = rcu_dereference_raw(op->next);
3869 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003870 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003871 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04003872}
3873
Steven Rostedte32d8952008-12-04 00:26:41 -05003874static void clear_ftrace_swapper(void)
3875{
3876 struct task_struct *p;
3877 int cpu;
3878
3879 get_online_cpus();
3880 for_each_online_cpu(cpu) {
3881 p = idle_task(cpu);
3882 clear_tsk_trace_trace(p);
3883 }
3884 put_online_cpus();
3885}
3886
3887static void set_ftrace_swapper(void)
3888{
3889 struct task_struct *p;
3890 int cpu;
3891
3892 get_online_cpus();
3893 for_each_online_cpu(cpu) {
3894 p = idle_task(cpu);
3895 set_tsk_trace_trace(p);
3896 }
3897 put_online_cpus();
3898}
3899
3900static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003901{
3902 struct task_struct *p;
3903
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003904 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05003905 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05003906 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05003907 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003908 rcu_read_unlock();
3909
Steven Rostedte32d8952008-12-04 00:26:41 -05003910 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003911}
3912
Steven Rostedte32d8952008-12-04 00:26:41 -05003913static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05003914{
3915 struct task_struct *p;
3916
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003917 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003918 do_each_pid_task(pid, PIDTYPE_PID, p) {
3919 set_tsk_trace_trace(p);
3920 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01003921 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05003922}
3923
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003924static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003925{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003926 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05003927 clear_ftrace_swapper();
3928 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003929 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05003930}
3931
3932static void set_ftrace_pid_task(struct pid *pid)
3933{
3934 if (pid == ftrace_swapper_pid)
3935 set_ftrace_swapper();
3936 else
3937 set_ftrace_pid(pid);
3938}
3939
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003940static int ftrace_pid_add(int p)
3941{
3942 struct pid *pid;
3943 struct ftrace_pid *fpid;
3944 int ret = -EINVAL;
3945
3946 mutex_lock(&ftrace_lock);
3947
3948 if (!p)
3949 pid = ftrace_swapper_pid;
3950 else
3951 pid = find_get_pid(p);
3952
3953 if (!pid)
3954 goto out;
3955
3956 ret = 0;
3957
3958 list_for_each_entry(fpid, &ftrace_pids, list)
3959 if (fpid->pid == pid)
3960 goto out_put;
3961
3962 ret = -ENOMEM;
3963
3964 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3965 if (!fpid)
3966 goto out_put;
3967
3968 list_add(&fpid->list, &ftrace_pids);
3969 fpid->pid = pid;
3970
3971 set_ftrace_pid_task(pid);
3972
3973 ftrace_update_pid_func();
3974 ftrace_startup_enable(0);
3975
3976 mutex_unlock(&ftrace_lock);
3977 return 0;
3978
3979out_put:
3980 if (pid != ftrace_swapper_pid)
3981 put_pid(pid);
3982
3983out:
3984 mutex_unlock(&ftrace_lock);
3985 return ret;
3986}
3987
3988static void ftrace_pid_reset(void)
3989{
3990 struct ftrace_pid *fpid, *safe;
3991
3992 mutex_lock(&ftrace_lock);
3993 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3994 struct pid *pid = fpid->pid;
3995
3996 clear_ftrace_pid_task(pid);
3997
3998 list_del(&fpid->list);
3999 kfree(fpid);
4000 }
4001
4002 ftrace_update_pid_func();
4003 ftrace_startup_enable(0);
4004
4005 mutex_unlock(&ftrace_lock);
4006}
4007
4008static void *fpid_start(struct seq_file *m, loff_t *pos)
4009{
4010 mutex_lock(&ftrace_lock);
4011
4012 if (list_empty(&ftrace_pids) && (!*pos))
4013 return (void *) 1;
4014
4015 return seq_list_start(&ftrace_pids, *pos);
4016}
4017
4018static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4019{
4020 if (v == (void *)1)
4021 return NULL;
4022
4023 return seq_list_next(v, &ftrace_pids, pos);
4024}
4025
4026static void fpid_stop(struct seq_file *m, void *p)
4027{
4028 mutex_unlock(&ftrace_lock);
4029}
4030
4031static int fpid_show(struct seq_file *m, void *v)
4032{
4033 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4034
4035 if (v == (void *)1) {
4036 seq_printf(m, "no pid\n");
4037 return 0;
4038 }
4039
4040 if (fpid->pid == ftrace_swapper_pid)
4041 seq_printf(m, "swapper tasks\n");
4042 else
4043 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4044
4045 return 0;
4046}
4047
4048static const struct seq_operations ftrace_pid_sops = {
4049 .start = fpid_start,
4050 .next = fpid_next,
4051 .stop = fpid_stop,
4052 .show = fpid_show,
4053};
4054
4055static int
4056ftrace_pid_open(struct inode *inode, struct file *file)
4057{
4058 int ret = 0;
4059
4060 if ((file->f_mode & FMODE_WRITE) &&
4061 (file->f_flags & O_TRUNC))
4062 ftrace_pid_reset();
4063
4064 if (file->f_mode & FMODE_READ)
4065 ret = seq_open(file, &ftrace_pid_sops);
4066
4067 return ret;
4068}
4069
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004070static ssize_t
4071ftrace_pid_write(struct file *filp, const char __user *ubuf,
4072 size_t cnt, loff_t *ppos)
4073{
Ingo Molnar457dc922009-11-23 11:03:28 +01004074 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004075 long val;
4076 int ret;
4077
4078 if (cnt >= sizeof(buf))
4079 return -EINVAL;
4080
4081 if (copy_from_user(&buf, ubuf, cnt))
4082 return -EFAULT;
4083
4084 buf[cnt] = 0;
4085
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004086 /*
4087 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4088 * to clean the filter quietly.
4089 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004090 tmp = strstrip(buf);
4091 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004092 return 1;
4093
Ingo Molnar457dc922009-11-23 11:03:28 +01004094 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004095 if (ret < 0)
4096 return ret;
4097
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004098 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004099
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004100 return ret ? ret : cnt;
4101}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004102
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004103static int
4104ftrace_pid_release(struct inode *inode, struct file *file)
4105{
4106 if (file->f_mode & FMODE_READ)
4107 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004108
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004109 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004110}
4111
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004112static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004113 .open = ftrace_pid_open,
4114 .write = ftrace_pid_write,
4115 .read = seq_read,
4116 .llseek = seq_lseek,
4117 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004118};
4119
4120static __init int ftrace_init_debugfs(void)
4121{
4122 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004123
4124 d_tracer = tracing_init_dentry();
4125 if (!d_tracer)
4126 return 0;
4127
4128 ftrace_init_dyn_debugfs(d_tracer);
4129
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004130 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4131 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004132
4133 ftrace_profile_debugfs(d_tracer);
4134
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004135 return 0;
4136}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004137fs_initcall(ftrace_init_debugfs);
4138
Steven Rostedt3d083392008-05-12 21:20:42 +02004139/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004140 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004141 *
4142 * This function should be used by panic code. It stops ftrace
4143 * but in a not so nice way. If you need to simply kill ftrace
4144 * from a non-atomic section, use ftrace_kill.
4145 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004146void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004147{
4148 ftrace_disabled = 1;
4149 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004150 clear_ftrace_function();
4151}
4152
4153/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004154 * Test if ftrace is dead or not.
4155 */
4156int ftrace_is_dead(void)
4157{
4158 return ftrace_disabled;
4159}
4160
4161/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004162 * register_ftrace_function - register a function for profiling
4163 * @ops - ops structure that holds the function for profiling.
4164 *
4165 * Register a function to be called by all functions in the
4166 * kernel.
4167 *
4168 * Note: @ops->func and all the functions it calls must be labeled
4169 * with "notrace", otherwise it will go into a
4170 * recursive loop.
4171 */
4172int register_ftrace_function(struct ftrace_ops *ops)
4173{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004174 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004175
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004176 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004177
Steven Rostedt45a4a232011-04-21 23:16:46 -04004178 if (unlikely(ftrace_disabled))
4179 goto out_unlock;
4180
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004181 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004182 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004183 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004184
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004185
Steven Rostedt45a4a232011-04-21 23:16:46 -04004186 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004187 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004188 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004189}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004190EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004191
4192/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004193 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004194 * @ops - ops structure that holds the function to unregister
4195 *
4196 * Unregister a function that was added to be called by ftrace profiling.
4197 */
4198int unregister_ftrace_function(struct ftrace_ops *ops)
4199{
4200 int ret;
4201
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004202 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004203 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004204 if (!ret)
4205 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004206 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004207
4208 return ret;
4209}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004210EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004211
Ingo Molnare309b412008-05-12 21:20:51 +02004212int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004213ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004214 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004215 loff_t *ppos)
4216{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004217 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004218
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004219 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004220
Steven Rostedt45a4a232011-04-21 23:16:46 -04004221 if (unlikely(ftrace_disabled))
4222 goto out;
4223
4224 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004225
Li Zefana32c7762009-06-26 16:55:51 +08004226 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004227 goto out;
4228
Li Zefana32c7762009-06-26 16:55:51 +08004229 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004230
4231 if (ftrace_enabled) {
4232
4233 ftrace_startup_sysctl();
4234
4235 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004236 if (ftrace_ops_list != &ftrace_list_end) {
4237 if (ftrace_ops_list->next == &ftrace_list_end)
4238 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004239 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004240 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004241 }
4242
4243 } else {
4244 /* stopping ftrace calls (just send to ftrace_stub) */
4245 ftrace_trace_function = ftrace_stub;
4246
4247 ftrace_shutdown_sysctl();
4248 }
4249
4250 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004251 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004252 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004253}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004254
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004255#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004256
Steven Rostedt597af812009-04-03 15:24:12 -04004257static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004258static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004259
Steven Rostedte49dc192008-12-02 23:50:05 -05004260int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4261{
4262 return 0;
4263}
4264
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004265/* The callbacks that hook a function */
4266trace_func_graph_ret_t ftrace_graph_return =
4267 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004268trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004269
4270/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4271static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4272{
4273 int i;
4274 int ret = 0;
4275 unsigned long flags;
4276 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4277 struct task_struct *g, *t;
4278
4279 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4280 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4281 * sizeof(struct ftrace_ret_stack),
4282 GFP_KERNEL);
4283 if (!ret_stack_list[i]) {
4284 start = 0;
4285 end = i;
4286 ret = -ENOMEM;
4287 goto free;
4288 }
4289 }
4290
4291 read_lock_irqsave(&tasklist_lock, flags);
4292 do_each_thread(g, t) {
4293 if (start == end) {
4294 ret = -EAGAIN;
4295 goto unlock;
4296 }
4297
4298 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004299 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004300 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004301 t->curr_ret_stack = -1;
4302 /* Make sure the tasks see the -1 first: */
4303 smp_wmb();
4304 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004305 }
4306 } while_each_thread(g, t);
4307
4308unlock:
4309 read_unlock_irqrestore(&tasklist_lock, flags);
4310free:
4311 for (i = start; i < end; i++)
4312 kfree(ret_stack_list[i]);
4313 return ret;
4314}
4315
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004316static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004317ftrace_graph_probe_sched_switch(void *ignore,
4318 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004319{
4320 unsigned long long timestamp;
4321 int index;
4322
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004323 /*
4324 * Does the user want to count the time a function was asleep.
4325 * If so, do not update the time stamps.
4326 */
4327 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4328 return;
4329
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004330 timestamp = trace_clock_local();
4331
4332 prev->ftrace_timestamp = timestamp;
4333
4334 /* only process tasks that we timestamped */
4335 if (!next->ftrace_timestamp)
4336 return;
4337
4338 /*
4339 * Update all the counters in next to make up for the
4340 * time next was sleeping.
4341 */
4342 timestamp -= next->ftrace_timestamp;
4343
4344 for (index = next->curr_ret_stack; index >= 0; index--)
4345 next->ret_stack[index].calltime += timestamp;
4346}
4347
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004348/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004349static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004350{
4351 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004352 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004353
4354 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4355 sizeof(struct ftrace_ret_stack *),
4356 GFP_KERNEL);
4357
4358 if (!ret_stack_list)
4359 return -ENOMEM;
4360
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004361 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004362 for_each_online_cpu(cpu) {
4363 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004364 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004365 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004366
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004367 do {
4368 ret = alloc_retstack_tasklist(ret_stack_list);
4369 } while (ret == -EAGAIN);
4370
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004371 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004372 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004373 if (ret)
4374 pr_info("ftrace_graph: Couldn't activate tracepoint"
4375 " probe to kernel_sched_switch\n");
4376 }
4377
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004378 kfree(ret_stack_list);
4379 return ret;
4380}
4381
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004382/*
4383 * Hibernation protection.
4384 * The state of the current task is too much unstable during
4385 * suspend/restore to disk. We want to protect against that.
4386 */
4387static int
4388ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4389 void *unused)
4390{
4391 switch (state) {
4392 case PM_HIBERNATION_PREPARE:
4393 pause_graph_tracing();
4394 break;
4395
4396 case PM_POST_HIBERNATION:
4397 unpause_graph_tracing();
4398 break;
4399 }
4400 return NOTIFY_DONE;
4401}
4402
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004403int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4404 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004405{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004406 int ret = 0;
4407
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004408 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004409
Steven Rostedt05ce5812009-03-24 00:18:31 -04004410 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004411 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004412 ret = -EBUSY;
4413 goto out;
4414 }
4415
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004416 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4417 register_pm_notifier(&ftrace_suspend_notifier);
4418
Steven Rostedt597af812009-04-03 15:24:12 -04004419 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004420 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004421 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004422 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004423 goto out;
4424 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004425
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004426 ftrace_graph_return = retfunc;
4427 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004428
Steven Rostedta1cd6172011-05-23 15:24:25 -04004429 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004430
4431out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004432 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004433 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004434}
4435
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004436void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004437{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004438 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004439
Steven Rostedt597af812009-04-03 15:24:12 -04004440 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004441 goto out;
4442
Steven Rostedt597af812009-04-03 15:24:12 -04004443 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004444 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004445 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004446 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004447 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004448 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004449
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004450 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004451 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004452}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004453
Steven Rostedt868baf02011-02-10 21:26:13 -05004454static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4455
4456static void
4457graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4458{
4459 atomic_set(&t->tracing_graph_pause, 0);
4460 atomic_set(&t->trace_overrun, 0);
4461 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004462 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004463 smp_wmb();
4464 t->ret_stack = ret_stack;
4465}
4466
4467/*
4468 * Allocate a return stack for the idle task. May be the first
4469 * time through, or it may be done by CPU hotplug online.
4470 */
4471void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4472{
4473 t->curr_ret_stack = -1;
4474 /*
4475 * The idle task has no parent, it either has its own
4476 * stack or no stack at all.
4477 */
4478 if (t->ret_stack)
4479 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4480
4481 if (ftrace_graph_active) {
4482 struct ftrace_ret_stack *ret_stack;
4483
4484 ret_stack = per_cpu(idle_ret_stack, cpu);
4485 if (!ret_stack) {
4486 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4487 * sizeof(struct ftrace_ret_stack),
4488 GFP_KERNEL);
4489 if (!ret_stack)
4490 return;
4491 per_cpu(idle_ret_stack, cpu) = ret_stack;
4492 }
4493 graph_init_task(t, ret_stack);
4494 }
4495}
4496
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004497/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004498void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004499{
Steven Rostedt84047e32009-06-02 16:51:55 -04004500 /* Make sure we do not use the parent ret_stack */
4501 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004502 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004503
Steven Rostedt597af812009-04-03 15:24:12 -04004504 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004505 struct ftrace_ret_stack *ret_stack;
4506
4507 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004508 * sizeof(struct ftrace_ret_stack),
4509 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004510 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004511 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004512 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004513 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004514}
4515
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004516void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004517{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004518 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4519
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004520 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004521 /* NULL must become visible to IRQs before we free it: */
4522 barrier();
4523
4524 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004525}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004526
4527void ftrace_graph_stop(void)
4528{
4529 ftrace_stop();
4530}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004531#endif