blob: 531ffa6f5a7b72a88753c36d92e1801fcaeac79e [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
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Jiri Olsae2484912012-02-15 15:51:48 +010065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
68#define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
70#else
71#define INIT_REGEX_LOCK(opsname)
72#endif
73
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040074static struct ftrace_ops ftrace_list_end __read_mostly = {
75 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040076 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077};
78
Steven Rostedt4eebcc82008-05-12 21:20:48 +020079/* ftrace_enabled is a method to turn ftrace on or off */
80int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020081static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020082
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050083/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040084int function_trace_stop __read_mostly;
85
86/* Current function tracing op */
87struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050088
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040089/* List for set_ftrace_pid's pids. */
90LIST_HEAD(ftrace_pids);
91struct ftrace_pid {
92 struct list_head list;
93 struct pid *pid;
94};
95
Steven Rostedt4eebcc82008-05-12 21:20:48 +020096/*
97 * ftrace_disabled is set when an anomaly is discovered.
98 * ftrace_disabled is much stronger than ftrace_enabled.
99 */
100static int ftrace_disabled __read_mostly;
101
Steven Rostedt52baf112009-02-14 01:15:39 -0500102static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200103
Steven Rostedtb8489142011-05-04 09:27:52 -0400104static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
Jiri Olsae2484912012-02-15 15:51:48 +0100105static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400106static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200107ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500108ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400109static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100110static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200111
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400112#if ARCH_SUPPORTS_FTRACE_OPS
113static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400114 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400115#else
116/* See comment below, where ftrace_ops_list_func is defined */
117static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
118#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
119#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400120
Steven Rostedt0a016402012-11-02 17:03:03 -0400121/*
122 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400123 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400124 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400125 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400126 * concurrent insertions into the ftrace_global_list.
127 *
128 * Silly Alpha and silly pointer-speculation compiler optimizations!
129 */
130#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400131 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400132 do
133
134/*
135 * Optimized for just a single item in the list (as that is the normal case).
136 */
137#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400138 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400139 unlikely((op) != &ftrace_list_end))
140
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900141static inline void ftrace_ops_init(struct ftrace_ops *ops)
142{
143#ifdef CONFIG_DYNAMIC_FTRACE
144 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
145 mutex_init(&ops->regex_lock);
146 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
147 }
148#endif
149}
150
Steven Rostedtea701f12012-07-20 13:08:05 -0400151/**
152 * ftrace_nr_registered_ops - return number of ops registered
153 *
154 * Returns the number of ftrace_ops registered and tracing functions
155 */
156int ftrace_nr_registered_ops(void)
157{
158 struct ftrace_ops *ops;
159 int cnt = 0;
160
161 mutex_lock(&ftrace_lock);
162
163 for (ops = ftrace_ops_list;
164 ops != &ftrace_list_end; ops = ops->next)
165 cnt++;
166
167 mutex_unlock(&ftrace_lock);
168
169 return cnt;
170}
171
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400172static void
173ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400174 struct ftrace_ops *op, struct pt_regs *regs)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200175{
Steven Rostedtc29f1222012-11-02 17:17:59 -0400176 int bit;
177
Steven Rostedtedc15ca2012-11-02 17:47:21 -0400178 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
179 if (bit < 0)
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400180 return;
181
Steven Rostedt0a016402012-11-02 17:03:03 -0400182 do_for_each_ftrace_op(op, ftrace_global_list) {
Steven Rostedta1e2e312011-08-09 12:50:46 -0400183 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -0400184 } while_for_each_ftrace_op(op);
Steven Rostedtedc15ca2012-11-02 17:47:21 -0400185
186 trace_clear_recursion(bit);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200187}
188
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400189static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400190 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500191{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500192 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500193 return;
194
Steven Rostedta1e2e312011-08-09 12:50:46 -0400195 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500196}
197
198static void set_ftrace_pid_function(ftrace_func_t func)
199{
200 /* do not set ftrace_pid_function to itself! */
201 if (func != ftrace_pid_func)
202 ftrace_pid_function = func;
203}
204
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200205/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200206 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200207 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200208 * This NULLs the ftrace function and in essence stops
209 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200210 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200211void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200212{
Steven Rostedt3d083392008-05-12 21:20:42 +0200213 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500214 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200215}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200216
Jiri Olsae2484912012-02-15 15:51:48 +0100217static void control_ops_disable_all(struct ftrace_ops *ops)
218{
219 int cpu;
220
221 for_each_possible_cpu(cpu)
222 *per_cpu_ptr(ops->disabled, cpu) = 1;
223}
224
225static int control_ops_alloc(struct ftrace_ops *ops)
226{
227 int __percpu *disabled;
228
229 disabled = alloc_percpu(int);
230 if (!disabled)
231 return -ENOMEM;
232
233 ops->disabled = disabled;
234 control_ops_disable_all(ops);
235 return 0;
236}
237
238static void control_ops_free(struct ftrace_ops *ops)
239{
240 free_percpu(ops->disabled);
241}
242
Steven Rostedt2b499382011-05-03 22:49:52 -0400243static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400244{
245 ftrace_func_t func;
246
247 /*
248 * If there's only one function registered, then call that
249 * function directly. Otherwise, we need to iterate over the
250 * registered callers.
251 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400252 if (ftrace_global_list == &ftrace_list_end ||
Steven Rostedt63503792012-11-02 16:58:56 -0400253 ftrace_global_list->next == &ftrace_list_end) {
Steven Rostedtb8489142011-05-04 09:27:52 -0400254 func = ftrace_global_list->func;
Steven Rostedt63503792012-11-02 16:58:56 -0400255 /*
256 * As we are calling the function directly.
257 * If it does not have recursion protection,
258 * the function_trace_op needs to be updated
259 * accordingly.
260 */
261 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
262 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
263 else
264 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
265 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -0400266 func = ftrace_global_list_func;
Steven Rostedt63503792012-11-02 16:58:56 -0400267 /* The list has its own recursion protection. */
268 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
269 }
270
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400271
272 /* If we filter on pids, update to use the pid function */
273 if (!list_empty(&ftrace_pids)) {
274 set_ftrace_pid_function(func);
275 func = ftrace_pid_func;
276 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400277
278 global_ops.func = func;
279}
280
281static void update_ftrace_function(void)
282{
283 ftrace_func_t func;
284
285 update_global_ops();
286
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400287 /*
288 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400289 * recursion safe and not dynamic and the arch supports passing ops,
290 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400291 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400292 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400293 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400294 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400295 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400296 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400297 /* Set the ftrace_ops that the arch callback uses */
298 if (ftrace_ops_list == &global_ops)
299 function_trace_op = ftrace_global_list;
300 else
301 function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400302 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400303 } else {
304 /* Just use the default ftrace_ops */
305 function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400306 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400307 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400308
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400309 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400310}
311
Steven Rostedt2b499382011-05-03 22:49:52 -0400312static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200313{
Steven Rostedt2b499382011-05-03 22:49:52 -0400314 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200315 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400316 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200317 * CPU might be walking that list. We need to make sure
318 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400319 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200320 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400321 rcu_assign_pointer(*list, ops);
322}
Steven Rostedt3d083392008-05-12 21:20:42 +0200323
Steven Rostedt2b499382011-05-03 22:49:52 -0400324static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
325{
326 struct ftrace_ops **p;
327
328 /*
329 * If we are removing the last function, then simply point
330 * to the ftrace_stub.
331 */
332 if (*list == ops && ops->next == &ftrace_list_end) {
333 *list = &ftrace_list_end;
334 return 0;
335 }
336
337 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
338 if (*p == ops)
339 break;
340
341 if (*p != ops)
342 return -1;
343
344 *p = (*p)->next;
345 return 0;
346}
347
Jiri Olsae2484912012-02-15 15:51:48 +0100348static void add_ftrace_list_ops(struct ftrace_ops **list,
349 struct ftrace_ops *main_ops,
350 struct ftrace_ops *ops)
351{
352 int first = *list == &ftrace_list_end;
353 add_ftrace_ops(list, ops);
354 if (first)
355 add_ftrace_ops(&ftrace_ops_list, main_ops);
356}
357
358static int remove_ftrace_list_ops(struct ftrace_ops **list,
359 struct ftrace_ops *main_ops,
360 struct ftrace_ops *ops)
361{
362 int ret = remove_ftrace_ops(list, ops);
363 if (!ret && *list == &ftrace_list_end)
364 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
365 return ret;
366}
367
Steven Rostedt2b499382011-05-03 22:49:52 -0400368static int __register_ftrace_function(struct ftrace_ops *ops)
369{
Steven Rostedt2b499382011-05-03 22:49:52 -0400370 if (FTRACE_WARN_ON(ops == &global_ops))
371 return -EINVAL;
372
Steven Rostedtb8489142011-05-04 09:27:52 -0400373 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
374 return -EBUSY;
375
Jiri Olsae2484912012-02-15 15:51:48 +0100376 /* We don't support both control and global flags set. */
377 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
378 return -EINVAL;
379
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900380#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400381 /*
382 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
383 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
384 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
385 */
386 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
387 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
388 return -EINVAL;
389
390 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
391 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
392#endif
393
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400394 if (!core_kernel_data((unsigned long)ops))
395 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
396
Steven Rostedtb8489142011-05-04 09:27:52 -0400397 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100398 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400399 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100400 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
401 if (control_ops_alloc(ops))
402 return -ENOMEM;
403 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400404 } else
405 add_ftrace_ops(&ftrace_ops_list, ops);
406
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400407 if (ftrace_enabled)
408 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200409
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200410 return 0;
411}
412
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400413static void ftrace_sync(struct work_struct *work)
414{
415 /*
416 * This function is just a stub to implement a hard force
417 * of synchronize_sched(). This requires synchronizing
418 * tasks even in userspace and idle.
419 *
420 * Yes, function tracing is rude.
421 */
422}
423
Ingo Molnare309b412008-05-12 21:20:51 +0200424static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200425{
Steven Rostedt2b499382011-05-03 22:49:52 -0400426 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200427
Steven Rostedtb8489142011-05-04 09:27:52 -0400428 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
429 return -EBUSY;
430
Steven Rostedt2b499382011-05-03 22:49:52 -0400431 if (FTRACE_WARN_ON(ops == &global_ops))
432 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200433
Steven Rostedtb8489142011-05-04 09:27:52 -0400434 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100435 ret = remove_ftrace_list_ops(&ftrace_global_list,
436 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400437 if (!ret)
438 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100439 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
440 ret = remove_ftrace_list_ops(&ftrace_control_list,
441 &control_ops, ops);
442 if (!ret) {
443 /*
444 * The ftrace_ops is now removed from the list,
445 * so there'll be no new users. We must ensure
446 * all current users are done before we free
447 * the control data.
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400448 * Note synchronize_sched() is not enough, as we
449 * use preempt_disable() to do RCU, but the function
450 * tracer can be called where RCU is not active
451 * (before user_exit()).
Jiri Olsae2484912012-02-15 15:51:48 +0100452 */
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400453 schedule_on_each_cpu(ftrace_sync);
Jiri Olsae2484912012-02-15 15:51:48 +0100454 control_ops_free(ops);
455 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400456 } else
457 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
458
Steven Rostedt2b499382011-05-03 22:49:52 -0400459 if (ret < 0)
460 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400461
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400462 if (ftrace_enabled)
463 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200464
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400465 /*
466 * Dynamic ops may be freed, we must make sure that all
467 * callers are done before leaving this function.
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400468 *
469 * Again, normal synchronize_sched() is not good enough.
470 * We need to do a hard force of sched synchronization.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400471 */
472 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
Steven Rostedt7614c3d2013-05-28 20:01:16 -0400473 schedule_on_each_cpu(ftrace_sync);
474
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400475
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500476 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200477}
478
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500479static void ftrace_update_pid_func(void)
480{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400481 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500482 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900483 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500484
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400485 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500486}
487
Steven Rostedt493762f2009-03-23 17:12:36 -0400488#ifdef CONFIG_FUNCTION_PROFILER
489struct ftrace_profile {
490 struct hlist_node node;
491 unsigned long ip;
492 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400493#ifdef CONFIG_FUNCTION_GRAPH_TRACER
494 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400495 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400496#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400497};
498
499struct ftrace_profile_page {
500 struct ftrace_profile_page *next;
501 unsigned long index;
502 struct ftrace_profile records[];
503};
504
Steven Rostedtcafb1682009-03-24 20:50:39 -0400505struct ftrace_profile_stat {
506 atomic_t disabled;
507 struct hlist_head *hash;
508 struct ftrace_profile_page *pages;
509 struct ftrace_profile_page *start;
510 struct tracer_stat stat;
511};
512
Steven Rostedt493762f2009-03-23 17:12:36 -0400513#define PROFILE_RECORDS_SIZE \
514 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
515
516#define PROFILES_PER_PAGE \
517 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
518
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400519static int ftrace_profile_enabled __read_mostly;
520
521/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400522static DEFINE_MUTEX(ftrace_profile_lock);
523
Steven Rostedtcafb1682009-03-24 20:50:39 -0400524static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400525
Namhyung Kim20079eb2013-04-10 08:55:50 +0900526#define FTRACE_PROFILE_HASH_BITS 10
527#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400528
Steven Rostedt493762f2009-03-23 17:12:36 -0400529static void *
530function_stat_next(void *v, int idx)
531{
532 struct ftrace_profile *rec = v;
533 struct ftrace_profile_page *pg;
534
535 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
536
537 again:
Li Zefan0296e422009-06-26 11:15:37 +0800538 if (idx != 0)
539 rec++;
540
Steven Rostedt493762f2009-03-23 17:12:36 -0400541 if ((void *)rec >= (void *)&pg->records[pg->index]) {
542 pg = pg->next;
543 if (!pg)
544 return NULL;
545 rec = &pg->records[0];
546 if (!rec->counter)
547 goto again;
548 }
549
550 return rec;
551}
552
553static void *function_stat_start(struct tracer_stat *trace)
554{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400555 struct ftrace_profile_stat *stat =
556 container_of(trace, struct ftrace_profile_stat, stat);
557
558 if (!stat || !stat->start)
559 return NULL;
560
561 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400562}
563
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400564#ifdef CONFIG_FUNCTION_GRAPH_TRACER
565/* function graph compares on total time */
566static int function_stat_cmp(void *p1, void *p2)
567{
568 struct ftrace_profile *a = p1;
569 struct ftrace_profile *b = p2;
570
571 if (a->time < b->time)
572 return -1;
573 if (a->time > b->time)
574 return 1;
575 else
576 return 0;
577}
578#else
579/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400580static int function_stat_cmp(void *p1, void *p2)
581{
582 struct ftrace_profile *a = p1;
583 struct ftrace_profile *b = p2;
584
585 if (a->counter < b->counter)
586 return -1;
587 if (a->counter > b->counter)
588 return 1;
589 else
590 return 0;
591}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400592#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400593
594static int function_stat_headers(struct seq_file *m)
595{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400596#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400597 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400598 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400599 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400600 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400601#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400602 seq_printf(m, " Function Hit\n"
603 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400604#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400605 return 0;
606}
607
608static int function_stat_show(struct seq_file *m, void *v)
609{
610 struct ftrace_profile *rec = v;
611 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800612 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400613#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400614 static struct trace_seq s;
615 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400616 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400617#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800618 mutex_lock(&ftrace_profile_lock);
619
620 /* we raced with function_profile_reset() */
621 if (unlikely(rec->counter == 0)) {
622 ret = -EBUSY;
623 goto out;
624 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400625
626 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400627 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400628
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400629#ifdef CONFIG_FUNCTION_GRAPH_TRACER
630 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400631 avg = rec->time;
632 do_div(avg, rec->counter);
633
Chase Douglase330b3b2010-04-26 14:02:05 -0400634 /* Sample standard deviation (s^2) */
635 if (rec->counter <= 1)
636 stddev = 0;
637 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200638 /*
639 * Apply Welford's method:
640 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
641 */
642 stddev = rec->counter * rec->time_squared -
643 rec->time * rec->time;
644
Chase Douglase330b3b2010-04-26 14:02:05 -0400645 /*
646 * Divide only 1000 for ns^2 -> us^2 conversion.
647 * trace_print_graph_duration will divide 1000 again.
648 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200649 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400650 }
651
Steven Rostedt34886c82009-03-25 21:00:47 -0400652 trace_seq_init(&s);
653 trace_print_graph_duration(rec->time, &s);
654 trace_seq_puts(&s, " ");
655 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400656 trace_seq_puts(&s, " ");
657 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400658 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400659#endif
660 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800661out:
662 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400663
Li Zefan3aaba202010-08-23 16:50:12 +0800664 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400665}
666
Steven Rostedtcafb1682009-03-24 20:50:39 -0400667static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400668{
669 struct ftrace_profile_page *pg;
670
Steven Rostedtcafb1682009-03-24 20:50:39 -0400671 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400672
673 while (pg) {
674 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
675 pg->index = 0;
676 pg = pg->next;
677 }
678
Steven Rostedtcafb1682009-03-24 20:50:39 -0400679 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400680 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
681}
682
Steven Rostedtcafb1682009-03-24 20:50:39 -0400683int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400684{
685 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400686 int functions;
687 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400688 int i;
689
690 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400691 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400692 return 0;
693
Steven Rostedtcafb1682009-03-24 20:50:39 -0400694 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
695 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400696 return -ENOMEM;
697
Steven Rostedt318e0a72009-03-25 20:06:34 -0400698#ifdef CONFIG_DYNAMIC_FTRACE
699 functions = ftrace_update_tot_cnt;
700#else
701 /*
702 * We do not know the number of functions that exist because
703 * dynamic tracing is what counts them. With past experience
704 * we have around 20K functions. That should be more than enough.
705 * It is highly unlikely we will execute every function in
706 * the kernel.
707 */
708 functions = 20000;
709#endif
710
Steven Rostedtcafb1682009-03-24 20:50:39 -0400711 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400712
Steven Rostedt318e0a72009-03-25 20:06:34 -0400713 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
714
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900715 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400716 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400717 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400718 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400719 pg = pg->next;
720 }
721
722 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400723
724 out_free:
725 pg = stat->start;
726 while (pg) {
727 unsigned long tmp = (unsigned long)pg;
728
729 pg = pg->next;
730 free_page(tmp);
731 }
732
Steven Rostedt318e0a72009-03-25 20:06:34 -0400733 stat->pages = NULL;
734 stat->start = NULL;
735
736 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400737}
738
Steven Rostedtcafb1682009-03-24 20:50:39 -0400739static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400740{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400742 int size;
743
Steven Rostedtcafb1682009-03-24 20:50:39 -0400744 stat = &per_cpu(ftrace_profile_stats, cpu);
745
746 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400747 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 return 0;
750 }
751
752 /*
753 * We are profiling all functions, but usually only a few thousand
754 * functions are hit. We'll make a hash of 1024 items.
755 */
756 size = FTRACE_PROFILE_HASH_SIZE;
757
Steven Rostedtcafb1682009-03-24 20:50:39 -0400758 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400759
Steven Rostedtcafb1682009-03-24 20:50:39 -0400760 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400761 return -ENOMEM;
762
Steven Rostedt318e0a72009-03-25 20:06:34 -0400763 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400764 if (ftrace_profile_pages_init(stat) < 0) {
765 kfree(stat->hash);
766 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400767 return -ENOMEM;
768 }
769
770 return 0;
771}
772
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773static int ftrace_profile_init(void)
774{
775 int cpu;
776 int ret = 0;
777
778 for_each_online_cpu(cpu) {
779 ret = ftrace_profile_init_cpu(cpu);
780 if (ret)
781 break;
782 }
783
784 return ret;
785}
786
Steven Rostedt493762f2009-03-23 17:12:36 -0400787/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400788static struct ftrace_profile *
789ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400790{
791 struct ftrace_profile *rec;
792 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400793 unsigned long key;
794
Namhyung Kim20079eb2013-04-10 08:55:50 +0900795 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400796 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400797
798 if (hlist_empty(hhd))
799 return NULL;
800
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400801 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400802 if (rec->ip == ip)
803 return rec;
804 }
805
806 return NULL;
807}
808
Steven Rostedtcafb1682009-03-24 20:50:39 -0400809static void ftrace_add_profile(struct ftrace_profile_stat *stat,
810 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400811{
812 unsigned long key;
813
Namhyung Kim20079eb2013-04-10 08:55:50 +0900814 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400815 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400816}
817
Steven Rostedt318e0a72009-03-25 20:06:34 -0400818/*
819 * The memory is already allocated, this simply finds a new record to use.
820 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400821static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400822ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400823{
824 struct ftrace_profile *rec = NULL;
825
Steven Rostedt318e0a72009-03-25 20:06:34 -0400826 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400827 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400828 goto out;
829
Steven Rostedt493762f2009-03-23 17:12:36 -0400830 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400831 * Try to find the function again since an NMI
832 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400833 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400834 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400835 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400836 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400837
Steven Rostedtcafb1682009-03-24 20:50:39 -0400838 if (stat->pages->index == PROFILES_PER_PAGE) {
839 if (!stat->pages->next)
840 goto out;
841 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400842 }
843
Steven Rostedtcafb1682009-03-24 20:50:39 -0400844 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400845 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400846 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400847
Steven Rostedt493762f2009-03-23 17:12:36 -0400848 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400849 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400850
851 return rec;
852}
853
Steven Rostedt493762f2009-03-23 17:12:36 -0400854static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400855function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400856 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400857{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400858 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400859 struct ftrace_profile *rec;
860 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400861
862 if (!ftrace_profile_enabled)
863 return;
864
Steven Rostedt493762f2009-03-23 17:12:36 -0400865 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400866
867 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400868 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400869 goto out;
870
871 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400872 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400873 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400874 if (!rec)
875 goto out;
876 }
877
878 rec->counter++;
879 out:
880 local_irq_restore(flags);
881}
882
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400883#ifdef CONFIG_FUNCTION_GRAPH_TRACER
884static int profile_graph_entry(struct ftrace_graph_ent *trace)
885{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400886 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400887 return 1;
888}
889
890static void profile_graph_return(struct ftrace_graph_ret *trace)
891{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400892 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400893 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400894 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400895 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400896
897 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400898 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400899 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400900 goto out;
901
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400902 /* If the calltime was zero'd ignore it */
903 if (!trace->calltime)
904 goto out;
905
Steven Rostedta2a16d62009-03-24 23:17:58 -0400906 calltime = trace->rettime - trace->calltime;
907
908 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
909 int index;
910
911 index = trace->depth;
912
913 /* Append this call time to the parent time to subtract */
914 if (index)
915 current->ret_stack[index - 1].subtime += calltime;
916
917 if (current->ret_stack[index].subtime < calltime)
918 calltime -= current->ret_stack[index].subtime;
919 else
920 calltime = 0;
921 }
922
Steven Rostedtcafb1682009-03-24 20:50:39 -0400923 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400924 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400925 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400926 rec->time_squared += calltime * calltime;
927 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400928
Steven Rostedtcafb1682009-03-24 20:50:39 -0400929 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400930 local_irq_restore(flags);
931}
932
933static int register_ftrace_profiler(void)
934{
935 return register_ftrace_graph(&profile_graph_return,
936 &profile_graph_entry);
937}
938
939static void unregister_ftrace_profiler(void)
940{
941 unregister_ftrace_graph();
942}
943#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100944static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400945 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900946 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
947 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400948};
949
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400950static int register_ftrace_profiler(void)
951{
952 return register_ftrace_function(&ftrace_profile_ops);
953}
954
955static void unregister_ftrace_profiler(void)
956{
957 unregister_ftrace_function(&ftrace_profile_ops);
958}
959#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
960
Steven Rostedt493762f2009-03-23 17:12:36 -0400961static ssize_t
962ftrace_profile_write(struct file *filp, const char __user *ubuf,
963 size_t cnt, loff_t *ppos)
964{
965 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400966 int ret;
967
Peter Huewe22fe9b52011-06-07 21:58:27 +0200968 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
969 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400970 return ret;
971
972 val = !!val;
973
974 mutex_lock(&ftrace_profile_lock);
975 if (ftrace_profile_enabled ^ val) {
976 if (val) {
977 ret = ftrace_profile_init();
978 if (ret < 0) {
979 cnt = ret;
980 goto out;
981 }
982
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400983 ret = register_ftrace_profiler();
984 if (ret < 0) {
985 cnt = ret;
986 goto out;
987 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400988 ftrace_profile_enabled = 1;
989 } else {
990 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400991 /*
992 * unregister_ftrace_profiler calls stop_machine
993 * so this acts like an synchronize_sched.
994 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400995 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400996 }
997 }
998 out:
999 mutex_unlock(&ftrace_profile_lock);
1000
Jiri Olsacf8517c2009-10-23 19:36:16 -04001001 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001002
1003 return cnt;
1004}
1005
1006static ssize_t
1007ftrace_profile_read(struct file *filp, char __user *ubuf,
1008 size_t cnt, loff_t *ppos)
1009{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001010 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001011 int r;
1012
1013 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1014 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1015}
1016
1017static const struct file_operations ftrace_profile_fops = {
1018 .open = tracing_open_generic,
1019 .read = ftrace_profile_read,
1020 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001021 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001022};
1023
Steven Rostedtcafb1682009-03-24 20:50:39 -04001024/* used to initialize the real stat files */
1025static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001026 .name = "functions",
1027 .stat_start = function_stat_start,
1028 .stat_next = function_stat_next,
1029 .stat_cmp = function_stat_cmp,
1030 .stat_headers = function_stat_headers,
1031 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001032};
1033
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001034static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001035{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001036 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001037 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001038 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001039 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001040 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001041
Steven Rostedtcafb1682009-03-24 20:50:39 -04001042 for_each_possible_cpu(cpu) {
1043 stat = &per_cpu(ftrace_profile_stats, cpu);
1044
1045 /* allocate enough for function name + cpu number */
1046 name = kmalloc(32, GFP_KERNEL);
1047 if (!name) {
1048 /*
1049 * The files created are permanent, if something happens
1050 * we still do not free memory.
1051 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001052 WARN(1,
1053 "Could not allocate stat file for cpu %d\n",
1054 cpu);
1055 return;
1056 }
1057 stat->stat = function_stats;
1058 snprintf(name, 32, "function%d", cpu);
1059 stat->stat.name = name;
1060 ret = register_stat_tracer(&stat->stat);
1061 if (ret) {
1062 WARN(1,
1063 "Could not register function stat for cpu %d\n",
1064 cpu);
1065 kfree(name);
1066 return;
1067 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001068 }
1069
1070 entry = debugfs_create_file("function_profile_enabled", 0644,
1071 d_tracer, NULL, &ftrace_profile_fops);
1072 if (!entry)
1073 pr_warning("Could not create debugfs "
1074 "'function_profile_enabled' entry\n");
1075}
1076
1077#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001078static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001079{
1080}
1081#endif /* CONFIG_FUNCTION_PROFILER */
1082
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001083static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1084
Steven Rostedt3d083392008-05-12 21:20:42 +02001085#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001086
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001087#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001088# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001089#endif
1090
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001091static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1092
Steven Rostedtb6887d72009-02-17 12:32:04 -05001093struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001094 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001095 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001096 unsigned long flags;
1097 unsigned long ip;
1098 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001099 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001100};
1101
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001102struct ftrace_func_entry {
1103 struct hlist_node hlist;
1104 unsigned long ip;
1105};
1106
1107struct ftrace_hash {
1108 unsigned long size_bits;
1109 struct hlist_head *buckets;
1110 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001111 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001112};
1113
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001114/*
1115 * We make these constant because no one should touch them,
1116 * but they are used as the default "empty hash", to avoid allocating
1117 * it all the time. These are in a read only section such that if
1118 * anyone does try to modify it, it will cause an exception.
1119 */
1120static const struct hlist_head empty_buckets[1];
1121static const struct ftrace_hash empty_hash = {
1122 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001123};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001124#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001125
Steven Rostedt2b499382011-05-03 22:49:52 -04001126static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001127 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001128 .notrace_hash = EMPTY_HASH,
1129 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001130 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1131 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001132};
1133
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001134struct ftrace_page {
1135 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001136 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001137 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001138 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001139};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001140
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001141static struct ftrace_page *ftrace_new_pgs;
1142
Steven Rostedta7900872011-12-16 16:23:44 -05001143#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1144#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001145
1146/* estimate from running different kernels */
1147#define NR_TO_INIT 10000
1148
1149static struct ftrace_page *ftrace_pages_start;
1150static struct ftrace_page *ftrace_pages;
1151
Steven Rostedt06a51d92011-12-19 19:07:36 -05001152static bool ftrace_hash_empty(struct ftrace_hash *hash)
1153{
1154 return !hash || !hash->count;
1155}
1156
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001157static struct ftrace_func_entry *
1158ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1159{
1160 unsigned long key;
1161 struct ftrace_func_entry *entry;
1162 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001163
Steven Rostedt06a51d92011-12-19 19:07:36 -05001164 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001165 return NULL;
1166
1167 if (hash->size_bits > 0)
1168 key = hash_long(ip, hash->size_bits);
1169 else
1170 key = 0;
1171
1172 hhd = &hash->buckets[key];
1173
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001174 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001175 if (entry->ip == ip)
1176 return entry;
1177 }
1178 return NULL;
1179}
1180
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001181static void __add_hash_entry(struct ftrace_hash *hash,
1182 struct ftrace_func_entry *entry)
1183{
1184 struct hlist_head *hhd;
1185 unsigned long key;
1186
1187 if (hash->size_bits)
1188 key = hash_long(entry->ip, hash->size_bits);
1189 else
1190 key = 0;
1191
1192 hhd = &hash->buckets[key];
1193 hlist_add_head(&entry->hlist, hhd);
1194 hash->count++;
1195}
1196
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001197static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1198{
1199 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001200
1201 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1202 if (!entry)
1203 return -ENOMEM;
1204
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001205 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001206 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001207
1208 return 0;
1209}
1210
1211static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001212free_hash_entry(struct ftrace_hash *hash,
1213 struct ftrace_func_entry *entry)
1214{
1215 hlist_del(&entry->hlist);
1216 kfree(entry);
1217 hash->count--;
1218}
1219
1220static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001221remove_hash_entry(struct ftrace_hash *hash,
1222 struct ftrace_func_entry *entry)
1223{
1224 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001225 hash->count--;
1226}
1227
1228static void ftrace_hash_clear(struct ftrace_hash *hash)
1229{
1230 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001231 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001232 struct ftrace_func_entry *entry;
1233 int size = 1 << hash->size_bits;
1234 int i;
1235
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001236 if (!hash->count)
1237 return;
1238
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001239 for (i = 0; i < size; i++) {
1240 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001241 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001242 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001243 }
1244 FTRACE_WARN_ON(hash->count);
1245}
1246
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001247static void free_ftrace_hash(struct ftrace_hash *hash)
1248{
1249 if (!hash || hash == EMPTY_HASH)
1250 return;
1251 ftrace_hash_clear(hash);
1252 kfree(hash->buckets);
1253 kfree(hash);
1254}
1255
Steven Rostedt07fd5512011-05-05 18:03:47 -04001256static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1257{
1258 struct ftrace_hash *hash;
1259
1260 hash = container_of(rcu, struct ftrace_hash, rcu);
1261 free_ftrace_hash(hash);
1262}
1263
1264static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1265{
1266 if (!hash || hash == EMPTY_HASH)
1267 return;
1268 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1269}
1270
Jiri Olsa5500fa52012-02-15 15:51:54 +01001271void ftrace_free_filter(struct ftrace_ops *ops)
1272{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001273 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001274 free_ftrace_hash(ops->filter_hash);
1275 free_ftrace_hash(ops->notrace_hash);
1276}
1277
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001278static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1279{
1280 struct ftrace_hash *hash;
1281 int size;
1282
1283 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1284 if (!hash)
1285 return NULL;
1286
1287 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001288 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001289
1290 if (!hash->buckets) {
1291 kfree(hash);
1292 return NULL;
1293 }
1294
1295 hash->size_bits = size_bits;
1296
1297 return hash;
1298}
1299
1300static struct ftrace_hash *
1301alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1302{
1303 struct ftrace_func_entry *entry;
1304 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001305 int size;
1306 int ret;
1307 int i;
1308
1309 new_hash = alloc_ftrace_hash(size_bits);
1310 if (!new_hash)
1311 return NULL;
1312
1313 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001314 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001315 return new_hash;
1316
1317 size = 1 << hash->size_bits;
1318 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001319 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001320 ret = add_hash_entry(new_hash, entry->ip);
1321 if (ret < 0)
1322 goto free_hash;
1323 }
1324 }
1325
1326 FTRACE_WARN_ON(new_hash->count != hash->count);
1327
1328 return new_hash;
1329
1330 free_hash:
1331 free_ftrace_hash(new_hash);
1332 return NULL;
1333}
1334
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001335static void
1336ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1337static void
1338ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1339
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001340static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001341ftrace_hash_move(struct ftrace_ops *ops, int enable,
1342 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001343{
1344 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001345 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001346 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001347 struct ftrace_hash *old_hash;
1348 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001349 int size = src->count;
1350 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001351 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001352 int i;
1353
1354 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001355 * Remove the current set, update the hash and add
1356 * them back.
1357 */
1358 ftrace_hash_rec_disable(ops, enable);
1359
1360 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001361 * If the new source is empty, just free dst and assign it
1362 * the empty_hash.
1363 */
1364 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001365 free_ftrace_hash_rcu(*dst);
1366 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001367 /* still need to update the function records */
1368 ret = 0;
1369 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001370 }
1371
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001372 /*
1373 * Make the hash size about 1/2 the # found
1374 */
1375 for (size /= 2; size; size >>= 1)
1376 bits++;
1377
1378 /* Don't allocate too much */
1379 if (bits > FTRACE_HASH_MAX_BITS)
1380 bits = FTRACE_HASH_MAX_BITS;
1381
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001382 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001383 new_hash = alloc_ftrace_hash(bits);
1384 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001385 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001386
1387 size = 1 << src->size_bits;
1388 for (i = 0; i < size; i++) {
1389 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001390 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001391 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001392 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001393 }
1394 }
1395
Steven Rostedt07fd5512011-05-05 18:03:47 -04001396 old_hash = *dst;
1397 rcu_assign_pointer(*dst, new_hash);
1398 free_ftrace_hash_rcu(old_hash);
1399
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001400 ret = 0;
1401 out:
1402 /*
1403 * Enable regardless of ret:
1404 * On success, we enable the new hash.
1405 * On failure, we re-enable the original hash.
1406 */
1407 ftrace_hash_rec_enable(ops, enable);
1408
1409 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001410}
1411
Steven Rostedt265c8312009-02-13 12:43:56 -05001412/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001413 * Test the hashes for this ops to see if we want to call
1414 * the ops->func or not.
1415 *
1416 * It's a match if the ip is in the ops->filter_hash or
1417 * the filter_hash does not exist or is empty,
1418 * AND
1419 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001420 *
1421 * This needs to be called with preemption disabled as
1422 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001423 */
1424static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001425ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001426{
1427 struct ftrace_hash *filter_hash;
1428 struct ftrace_hash *notrace_hash;
1429 int ret;
1430
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001431#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1432 /*
1433 * There's a small race when adding ops that the ftrace handler
1434 * that wants regs, may be called without them. We can not
1435 * allow that handler to be called if regs is NULL.
1436 */
1437 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1438 return 0;
1439#endif
1440
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001441 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1442 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001443
Steven Rostedt06a51d92011-12-19 19:07:36 -05001444 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001445 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001446 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001447 !ftrace_lookup_ip(notrace_hash, ip)))
1448 ret = 1;
1449 else
1450 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001451
1452 return ret;
1453}
1454
1455/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001456 * This is a double for. Do not use 'break' to break out of the loop,
1457 * you must use a goto.
1458 */
1459#define do_for_each_ftrace_rec(pg, rec) \
1460 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1461 int _____i; \
1462 for (_____i = 0; _____i < pg->index; _____i++) { \
1463 rec = &pg->records[_____i];
1464
1465#define while_for_each_ftrace_rec() \
1466 } \
1467 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301468
Steven Rostedt5855fea2011-12-16 19:27:42 -05001469
1470static int ftrace_cmp_recs(const void *a, const void *b)
1471{
Steven Rostedta650e022012-04-25 13:48:13 -04001472 const struct dyn_ftrace *key = a;
1473 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001474
Steven Rostedta650e022012-04-25 13:48:13 -04001475 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001476 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001477 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1478 return 1;
1479 return 0;
1480}
1481
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001482static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001483{
1484 struct ftrace_page *pg;
1485 struct dyn_ftrace *rec;
1486 struct dyn_ftrace key;
1487
1488 key.ip = start;
1489 key.flags = end; /* overload flags, as it is unsigned long */
1490
1491 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1492 if (end < pg->records[0].ip ||
1493 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1494 continue;
1495 rec = bsearch(&key, pg->records, pg->index,
1496 sizeof(struct dyn_ftrace),
1497 ftrace_cmp_recs);
1498 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001499 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001500 }
1501
Steven Rostedt5855fea2011-12-16 19:27:42 -05001502 return 0;
1503}
1504
Steven Rostedtc88fd862011-08-16 09:53:39 -04001505/**
1506 * ftrace_location - return true if the ip giving is a traced location
1507 * @ip: the instruction pointer to check
1508 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001509 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001510 * That is, the instruction that is either a NOP or call to
1511 * the function tracer. It checks the ftrace internal tables to
1512 * determine if the address belongs or not.
1513 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001514unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001515{
Steven Rostedta650e022012-04-25 13:48:13 -04001516 return ftrace_location_range(ip, ip);
1517}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001518
Steven Rostedta650e022012-04-25 13:48:13 -04001519/**
1520 * ftrace_text_reserved - return true if range contains an ftrace location
1521 * @start: start of range to search
1522 * @end: end of range to search (inclusive). @end points to the last byte to check.
1523 *
1524 * Returns 1 if @start and @end contains a ftrace location.
1525 * That is, the instruction that is either a NOP or call to
1526 * the function tracer. It checks the ftrace internal tables to
1527 * determine if the address belongs or not.
1528 */
1529int ftrace_text_reserved(void *start, void *end)
1530{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001531 unsigned long ret;
1532
1533 ret = ftrace_location_range((unsigned long)start,
1534 (unsigned long)end);
1535
1536 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001537}
1538
Steven Rostedted926f92011-05-03 13:25:24 -04001539static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1540 int filter_hash,
1541 bool inc)
1542{
1543 struct ftrace_hash *hash;
1544 struct ftrace_hash *other_hash;
1545 struct ftrace_page *pg;
1546 struct dyn_ftrace *rec;
1547 int count = 0;
1548 int all = 0;
1549
1550 /* Only update if the ops has been registered */
1551 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1552 return;
1553
1554 /*
1555 * In the filter_hash case:
1556 * If the count is zero, we update all records.
1557 * Otherwise we just update the items in the hash.
1558 *
1559 * In the notrace_hash case:
1560 * We enable the update in the hash.
1561 * As disabling notrace means enabling the tracing,
1562 * and enabling notrace means disabling, the inc variable
1563 * gets inversed.
1564 */
1565 if (filter_hash) {
1566 hash = ops->filter_hash;
1567 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001568 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001569 all = 1;
1570 } else {
1571 inc = !inc;
1572 hash = ops->notrace_hash;
1573 other_hash = ops->filter_hash;
1574 /*
1575 * If the notrace hash has no items,
1576 * then there's nothing to do.
1577 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001578 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001579 return;
1580 }
1581
1582 do_for_each_ftrace_rec(pg, rec) {
1583 int in_other_hash = 0;
1584 int in_hash = 0;
1585 int match = 0;
1586
1587 if (all) {
1588 /*
1589 * Only the filter_hash affects all records.
1590 * Update if the record is not in the notrace hash.
1591 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001592 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001593 match = 1;
1594 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001595 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1596 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001597
1598 /*
1599 *
1600 */
1601 if (filter_hash && in_hash && !in_other_hash)
1602 match = 1;
1603 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001604 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001605 match = 1;
1606 }
1607 if (!match)
1608 continue;
1609
1610 if (inc) {
1611 rec->flags++;
1612 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1613 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001614 /*
1615 * If any ops wants regs saved for this function
1616 * then all ops will get saved regs.
1617 */
1618 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1619 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001620 } else {
1621 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1622 return;
1623 rec->flags--;
1624 }
1625 count++;
1626 /* Shortcut, if we handled all records, we are done. */
1627 if (!all && count == hash->count)
1628 return;
1629 } while_for_each_ftrace_rec();
1630}
1631
1632static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1633 int filter_hash)
1634{
1635 __ftrace_hash_rec_update(ops, filter_hash, 0);
1636}
1637
1638static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1639 int filter_hash)
1640{
1641 __ftrace_hash_rec_update(ops, filter_hash, 1);
1642}
1643
Steven Rostedt05736a42008-09-22 14:55:47 -07001644static void print_ip_ins(const char *fmt, unsigned char *p)
1645{
1646 int i;
1647
1648 printk(KERN_CONT "%s", fmt);
1649
1650 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1651 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1652}
1653
Steven Rostedtc88fd862011-08-16 09:53:39 -04001654/**
1655 * ftrace_bug - report and shutdown function tracer
1656 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1657 * @ip: The address that failed
1658 *
1659 * The arch code that enables or disables the function tracing
1660 * can call ftrace_bug() when it has detected a problem in
1661 * modifying the code. @failed should be one of either:
1662 * EFAULT - if the problem happens on reading the @ip address
1663 * EINVAL - if what is read at @ip is not what was expected
1664 * EPERM - if the problem happens on writting to the @ip address
1665 */
1666void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001667{
1668 switch (failed) {
1669 case -EFAULT:
1670 FTRACE_WARN_ON_ONCE(1);
1671 pr_info("ftrace faulted on modifying ");
1672 print_ip_sym(ip);
1673 break;
1674 case -EINVAL:
1675 FTRACE_WARN_ON_ONCE(1);
1676 pr_info("ftrace failed to modify ");
1677 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001678 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001679 printk(KERN_CONT "\n");
1680 break;
1681 case -EPERM:
1682 FTRACE_WARN_ON_ONCE(1);
1683 pr_info("ftrace faulted on writing ");
1684 print_ip_sym(ip);
1685 break;
1686 default:
1687 FTRACE_WARN_ON_ONCE(1);
1688 pr_info("ftrace faulted on unknown error ");
1689 print_ip_sym(ip);
1690 }
1691}
1692
Steven Rostedtc88fd862011-08-16 09:53:39 -04001693static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001694{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001695 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001696
Steven Rostedt982c3502008-11-15 16:31:41 -05001697 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001698 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001699 *
Steven Rostedted926f92011-05-03 13:25:24 -04001700 * If the record has a ref count, then we need to enable it
1701 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001702 *
Steven Rostedted926f92011-05-03 13:25:24 -04001703 * Otherwise we make sure its disabled.
1704 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001705 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001706 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001707 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001708 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001709 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001710
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001711 /*
1712 * If enabling and the REGS flag does not match the REGS_EN, then
1713 * do not ignore this record. Set flags to fail the compare against
1714 * ENABLED.
1715 */
1716 if (flag &&
1717 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1718 flag |= FTRACE_FL_REGS;
1719
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001720 /* If the state of this record hasn't changed, then do nothing */
1721 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001722 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001723
1724 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001725 /* Save off if rec is being enabled (for return value) */
1726 flag ^= rec->flags & FTRACE_FL_ENABLED;
1727
1728 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001729 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001730 if (flag & FTRACE_FL_REGS) {
1731 if (rec->flags & FTRACE_FL_REGS)
1732 rec->flags |= FTRACE_FL_REGS_EN;
1733 else
1734 rec->flags &= ~FTRACE_FL_REGS_EN;
1735 }
1736 }
1737
1738 /*
1739 * If this record is being updated from a nop, then
1740 * return UPDATE_MAKE_CALL.
1741 * Otherwise, if the EN flag is set, then return
1742 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1743 * from the non-save regs, to a save regs function.
1744 * Otherwise,
1745 * return UPDATE_MODIFY_CALL to tell the caller to convert
1746 * from the save regs, to a non-save regs function.
1747 */
1748 if (flag & FTRACE_FL_ENABLED)
1749 return FTRACE_UPDATE_MAKE_CALL;
1750 else if (rec->flags & FTRACE_FL_REGS_EN)
1751 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1752 else
1753 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001754 }
1755
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001756 if (update) {
1757 /* If there's no more users, clear all flags */
1758 if (!(rec->flags & ~FTRACE_FL_MASK))
1759 rec->flags = 0;
1760 else
1761 /* Just disable the record (keep REGS state) */
1762 rec->flags &= ~FTRACE_FL_ENABLED;
1763 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001764
1765 return FTRACE_UPDATE_MAKE_NOP;
1766}
1767
1768/**
1769 * ftrace_update_record, set a record that now is tracing or not
1770 * @rec: the record to update
1771 * @enable: set to 1 if the record is tracing, zero to force disable
1772 *
1773 * The records that represent all functions that can be traced need
1774 * to be updated when tracing has been enabled.
1775 */
1776int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1777{
1778 return ftrace_check_record(rec, enable, 1);
1779}
1780
1781/**
1782 * ftrace_test_record, check if the record has been enabled or not
1783 * @rec: the record to test
1784 * @enable: set to 1 to check if enabled, 0 if it is disabled
1785 *
1786 * The arch code may need to test if a record is already set to
1787 * tracing to determine how to modify the function code that it
1788 * represents.
1789 */
1790int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1791{
1792 return ftrace_check_record(rec, enable, 0);
1793}
1794
1795static int
1796__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1797{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001798 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001799 unsigned long ftrace_addr;
1800 int ret;
1801
Steven Rostedtc88fd862011-08-16 09:53:39 -04001802 ret = ftrace_update_record(rec, enable);
1803
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001804 if (rec->flags & FTRACE_FL_REGS)
1805 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1806 else
1807 ftrace_addr = (unsigned long)FTRACE_ADDR;
1808
Steven Rostedtc88fd862011-08-16 09:53:39 -04001809 switch (ret) {
1810 case FTRACE_UPDATE_IGNORE:
1811 return 0;
1812
1813 case FTRACE_UPDATE_MAKE_CALL:
1814 return ftrace_make_call(rec, ftrace_addr);
1815
1816 case FTRACE_UPDATE_MAKE_NOP:
1817 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001818
1819 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1820 case FTRACE_UPDATE_MODIFY_CALL:
1821 if (rec->flags & FTRACE_FL_REGS)
1822 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1823 else
1824 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1825
1826 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001827 }
1828
1829 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001830}
1831
Steven Rostedte4f5d542012-04-27 09:13:18 -04001832void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001833{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001834 struct dyn_ftrace *rec;
1835 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001836 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001837
Steven Rostedt45a4a232011-04-21 23:16:46 -04001838 if (unlikely(ftrace_disabled))
1839 return;
1840
Steven Rostedt265c8312009-02-13 12:43:56 -05001841 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001842 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001843 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001844 ftrace_bug(failed, rec->ip);
1845 /* Stop processing */
1846 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001847 }
1848 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001849}
1850
Steven Rostedtc88fd862011-08-16 09:53:39 -04001851struct ftrace_rec_iter {
1852 struct ftrace_page *pg;
1853 int index;
1854};
1855
1856/**
1857 * ftrace_rec_iter_start, start up iterating over traced functions
1858 *
1859 * Returns an iterator handle that is used to iterate over all
1860 * the records that represent address locations where functions
1861 * are traced.
1862 *
1863 * May return NULL if no records are available.
1864 */
1865struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1866{
1867 /*
1868 * We only use a single iterator.
1869 * Protected by the ftrace_lock mutex.
1870 */
1871 static struct ftrace_rec_iter ftrace_rec_iter;
1872 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1873
1874 iter->pg = ftrace_pages_start;
1875 iter->index = 0;
1876
1877 /* Could have empty pages */
1878 while (iter->pg && !iter->pg->index)
1879 iter->pg = iter->pg->next;
1880
1881 if (!iter->pg)
1882 return NULL;
1883
1884 return iter;
1885}
1886
1887/**
1888 * ftrace_rec_iter_next, get the next record to process.
1889 * @iter: The handle to the iterator.
1890 *
1891 * Returns the next iterator after the given iterator @iter.
1892 */
1893struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1894{
1895 iter->index++;
1896
1897 if (iter->index >= iter->pg->index) {
1898 iter->pg = iter->pg->next;
1899 iter->index = 0;
1900
1901 /* Could have empty pages */
1902 while (iter->pg && !iter->pg->index)
1903 iter->pg = iter->pg->next;
1904 }
1905
1906 if (!iter->pg)
1907 return NULL;
1908
1909 return iter;
1910}
1911
1912/**
1913 * ftrace_rec_iter_record, get the record at the iterator location
1914 * @iter: The current iterator location
1915 *
1916 * Returns the record that the current @iter is at.
1917 */
1918struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1919{
1920 return &iter->pg->records[iter->index];
1921}
1922
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301923static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001924ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001925{
1926 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001927 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001928
1929 ip = rec->ip;
1930
Steven Rostedt45a4a232011-04-21 23:16:46 -04001931 if (unlikely(ftrace_disabled))
1932 return 0;
1933
Shaohua Li25aac9d2009-01-09 11:29:40 +08001934 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001935 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001936 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301937 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001938 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301939 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001940}
1941
Steven Rostedt000ab692009-02-17 13:35:06 -05001942/*
1943 * archs can override this function if they must do something
1944 * before the modifying code is performed.
1945 */
1946int __weak ftrace_arch_code_modify_prepare(void)
1947{
1948 return 0;
1949}
1950
1951/*
1952 * archs can override this function if they must do something
1953 * after the modifying code is performed.
1954 */
1955int __weak ftrace_arch_code_modify_post_process(void)
1956{
1957 return 0;
1958}
1959
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001960void ftrace_modify_all_code(int command)
1961{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001962 int update = command & FTRACE_UPDATE_TRACE_FUNC;
1963
1964 /*
1965 * If the ftrace_caller calls a ftrace_ops func directly,
1966 * we need to make sure that it only traces functions it
1967 * expects to trace. When doing the switch of functions,
1968 * we need to update to the ftrace_ops_list_func first
1969 * before the transition between old and new calls are set,
1970 * as the ftrace_ops_list_func will check the ops hashes
1971 * to make sure the ops are having the right functions
1972 * traced.
1973 */
1974 if (update)
1975 ftrace_update_ftrace_func(ftrace_ops_list_func);
1976
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001977 if (command & FTRACE_UPDATE_CALLS)
1978 ftrace_replace_code(1);
1979 else if (command & FTRACE_DISABLE_CALLS)
1980 ftrace_replace_code(0);
1981
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001982 if (update && ftrace_trace_function != ftrace_ops_list_func)
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001983 ftrace_update_ftrace_func(ftrace_trace_function);
1984
1985 if (command & FTRACE_START_FUNC_RET)
1986 ftrace_enable_ftrace_graph_caller();
1987 else if (command & FTRACE_STOP_FUNC_RET)
1988 ftrace_disable_ftrace_graph_caller();
1989}
1990
Ingo Molnare309b412008-05-12 21:20:51 +02001991static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001992{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001993 int *command = data;
1994
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001995 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001996
Steven Rostedtc88fd862011-08-16 09:53:39 -04001997 return 0;
1998}
1999
2000/**
2001 * ftrace_run_stop_machine, go back to the stop machine method
2002 * @command: The command to tell ftrace what to do
2003 *
2004 * If an arch needs to fall back to the stop machine method, the
2005 * it can call this function.
2006 */
2007void ftrace_run_stop_machine(int command)
2008{
2009 stop_machine(__ftrace_modify_code, &command, NULL);
2010}
2011
2012/**
2013 * arch_ftrace_update_code, modify the code to trace or not trace
2014 * @command: The command that needs to be done
2015 *
2016 * Archs can override this function if it does not need to
2017 * run stop_machine() to modify code.
2018 */
2019void __weak arch_ftrace_update_code(int command)
2020{
2021 ftrace_run_stop_machine(command);
2022}
2023
2024static void ftrace_run_update_code(int command)
2025{
2026 int ret;
2027
2028 ret = ftrace_arch_code_modify_prepare();
2029 FTRACE_WARN_ON(ret);
2030 if (ret)
2031 return;
2032 /*
2033 * Do not call function tracer while we update the code.
2034 * We are in stop machine.
2035 */
2036 function_trace_stop++;
2037
2038 /*
2039 * By default we use stop_machine() to modify the code.
2040 * But archs can do what ever they want as long as it
2041 * is safe. The stop_machine() is the safest, but also
2042 * produces the most overhead.
2043 */
2044 arch_ftrace_update_code(command);
2045
Steven Rostedt6331c282011-07-13 15:11:02 -04002046 function_trace_stop--;
2047
Steven Rostedt000ab692009-02-17 13:35:06 -05002048 ret = ftrace_arch_code_modify_post_process();
2049 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002050}
2051
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002052static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002053static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002054static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002055
2056static void ftrace_startup_enable(int command)
2057{
2058 if (saved_ftrace_func != ftrace_trace_function) {
2059 saved_ftrace_func = ftrace_trace_function;
2060 command |= FTRACE_UPDATE_TRACE_FUNC;
2061 }
2062
2063 if (!command || !ftrace_enabled)
2064 return;
2065
2066 ftrace_run_update_code(command);
2067}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002068
Steven Rostedta1cd6172011-05-23 15:24:25 -04002069static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002070{
Steven Rostedtb8489142011-05-04 09:27:52 -04002071 bool hash_enable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002072 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002073
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002074 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002075 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002076
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002077 ret = __register_ftrace_function(ops);
2078 if (ret)
2079 return ret;
2080
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002081 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002082 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002083
Steven Rostedtb8489142011-05-04 09:27:52 -04002084 /* ops marked global share the filter hashes */
2085 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2086 ops = &global_ops;
2087 /* Don't update hash if global is already set */
2088 if (global_start_up)
2089 hash_enable = false;
2090 global_start_up++;
2091 }
2092
Steven Rostedted926f92011-05-03 13:25:24 -04002093 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002094 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002095 ftrace_hash_rec_enable(ops, 1);
2096
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002097 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002098
2099 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002100}
2101
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002102static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002103{
Steven Rostedtb8489142011-05-04 09:27:52 -04002104 bool hash_disable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002105 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002106
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002107 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002108 return -ENODEV;
2109
2110 ret = __unregister_ftrace_function(ops);
2111 if (ret)
2112 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002113
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002114 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002115 /*
2116 * Just warn in case of unbalance, no need to kill ftrace, it's not
2117 * critical but the ftrace_call callers may be never nopped again after
2118 * further ftrace uses.
2119 */
2120 WARN_ON_ONCE(ftrace_start_up < 0);
2121
Steven Rostedtb8489142011-05-04 09:27:52 -04002122 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2123 ops = &global_ops;
2124 global_start_up--;
2125 WARN_ON_ONCE(global_start_up < 0);
2126 /* Don't update hash if global still has users */
2127 if (global_start_up) {
2128 WARN_ON_ONCE(!ftrace_start_up);
2129 hash_disable = false;
2130 }
2131 }
2132
2133 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002134 ftrace_hash_rec_disable(ops, 1);
2135
Steven Rostedtb8489142011-05-04 09:27:52 -04002136 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002137 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002138
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002139 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002140
2141 if (saved_ftrace_func != ftrace_trace_function) {
2142 saved_ftrace_func = ftrace_trace_function;
2143 command |= FTRACE_UPDATE_TRACE_FUNC;
2144 }
2145
2146 if (!command || !ftrace_enabled)
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002147 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002148
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002149 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002150 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002151}
2152
Ingo Molnare309b412008-05-12 21:20:51 +02002153static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002154{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002155 if (unlikely(ftrace_disabled))
2156 return;
2157
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002158 /* Force update next time */
2159 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002160 /* ftrace_start_up is true if we want ftrace running */
2161 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002162 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002163}
2164
Ingo Molnare309b412008-05-12 21:20:51 +02002165static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002166{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002167 if (unlikely(ftrace_disabled))
2168 return;
2169
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002170 /* ftrace_start_up is true if ftrace is running */
2171 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002172 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002173}
2174
Steven Rostedt3d083392008-05-12 21:20:42 +02002175static cycle_t ftrace_update_time;
2176static unsigned long ftrace_update_cnt;
2177unsigned long ftrace_update_tot_cnt;
2178
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002179static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002180{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002181 /*
2182 * Filter_hash being empty will default to trace module.
2183 * But notrace hash requires a test of individual module functions.
2184 */
2185 return ftrace_hash_empty(ops->filter_hash) &&
2186 ftrace_hash_empty(ops->notrace_hash);
2187}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002188
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002189/*
2190 * Check if the current ops references the record.
2191 *
2192 * If the ops traces all functions, then it was already accounted for.
2193 * If the ops does not trace the current record function, skip it.
2194 * If the ops ignores the function via notrace filter, skip it.
2195 */
2196static inline bool
2197ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2198{
2199 /* If ops isn't enabled, ignore it */
2200 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2201 return 0;
2202
2203 /* If ops traces all mods, we already accounted for it */
2204 if (ops_traces_mod(ops))
2205 return 0;
2206
2207 /* The function must be in the filter */
2208 if (!ftrace_hash_empty(ops->filter_hash) &&
2209 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2210 return 0;
2211
2212 /* If in notrace hash, we ignore it too */
2213 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2214 return 0;
2215
2216 return 1;
2217}
2218
2219static int referenced_filters(struct dyn_ftrace *rec)
2220{
2221 struct ftrace_ops *ops;
2222 int cnt = 0;
2223
2224 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2225 if (ops_references_rec(ops, rec))
2226 cnt++;
2227 }
2228
2229 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002230}
2231
Steven Rostedt31e88902008-11-14 16:21:19 -08002232static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002233{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002234 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002235 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302236 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002237 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002238 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002239 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002240
2241 /*
2242 * When adding a module, we need to check if tracers are
2243 * currently enabled and if they are set to trace all functions.
2244 * If they are, we need to enable the module functions as well
2245 * as update the reference counts for those function records.
2246 */
2247 if (mod) {
2248 struct ftrace_ops *ops;
2249
2250 for (ops = ftrace_ops_list;
2251 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002252 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2253 if (ops_traces_mod(ops))
2254 ref++;
2255 else
2256 test = true;
2257 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002258 }
2259 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002260
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002261 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002262 ftrace_update_cnt = 0;
2263
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002264 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302265
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002266 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002267 int cnt = ref;
2268
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002269 /* If something went wrong, bail without enabling anything */
2270 if (unlikely(ftrace_disabled))
2271 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002272
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002273 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002274 if (test)
2275 cnt += referenced_filters(p);
2276 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302277
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002278 /*
2279 * Do the initial record conversion from mcount jump
2280 * to the NOP instructions.
2281 */
2282 if (!ftrace_code_disable(mod, p))
2283 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002284
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002285 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002286
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002287 /*
2288 * If the tracing is enabled, go ahead and enable the record.
2289 *
2290 * The reason not to enable the record immediatelly is the
2291 * inherent check of ftrace_make_nop/ftrace_make_call for
2292 * correct previous instructions. Making first the NOP
2293 * conversion puts the module to the correct state, thus
2294 * passing the ftrace_make_call check.
2295 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002296 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002297 int failed = __ftrace_replace_code(p, 1);
2298 if (failed)
2299 ftrace_bug(failed, p->ip);
2300 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002301 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002302 }
2303
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002304 ftrace_new_pgs = NULL;
2305
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002306 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002307 ftrace_update_time = stop - start;
2308 ftrace_update_tot_cnt += ftrace_update_cnt;
2309
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002310 return 0;
2311}
2312
Steven Rostedta7900872011-12-16 16:23:44 -05002313static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002314{
Steven Rostedta7900872011-12-16 16:23:44 -05002315 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002316 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002317
Steven Rostedta7900872011-12-16 16:23:44 -05002318 if (WARN_ON(!count))
2319 return -EINVAL;
2320
2321 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002322
2323 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002324 * We want to fill as much as possible. No more than a page
2325 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002326 */
Steven Rostedta7900872011-12-16 16:23:44 -05002327 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2328 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002329
Steven Rostedta7900872011-12-16 16:23:44 -05002330 again:
2331 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2332
2333 if (!pg->records) {
2334 /* if we can't allocate this size, try something smaller */
2335 if (!order)
2336 return -ENOMEM;
2337 order >>= 1;
2338 goto again;
2339 }
2340
2341 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2342 pg->size = cnt;
2343
2344 if (cnt > count)
2345 cnt = count;
2346
2347 return cnt;
2348}
2349
2350static struct ftrace_page *
2351ftrace_allocate_pages(unsigned long num_to_init)
2352{
2353 struct ftrace_page *start_pg;
2354 struct ftrace_page *pg;
2355 int order;
2356 int cnt;
2357
2358 if (!num_to_init)
2359 return 0;
2360
2361 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2362 if (!pg)
2363 return NULL;
2364
2365 /*
2366 * Try to allocate as much as possible in one continues
2367 * location that fills in all of the space. We want to
2368 * waste as little space as possible.
2369 */
2370 for (;;) {
2371 cnt = ftrace_allocate_records(pg, num_to_init);
2372 if (cnt < 0)
2373 goto free_pages;
2374
2375 num_to_init -= cnt;
2376 if (!num_to_init)
2377 break;
2378
2379 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2380 if (!pg->next)
2381 goto free_pages;
2382
2383 pg = pg->next;
2384 }
2385
2386 return start_pg;
2387
2388 free_pages:
2389 while (start_pg) {
2390 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2391 free_pages((unsigned long)pg->records, order);
2392 start_pg = pg->next;
2393 kfree(pg);
2394 pg = start_pg;
2395 }
2396 pr_info("ftrace: FAILED to allocate memory for functions\n");
2397 return NULL;
2398}
2399
2400static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2401{
2402 int cnt;
2403
2404 if (!num_to_init) {
2405 pr_info("ftrace: No functions to be traced?\n");
2406 return -1;
2407 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002408
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002409 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002410 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002411 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002412
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002413 return 0;
2414}
2415
Steven Rostedt5072c592008-05-12 21:20:43 +02002416#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2417
2418struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002419 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002420 loff_t func_pos;
2421 struct ftrace_page *pg;
2422 struct dyn_ftrace *func;
2423 struct ftrace_func_probe *probe;
2424 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002425 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002426 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002427 int hidx;
2428 int idx;
2429 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002430};
2431
Ingo Molnare309b412008-05-12 21:20:51 +02002432static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002433t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002434{
2435 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002436 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002437 struct hlist_head *hhd;
2438
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002439 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002440 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002441
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002442 if (iter->probe)
2443 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002444 retry:
2445 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2446 return NULL;
2447
2448 hhd = &ftrace_func_hash[iter->hidx];
2449
2450 if (hlist_empty(hhd)) {
2451 iter->hidx++;
2452 hnd = NULL;
2453 goto retry;
2454 }
2455
2456 if (!hnd)
2457 hnd = hhd->first;
2458 else {
2459 hnd = hnd->next;
2460 if (!hnd) {
2461 iter->hidx++;
2462 goto retry;
2463 }
2464 }
2465
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002466 if (WARN_ON_ONCE(!hnd))
2467 return NULL;
2468
2469 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2470
2471 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002472}
2473
2474static void *t_hash_start(struct seq_file *m, loff_t *pos)
2475{
2476 struct ftrace_iterator *iter = m->private;
2477 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002478 loff_t l;
2479
Steven Rostedt69a30832011-12-19 15:21:16 -05002480 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2481 return NULL;
2482
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002483 if (iter->func_pos > *pos)
2484 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002485
Li Zefand82d6242009-06-24 09:54:54 +08002486 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002487 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002488 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002489 if (!p)
2490 break;
2491 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002492 if (!p)
2493 return NULL;
2494
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002495 /* Only set this if we have an item */
2496 iter->flags |= FTRACE_ITER_HASH;
2497
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002498 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002499}
2500
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002501static int
2502t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002503{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002504 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002505
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002506 rec = iter->probe;
2507 if (WARN_ON_ONCE(!rec))
2508 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002509
Steven Rostedt809dcf22009-02-16 23:06:01 -05002510 if (rec->ops->print)
2511 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2512
Steven Rostedtb375a112009-09-17 00:05:58 -04002513 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002514
2515 if (rec->data)
2516 seq_printf(m, ":%p", rec->data);
2517 seq_putc(m, '\n');
2518
2519 return 0;
2520}
2521
2522static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002523t_next(struct seq_file *m, void *v, loff_t *pos)
2524{
2525 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002526 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002527 struct dyn_ftrace *rec = NULL;
2528
Steven Rostedt45a4a232011-04-21 23:16:46 -04002529 if (unlikely(ftrace_disabled))
2530 return NULL;
2531
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002532 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002533 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002534
Steven Rostedt5072c592008-05-12 21:20:43 +02002535 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002536 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002537
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002538 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002539 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002540
Steven Rostedt5072c592008-05-12 21:20:43 +02002541 retry:
2542 if (iter->idx >= iter->pg->index) {
2543 if (iter->pg->next) {
2544 iter->pg = iter->pg->next;
2545 iter->idx = 0;
2546 goto retry;
2547 }
2548 } else {
2549 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002550 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002551 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002552
Steven Rostedt41c52c02008-05-22 11:46:33 -04002553 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002554 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2555
2556 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002557 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002558
Steven Rostedt5072c592008-05-12 21:20:43 +02002559 rec = NULL;
2560 goto retry;
2561 }
2562 }
2563
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002564 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002565 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002566
2567 iter->func = rec;
2568
2569 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002570}
2571
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002572static void reset_iter_read(struct ftrace_iterator *iter)
2573{
2574 iter->pos = 0;
2575 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002576 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002577}
2578
2579static void *t_start(struct seq_file *m, loff_t *pos)
2580{
2581 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002582 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002583 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002584 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002585
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002586 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002587
2588 if (unlikely(ftrace_disabled))
2589 return NULL;
2590
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002591 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002592 * If an lseek was done, then reset and start from beginning.
2593 */
2594 if (*pos < iter->pos)
2595 reset_iter_read(iter);
2596
2597 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002598 * For set_ftrace_filter reading, if we have the filter
2599 * off, we can short cut and just print out that all
2600 * functions are enabled.
2601 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002602 if (iter->flags & FTRACE_ITER_FILTER &&
2603 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002604 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002605 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002606 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002607 /* reset in case of seek/pread */
2608 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002609 return iter;
2610 }
2611
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002612 if (iter->flags & FTRACE_ITER_HASH)
2613 return t_hash_start(m, pos);
2614
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002615 /*
2616 * Unfortunately, we need to restart at ftrace_pages_start
2617 * every time we let go of the ftrace_mutex. This is because
2618 * those pointers can change without the lock.
2619 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002620 iter->pg = ftrace_pages_start;
2621 iter->idx = 0;
2622 for (l = 0; l <= *pos; ) {
2623 p = t_next(m, p, &l);
2624 if (!p)
2625 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002626 }
walimis5821e1b2008-11-15 15:19:06 +08002627
Steven Rostedt69a30832011-12-19 15:21:16 -05002628 if (!p)
2629 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002630
2631 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002632}
2633
2634static void t_stop(struct seq_file *m, void *p)
2635{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002636 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002637}
2638
2639static int t_show(struct seq_file *m, void *v)
2640{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002641 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002642 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002643
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002644 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002645 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002646
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002647 if (iter->flags & FTRACE_ITER_PRINTALL) {
2648 seq_printf(m, "#### all functions enabled ####\n");
2649 return 0;
2650 }
2651
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002652 rec = iter->func;
2653
Steven Rostedt5072c592008-05-12 21:20:43 +02002654 if (!rec)
2655 return 0;
2656
Steven Rostedt647bcd02011-05-03 14:39:21 -04002657 seq_printf(m, "%ps", (void *)rec->ip);
2658 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002659 seq_printf(m, " (%ld)%s",
2660 rec->flags & ~FTRACE_FL_MASK,
2661 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002662 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002663
2664 return 0;
2665}
2666
James Morris88e9d342009-09-22 16:43:43 -07002667static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002668 .start = t_start,
2669 .next = t_next,
2670 .stop = t_stop,
2671 .show = t_show,
2672};
2673
Ingo Molnare309b412008-05-12 21:20:51 +02002674static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002675ftrace_avail_open(struct inode *inode, struct file *file)
2676{
2677 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002678
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002679 if (unlikely(ftrace_disabled))
2680 return -ENODEV;
2681
Jiri Olsa50e18b92012-04-25 10:23:39 +02002682 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2683 if (iter) {
2684 iter->pg = ftrace_pages_start;
2685 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002686 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002687
Jiri Olsa50e18b92012-04-25 10:23:39 +02002688 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002689}
2690
Steven Rostedt647bcd02011-05-03 14:39:21 -04002691static int
2692ftrace_enabled_open(struct inode *inode, struct file *file)
2693{
2694 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002695
2696 if (unlikely(ftrace_disabled))
2697 return -ENODEV;
2698
Jiri Olsa50e18b92012-04-25 10:23:39 +02002699 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2700 if (iter) {
2701 iter->pg = ftrace_pages_start;
2702 iter->flags = FTRACE_ITER_ENABLED;
2703 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002704 }
2705
Jiri Olsa50e18b92012-04-25 10:23:39 +02002706 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002707}
2708
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002709static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002710{
Steven Rostedt52baf112009-02-14 01:15:39 -05002711 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002712 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002713 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002714}
2715
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002716/**
2717 * ftrace_regex_open - initialize function tracer filter files
2718 * @ops: The ftrace_ops that hold the hash filters
2719 * @flag: The type of filter to process
2720 * @inode: The inode, usually passed in to your open routine
2721 * @file: The file, usually passed in to your open routine
2722 *
2723 * ftrace_regex_open() initializes the filter files for the
2724 * @ops. Depending on @flag it may process the filter hash or
2725 * the notrace hash of @ops. With this called from the open
2726 * routine, you can use ftrace_filter_write() for the write
2727 * routine if @flag has FTRACE_ITER_FILTER set, or
2728 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05002729 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002730 * release must call ftrace_regex_release().
2731 */
2732int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002733ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002734 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002735{
2736 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002737 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002738 int ret = 0;
2739
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002740 ftrace_ops_init(ops);
2741
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002742 if (unlikely(ftrace_disabled))
2743 return -ENODEV;
2744
Steven Rostedt5072c592008-05-12 21:20:43 +02002745 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2746 if (!iter)
2747 return -ENOMEM;
2748
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002749 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2750 kfree(iter);
2751 return -ENOMEM;
2752 }
2753
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002754 iter->ops = ops;
2755 iter->flags = flag;
2756
2757 mutex_lock(&ops->regex_lock);
2758
Steven Rostedtf45948e2011-05-02 12:29:25 -04002759 if (flag & FTRACE_ITER_NOTRACE)
2760 hash = ops->notrace_hash;
2761 else
2762 hash = ops->filter_hash;
2763
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002764 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002765 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002766 if (!iter->hash) {
2767 trace_parser_put(&iter->parser);
2768 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002769 ret = -ENOMEM;
2770 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002771 }
2772 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002773
Steven Rostedt5072c592008-05-12 21:20:43 +02002774 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002775 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002776 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002777
2778 if (file->f_mode & FMODE_READ) {
2779 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002780
2781 ret = seq_open(file, &show_ftrace_seq_ops);
2782 if (!ret) {
2783 struct seq_file *m = file->private_data;
2784 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002785 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002786 /* Failed */
2787 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002788 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002789 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002790 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002791 } else
2792 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002793
2794 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002795 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002796
2797 return ret;
2798}
2799
Steven Rostedt41c52c02008-05-22 11:46:33 -04002800static int
2801ftrace_filter_open(struct inode *inode, struct file *file)
2802{
Steven Rostedt69a30832011-12-19 15:21:16 -05002803 return ftrace_regex_open(&global_ops,
2804 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2805 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002806}
2807
2808static int
2809ftrace_notrace_open(struct inode *inode, struct file *file)
2810{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002811 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002812 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002813}
2814
Steven Rostedt64e7c442009-02-13 17:08:48 -05002815static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002816{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002817 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002818 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002819
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002820 switch (type) {
2821 case MATCH_FULL:
2822 if (strcmp(str, regex) == 0)
2823 matched = 1;
2824 break;
2825 case MATCH_FRONT_ONLY:
2826 if (strncmp(str, regex, len) == 0)
2827 matched = 1;
2828 break;
2829 case MATCH_MIDDLE_ONLY:
2830 if (strstr(str, regex))
2831 matched = 1;
2832 break;
2833 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002834 slen = strlen(str);
2835 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002836 matched = 1;
2837 break;
2838 }
2839
2840 return matched;
2841}
2842
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002843static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002844enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002845{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002846 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002847 int ret = 0;
2848
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002849 entry = ftrace_lookup_ip(hash, rec->ip);
2850 if (not) {
2851 /* Do nothing if it doesn't exist */
2852 if (!entry)
2853 return 0;
2854
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002855 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002856 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002857 /* Do nothing if it exists */
2858 if (entry)
2859 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002860
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002861 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002862 }
2863 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002864}
2865
Steven Rostedt64e7c442009-02-13 17:08:48 -05002866static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002867ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2868 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002869{
2870 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002871 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002872
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002873 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2874
2875 if (mod) {
2876 /* module lookup requires matching the module */
2877 if (!modname || strcmp(modname, mod))
2878 return 0;
2879
2880 /* blank search means to match all funcs in the mod */
2881 if (!len)
2882 return 1;
2883 }
2884
Steven Rostedt64e7c442009-02-13 17:08:48 -05002885 return ftrace_match(str, regex, len, type);
2886}
2887
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002888static int
2889match_records(struct ftrace_hash *hash, char *buff,
2890 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002891{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002892 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002893 struct ftrace_page *pg;
2894 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002895 int type = MATCH_FULL;
2896 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002897 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002898 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002899
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002900 if (len) {
2901 type = filter_parse_regex(buff, len, &search, &not);
2902 search_len = strlen(search);
2903 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002904
Steven Rostedt52baf112009-02-14 01:15:39 -05002905 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002906
2907 if (unlikely(ftrace_disabled))
2908 goto out_unlock;
2909
Steven Rostedt265c8312009-02-13 12:43:56 -05002910 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002911 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002912 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002913 if (ret < 0) {
2914 found = ret;
2915 goto out_unlock;
2916 }
Li Zefan311d16d2009-12-08 11:15:11 +08002917 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002918 }
2919 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002920 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002921 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002922
2923 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002924}
2925
Steven Rostedt64e7c442009-02-13 17:08:48 -05002926static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002927ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002928{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002929 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002930}
2931
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002932static int
2933ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002934{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002935 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002936
Steven Rostedt64e7c442009-02-13 17:08:48 -05002937 /* blank or '*' mean the same */
2938 if (strcmp(buff, "*") == 0)
2939 buff[0] = 0;
2940
2941 /* handle the case of 'dont filter this module' */
2942 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2943 buff[0] = 0;
2944 not = 1;
2945 }
2946
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002947 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002948}
2949
Steven Rostedtf6180772009-02-14 00:40:25 -05002950/*
2951 * We register the module command as a template to show others how
2952 * to register the a command as well.
2953 */
2954
2955static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002956ftrace_mod_callback(struct ftrace_hash *hash,
2957 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002958{
2959 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002960 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002961
2962 /*
2963 * cmd == 'mod' because we only registered this func
2964 * for the 'mod' ftrace_func_command.
2965 * But if you register one func with multiple commands,
2966 * you can tell which command was used by the cmd
2967 * parameter.
2968 */
2969
2970 /* we must have a module name */
2971 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002972 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002973
2974 mod = strsep(&param, ":");
2975 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002976 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002977
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002978 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002979 if (!ret)
2980 ret = -EINVAL;
2981 if (ret < 0)
2982 return ret;
2983
2984 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002985}
2986
2987static struct ftrace_func_command ftrace_mod_cmd = {
2988 .name = "mod",
2989 .func = ftrace_mod_callback,
2990};
2991
2992static int __init ftrace_mod_cmd_init(void)
2993{
2994 return register_ftrace_command(&ftrace_mod_cmd);
2995}
Steven Rostedt6f415672012-10-05 12:13:07 -04002996core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002997
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002998static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002999 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003000{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003001 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003002 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003003 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003004
3005 key = hash_long(ip, FTRACE_HASH_BITS);
3006
3007 hhd = &ftrace_func_hash[key];
3008
3009 if (hlist_empty(hhd))
3010 return;
3011
3012 /*
3013 * Disable preemption for these calls to prevent a RCU grace
3014 * period. This syncs the hash iteration and freeing of items
3015 * on the hash. rcu_read_lock is too dangerous here.
3016 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003017 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003018 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003019 if (entry->ip == ip)
3020 entry->ops->func(ip, parent_ip, &entry->data);
3021 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003022 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003023}
3024
Steven Rostedtb6887d72009-02-17 12:32:04 -05003025static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003026{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003027 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003028 .flags = FTRACE_OPS_FL_INITIALIZED,
3029 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003030};
3031
Steven Rostedtb6887d72009-02-17 12:32:04 -05003032static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003033
Steven Rostedtb6887d72009-02-17 12:32:04 -05003034static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003035{
Steven Rostedtb8489142011-05-04 09:27:52 -04003036 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003037 int i;
3038
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003039 if (ftrace_probe_registered) {
3040 /* still need to update the function call sites */
3041 if (ftrace_enabled)
3042 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003043 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003044 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003045
3046 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3047 struct hlist_head *hhd = &ftrace_func_hash[i];
3048 if (hhd->first)
3049 break;
3050 }
3051 /* Nothing registered? */
3052 if (i == FTRACE_FUNC_HASHSIZE)
3053 return;
3054
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003055 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003056
Steven Rostedtb6887d72009-02-17 12:32:04 -05003057 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003058}
3059
Steven Rostedtb6887d72009-02-17 12:32:04 -05003060static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003061{
3062 int i;
3063
Steven Rostedtb6887d72009-02-17 12:32:04 -05003064 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003065 return;
3066
3067 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3068 struct hlist_head *hhd = &ftrace_func_hash[i];
3069 if (hhd->first)
3070 return;
3071 }
3072
3073 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003074 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003075
Steven Rostedtb6887d72009-02-17 12:32:04 -05003076 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003077}
3078
3079
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003080static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003081{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003082 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003083 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003084 kfree(entry);
3085}
3086
Steven Rostedt59df055f2009-02-14 15:29:06 -05003087int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003088register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003089 void *data)
3090{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003091 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003092 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3093 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003094 struct ftrace_page *pg;
3095 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003096 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003097 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003098 int count = 0;
3099 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003100 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003101
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003102 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003103 len = strlen(search);
3104
Steven Rostedtb6887d72009-02-17 12:32:04 -05003105 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003106 if (WARN_ON(not))
3107 return -EINVAL;
3108
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003109 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003110
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003111 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3112 if (!hash) {
3113 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003114 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003115 }
3116
3117 if (unlikely(ftrace_disabled)) {
3118 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003119 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003120 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003121
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003122 mutex_lock(&ftrace_lock);
3123
Steven Rostedt59df055f2009-02-14 15:29:06 -05003124 do_for_each_ftrace_rec(pg, rec) {
3125
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003126 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003127 continue;
3128
3129 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3130 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003131 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003132 if (!count)
3133 count = -ENOMEM;
3134 goto out_unlock;
3135 }
3136
3137 count++;
3138
3139 entry->data = data;
3140
3141 /*
3142 * The caller might want to do something special
3143 * for each function we find. We call the callback
3144 * to give the caller an opportunity to do so.
3145 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003146 if (ops->init) {
3147 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003148 /* caller does not like this func */
3149 kfree(entry);
3150 continue;
3151 }
3152 }
3153
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003154 ret = enter_record(hash, rec, 0);
3155 if (ret < 0) {
3156 kfree(entry);
3157 count = ret;
3158 goto out_unlock;
3159 }
3160
Steven Rostedt59df055f2009-02-14 15:29:06 -05003161 entry->ops = ops;
3162 entry->ip = rec->ip;
3163
3164 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3165 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3166
3167 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003168
3169 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3170 if (ret < 0)
3171 count = ret;
3172
Steven Rostedtb6887d72009-02-17 12:32:04 -05003173 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003174
3175 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003176 mutex_unlock(&ftrace_lock);
3177 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003178 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003179 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003180
3181 return count;
3182}
3183
3184enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003185 PROBE_TEST_FUNC = 1,
3186 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003187};
3188
3189static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003190__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003191 void *data, int flags)
3192{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003193 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003194 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003195 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003196 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003197 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003198 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003199 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003200 char str[KSYM_SYMBOL_LEN];
3201 int type = MATCH_FULL;
3202 int i, len = 0;
3203 char *search;
3204
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003205 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003206 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003207 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003208 int not;
3209
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003210 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003211 len = strlen(search);
3212
Steven Rostedtb6887d72009-02-17 12:32:04 -05003213 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003214 if (WARN_ON(not))
3215 return;
3216 }
3217
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003218 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003219
3220 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3221 if (!hash)
3222 /* Hmm, should report this somehow */
3223 goto out_unlock;
3224
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003225 INIT_LIST_HEAD(&free_list);
3226
Steven Rostedt59df055f2009-02-14 15:29:06 -05003227 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3228 struct hlist_head *hhd = &ftrace_func_hash[i];
3229
Sasha Levinb67bfe02013-02-27 17:06:00 -08003230 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003231
3232 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003233 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003234 continue;
3235
Steven Rostedtb6887d72009-02-17 12:32:04 -05003236 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003237 continue;
3238
3239 /* do this last, since it is the most expensive */
3240 if (glob) {
3241 kallsyms_lookup(entry->ip, NULL, NULL,
3242 NULL, str);
3243 if (!ftrace_match(str, glob, len, type))
3244 continue;
3245 }
3246
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003247 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3248 /* It is possible more than one entry had this ip */
3249 if (rec_entry)
3250 free_hash_entry(hash, rec_entry);
3251
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003252 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003253 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003254 }
3255 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003256 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003257 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003258 /*
3259 * Remove after the disable is called. Otherwise, if the last
3260 * probe is removed, a null hash means *all enabled*.
3261 */
3262 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003263 synchronize_sched();
3264 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3265 list_del(&entry->free_list);
3266 ftrace_free_entry(entry);
3267 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003268 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003269
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003270 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003271 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003272 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003273}
3274
3275void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003276unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003277 void *data)
3278{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003279 __unregister_ftrace_function_probe(glob, ops, data,
3280 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003281}
3282
3283void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003284unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003285{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003286 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003287}
3288
Steven Rostedtb6887d72009-02-17 12:32:04 -05003289void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003290{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003291 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003292}
3293
Steven Rostedtf6180772009-02-14 00:40:25 -05003294static LIST_HEAD(ftrace_commands);
3295static DEFINE_MUTEX(ftrace_cmd_mutex);
3296
Tom Zanussi38de93a2013-10-24 08:34:18 -05003297/*
3298 * Currently we only register ftrace commands from __init, so mark this
3299 * __init too.
3300 */
3301__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003302{
3303 struct ftrace_func_command *p;
3304 int ret = 0;
3305
3306 mutex_lock(&ftrace_cmd_mutex);
3307 list_for_each_entry(p, &ftrace_commands, list) {
3308 if (strcmp(cmd->name, p->name) == 0) {
3309 ret = -EBUSY;
3310 goto out_unlock;
3311 }
3312 }
3313 list_add(&cmd->list, &ftrace_commands);
3314 out_unlock:
3315 mutex_unlock(&ftrace_cmd_mutex);
3316
3317 return ret;
3318}
3319
Tom Zanussi38de93a2013-10-24 08:34:18 -05003320/*
3321 * Currently we only unregister ftrace commands from __init, so mark
3322 * this __init too.
3323 */
3324__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003325{
3326 struct ftrace_func_command *p, *n;
3327 int ret = -ENODEV;
3328
3329 mutex_lock(&ftrace_cmd_mutex);
3330 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3331 if (strcmp(cmd->name, p->name) == 0) {
3332 ret = 0;
3333 list_del_init(&p->list);
3334 goto out_unlock;
3335 }
3336 }
3337 out_unlock:
3338 mutex_unlock(&ftrace_cmd_mutex);
3339
3340 return ret;
3341}
3342
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003343static int ftrace_process_regex(struct ftrace_hash *hash,
3344 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003345{
Steven Rostedtf6180772009-02-14 00:40:25 -05003346 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003347 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003348 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003349
3350 func = strsep(&next, ":");
3351
3352 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003353 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003354 if (!ret)
3355 ret = -EINVAL;
3356 if (ret < 0)
3357 return ret;
3358 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003359 }
3360
Steven Rostedtf6180772009-02-14 00:40:25 -05003361 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003362
3363 command = strsep(&next, ":");
3364
Steven Rostedtf6180772009-02-14 00:40:25 -05003365 mutex_lock(&ftrace_cmd_mutex);
3366 list_for_each_entry(p, &ftrace_commands, list) {
3367 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003368 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003369 goto out_unlock;
3370 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003371 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003372 out_unlock:
3373 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003374
Steven Rostedtf6180772009-02-14 00:40:25 -05003375 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003376}
3377
Ingo Molnare309b412008-05-12 21:20:51 +02003378static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003379ftrace_regex_write(struct file *file, const char __user *ubuf,
3380 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003381{
3382 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003383 struct trace_parser *parser;
3384 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003385
Li Zefan4ba79782009-09-22 13:52:20 +08003386 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003387 return 0;
3388
Steven Rostedt5072c592008-05-12 21:20:43 +02003389 if (file->f_mode & FMODE_READ) {
3390 struct seq_file *m = file->private_data;
3391 iter = m->private;
3392 } else
3393 iter = file->private_data;
3394
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003395 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003396 return -ENODEV;
3397
3398 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003399
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003400 parser = &iter->parser;
3401 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003402
Li Zefan4ba79782009-09-22 13:52:20 +08003403 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003404 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003405 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003406 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003407 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003408 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003409 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003410 }
3411
Steven Rostedt5072c592008-05-12 21:20:43 +02003412 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003413 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003414 return ret;
3415}
3416
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003417ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003418ftrace_filter_write(struct file *file, const char __user *ubuf,
3419 size_t cnt, loff_t *ppos)
3420{
3421 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3422}
3423
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003424ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003425ftrace_notrace_write(struct file *file, const char __user *ubuf,
3426 size_t cnt, loff_t *ppos)
3427{
3428 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3429}
3430
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003431static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003432ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3433{
3434 struct ftrace_func_entry *entry;
3435
3436 if (!ftrace_location(ip))
3437 return -EINVAL;
3438
3439 if (remove) {
3440 entry = ftrace_lookup_ip(hash, ip);
3441 if (!entry)
3442 return -ENOENT;
3443 free_hash_entry(hash, entry);
3444 return 0;
3445 }
3446
3447 return add_hash_entry(hash, ip);
3448}
3449
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003450static void ftrace_ops_update_code(struct ftrace_ops *ops)
3451{
3452 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3453 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3454}
3455
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003456static int
3457ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3458 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003459{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003460 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003461 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003462 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003463
Steven Rostedt936e0742011-05-05 22:54:01 -04003464 /* All global ops uses the global ops filters */
3465 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3466 ops = &global_ops;
3467
Steven Rostedt41c52c02008-05-22 11:46:33 -04003468 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003469 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003470
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003471 mutex_lock(&ops->regex_lock);
3472
Steven Rostedtf45948e2011-05-02 12:29:25 -04003473 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003474 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003475 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003476 orig_hash = &ops->notrace_hash;
3477
3478 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003479 if (!hash) {
3480 ret = -ENOMEM;
3481 goto out_regex_unlock;
3482 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003483
Steven Rostedt41c52c02008-05-22 11:46:33 -04003484 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003485 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003486 if (buf && !ftrace_match_records(hash, buf, len)) {
3487 ret = -EINVAL;
3488 goto out_regex_unlock;
3489 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003490 if (ip) {
3491 ret = ftrace_match_addr(hash, ip, remove);
3492 if (ret < 0)
3493 goto out_regex_unlock;
3494 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003495
3496 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003497 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003498 if (!ret)
3499 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003500
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003501 mutex_unlock(&ftrace_lock);
3502
Jiri Olsaac483c42012-01-02 10:04:14 +01003503 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003504 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003505
3506 free_ftrace_hash(hash);
3507 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003508}
3509
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003510static int
3511ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3512 int reset, int enable)
3513{
3514 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3515}
3516
3517/**
3518 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3519 * @ops - the ops to set the filter with
3520 * @ip - the address to add to or remove from the filter.
3521 * @remove - non zero to remove the ip from the filter
3522 * @reset - non zero to reset all filters before applying this filter.
3523 *
3524 * Filters denote which functions should be enabled when tracing is enabled
3525 * If @ip is NULL, it failes to update filter.
3526 */
3527int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3528 int remove, int reset)
3529{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003530 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003531 return ftrace_set_addr(ops, ip, remove, reset, 1);
3532}
3533EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3534
3535static int
3536ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3537 int reset, int enable)
3538{
3539 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3540}
3541
Steven Rostedt77a2b372008-05-12 21:20:45 +02003542/**
3543 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003544 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003545 * @buf - the string that holds the function filter text.
3546 * @len - the length of the string.
3547 * @reset - non zero to reset all filters before applying this filter.
3548 *
3549 * Filters denote which functions should be enabled when tracing is enabled.
3550 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3551 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003552int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003553 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003554{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003555 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003556 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003557}
Steven Rostedt936e0742011-05-05 22:54:01 -04003558EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003559
Steven Rostedt41c52c02008-05-22 11:46:33 -04003560/**
3561 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003562 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003563 * @buf - the string that holds the function notrace text.
3564 * @len - the length of the string.
3565 * @reset - non zero to reset all filters before applying this filter.
3566 *
3567 * Notrace Filters denote which functions should not be enabled when tracing
3568 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3569 * for tracing.
3570 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003571int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003572 int len, int reset)
3573{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003574 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003575 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003576}
3577EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3578/**
3579 * ftrace_set_filter - set a function to filter on in ftrace
3580 * @ops - the ops to set the filter with
3581 * @buf - the string that holds the function filter text.
3582 * @len - the length of the string.
3583 * @reset - non zero to reset all filters before applying this filter.
3584 *
3585 * Filters denote which functions should be enabled when tracing is enabled.
3586 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3587 */
3588void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3589{
3590 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3591}
3592EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3593
3594/**
3595 * ftrace_set_notrace - set a function to not trace in ftrace
3596 * @ops - the ops to set the notrace filter with
3597 * @buf - the string that holds the function notrace text.
3598 * @len - the length of the string.
3599 * @reset - non zero to reset all filters before applying this filter.
3600 *
3601 * Notrace Filters denote which functions should not be enabled when tracing
3602 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3603 * for tracing.
3604 */
3605void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003606{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003607 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003608}
Steven Rostedt936e0742011-05-05 22:54:01 -04003609EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003610
Steven Rostedt2af15d62009-05-28 13:37:24 -04003611/*
3612 * command line interface to allow users to set filters on boot up.
3613 */
3614#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3615static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3616static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3617
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003618/* Used by function selftest to not test if filter is set */
3619bool ftrace_filter_param __initdata;
3620
Steven Rostedt2af15d62009-05-28 13:37:24 -04003621static int __init set_ftrace_notrace(char *str)
3622{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003623 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003624 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003625 return 1;
3626}
3627__setup("ftrace_notrace=", set_ftrace_notrace);
3628
3629static int __init set_ftrace_filter(char *str)
3630{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003631 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003632 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003633 return 1;
3634}
3635__setup("ftrace_filter=", set_ftrace_filter);
3636
Stefan Assmann369bc182009-10-12 22:17:21 +02003637#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003638static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003639static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003640
Stefan Assmann369bc182009-10-12 22:17:21 +02003641static int __init set_graph_function(char *str)
3642{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003643 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003644 return 1;
3645}
3646__setup("ftrace_graph_filter=", set_graph_function);
3647
3648static void __init set_ftrace_early_graph(char *buf)
3649{
3650 int ret;
3651 char *func;
3652
3653 while (buf) {
3654 func = strsep(&buf, ",");
3655 /* we allow only one expression at a time */
3656 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003657 FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003658 if (ret)
3659 printk(KERN_DEBUG "ftrace: function %s not "
3660 "traceable\n", func);
3661 }
3662}
3663#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3664
Steven Rostedt2a85a372011-12-19 21:57:44 -05003665void __init
3666ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003667{
3668 char *func;
3669
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003670 ftrace_ops_init(ops);
3671
Steven Rostedt2af15d62009-05-28 13:37:24 -04003672 while (buf) {
3673 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003674 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003675 }
3676}
3677
3678static void __init set_ftrace_early_filters(void)
3679{
3680 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003681 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003682 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003683 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003684#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3685 if (ftrace_graph_buf[0])
3686 set_ftrace_early_graph(ftrace_graph_buf);
3687#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003688}
3689
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003690int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003691{
3692 struct seq_file *m = (struct seq_file *)file->private_data;
3693 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003694 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003695 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003696 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003697 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003698
Steven Rostedt5072c592008-05-12 21:20:43 +02003699 if (file->f_mode & FMODE_READ) {
3700 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003701 seq_release(inode, file);
3702 } else
3703 iter = file->private_data;
3704
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003705 parser = &iter->parser;
3706 if (trace_parser_loaded(parser)) {
3707 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003708 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003709 }
3710
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003711 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003712
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003713 mutex_lock(&iter->ops->regex_lock);
3714
Steven Rostedt058e2972011-04-29 22:35:33 -04003715 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003716 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3717
3718 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003719 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003720 else
3721 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003722
Steven Rostedt058e2972011-04-29 22:35:33 -04003723 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003724 ret = ftrace_hash_move(iter->ops, filter_hash,
3725 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003726 if (!ret)
3727 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003728
Steven Rostedt058e2972011-04-29 22:35:33 -04003729 mutex_unlock(&ftrace_lock);
3730 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003731
3732 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003733 free_ftrace_hash(iter->hash);
3734 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003735
Steven Rostedt5072c592008-05-12 21:20:43 +02003736 return 0;
3737}
3738
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003739static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003740 .open = ftrace_avail_open,
3741 .read = seq_read,
3742 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003743 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003744};
3745
Steven Rostedt647bcd02011-05-03 14:39:21 -04003746static const struct file_operations ftrace_enabled_fops = {
3747 .open = ftrace_enabled_open,
3748 .read = seq_read,
3749 .llseek = seq_lseek,
3750 .release = seq_release_private,
3751};
3752
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003753static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003754 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003755 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003756 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003757 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003758 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003759};
3760
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003761static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003762 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003763 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003764 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003765 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003766 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003767};
3768
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003769#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3770
3771static DEFINE_MUTEX(graph_lock);
3772
3773int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003774int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003775unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003776unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003777
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003778struct ftrace_graph_data {
3779 unsigned long *table;
3780 size_t size;
3781 int *count;
3782 const struct seq_operations *seq_ops;
3783};
3784
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003785static void *
Li Zefan85951842009-06-24 09:54:00 +08003786__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003787{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003788 struct ftrace_graph_data *fgd = m->private;
3789
3790 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003791 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003792 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003793}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003794
Li Zefan85951842009-06-24 09:54:00 +08003795static void *
3796g_next(struct seq_file *m, void *v, loff_t *pos)
3797{
3798 (*pos)++;
3799 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003800}
3801
3802static void *g_start(struct seq_file *m, loff_t *pos)
3803{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003804 struct ftrace_graph_data *fgd = m->private;
3805
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003806 mutex_lock(&graph_lock);
3807
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003808 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003809 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003810 return (void *)1;
3811
Li Zefan85951842009-06-24 09:54:00 +08003812 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003813}
3814
3815static void g_stop(struct seq_file *m, void *p)
3816{
3817 mutex_unlock(&graph_lock);
3818}
3819
3820static int g_show(struct seq_file *m, void *v)
3821{
3822 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003823
3824 if (!ptr)
3825 return 0;
3826
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003827 if (ptr == (unsigned long *)1) {
3828 seq_printf(m, "#### all functions enabled ####\n");
3829 return 0;
3830 }
3831
Steven Rostedtb375a112009-09-17 00:05:58 -04003832 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003833
3834 return 0;
3835}
3836
James Morris88e9d342009-09-22 16:43:43 -07003837static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003838 .start = g_start,
3839 .next = g_next,
3840 .stop = g_stop,
3841 .show = g_show,
3842};
3843
3844static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003845__ftrace_graph_open(struct inode *inode, struct file *file,
3846 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003847{
3848 int ret = 0;
3849
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003850 mutex_lock(&graph_lock);
3851 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003852 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003853 *fgd->count = 0;
3854 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003855 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003856 mutex_unlock(&graph_lock);
3857
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003858 if (file->f_mode & FMODE_READ) {
3859 ret = seq_open(file, fgd->seq_ops);
3860 if (!ret) {
3861 struct seq_file *m = file->private_data;
3862 m->private = fgd;
3863 }
3864 } else
3865 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08003866
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003867 return ret;
3868}
3869
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003870static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003871ftrace_graph_open(struct inode *inode, struct file *file)
3872{
3873 struct ftrace_graph_data *fgd;
3874
3875 if (unlikely(ftrace_disabled))
3876 return -ENODEV;
3877
3878 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3879 if (fgd == NULL)
3880 return -ENOMEM;
3881
3882 fgd->table = ftrace_graph_funcs;
3883 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3884 fgd->count = &ftrace_graph_count;
3885 fgd->seq_ops = &ftrace_graph_seq_ops;
3886
3887 return __ftrace_graph_open(inode, file, fgd);
3888}
3889
3890static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003891ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3892{
3893 struct ftrace_graph_data *fgd;
3894
3895 if (unlikely(ftrace_disabled))
3896 return -ENODEV;
3897
3898 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3899 if (fgd == NULL)
3900 return -ENOMEM;
3901
3902 fgd->table = ftrace_graph_notrace_funcs;
3903 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3904 fgd->count = &ftrace_graph_notrace_count;
3905 fgd->seq_ops = &ftrace_graph_seq_ops;
3906
3907 return __ftrace_graph_open(inode, file, fgd);
3908}
3909
3910static int
Li Zefan87827112009-07-23 11:29:11 +08003911ftrace_graph_release(struct inode *inode, struct file *file)
3912{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003913 if (file->f_mode & FMODE_READ) {
3914 struct seq_file *m = file->private_data;
3915
3916 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08003917 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003918 } else {
3919 kfree(file->private_data);
3920 }
3921
Li Zefan87827112009-07-23 11:29:11 +08003922 return 0;
3923}
3924
3925static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003926ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003927{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003928 struct dyn_ftrace *rec;
3929 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003930 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003931 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003932 int type, not;
3933 char *search;
3934 bool exists;
3935 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003936
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003937 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003938 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003939 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003940 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003941
3942 search_len = strlen(search);
3943
Steven Rostedt52baf112009-02-14 01:15:39 -05003944 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003945
3946 if (unlikely(ftrace_disabled)) {
3947 mutex_unlock(&ftrace_lock);
3948 return -ENODEV;
3949 }
3950
Steven Rostedt265c8312009-02-13 12:43:56 -05003951 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003952
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003953 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003954 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003955 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003956 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003957 if (array[i] == rec->ip) {
3958 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003959 break;
3960 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003961 }
3962
3963 if (!not) {
3964 fail = 0;
3965 if (!exists) {
3966 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003967 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003968 goto out;
3969 }
3970 } else {
3971 if (exists) {
3972 array[i] = array[--(*idx)];
3973 array[*idx] = 0;
3974 fail = 0;
3975 }
3976 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003977 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003978 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003979out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003980 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003981
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003982 if (fail)
3983 return -EINVAL;
3984
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003985 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003986}
3987
3988static ssize_t
3989ftrace_graph_write(struct file *file, const char __user *ubuf,
3990 size_t cnt, loff_t *ppos)
3991{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003992 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09003993 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003994 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003995
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003996 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003997 return 0;
3998
Namhyung Kim6a101082013-10-14 17:24:25 +09003999 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4000 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004001
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004002 read = trace_get_user(&parser, ubuf, cnt, ppos);
4003
Li Zefan4ba79782009-09-22 13:52:20 +08004004 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004005 parser.buffer[parser.idx] = 0;
4006
Namhyung Kim6a101082013-10-14 17:24:25 +09004007 mutex_lock(&graph_lock);
4008
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004009 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004010 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4011 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004012
4013 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004014 }
4015
Namhyung Kim6a101082013-10-14 17:24:25 +09004016 if (!ret)
4017 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004018
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004019 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004020
4021 return ret;
4022}
4023
4024static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004025 .open = ftrace_graph_open,
4026 .read = seq_read,
4027 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004028 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004029 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004030};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004031
4032static const struct file_operations ftrace_graph_notrace_fops = {
4033 .open = ftrace_graph_notrace_open,
4034 .read = seq_read,
4035 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004036 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004037 .release = ftrace_graph_release,
4038};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004039#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4040
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004041static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004042{
Steven Rostedt5072c592008-05-12 21:20:43 +02004043
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004044 trace_create_file("available_filter_functions", 0444,
4045 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004046
Steven Rostedt647bcd02011-05-03 14:39:21 -04004047 trace_create_file("enabled_functions", 0444,
4048 d_tracer, NULL, &ftrace_enabled_fops);
4049
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004050 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4051 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004052
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004053 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004054 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004055
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004056#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004057 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004058 NULL,
4059 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004060 trace_create_file("set_graph_notrace", 0444, d_tracer,
4061 NULL,
4062 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004063#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4064
Steven Rostedt5072c592008-05-12 21:20:43 +02004065 return 0;
4066}
4067
Steven Rostedt9fd49322012-04-24 22:32:06 -04004068static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004069{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004070 const unsigned long *ipa = a;
4071 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004072
Steven Rostedt9fd49322012-04-24 22:32:06 -04004073 if (*ipa > *ipb)
4074 return 1;
4075 if (*ipa < *ipb)
4076 return -1;
4077 return 0;
4078}
4079
4080static void ftrace_swap_ips(void *a, void *b, int size)
4081{
4082 unsigned long *ipa = a;
4083 unsigned long *ipb = b;
4084 unsigned long t;
4085
4086 t = *ipa;
4087 *ipa = *ipb;
4088 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004089}
4090
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004091static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004092 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004093 unsigned long *end)
4094{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004095 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004096 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004097 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004098 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004099 unsigned long *p;
4100 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004101 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004102 int ret = -ENOMEM;
4103
4104 count = end - start;
4105
4106 if (!count)
4107 return 0;
4108
Steven Rostedt9fd49322012-04-24 22:32:06 -04004109 sort(start, count, sizeof(*start),
4110 ftrace_cmp_ips, ftrace_swap_ips);
4111
Steven Rostedt706c81f2012-04-24 23:45:26 -04004112 start_pg = ftrace_allocate_pages(count);
4113 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004114 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004115
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004116 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004117
Steven Rostedt32082302011-12-16 14:42:37 -05004118 /*
4119 * Core and each module needs their own pages, as
4120 * modules will free them when they are removed.
4121 * Force a new page to be allocated for modules.
4122 */
Steven Rostedta7900872011-12-16 16:23:44 -05004123 if (!mod) {
4124 WARN_ON(ftrace_pages || ftrace_pages_start);
4125 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004126 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004127 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004128 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004129 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004130
Steven Rostedta7900872011-12-16 16:23:44 -05004131 if (WARN_ON(ftrace_pages->next)) {
4132 /* Hmm, we have free pages? */
4133 while (ftrace_pages->next)
4134 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004135 }
Steven Rostedta7900872011-12-16 16:23:44 -05004136
Steven Rostedt706c81f2012-04-24 23:45:26 -04004137 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004138 }
4139
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004140 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004141 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004142 while (p < end) {
4143 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004144 /*
4145 * Some architecture linkers will pad between
4146 * the different mcount_loc sections of different
4147 * object files to satisfy alignments.
4148 * Skip any NULL pointers.
4149 */
4150 if (!addr)
4151 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004152
4153 if (pg->index == pg->size) {
4154 /* We should have allocated enough */
4155 if (WARN_ON(!pg->next))
4156 break;
4157 pg = pg->next;
4158 }
4159
4160 rec = &pg->records[pg->index++];
4161 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004162 }
4163
Steven Rostedt706c81f2012-04-24 23:45:26 -04004164 /* We should have used all pages */
4165 WARN_ON(pg->next);
4166
4167 /* Assign the last page to ftrace_pages */
4168 ftrace_pages = pg;
4169
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004170 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004171 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004172
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004173 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004174 * We only need to disable interrupts on start up
4175 * because we are modifying code that an interrupt
4176 * may execute, and the modification is not atomic.
4177 * But for modules, nothing runs the code we modify
4178 * until we are finished with it, and there's no
4179 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004180 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004181 if (!mod)
4182 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08004183 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004184 if (!mod)
4185 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004186 ret = 0;
4187 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004188 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004189
Steven Rostedta7900872011-12-16 16:23:44 -05004190 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004191}
4192
Steven Rostedt93eb6772009-04-15 13:24:06 -04004193#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004194
4195#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4196
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004197void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004198{
4199 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004200 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004201 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004202 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004203
Steven Rostedt93eb6772009-04-15 13:24:06 -04004204 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004205
4206 if (ftrace_disabled)
4207 goto out_unlock;
4208
Steven Rostedt32082302011-12-16 14:42:37 -05004209 /*
4210 * Each module has its own ftrace_pages, remove
4211 * them from the list.
4212 */
4213 last_pg = &ftrace_pages_start;
4214 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4215 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004216 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004217 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004218 * As core pages are first, the first
4219 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004220 */
Steven Rostedt32082302011-12-16 14:42:37 -05004221 if (WARN_ON(pg == ftrace_pages_start))
4222 goto out_unlock;
4223
4224 /* Check if we are deleting the last page */
4225 if (pg == ftrace_pages)
4226 ftrace_pages = next_to_ftrace_page(last_pg);
4227
4228 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004229 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4230 free_pages((unsigned long)pg->records, order);
4231 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004232 } else
4233 last_pg = &pg->next;
4234 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004235 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004236 mutex_unlock(&ftrace_lock);
4237}
4238
4239static void ftrace_init_module(struct module *mod,
4240 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004241{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004242 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004243 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004244 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004245}
4246
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004247static int ftrace_module_notify_enter(struct notifier_block *self,
4248 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004249{
4250 struct module *mod = data;
4251
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004252 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004253 ftrace_init_module(mod, mod->ftrace_callsites,
4254 mod->ftrace_callsites +
4255 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004256 return 0;
4257}
4258
4259static int ftrace_module_notify_exit(struct notifier_block *self,
4260 unsigned long val, void *data)
4261{
4262 struct module *mod = data;
4263
4264 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004265 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004266
4267 return 0;
4268}
4269#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004270static int ftrace_module_notify_enter(struct notifier_block *self,
4271 unsigned long val, void *data)
4272{
4273 return 0;
4274}
4275static int ftrace_module_notify_exit(struct notifier_block *self,
4276 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004277{
4278 return 0;
4279}
4280#endif /* CONFIG_MODULES */
4281
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004282struct notifier_block ftrace_module_enter_nb = {
4283 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004284 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004285};
4286
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004287struct notifier_block ftrace_module_exit_nb = {
4288 .notifier_call = ftrace_module_notify_exit,
4289 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4290};
4291
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004292extern unsigned long __start_mcount_loc[];
4293extern unsigned long __stop_mcount_loc[];
4294
4295void __init ftrace_init(void)
4296{
4297 unsigned long count, addr, flags;
4298 int ret;
4299
4300 /* Keep the ftrace pointer to the stub */
4301 addr = (unsigned long)ftrace_stub;
4302
4303 local_irq_save(flags);
4304 ftrace_dyn_arch_init(&addr);
4305 local_irq_restore(flags);
4306
4307 /* ftrace_dyn_arch_init places the return code in addr */
4308 if (addr)
4309 goto failed;
4310
4311 count = __stop_mcount_loc - __start_mcount_loc;
4312
4313 ret = ftrace_dyn_table_alloc(count);
4314 if (ret)
4315 goto failed;
4316
4317 last_ftrace_enabled = ftrace_enabled = 1;
4318
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004319 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004320 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004321 __stop_mcount_loc);
4322
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004323 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004324 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004325 pr_warning("Failed to register trace ftrace module enter notifier\n");
4326
4327 ret = register_module_notifier(&ftrace_module_exit_nb);
4328 if (ret)
4329 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004330
Steven Rostedt2af15d62009-05-28 13:37:24 -04004331 set_ftrace_early_filters();
4332
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004333 return;
4334 failed:
4335 ftrace_disabled = 1;
4336}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004337
Steven Rostedt3d083392008-05-12 21:20:42 +02004338#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004339
Steven Rostedt2b499382011-05-03 22:49:52 -04004340static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004341 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004342 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4343 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004344};
4345
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004346static int __init ftrace_nodyn_init(void)
4347{
4348 ftrace_enabled = 1;
4349 return 0;
4350}
Steven Rostedt6f415672012-10-05 12:13:07 -04004351core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004352
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004353static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4354static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004355/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004356# define ftrace_startup(ops, command) \
4357 ({ \
4358 int ___ret = __register_ftrace_function(ops); \
4359 if (!___ret) \
4360 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4361 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004362 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004363# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4364
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004365# define ftrace_startup_sysctl() do { } while (0)
4366# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004367
4368static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004369ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004370{
4371 return 1;
4372}
4373
Steven Rostedt3d083392008-05-12 21:20:42 +02004374#endif /* CONFIG_DYNAMIC_FTRACE */
4375
Steven Rostedtb8489142011-05-04 09:27:52 -04004376static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004377ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004378 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004379{
Jiri Olsae2484912012-02-15 15:51:48 +01004380 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4381 return;
4382
4383 /*
4384 * Some of the ops may be dynamically allocated,
4385 * they must be freed after a synchronize_sched().
4386 */
4387 preempt_disable_notrace();
4388 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004389
4390 /*
4391 * Control funcs (perf) uses RCU. Only trace if
4392 * RCU is currently active.
4393 */
4394 if (!rcu_is_watching())
4395 goto out;
4396
Steven Rostedt0a016402012-11-02 17:03:03 -04004397 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004398 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4399 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004400 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004401 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004402 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004403 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004404 trace_recursion_clear(TRACE_CONTROL_BIT);
4405 preempt_enable_notrace();
4406}
4407
4408static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004409 .func = ftrace_ops_control_func,
4410 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4411 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004412};
4413
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004414static inline void
4415__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004416 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004417{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004418 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004419 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004420
Steven Rostedtccf36722012-06-05 09:44:25 -04004421 if (function_trace_stop)
4422 return;
4423
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004424 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4425 if (bit < 0)
4426 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004427
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004428 /*
4429 * Some of the ops may be dynamically allocated,
4430 * they must be freed after a synchronize_sched().
4431 */
4432 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004433 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004434 if (ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004435 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004436 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004437 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004438 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004439}
4440
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004441/*
4442 * Some archs only support passing ip and parent_ip. Even though
4443 * the list function ignores the op parameter, we do not want any
4444 * C side effects, where a function is called without the caller
4445 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004446 * Archs are to support both the regs and ftrace_ops at the same time.
4447 * If they support ftrace_ops, it is assumed they support regs.
4448 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004449 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4450 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004451 * An architecture can pass partial regs with ftrace_ops and still
4452 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004453 */
4454#if ARCH_SUPPORTS_FTRACE_OPS
4455static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004456 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004457{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004458 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004459}
4460#else
4461static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4462{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004463 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004464}
4465#endif
4466
Steven Rostedte32d8952008-12-04 00:26:41 -05004467static void clear_ftrace_swapper(void)
4468{
4469 struct task_struct *p;
4470 int cpu;
4471
4472 get_online_cpus();
4473 for_each_online_cpu(cpu) {
4474 p = idle_task(cpu);
4475 clear_tsk_trace_trace(p);
4476 }
4477 put_online_cpus();
4478}
4479
4480static void set_ftrace_swapper(void)
4481{
4482 struct task_struct *p;
4483 int cpu;
4484
4485 get_online_cpus();
4486 for_each_online_cpu(cpu) {
4487 p = idle_task(cpu);
4488 set_tsk_trace_trace(p);
4489 }
4490 put_online_cpus();
4491}
4492
4493static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004494{
4495 struct task_struct *p;
4496
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004497 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004498 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004499 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004500 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004501 rcu_read_unlock();
4502
Steven Rostedte32d8952008-12-04 00:26:41 -05004503 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004504}
4505
Steven Rostedte32d8952008-12-04 00:26:41 -05004506static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004507{
4508 struct task_struct *p;
4509
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004510 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004511 do_each_pid_task(pid, PIDTYPE_PID, p) {
4512 set_tsk_trace_trace(p);
4513 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004514 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004515}
4516
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004517static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004518{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004519 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004520 clear_ftrace_swapper();
4521 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004522 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004523}
4524
4525static void set_ftrace_pid_task(struct pid *pid)
4526{
4527 if (pid == ftrace_swapper_pid)
4528 set_ftrace_swapper();
4529 else
4530 set_ftrace_pid(pid);
4531}
4532
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004533static int ftrace_pid_add(int p)
4534{
4535 struct pid *pid;
4536 struct ftrace_pid *fpid;
4537 int ret = -EINVAL;
4538
4539 mutex_lock(&ftrace_lock);
4540
4541 if (!p)
4542 pid = ftrace_swapper_pid;
4543 else
4544 pid = find_get_pid(p);
4545
4546 if (!pid)
4547 goto out;
4548
4549 ret = 0;
4550
4551 list_for_each_entry(fpid, &ftrace_pids, list)
4552 if (fpid->pid == pid)
4553 goto out_put;
4554
4555 ret = -ENOMEM;
4556
4557 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4558 if (!fpid)
4559 goto out_put;
4560
4561 list_add(&fpid->list, &ftrace_pids);
4562 fpid->pid = pid;
4563
4564 set_ftrace_pid_task(pid);
4565
4566 ftrace_update_pid_func();
4567 ftrace_startup_enable(0);
4568
4569 mutex_unlock(&ftrace_lock);
4570 return 0;
4571
4572out_put:
4573 if (pid != ftrace_swapper_pid)
4574 put_pid(pid);
4575
4576out:
4577 mutex_unlock(&ftrace_lock);
4578 return ret;
4579}
4580
4581static void ftrace_pid_reset(void)
4582{
4583 struct ftrace_pid *fpid, *safe;
4584
4585 mutex_lock(&ftrace_lock);
4586 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4587 struct pid *pid = fpid->pid;
4588
4589 clear_ftrace_pid_task(pid);
4590
4591 list_del(&fpid->list);
4592 kfree(fpid);
4593 }
4594
4595 ftrace_update_pid_func();
4596 ftrace_startup_enable(0);
4597
4598 mutex_unlock(&ftrace_lock);
4599}
4600
4601static void *fpid_start(struct seq_file *m, loff_t *pos)
4602{
4603 mutex_lock(&ftrace_lock);
4604
4605 if (list_empty(&ftrace_pids) && (!*pos))
4606 return (void *) 1;
4607
4608 return seq_list_start(&ftrace_pids, *pos);
4609}
4610
4611static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4612{
4613 if (v == (void *)1)
4614 return NULL;
4615
4616 return seq_list_next(v, &ftrace_pids, pos);
4617}
4618
4619static void fpid_stop(struct seq_file *m, void *p)
4620{
4621 mutex_unlock(&ftrace_lock);
4622}
4623
4624static int fpid_show(struct seq_file *m, void *v)
4625{
4626 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4627
4628 if (v == (void *)1) {
4629 seq_printf(m, "no pid\n");
4630 return 0;
4631 }
4632
4633 if (fpid->pid == ftrace_swapper_pid)
4634 seq_printf(m, "swapper tasks\n");
4635 else
4636 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4637
4638 return 0;
4639}
4640
4641static const struct seq_operations ftrace_pid_sops = {
4642 .start = fpid_start,
4643 .next = fpid_next,
4644 .stop = fpid_stop,
4645 .show = fpid_show,
4646};
4647
4648static int
4649ftrace_pid_open(struct inode *inode, struct file *file)
4650{
4651 int ret = 0;
4652
4653 if ((file->f_mode & FMODE_WRITE) &&
4654 (file->f_flags & O_TRUNC))
4655 ftrace_pid_reset();
4656
4657 if (file->f_mode & FMODE_READ)
4658 ret = seq_open(file, &ftrace_pid_sops);
4659
4660 return ret;
4661}
4662
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004663static ssize_t
4664ftrace_pid_write(struct file *filp, const char __user *ubuf,
4665 size_t cnt, loff_t *ppos)
4666{
Ingo Molnar457dc922009-11-23 11:03:28 +01004667 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004668 long val;
4669 int ret;
4670
4671 if (cnt >= sizeof(buf))
4672 return -EINVAL;
4673
4674 if (copy_from_user(&buf, ubuf, cnt))
4675 return -EFAULT;
4676
4677 buf[cnt] = 0;
4678
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004679 /*
4680 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4681 * to clean the filter quietly.
4682 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004683 tmp = strstrip(buf);
4684 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004685 return 1;
4686
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004687 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004688 if (ret < 0)
4689 return ret;
4690
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004691 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004692
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004693 return ret ? ret : cnt;
4694}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004695
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004696static int
4697ftrace_pid_release(struct inode *inode, struct file *file)
4698{
4699 if (file->f_mode & FMODE_READ)
4700 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004701
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004702 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004703}
4704
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004705static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004706 .open = ftrace_pid_open,
4707 .write = ftrace_pid_write,
4708 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004709 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004710 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004711};
4712
4713static __init int ftrace_init_debugfs(void)
4714{
4715 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004716
4717 d_tracer = tracing_init_dentry();
4718 if (!d_tracer)
4719 return 0;
4720
4721 ftrace_init_dyn_debugfs(d_tracer);
4722
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004723 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4724 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004725
4726 ftrace_profile_debugfs(d_tracer);
4727
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004728 return 0;
4729}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004730fs_initcall(ftrace_init_debugfs);
4731
Steven Rostedt3d083392008-05-12 21:20:42 +02004732/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004733 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004734 *
4735 * This function should be used by panic code. It stops ftrace
4736 * but in a not so nice way. If you need to simply kill ftrace
4737 * from a non-atomic section, use ftrace_kill.
4738 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004739void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004740{
4741 ftrace_disabled = 1;
4742 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004743 clear_ftrace_function();
4744}
4745
4746/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004747 * Test if ftrace is dead or not.
4748 */
4749int ftrace_is_dead(void)
4750{
4751 return ftrace_disabled;
4752}
4753
4754/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004755 * register_ftrace_function - register a function for profiling
4756 * @ops - ops structure that holds the function for profiling.
4757 *
4758 * Register a function to be called by all functions in the
4759 * kernel.
4760 *
4761 * Note: @ops->func and all the functions it calls must be labeled
4762 * with "notrace", otherwise it will go into a
4763 * recursive loop.
4764 */
4765int register_ftrace_function(struct ftrace_ops *ops)
4766{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004767 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004768
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004769 ftrace_ops_init(ops);
4770
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004771 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004772
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004773 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004774
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004775 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004776
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004777 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004778}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004779EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004780
4781/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004782 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004783 * @ops - ops structure that holds the function to unregister
4784 *
4785 * Unregister a function that was added to be called by ftrace profiling.
4786 */
4787int unregister_ftrace_function(struct ftrace_ops *ops)
4788{
4789 int ret;
4790
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004791 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004792 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004793 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004794
4795 return ret;
4796}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004797EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004798
Ingo Molnare309b412008-05-12 21:20:51 +02004799int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004800ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004801 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004802 loff_t *ppos)
4803{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004804 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004805
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004806 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004807
Steven Rostedt45a4a232011-04-21 23:16:46 -04004808 if (unlikely(ftrace_disabled))
4809 goto out;
4810
4811 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004812
Li Zefana32c7762009-06-26 16:55:51 +08004813 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004814 goto out;
4815
Li Zefana32c7762009-06-26 16:55:51 +08004816 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004817
4818 if (ftrace_enabled) {
4819
4820 ftrace_startup_sysctl();
4821
4822 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004823 if (ftrace_ops_list != &ftrace_list_end)
4824 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004825
4826 } else {
4827 /* stopping ftrace calls (just send to ftrace_stub) */
4828 ftrace_trace_function = ftrace_stub;
4829
4830 ftrace_shutdown_sysctl();
4831 }
4832
4833 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004834 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004835 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004836}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004837
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004838#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004839
Steven Rostedt597af812009-04-03 15:24:12 -04004840static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004841static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004842
Steven Rostedte49dc192008-12-02 23:50:05 -05004843int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4844{
4845 return 0;
4846}
4847
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004848/* The callbacks that hook a function */
4849trace_func_graph_ret_t ftrace_graph_return =
4850 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004851trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004852
4853/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4854static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4855{
4856 int i;
4857 int ret = 0;
4858 unsigned long flags;
4859 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4860 struct task_struct *g, *t;
4861
4862 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4863 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4864 * sizeof(struct ftrace_ret_stack),
4865 GFP_KERNEL);
4866 if (!ret_stack_list[i]) {
4867 start = 0;
4868 end = i;
4869 ret = -ENOMEM;
4870 goto free;
4871 }
4872 }
4873
4874 read_lock_irqsave(&tasklist_lock, flags);
4875 do_each_thread(g, t) {
4876 if (start == end) {
4877 ret = -EAGAIN;
4878 goto unlock;
4879 }
4880
4881 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004882 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004883 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004884 t->curr_ret_stack = -1;
4885 /* Make sure the tasks see the -1 first: */
4886 smp_wmb();
4887 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004888 }
4889 } while_each_thread(g, t);
4890
4891unlock:
4892 read_unlock_irqrestore(&tasklist_lock, flags);
4893free:
4894 for (i = start; i < end; i++)
4895 kfree(ret_stack_list[i]);
4896 return ret;
4897}
4898
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004899static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004900ftrace_graph_probe_sched_switch(void *ignore,
4901 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004902{
4903 unsigned long long timestamp;
4904 int index;
4905
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004906 /*
4907 * Does the user want to count the time a function was asleep.
4908 * If so, do not update the time stamps.
4909 */
4910 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4911 return;
4912
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004913 timestamp = trace_clock_local();
4914
4915 prev->ftrace_timestamp = timestamp;
4916
4917 /* only process tasks that we timestamped */
4918 if (!next->ftrace_timestamp)
4919 return;
4920
4921 /*
4922 * Update all the counters in next to make up for the
4923 * time next was sleeping.
4924 */
4925 timestamp -= next->ftrace_timestamp;
4926
4927 for (index = next->curr_ret_stack; index >= 0; index--)
4928 next->ret_stack[index].calltime += timestamp;
4929}
4930
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004931/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004932static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004933{
4934 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004935 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004936
4937 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4938 sizeof(struct ftrace_ret_stack *),
4939 GFP_KERNEL);
4940
4941 if (!ret_stack_list)
4942 return -ENOMEM;
4943
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004944 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004945 for_each_online_cpu(cpu) {
4946 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004947 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004948 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004949
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004950 do {
4951 ret = alloc_retstack_tasklist(ret_stack_list);
4952 } while (ret == -EAGAIN);
4953
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004954 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004955 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004956 if (ret)
4957 pr_info("ftrace_graph: Couldn't activate tracepoint"
4958 " probe to kernel_sched_switch\n");
4959 }
4960
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004961 kfree(ret_stack_list);
4962 return ret;
4963}
4964
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004965/*
4966 * Hibernation protection.
4967 * The state of the current task is too much unstable during
4968 * suspend/restore to disk. We want to protect against that.
4969 */
4970static int
4971ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4972 void *unused)
4973{
4974 switch (state) {
4975 case PM_HIBERNATION_PREPARE:
4976 pause_graph_tracing();
4977 break;
4978
4979 case PM_POST_HIBERNATION:
4980 unpause_graph_tracing();
4981 break;
4982 }
4983 return NOTIFY_DONE;
4984}
4985
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004986/* Just a place holder for function graph */
4987static struct ftrace_ops fgraph_ops __read_mostly = {
4988 .func = ftrace_stub,
4989 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
4990 FTRACE_OPS_FL_RECURSION_SAFE,
4991};
4992
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004993int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4994 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004995{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004996 int ret = 0;
4997
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004998 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004999
Steven Rostedt05ce5812009-03-24 00:18:31 -04005000 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005001 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005002 ret = -EBUSY;
5003 goto out;
5004 }
5005
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005006 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5007 register_pm_notifier(&ftrace_suspend_notifier);
5008
Steven Rostedt597af812009-04-03 15:24:12 -04005009 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005010 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005011 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005012 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005013 goto out;
5014 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005015
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005016 ftrace_graph_return = retfunc;
5017 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05005018
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005019 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005020
5021out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005022 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005023 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005024}
5025
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005026void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005027{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005028 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005029
Steven Rostedt597af812009-04-03 15:24:12 -04005030 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005031 goto out;
5032
Steven Rostedt597af812009-04-03 15:24:12 -04005033 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005034 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005035 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005036 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005037 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005038 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005039
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005040 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005041 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005042}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005043
Steven Rostedt868baf02011-02-10 21:26:13 -05005044static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5045
5046static void
5047graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5048{
5049 atomic_set(&t->tracing_graph_pause, 0);
5050 atomic_set(&t->trace_overrun, 0);
5051 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005052 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005053 smp_wmb();
5054 t->ret_stack = ret_stack;
5055}
5056
5057/*
5058 * Allocate a return stack for the idle task. May be the first
5059 * time through, or it may be done by CPU hotplug online.
5060 */
5061void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5062{
5063 t->curr_ret_stack = -1;
5064 /*
5065 * The idle task has no parent, it either has its own
5066 * stack or no stack at all.
5067 */
5068 if (t->ret_stack)
5069 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5070
5071 if (ftrace_graph_active) {
5072 struct ftrace_ret_stack *ret_stack;
5073
5074 ret_stack = per_cpu(idle_ret_stack, cpu);
5075 if (!ret_stack) {
5076 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5077 * sizeof(struct ftrace_ret_stack),
5078 GFP_KERNEL);
5079 if (!ret_stack)
5080 return;
5081 per_cpu(idle_ret_stack, cpu) = ret_stack;
5082 }
5083 graph_init_task(t, ret_stack);
5084 }
5085}
5086
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005087/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005088void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005089{
Steven Rostedt84047e32009-06-02 16:51:55 -04005090 /* Make sure we do not use the parent ret_stack */
5091 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005092 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005093
Steven Rostedt597af812009-04-03 15:24:12 -04005094 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005095 struct ftrace_ret_stack *ret_stack;
5096
5097 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005098 * sizeof(struct ftrace_ret_stack),
5099 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005100 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005101 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005102 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005103 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005104}
5105
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005106void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005107{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005108 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5109
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005110 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005111 /* NULL must become visible to IRQs before we free it: */
5112 barrier();
5113
5114 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005115}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005116
5117void ftrace_graph_stop(void)
5118{
5119 ftrace_stop();
5120}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005121#endif