blob: 5916a8e59e878d4c6d9fc87bc370949f5501d962 [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 Rostedt6912896e2008-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 Rostedt6912896e2008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt6912896e2008-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 Rostedt6912896e2008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -050065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
Jiri Olsae2484912012-02-15 15:51:48 +010066
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040068#define INIT_OPS_HASH(opsname) \
69 .func_hash = &opsname.local_hash, \
70 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040071#define ASSIGN_OPS_HASH(opsname, val) \
72 .func_hash = val, \
73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090074#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040075#define INIT_OPS_HASH(opsname)
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040076#define ASSIGN_OPS_HASH(opsname, val)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090077#endif
78
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040079static struct ftrace_ops ftrace_list_end __read_mostly = {
80 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040081 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040082 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040083};
84
Steven Rostedt4eebcc82008-05-12 21:20:48 +020085/* ftrace_enabled is a method to turn ftrace on or off */
86int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020087static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020088
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040089/* Current function tracing op */
90struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050091/* What to set function_trace_op to */
92static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050093
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040094/* List for set_ftrace_pid's pids. */
95LIST_HEAD(ftrace_pids);
96struct ftrace_pid {
97 struct list_head list;
98 struct pid *pid;
99};
100
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200101/*
102 * ftrace_disabled is set when an anomaly is discovered.
103 * ftrace_disabled is much stronger than ftrace_enabled.
104 */
105static int ftrace_disabled __read_mostly;
106
Steven Rostedt52baf112009-02-14 01:15:39 -0500107static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200108
Jiri Olsae2484912012-02-15 15:51:48 +0100109static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400110static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200111ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500112ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400113static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100114static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200115
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400116#if ARCH_SUPPORTS_FTRACE_OPS
117static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400118 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400119#else
120/* See comment below, where ftrace_ops_list_func is defined */
121static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
122#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
123#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400124
Steven Rostedt0a016402012-11-02 17:03:03 -0400125/*
126 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400127 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400128 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400129 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400130 * concurrent insertions into the ftrace_global_list.
131 *
132 * Silly Alpha and silly pointer-speculation compiler optimizations!
133 */
134#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400135 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400136 do
137
138/*
139 * Optimized for just a single item in the list (as that is the normal case).
140 */
141#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400142 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400143 unlikely((op) != &ftrace_list_end))
144
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900145static inline void ftrace_ops_init(struct ftrace_ops *ops)
146{
147#ifdef CONFIG_DYNAMIC_FTRACE
148 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400149 mutex_init(&ops->local_hash.regex_lock);
150 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900151 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
152 }
153#endif
154}
155
Steven Rostedtea701f12012-07-20 13:08:05 -0400156/**
157 * ftrace_nr_registered_ops - return number of ops registered
158 *
159 * Returns the number of ftrace_ops registered and tracing functions
160 */
161int ftrace_nr_registered_ops(void)
162{
163 struct ftrace_ops *ops;
164 int cnt = 0;
165
166 mutex_lock(&ftrace_lock);
167
168 for (ops = ftrace_ops_list;
169 ops != &ftrace_list_end; ops = ops->next)
170 cnt++;
171
172 mutex_unlock(&ftrace_lock);
173
174 return cnt;
175}
176
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400177static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400178 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500179{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500180 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500181 return;
182
Steven Rostedta1e2e312011-08-09 12:50:46 -0400183 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500184}
185
186static void set_ftrace_pid_function(ftrace_func_t func)
187{
188 /* do not set ftrace_pid_function to itself! */
189 if (func != ftrace_pid_func)
190 ftrace_pid_function = func;
191}
192
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200193/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200194 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200195 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200196 * This NULLs the ftrace function and in essence stops
197 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200198 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200199void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200200{
Steven Rostedt3d083392008-05-12 21:20:42 +0200201 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500202 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200203}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200204
Jiri Olsae2484912012-02-15 15:51:48 +0100205static void control_ops_disable_all(struct ftrace_ops *ops)
206{
207 int cpu;
208
209 for_each_possible_cpu(cpu)
210 *per_cpu_ptr(ops->disabled, cpu) = 1;
211}
212
213static int control_ops_alloc(struct ftrace_ops *ops)
214{
215 int __percpu *disabled;
216
217 disabled = alloc_percpu(int);
218 if (!disabled)
219 return -ENOMEM;
220
221 ops->disabled = disabled;
222 control_ops_disable_all(ops);
223 return 0;
224}
225
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500226static void ftrace_sync(struct work_struct *work)
227{
228 /*
229 * This function is just a stub to implement a hard force
230 * of synchronize_sched(). This requires synchronizing
231 * tasks even in userspace and idle.
232 *
233 * Yes, function tracing is rude.
234 */
235}
236
237static void ftrace_sync_ipi(void *data)
238{
239 /* Probably not needed, but do it anyway */
240 smp_rmb();
241}
242
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500243#ifdef CONFIG_FUNCTION_GRAPH_TRACER
244static void update_function_graph_func(void);
245#else
246static inline void update_function_graph_func(void) { }
247#endif
248
Steven Rostedt2b499382011-05-03 22:49:52 -0400249static void update_ftrace_function(void)
250{
251 ftrace_func_t func;
252
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400253 /*
254 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400255 * recursion safe and not dynamic and the arch supports passing ops,
256 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400257 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400258 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400259 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400260 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400261 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400262 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400263 /* Set the ftrace_ops that the arch callback uses */
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500264 set_function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400265 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400266 } else {
267 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500268 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400269 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400270 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400271
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400272 update_function_graph_func();
273
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500274 /* If there's no change, then do nothing more here */
275 if (ftrace_trace_function == func)
276 return;
277
278 /*
279 * If we are using the list function, it doesn't care
280 * about the function_trace_ops.
281 */
282 if (func == ftrace_ops_list_func) {
283 ftrace_trace_function = func;
284 /*
285 * Don't even bother setting function_trace_ops,
286 * it would be racy to do so anyway.
287 */
288 return;
289 }
290
291#ifndef CONFIG_DYNAMIC_FTRACE
292 /*
293 * For static tracing, we need to be a bit more careful.
294 * The function change takes affect immediately. Thus,
295 * we need to coorditate the setting of the function_trace_ops
296 * with the setting of the ftrace_trace_function.
297 *
298 * Set the function to the list ops, which will call the
299 * function we want, albeit indirectly, but it handles the
300 * ftrace_ops and doesn't depend on function_trace_op.
301 */
302 ftrace_trace_function = ftrace_ops_list_func;
303 /*
304 * Make sure all CPUs see this. Yes this is slow, but static
305 * tracing is slow and nasty to have enabled.
306 */
307 schedule_on_each_cpu(ftrace_sync);
308 /* Now all cpus are using the list ops. */
309 function_trace_op = set_function_trace_op;
310 /* Make sure the function_trace_op is visible on all CPUs */
311 smp_wmb();
312 /* Nasty way to force a rmb on all cpus */
313 smp_call_function(ftrace_sync_ipi, NULL, 1);
314 /* OK, we are all set to update the ftrace_trace_function now! */
315#endif /* !CONFIG_DYNAMIC_FTRACE */
316
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400317 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400318}
319
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800320int using_ftrace_ops_list_func(void)
321{
322 return ftrace_trace_function == ftrace_ops_list_func;
323}
324
Steven Rostedt2b499382011-05-03 22:49:52 -0400325static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200326{
Steven Rostedt2b499382011-05-03 22:49:52 -0400327 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200328 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400329 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200330 * CPU might be walking that list. We need to make sure
331 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400332 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200333 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400334 rcu_assign_pointer(*list, ops);
335}
Steven Rostedt3d083392008-05-12 21:20:42 +0200336
Steven Rostedt2b499382011-05-03 22:49:52 -0400337static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
338{
339 struct ftrace_ops **p;
340
341 /*
342 * If we are removing the last function, then simply point
343 * to the ftrace_stub.
344 */
345 if (*list == ops && ops->next == &ftrace_list_end) {
346 *list = &ftrace_list_end;
347 return 0;
348 }
349
350 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
351 if (*p == ops)
352 break;
353
354 if (*p != ops)
355 return -1;
356
357 *p = (*p)->next;
358 return 0;
359}
360
Jiri Olsae2484912012-02-15 15:51:48 +0100361static void add_ftrace_list_ops(struct ftrace_ops **list,
362 struct ftrace_ops *main_ops,
363 struct ftrace_ops *ops)
364{
365 int first = *list == &ftrace_list_end;
366 add_ftrace_ops(list, ops);
367 if (first)
368 add_ftrace_ops(&ftrace_ops_list, main_ops);
369}
370
371static int remove_ftrace_list_ops(struct ftrace_ops **list,
372 struct ftrace_ops *main_ops,
373 struct ftrace_ops *ops)
374{
375 int ret = remove_ftrace_ops(list, ops);
376 if (!ret && *list == &ftrace_list_end)
377 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
378 return ret;
379}
380
Steven Rostedt2b499382011-05-03 22:49:52 -0400381static int __register_ftrace_function(struct ftrace_ops *ops)
382{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500383 if (ops->flags & FTRACE_OPS_FL_DELETED)
384 return -EINVAL;
385
Steven Rostedtb8489142011-05-04 09:27:52 -0400386 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
387 return -EBUSY;
388
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900389#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400390 /*
391 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
392 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
393 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
394 */
395 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
396 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
397 return -EINVAL;
398
399 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
400 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
401#endif
402
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400403 if (!core_kernel_data((unsigned long)ops))
404 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
405
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500406 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100407 if (control_ops_alloc(ops))
408 return -ENOMEM;
409 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400410 } else
411 add_ftrace_ops(&ftrace_ops_list, ops);
412
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400413 if (ftrace_enabled)
414 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200415
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200416 return 0;
417}
418
Ingo Molnare309b412008-05-12 21:20:51 +0200419static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200420{
Steven Rostedt2b499382011-05-03 22:49:52 -0400421 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200422
Steven Rostedtb8489142011-05-04 09:27:52 -0400423 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
424 return -EBUSY;
425
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500426 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100427 ret = remove_ftrace_list_ops(&ftrace_control_list,
428 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400429 } else
430 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
431
Steven Rostedt2b499382011-05-03 22:49:52 -0400432 if (ret < 0)
433 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400434
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400435 if (ftrace_enabled)
436 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200437
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500438 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200439}
440
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500441static void ftrace_update_pid_func(void)
442{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400443 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500444 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900445 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500446
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400447 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500448}
449
Steven Rostedt493762f2009-03-23 17:12:36 -0400450#ifdef CONFIG_FUNCTION_PROFILER
451struct ftrace_profile {
452 struct hlist_node node;
453 unsigned long ip;
454 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400455#ifdef CONFIG_FUNCTION_GRAPH_TRACER
456 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400457 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400458#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400459};
460
461struct ftrace_profile_page {
462 struct ftrace_profile_page *next;
463 unsigned long index;
464 struct ftrace_profile records[];
465};
466
Steven Rostedtcafb1682009-03-24 20:50:39 -0400467struct ftrace_profile_stat {
468 atomic_t disabled;
469 struct hlist_head *hash;
470 struct ftrace_profile_page *pages;
471 struct ftrace_profile_page *start;
472 struct tracer_stat stat;
473};
474
Steven Rostedt493762f2009-03-23 17:12:36 -0400475#define PROFILE_RECORDS_SIZE \
476 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
477
478#define PROFILES_PER_PAGE \
479 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
480
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400481static int ftrace_profile_enabled __read_mostly;
482
483/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400484static DEFINE_MUTEX(ftrace_profile_lock);
485
Steven Rostedtcafb1682009-03-24 20:50:39 -0400486static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400487
Namhyung Kim20079eb2013-04-10 08:55:50 +0900488#define FTRACE_PROFILE_HASH_BITS 10
489#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400490
Steven Rostedt493762f2009-03-23 17:12:36 -0400491static void *
492function_stat_next(void *v, int idx)
493{
494 struct ftrace_profile *rec = v;
495 struct ftrace_profile_page *pg;
496
497 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
498
499 again:
Li Zefan0296e422009-06-26 11:15:37 +0800500 if (idx != 0)
501 rec++;
502
Steven Rostedt493762f2009-03-23 17:12:36 -0400503 if ((void *)rec >= (void *)&pg->records[pg->index]) {
504 pg = pg->next;
505 if (!pg)
506 return NULL;
507 rec = &pg->records[0];
508 if (!rec->counter)
509 goto again;
510 }
511
512 return rec;
513}
514
515static void *function_stat_start(struct tracer_stat *trace)
516{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400517 struct ftrace_profile_stat *stat =
518 container_of(trace, struct ftrace_profile_stat, stat);
519
520 if (!stat || !stat->start)
521 return NULL;
522
523 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400524}
525
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400526#ifdef CONFIG_FUNCTION_GRAPH_TRACER
527/* function graph compares on total time */
528static int function_stat_cmp(void *p1, void *p2)
529{
530 struct ftrace_profile *a = p1;
531 struct ftrace_profile *b = p2;
532
533 if (a->time < b->time)
534 return -1;
535 if (a->time > b->time)
536 return 1;
537 else
538 return 0;
539}
540#else
541/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400542static int function_stat_cmp(void *p1, void *p2)
543{
544 struct ftrace_profile *a = p1;
545 struct ftrace_profile *b = p2;
546
547 if (a->counter < b->counter)
548 return -1;
549 if (a->counter > b->counter)
550 return 1;
551 else
552 return 0;
553}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400554#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400555
556static int function_stat_headers(struct seq_file *m)
557{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400558#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400559 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400560 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400561 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400562 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400563#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400564 seq_printf(m, " Function Hit\n"
565 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400566#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400567 return 0;
568}
569
570static int function_stat_show(struct seq_file *m, void *v)
571{
572 struct ftrace_profile *rec = v;
573 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800574 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400575#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400576 static struct trace_seq s;
577 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400578 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400579#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800580 mutex_lock(&ftrace_profile_lock);
581
582 /* we raced with function_profile_reset() */
583 if (unlikely(rec->counter == 0)) {
584 ret = -EBUSY;
585 goto out;
586 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400587
588 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400589 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400590
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400591#ifdef CONFIG_FUNCTION_GRAPH_TRACER
592 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400593 avg = rec->time;
594 do_div(avg, rec->counter);
595
Chase Douglase330b3b2010-04-26 14:02:05 -0400596 /* Sample standard deviation (s^2) */
597 if (rec->counter <= 1)
598 stddev = 0;
599 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200600 /*
601 * Apply Welford's method:
602 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
603 */
604 stddev = rec->counter * rec->time_squared -
605 rec->time * rec->time;
606
Chase Douglase330b3b2010-04-26 14:02:05 -0400607 /*
608 * Divide only 1000 for ns^2 -> us^2 conversion.
609 * trace_print_graph_duration will divide 1000 again.
610 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200611 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400612 }
613
Steven Rostedt34886c82009-03-25 21:00:47 -0400614 trace_seq_init(&s);
615 trace_print_graph_duration(rec->time, &s);
616 trace_seq_puts(&s, " ");
617 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400618 trace_seq_puts(&s, " ");
619 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400620 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400621#endif
622 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800623out:
624 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400625
Li Zefan3aaba202010-08-23 16:50:12 +0800626 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400627}
628
Steven Rostedtcafb1682009-03-24 20:50:39 -0400629static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400630{
631 struct ftrace_profile_page *pg;
632
Steven Rostedtcafb1682009-03-24 20:50:39 -0400633 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400634
635 while (pg) {
636 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
637 pg->index = 0;
638 pg = pg->next;
639 }
640
Steven Rostedtcafb1682009-03-24 20:50:39 -0400641 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400642 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
643}
644
Steven Rostedtcafb1682009-03-24 20:50:39 -0400645int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400646{
647 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400648 int functions;
649 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400650 int i;
651
652 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400653 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400654 return 0;
655
Steven Rostedtcafb1682009-03-24 20:50:39 -0400656 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
657 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400658 return -ENOMEM;
659
Steven Rostedt318e0a72009-03-25 20:06:34 -0400660#ifdef CONFIG_DYNAMIC_FTRACE
661 functions = ftrace_update_tot_cnt;
662#else
663 /*
664 * We do not know the number of functions that exist because
665 * dynamic tracing is what counts them. With past experience
666 * we have around 20K functions. That should be more than enough.
667 * It is highly unlikely we will execute every function in
668 * the kernel.
669 */
670 functions = 20000;
671#endif
672
Steven Rostedtcafb1682009-03-24 20:50:39 -0400673 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400674
Steven Rostedt318e0a72009-03-25 20:06:34 -0400675 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
676
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900677 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400678 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400679 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400680 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400681 pg = pg->next;
682 }
683
684 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400685
686 out_free:
687 pg = stat->start;
688 while (pg) {
689 unsigned long tmp = (unsigned long)pg;
690
691 pg = pg->next;
692 free_page(tmp);
693 }
694
Steven Rostedt318e0a72009-03-25 20:06:34 -0400695 stat->pages = NULL;
696 stat->start = NULL;
697
698 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400699}
700
Steven Rostedtcafb1682009-03-24 20:50:39 -0400701static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400702{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400703 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400704 int size;
705
Steven Rostedtcafb1682009-03-24 20:50:39 -0400706 stat = &per_cpu(ftrace_profile_stats, cpu);
707
708 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400709 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400710 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400711 return 0;
712 }
713
714 /*
715 * We are profiling all functions, but usually only a few thousand
716 * functions are hit. We'll make a hash of 1024 items.
717 */
718 size = FTRACE_PROFILE_HASH_SIZE;
719
Steven Rostedtcafb1682009-03-24 20:50:39 -0400720 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400721
Steven Rostedtcafb1682009-03-24 20:50:39 -0400722 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400723 return -ENOMEM;
724
Steven Rostedt318e0a72009-03-25 20:06:34 -0400725 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400726 if (ftrace_profile_pages_init(stat) < 0) {
727 kfree(stat->hash);
728 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400729 return -ENOMEM;
730 }
731
732 return 0;
733}
734
Steven Rostedtcafb1682009-03-24 20:50:39 -0400735static int ftrace_profile_init(void)
736{
737 int cpu;
738 int ret = 0;
739
Miao Xiec4602c12013-12-16 15:20:01 +0800740 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741 ret = ftrace_profile_init_cpu(cpu);
742 if (ret)
743 break;
744 }
745
746 return ret;
747}
748
Steven Rostedt493762f2009-03-23 17:12:36 -0400749/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750static struct ftrace_profile *
751ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400752{
753 struct ftrace_profile *rec;
754 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400755 unsigned long key;
756
Namhyung Kim20079eb2013-04-10 08:55:50 +0900757 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400758 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400759
760 if (hlist_empty(hhd))
761 return NULL;
762
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400763 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400764 if (rec->ip == ip)
765 return rec;
766 }
767
768 return NULL;
769}
770
Steven Rostedtcafb1682009-03-24 20:50:39 -0400771static void ftrace_add_profile(struct ftrace_profile_stat *stat,
772 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400773{
774 unsigned long key;
775
Namhyung Kim20079eb2013-04-10 08:55:50 +0900776 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400777 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400778}
779
Steven Rostedt318e0a72009-03-25 20:06:34 -0400780/*
781 * The memory is already allocated, this simply finds a new record to use.
782 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400783static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400784ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400785{
786 struct ftrace_profile *rec = NULL;
787
Steven Rostedt318e0a72009-03-25 20:06:34 -0400788 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400789 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400790 goto out;
791
Steven Rostedt493762f2009-03-23 17:12:36 -0400792 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400793 * Try to find the function again since an NMI
794 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400795 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400796 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400797 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400798 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400799
Steven Rostedtcafb1682009-03-24 20:50:39 -0400800 if (stat->pages->index == PROFILES_PER_PAGE) {
801 if (!stat->pages->next)
802 goto out;
803 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400804 }
805
Steven Rostedtcafb1682009-03-24 20:50:39 -0400806 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400807 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400808 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400809
Steven Rostedt493762f2009-03-23 17:12:36 -0400810 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400811 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400812
813 return rec;
814}
815
Steven Rostedt493762f2009-03-23 17:12:36 -0400816static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400817function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400818 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400819{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400820 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400821 struct ftrace_profile *rec;
822 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400823
824 if (!ftrace_profile_enabled)
825 return;
826
Steven Rostedt493762f2009-03-23 17:12:36 -0400827 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400828
Christoph Lameterbdffd892014-04-29 14:17:40 -0500829 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400830 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400831 goto out;
832
833 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400834 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400835 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400836 if (!rec)
837 goto out;
838 }
839
840 rec->counter++;
841 out:
842 local_irq_restore(flags);
843}
844
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400845#ifdef CONFIG_FUNCTION_GRAPH_TRACER
846static int profile_graph_entry(struct ftrace_graph_ent *trace)
847{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400848 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400849 return 1;
850}
851
852static void profile_graph_return(struct ftrace_graph_ret *trace)
853{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400854 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400855 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400856 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400857 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400858
859 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500860 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400861 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400862 goto out;
863
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400864 /* If the calltime was zero'd ignore it */
865 if (!trace->calltime)
866 goto out;
867
Steven Rostedta2a16d62009-03-24 23:17:58 -0400868 calltime = trace->rettime - trace->calltime;
869
870 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
871 int index;
872
873 index = trace->depth;
874
875 /* Append this call time to the parent time to subtract */
876 if (index)
877 current->ret_stack[index - 1].subtime += calltime;
878
879 if (current->ret_stack[index].subtime < calltime)
880 calltime -= current->ret_stack[index].subtime;
881 else
882 calltime = 0;
883 }
884
Steven Rostedtcafb1682009-03-24 20:50:39 -0400885 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400886 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400887 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400888 rec->time_squared += calltime * calltime;
889 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400890
Steven Rostedtcafb1682009-03-24 20:50:39 -0400891 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400892 local_irq_restore(flags);
893}
894
895static int register_ftrace_profiler(void)
896{
897 return register_ftrace_graph(&profile_graph_return,
898 &profile_graph_entry);
899}
900
901static void unregister_ftrace_profiler(void)
902{
903 unregister_ftrace_graph();
904}
905#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100906static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400907 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900908 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400909 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400910};
911
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400912static int register_ftrace_profiler(void)
913{
914 return register_ftrace_function(&ftrace_profile_ops);
915}
916
917static void unregister_ftrace_profiler(void)
918{
919 unregister_ftrace_function(&ftrace_profile_ops);
920}
921#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
922
Steven Rostedt493762f2009-03-23 17:12:36 -0400923static ssize_t
924ftrace_profile_write(struct file *filp, const char __user *ubuf,
925 size_t cnt, loff_t *ppos)
926{
927 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400928 int ret;
929
Peter Huewe22fe9b52011-06-07 21:58:27 +0200930 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
931 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400932 return ret;
933
934 val = !!val;
935
936 mutex_lock(&ftrace_profile_lock);
937 if (ftrace_profile_enabled ^ val) {
938 if (val) {
939 ret = ftrace_profile_init();
940 if (ret < 0) {
941 cnt = ret;
942 goto out;
943 }
944
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400945 ret = register_ftrace_profiler();
946 if (ret < 0) {
947 cnt = ret;
948 goto out;
949 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400950 ftrace_profile_enabled = 1;
951 } else {
952 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400953 /*
954 * unregister_ftrace_profiler calls stop_machine
955 * so this acts like an synchronize_sched.
956 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400957 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400958 }
959 }
960 out:
961 mutex_unlock(&ftrace_profile_lock);
962
Jiri Olsacf8517c2009-10-23 19:36:16 -0400963 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400964
965 return cnt;
966}
967
968static ssize_t
969ftrace_profile_read(struct file *filp, char __user *ubuf,
970 size_t cnt, loff_t *ppos)
971{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400972 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400973 int r;
974
975 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
976 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
977}
978
979static const struct file_operations ftrace_profile_fops = {
980 .open = tracing_open_generic,
981 .read = ftrace_profile_read,
982 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200983 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400984};
985
Steven Rostedtcafb1682009-03-24 20:50:39 -0400986/* used to initialize the real stat files */
987static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400988 .name = "functions",
989 .stat_start = function_stat_start,
990 .stat_next = function_stat_next,
991 .stat_cmp = function_stat_cmp,
992 .stat_headers = function_stat_headers,
993 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400994};
995
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400996static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400997{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400998 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400999 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001000 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001001 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001002 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001003
Steven Rostedtcafb1682009-03-24 20:50:39 -04001004 for_each_possible_cpu(cpu) {
1005 stat = &per_cpu(ftrace_profile_stats, cpu);
1006
1007 /* allocate enough for function name + cpu number */
1008 name = kmalloc(32, GFP_KERNEL);
1009 if (!name) {
1010 /*
1011 * The files created are permanent, if something happens
1012 * we still do not free memory.
1013 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001014 WARN(1,
1015 "Could not allocate stat file for cpu %d\n",
1016 cpu);
1017 return;
1018 }
1019 stat->stat = function_stats;
1020 snprintf(name, 32, "function%d", cpu);
1021 stat->stat.name = name;
1022 ret = register_stat_tracer(&stat->stat);
1023 if (ret) {
1024 WARN(1,
1025 "Could not register function stat for cpu %d\n",
1026 cpu);
1027 kfree(name);
1028 return;
1029 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001030 }
1031
1032 entry = debugfs_create_file("function_profile_enabled", 0644,
1033 d_tracer, NULL, &ftrace_profile_fops);
1034 if (!entry)
1035 pr_warning("Could not create debugfs "
1036 "'function_profile_enabled' entry\n");
1037}
1038
1039#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001040static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001041{
1042}
1043#endif /* CONFIG_FUNCTION_PROFILER */
1044
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001045static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1046
Steven Rostedt3d083392008-05-12 21:20:42 +02001047#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001048
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001049static struct ftrace_ops *removed_ops;
1050
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001051#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001052# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001053#endif
1054
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001055static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1056
Steven Rostedtb6887d72009-02-17 12:32:04 -05001057struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001058 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001059 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001060 unsigned long flags;
1061 unsigned long ip;
1062 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001063 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001064};
1065
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001066struct ftrace_func_entry {
1067 struct hlist_node hlist;
1068 unsigned long ip;
1069};
1070
1071struct ftrace_hash {
1072 unsigned long size_bits;
1073 struct hlist_head *buckets;
1074 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001075 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001076};
1077
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001078/*
1079 * We make these constant because no one should touch them,
1080 * but they are used as the default "empty hash", to avoid allocating
1081 * it all the time. These are in a read only section such that if
1082 * anyone does try to modify it, it will cause an exception.
1083 */
1084static const struct hlist_head empty_buckets[1];
1085static const struct ftrace_hash empty_hash = {
1086 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001087};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001088#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001089
Steven Rostedt2b499382011-05-03 22:49:52 -04001090static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001091 .func = ftrace_stub,
1092 .local_hash.notrace_hash = EMPTY_HASH,
1093 .local_hash.filter_hash = EMPTY_HASH,
1094 INIT_OPS_HASH(global_ops)
1095 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1096 FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001097};
1098
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001099struct ftrace_page {
1100 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001101 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001102 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001103 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001104};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001105
Steven Rostedta7900872011-12-16 16:23:44 -05001106#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1107#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001108
1109/* estimate from running different kernels */
1110#define NR_TO_INIT 10000
1111
1112static struct ftrace_page *ftrace_pages_start;
1113static struct ftrace_page *ftrace_pages;
1114
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001115static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001116{
1117 return !hash || !hash->count;
1118}
1119
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001120static struct ftrace_func_entry *
1121ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1122{
1123 unsigned long key;
1124 struct ftrace_func_entry *entry;
1125 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001126
Steven Rostedt06a51d92011-12-19 19:07:36 -05001127 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001128 return NULL;
1129
1130 if (hash->size_bits > 0)
1131 key = hash_long(ip, hash->size_bits);
1132 else
1133 key = 0;
1134
1135 hhd = &hash->buckets[key];
1136
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001137 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001138 if (entry->ip == ip)
1139 return entry;
1140 }
1141 return NULL;
1142}
1143
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001144static void __add_hash_entry(struct ftrace_hash *hash,
1145 struct ftrace_func_entry *entry)
1146{
1147 struct hlist_head *hhd;
1148 unsigned long key;
1149
1150 if (hash->size_bits)
1151 key = hash_long(entry->ip, hash->size_bits);
1152 else
1153 key = 0;
1154
1155 hhd = &hash->buckets[key];
1156 hlist_add_head(&entry->hlist, hhd);
1157 hash->count++;
1158}
1159
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001160static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1161{
1162 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001163
1164 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1165 if (!entry)
1166 return -ENOMEM;
1167
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001168 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001169 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001170
1171 return 0;
1172}
1173
1174static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001175free_hash_entry(struct ftrace_hash *hash,
1176 struct ftrace_func_entry *entry)
1177{
1178 hlist_del(&entry->hlist);
1179 kfree(entry);
1180 hash->count--;
1181}
1182
1183static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001184remove_hash_entry(struct ftrace_hash *hash,
1185 struct ftrace_func_entry *entry)
1186{
1187 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001188 hash->count--;
1189}
1190
1191static void ftrace_hash_clear(struct ftrace_hash *hash)
1192{
1193 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001194 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001195 struct ftrace_func_entry *entry;
1196 int size = 1 << hash->size_bits;
1197 int i;
1198
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001199 if (!hash->count)
1200 return;
1201
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001202 for (i = 0; i < size; i++) {
1203 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001204 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001205 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001206 }
1207 FTRACE_WARN_ON(hash->count);
1208}
1209
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001210static void free_ftrace_hash(struct ftrace_hash *hash)
1211{
1212 if (!hash || hash == EMPTY_HASH)
1213 return;
1214 ftrace_hash_clear(hash);
1215 kfree(hash->buckets);
1216 kfree(hash);
1217}
1218
Steven Rostedt07fd5512011-05-05 18:03:47 -04001219static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1220{
1221 struct ftrace_hash *hash;
1222
1223 hash = container_of(rcu, struct ftrace_hash, rcu);
1224 free_ftrace_hash(hash);
1225}
1226
1227static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1228{
1229 if (!hash || hash == EMPTY_HASH)
1230 return;
1231 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1232}
1233
Jiri Olsa5500fa52012-02-15 15:51:54 +01001234void ftrace_free_filter(struct ftrace_ops *ops)
1235{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001236 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001237 free_ftrace_hash(ops->func_hash->filter_hash);
1238 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001239}
1240
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001241static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1242{
1243 struct ftrace_hash *hash;
1244 int size;
1245
1246 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1247 if (!hash)
1248 return NULL;
1249
1250 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001251 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001252
1253 if (!hash->buckets) {
1254 kfree(hash);
1255 return NULL;
1256 }
1257
1258 hash->size_bits = size_bits;
1259
1260 return hash;
1261}
1262
1263static struct ftrace_hash *
1264alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1265{
1266 struct ftrace_func_entry *entry;
1267 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001268 int size;
1269 int ret;
1270 int i;
1271
1272 new_hash = alloc_ftrace_hash(size_bits);
1273 if (!new_hash)
1274 return NULL;
1275
1276 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001277 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001278 return new_hash;
1279
1280 size = 1 << hash->size_bits;
1281 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001282 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001283 ret = add_hash_entry(new_hash, entry->ip);
1284 if (ret < 0)
1285 goto free_hash;
1286 }
1287 }
1288
1289 FTRACE_WARN_ON(new_hash->count != hash->count);
1290
1291 return new_hash;
1292
1293 free_hash:
1294 free_ftrace_hash(new_hash);
1295 return NULL;
1296}
1297
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001298static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001299ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001300static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001301ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001302
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001303static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001304ftrace_hash_move(struct ftrace_ops *ops, int enable,
1305 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001306{
1307 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001308 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001309 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001310 struct ftrace_hash *old_hash;
1311 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001312 int size = src->count;
1313 int bits = 0;
1314 int i;
1315
1316 /*
1317 * If the new source is empty, just free dst and assign it
1318 * the empty_hash.
1319 */
1320 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001321 new_hash = EMPTY_HASH;
1322 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001323 }
1324
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001325 /*
1326 * Make the hash size about 1/2 the # found
1327 */
1328 for (size /= 2; size; size >>= 1)
1329 bits++;
1330
1331 /* Don't allocate too much */
1332 if (bits > FTRACE_HASH_MAX_BITS)
1333 bits = FTRACE_HASH_MAX_BITS;
1334
Steven Rostedt07fd5512011-05-05 18:03:47 -04001335 new_hash = alloc_ftrace_hash(bits);
1336 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001337 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001338
1339 size = 1 << src->size_bits;
1340 for (i = 0; i < size; i++) {
1341 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001342 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001343 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001344 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001345 }
1346 }
1347
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001348update:
1349 /*
1350 * Remove the current set, update the hash and add
1351 * them back.
1352 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001353 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001354
Steven Rostedt07fd5512011-05-05 18:03:47 -04001355 old_hash = *dst;
1356 rcu_assign_pointer(*dst, new_hash);
1357 free_ftrace_hash_rcu(old_hash);
1358
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001359 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001360
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001361 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001362}
1363
Steven Rostedt265c8312009-02-13 12:43:56 -05001364/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001365 * Test the hashes for this ops to see if we want to call
1366 * the ops->func or not.
1367 *
1368 * It's a match if the ip is in the ops->filter_hash or
1369 * the filter_hash does not exist or is empty,
1370 * AND
1371 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001372 *
1373 * This needs to be called with preemption disabled as
1374 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001375 */
1376static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001377ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001378{
1379 struct ftrace_hash *filter_hash;
1380 struct ftrace_hash *notrace_hash;
1381 int ret;
1382
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001383#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1384 /*
1385 * There's a small race when adding ops that the ftrace handler
1386 * that wants regs, may be called without them. We can not
1387 * allow that handler to be called if regs is NULL.
1388 */
1389 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1390 return 0;
1391#endif
1392
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001393 filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1394 notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001395
Steven Rostedt06a51d92011-12-19 19:07:36 -05001396 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001397 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001398 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001399 !ftrace_lookup_ip(notrace_hash, ip)))
1400 ret = 1;
1401 else
1402 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001403
1404 return ret;
1405}
1406
1407/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001408 * This is a double for. Do not use 'break' to break out of the loop,
1409 * you must use a goto.
1410 */
1411#define do_for_each_ftrace_rec(pg, rec) \
1412 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1413 int _____i; \
1414 for (_____i = 0; _____i < pg->index; _____i++) { \
1415 rec = &pg->records[_____i];
1416
1417#define while_for_each_ftrace_rec() \
1418 } \
1419 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301420
Steven Rostedt5855fea2011-12-16 19:27:42 -05001421
1422static int ftrace_cmp_recs(const void *a, const void *b)
1423{
Steven Rostedta650e022012-04-25 13:48:13 -04001424 const struct dyn_ftrace *key = a;
1425 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001426
Steven Rostedta650e022012-04-25 13:48:13 -04001427 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001428 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001429 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1430 return 1;
1431 return 0;
1432}
1433
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001434static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001435{
1436 struct ftrace_page *pg;
1437 struct dyn_ftrace *rec;
1438 struct dyn_ftrace key;
1439
1440 key.ip = start;
1441 key.flags = end; /* overload flags, as it is unsigned long */
1442
1443 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1444 if (end < pg->records[0].ip ||
1445 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1446 continue;
1447 rec = bsearch(&key, pg->records, pg->index,
1448 sizeof(struct dyn_ftrace),
1449 ftrace_cmp_recs);
1450 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001451 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001452 }
1453
Steven Rostedt5855fea2011-12-16 19:27:42 -05001454 return 0;
1455}
1456
Steven Rostedtc88fd862011-08-16 09:53:39 -04001457/**
1458 * ftrace_location - return true if the ip giving is a traced location
1459 * @ip: the instruction pointer to check
1460 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001461 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001462 * That is, the instruction that is either a NOP or call to
1463 * the function tracer. It checks the ftrace internal tables to
1464 * determine if the address belongs or not.
1465 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001466unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001467{
Steven Rostedta650e022012-04-25 13:48:13 -04001468 return ftrace_location_range(ip, ip);
1469}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001470
Steven Rostedta650e022012-04-25 13:48:13 -04001471/**
1472 * ftrace_text_reserved - return true if range contains an ftrace location
1473 * @start: start of range to search
1474 * @end: end of range to search (inclusive). @end points to the last byte to check.
1475 *
1476 * Returns 1 if @start and @end contains a ftrace location.
1477 * That is, the instruction that is either a NOP or call to
1478 * the function tracer. It checks the ftrace internal tables to
1479 * determine if the address belongs or not.
1480 */
Sasha Levind88471c2013-01-09 18:09:20 -05001481int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001482{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001483 unsigned long ret;
1484
1485 ret = ftrace_location_range((unsigned long)start,
1486 (unsigned long)end);
1487
1488 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001489}
1490
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001491/* Test if ops registered to this rec needs regs */
1492static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1493{
1494 struct ftrace_ops *ops;
1495 bool keep_regs = false;
1496
1497 for (ops = ftrace_ops_list;
1498 ops != &ftrace_list_end; ops = ops->next) {
1499 /* pass rec in as regs to have non-NULL val */
1500 if (ftrace_ops_test(ops, rec->ip, rec)) {
1501 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1502 keep_regs = true;
1503 break;
1504 }
1505 }
1506 }
1507
1508 return keep_regs;
1509}
1510
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001511static void ftrace_remove_tramp(struct ftrace_ops *ops,
1512 struct dyn_ftrace *rec)
1513{
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001514 /* If TRAMP is not set, no ops should have a trampoline for this */
1515 if (!(rec->flags & FTRACE_FL_TRAMP))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001516 return;
1517
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001518 rec->flags &= ~FTRACE_FL_TRAMP;
1519
1520 if ((!ftrace_hash_empty(ops->func_hash->filter_hash) &&
1521 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip)) ||
1522 ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
1523 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001524 /*
1525 * The tramp_hash entry will be removed at time
1526 * of update.
1527 */
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04001528 ops->nr_trampolines--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001529}
1530
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001531static void ftrace_clear_tramps(struct dyn_ftrace *rec, struct ftrace_ops *ops)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001532{
1533 struct ftrace_ops *op;
1534
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001535 /* If TRAMP is not set, no ops should have a trampoline for this */
1536 if (!(rec->flags & FTRACE_FL_TRAMP))
1537 return;
1538
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001539 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001540 /*
1541 * This function is called to clear other tramps
1542 * not the one that is being updated.
1543 */
1544 if (op == ops)
1545 continue;
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04001546 if (op->nr_trampolines)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001547 ftrace_remove_tramp(op, rec);
1548 } while_for_each_ftrace_op(op);
1549}
1550
Steven Rostedted926f92011-05-03 13:25:24 -04001551static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1552 int filter_hash,
1553 bool inc)
1554{
1555 struct ftrace_hash *hash;
1556 struct ftrace_hash *other_hash;
1557 struct ftrace_page *pg;
1558 struct dyn_ftrace *rec;
1559 int count = 0;
1560 int all = 0;
1561
1562 /* Only update if the ops has been registered */
1563 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1564 return;
1565
1566 /*
1567 * In the filter_hash case:
1568 * If the count is zero, we update all records.
1569 * Otherwise we just update the items in the hash.
1570 *
1571 * In the notrace_hash case:
1572 * We enable the update in the hash.
1573 * As disabling notrace means enabling the tracing,
1574 * and enabling notrace means disabling, the inc variable
1575 * gets inversed.
1576 */
1577 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001578 hash = ops->func_hash->filter_hash;
1579 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001580 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001581 all = 1;
1582 } else {
1583 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001584 hash = ops->func_hash->notrace_hash;
1585 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001586 /*
1587 * If the notrace hash has no items,
1588 * then there's nothing to do.
1589 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001590 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001591 return;
1592 }
1593
1594 do_for_each_ftrace_rec(pg, rec) {
1595 int in_other_hash = 0;
1596 int in_hash = 0;
1597 int match = 0;
1598
1599 if (all) {
1600 /*
1601 * Only the filter_hash affects all records.
1602 * Update if the record is not in the notrace hash.
1603 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001604 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001605 match = 1;
1606 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001607 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1608 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001609
1610 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001611 * If filter_hash is set, we want to match all functions
1612 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001613 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001614 * If filter_hash is not set, then we are decrementing.
1615 * That means we match anything that is in the hash
1616 * and also in the other_hash. That is, we need to turn
1617 * off functions in the other hash because they are disabled
1618 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001619 */
1620 if (filter_hash && in_hash && !in_other_hash)
1621 match = 1;
1622 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001623 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001624 match = 1;
1625 }
1626 if (!match)
1627 continue;
1628
1629 if (inc) {
1630 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001631 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001632 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001633
1634 /*
1635 * If there's only a single callback registered to a
1636 * function, and the ops has a trampoline registered
1637 * for it, then we can call it directly.
1638 */
1639 if (ftrace_rec_count(rec) == 1 && ops->trampoline) {
1640 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04001641 ops->nr_trampolines++;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001642 } else {
1643 /*
1644 * If we are adding another function callback
1645 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001646 * custom trampoline in use, then we need to go
1647 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001648 */
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001649 ftrace_clear_tramps(rec, ops);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001650 }
1651
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001652 /*
1653 * If any ops wants regs saved for this function
1654 * then all ops will get saved regs.
1655 */
1656 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1657 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001658 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001659 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Steven Rostedted926f92011-05-03 13:25:24 -04001660 return;
1661 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001662
1663 if (ops->trampoline && !ftrace_rec_count(rec))
1664 ftrace_remove_tramp(ops, rec);
1665
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001666 /*
1667 * If the rec had REGS enabled and the ops that is
1668 * being removed had REGS set, then see if there is
1669 * still any ops for this record that wants regs.
1670 * If not, we can stop recording them.
1671 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001672 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001673 rec->flags & FTRACE_FL_REGS &&
1674 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1675 if (!test_rec_ops_needs_regs(rec))
1676 rec->flags &= ~FTRACE_FL_REGS;
1677 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001678
1679 /*
1680 * flags will be cleared in ftrace_check_record()
1681 * if rec count is zero.
1682 */
Steven Rostedted926f92011-05-03 13:25:24 -04001683 }
1684 count++;
1685 /* Shortcut, if we handled all records, we are done. */
1686 if (!all && count == hash->count)
1687 return;
1688 } while_for_each_ftrace_rec();
1689}
1690
1691static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1692 int filter_hash)
1693{
1694 __ftrace_hash_rec_update(ops, filter_hash, 0);
1695}
1696
1697static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1698 int filter_hash)
1699{
1700 __ftrace_hash_rec_update(ops, filter_hash, 1);
1701}
1702
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001703static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1704 int filter_hash, int inc)
1705{
1706 struct ftrace_ops *op;
1707
1708 __ftrace_hash_rec_update(ops, filter_hash, inc);
1709
1710 if (ops->func_hash != &global_ops.local_hash)
1711 return;
1712
1713 /*
1714 * If the ops shares the global_ops hash, then we need to update
1715 * all ops that are enabled and use this hash.
1716 */
1717 do_for_each_ftrace_op(op, ftrace_ops_list) {
1718 /* Already done */
1719 if (op == ops)
1720 continue;
1721 if (op->func_hash == &global_ops.local_hash)
1722 __ftrace_hash_rec_update(op, filter_hash, inc);
1723 } while_for_each_ftrace_op(op);
1724}
1725
1726static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1727 int filter_hash)
1728{
1729 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1730}
1731
1732static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1733 int filter_hash)
1734{
1735 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1736}
1737
Steven Rostedt05736a42008-09-22 14:55:47 -07001738static void print_ip_ins(const char *fmt, unsigned char *p)
1739{
1740 int i;
1741
1742 printk(KERN_CONT "%s", fmt);
1743
1744 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1745 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1746}
1747
Steven Rostedtc88fd862011-08-16 09:53:39 -04001748/**
1749 * ftrace_bug - report and shutdown function tracer
1750 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1751 * @ip: The address that failed
1752 *
1753 * The arch code that enables or disables the function tracing
1754 * can call ftrace_bug() when it has detected a problem in
1755 * modifying the code. @failed should be one of either:
1756 * EFAULT - if the problem happens on reading the @ip address
1757 * EINVAL - if what is read at @ip is not what was expected
1758 * EPERM - if the problem happens on writting to the @ip address
1759 */
1760void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001761{
1762 switch (failed) {
1763 case -EFAULT:
1764 FTRACE_WARN_ON_ONCE(1);
1765 pr_info("ftrace faulted on modifying ");
1766 print_ip_sym(ip);
1767 break;
1768 case -EINVAL:
1769 FTRACE_WARN_ON_ONCE(1);
1770 pr_info("ftrace failed to modify ");
1771 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001772 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001773 printk(KERN_CONT "\n");
1774 break;
1775 case -EPERM:
1776 FTRACE_WARN_ON_ONCE(1);
1777 pr_info("ftrace faulted on writing ");
1778 print_ip_sym(ip);
1779 break;
1780 default:
1781 FTRACE_WARN_ON_ONCE(1);
1782 pr_info("ftrace faulted on unknown error ");
1783 print_ip_sym(ip);
1784 }
1785}
1786
Steven Rostedtc88fd862011-08-16 09:53:39 -04001787static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001788{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001789 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001790
Steven Rostedt982c3502008-11-15 16:31:41 -05001791 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001792 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001793 *
Steven Rostedted926f92011-05-03 13:25:24 -04001794 * If the record has a ref count, then we need to enable it
1795 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001796 *
Steven Rostedted926f92011-05-03 13:25:24 -04001797 * Otherwise we make sure its disabled.
1798 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001799 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001800 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001801 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001802 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04001803 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001804
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001805 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001806 * If enabling and the REGS flag does not match the REGS_EN, or
1807 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1808 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001809 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001810 if (flag) {
1811 if (!(rec->flags & FTRACE_FL_REGS) !=
1812 !(rec->flags & FTRACE_FL_REGS_EN))
1813 flag |= FTRACE_FL_REGS;
1814
1815 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1816 !(rec->flags & FTRACE_FL_TRAMP_EN))
1817 flag |= FTRACE_FL_TRAMP;
1818 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001819
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001820 /* If the state of this record hasn't changed, then do nothing */
1821 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001822 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001823
1824 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001825 /* Save off if rec is being enabled (for return value) */
1826 flag ^= rec->flags & FTRACE_FL_ENABLED;
1827
1828 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001829 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001830 if (flag & FTRACE_FL_REGS) {
1831 if (rec->flags & FTRACE_FL_REGS)
1832 rec->flags |= FTRACE_FL_REGS_EN;
1833 else
1834 rec->flags &= ~FTRACE_FL_REGS_EN;
1835 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001836 if (flag & FTRACE_FL_TRAMP) {
1837 if (rec->flags & FTRACE_FL_TRAMP)
1838 rec->flags |= FTRACE_FL_TRAMP_EN;
1839 else
1840 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1841 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001842 }
1843
1844 /*
1845 * If this record is being updated from a nop, then
1846 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001847 * Otherwise,
1848 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001849 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001850 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001851 */
1852 if (flag & FTRACE_FL_ENABLED)
1853 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001854
1855 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001856 }
1857
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001858 if (update) {
1859 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001860 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001861 rec->flags = 0;
1862 else
1863 /* Just disable the record (keep REGS state) */
1864 rec->flags &= ~FTRACE_FL_ENABLED;
1865 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001866
1867 return FTRACE_UPDATE_MAKE_NOP;
1868}
1869
1870/**
1871 * ftrace_update_record, set a record that now is tracing or not
1872 * @rec: the record to update
1873 * @enable: set to 1 if the record is tracing, zero to force disable
1874 *
1875 * The records that represent all functions that can be traced need
1876 * to be updated when tracing has been enabled.
1877 */
1878int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1879{
1880 return ftrace_check_record(rec, enable, 1);
1881}
1882
1883/**
1884 * ftrace_test_record, check if the record has been enabled or not
1885 * @rec: the record to test
1886 * @enable: set to 1 to check if enabled, 0 if it is disabled
1887 *
1888 * The arch code may need to test if a record is already set to
1889 * tracing to determine how to modify the function code that it
1890 * represents.
1891 */
1892int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1893{
1894 return ftrace_check_record(rec, enable, 0);
1895}
1896
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001897static struct ftrace_ops *
1898ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1899{
1900 struct ftrace_ops *op;
1901
1902 /* Removed ops need to be tested first */
1903 if (removed_ops && removed_ops->tramp_hash) {
1904 if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip))
1905 return removed_ops;
1906 }
1907
1908 do_for_each_ftrace_op(op, ftrace_ops_list) {
1909 if (!op->tramp_hash)
1910 continue;
1911
1912 if (ftrace_lookup_ip(op->tramp_hash, rec->ip))
1913 return op;
1914
1915 } while_for_each_ftrace_op(op);
1916
1917 return NULL;
1918}
1919
1920static struct ftrace_ops *
1921ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1922{
1923 struct ftrace_ops *op;
1924
1925 do_for_each_ftrace_op(op, ftrace_ops_list) {
1926 /* pass rec in as regs to have non-NULL val */
1927 if (ftrace_ops_test(op, rec->ip, rec))
1928 return op;
1929 } while_for_each_ftrace_op(op);
1930
1931 return NULL;
1932}
1933
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001934/**
1935 * ftrace_get_addr_new - Get the call address to set to
1936 * @rec: The ftrace record descriptor
1937 *
1938 * If the record has the FTRACE_FL_REGS set, that means that it
1939 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1940 * is not not set, then it wants to convert to the normal callback.
1941 *
1942 * Returns the address of the trampoline to set to
1943 */
1944unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1945{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001946 struct ftrace_ops *ops;
1947
1948 /* Trampolines take precedence over regs */
1949 if (rec->flags & FTRACE_FL_TRAMP) {
1950 ops = ftrace_find_tramp_ops_new(rec);
1951 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001952 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
1953 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001954 /* Ftrace is shutting down, return anything */
1955 return (unsigned long)FTRACE_ADDR;
1956 }
1957 return ops->trampoline;
1958 }
1959
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001960 if (rec->flags & FTRACE_FL_REGS)
1961 return (unsigned long)FTRACE_REGS_ADDR;
1962 else
1963 return (unsigned long)FTRACE_ADDR;
1964}
1965
1966/**
1967 * ftrace_get_addr_curr - Get the call address that is already there
1968 * @rec: The ftrace record descriptor
1969 *
1970 * The FTRACE_FL_REGS_EN is set when the record already points to
1971 * a function that saves all the regs. Basically the '_EN' version
1972 * represents the current state of the function.
1973 *
1974 * Returns the address of the trampoline that is currently being called
1975 */
1976unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1977{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001978 struct ftrace_ops *ops;
1979
1980 /* Trampolines take precedence over regs */
1981 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1982 ops = ftrace_find_tramp_ops_curr(rec);
1983 if (FTRACE_WARN_ON(!ops)) {
1984 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1985 (void *)rec->ip, (void *)rec->ip);
1986 /* Ftrace is shutting down, return anything */
1987 return (unsigned long)FTRACE_ADDR;
1988 }
1989 return ops->trampoline;
1990 }
1991
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001992 if (rec->flags & FTRACE_FL_REGS_EN)
1993 return (unsigned long)FTRACE_REGS_ADDR;
1994 else
1995 return (unsigned long)FTRACE_ADDR;
1996}
1997
Steven Rostedtc88fd862011-08-16 09:53:39 -04001998static int
1999__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2000{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002001 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002002 unsigned long ftrace_addr;
2003 int ret;
2004
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002005 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002006
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002007 /* This needs to be done before we call ftrace_update_record */
2008 ftrace_old_addr = ftrace_get_addr_curr(rec);
2009
2010 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002011
Steven Rostedtc88fd862011-08-16 09:53:39 -04002012 switch (ret) {
2013 case FTRACE_UPDATE_IGNORE:
2014 return 0;
2015
2016 case FTRACE_UPDATE_MAKE_CALL:
2017 return ftrace_make_call(rec, ftrace_addr);
2018
2019 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002020 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002021
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002022 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002023 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002024 }
2025
2026 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002027}
2028
Steven Rostedte4f5d542012-04-27 09:13:18 -04002029void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002030{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002031 struct dyn_ftrace *rec;
2032 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002033 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002034
Steven Rostedt45a4a232011-04-21 23:16:46 -04002035 if (unlikely(ftrace_disabled))
2036 return;
2037
Steven Rostedt265c8312009-02-13 12:43:56 -05002038 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04002039 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002040 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04002041 ftrace_bug(failed, rec->ip);
2042 /* Stop processing */
2043 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002044 }
2045 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002046}
2047
Steven Rostedtc88fd862011-08-16 09:53:39 -04002048struct ftrace_rec_iter {
2049 struct ftrace_page *pg;
2050 int index;
2051};
2052
2053/**
2054 * ftrace_rec_iter_start, start up iterating over traced functions
2055 *
2056 * Returns an iterator handle that is used to iterate over all
2057 * the records that represent address locations where functions
2058 * are traced.
2059 *
2060 * May return NULL if no records are available.
2061 */
2062struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2063{
2064 /*
2065 * We only use a single iterator.
2066 * Protected by the ftrace_lock mutex.
2067 */
2068 static struct ftrace_rec_iter ftrace_rec_iter;
2069 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2070
2071 iter->pg = ftrace_pages_start;
2072 iter->index = 0;
2073
2074 /* Could have empty pages */
2075 while (iter->pg && !iter->pg->index)
2076 iter->pg = iter->pg->next;
2077
2078 if (!iter->pg)
2079 return NULL;
2080
2081 return iter;
2082}
2083
2084/**
2085 * ftrace_rec_iter_next, get the next record to process.
2086 * @iter: The handle to the iterator.
2087 *
2088 * Returns the next iterator after the given iterator @iter.
2089 */
2090struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2091{
2092 iter->index++;
2093
2094 if (iter->index >= iter->pg->index) {
2095 iter->pg = iter->pg->next;
2096 iter->index = 0;
2097
2098 /* Could have empty pages */
2099 while (iter->pg && !iter->pg->index)
2100 iter->pg = iter->pg->next;
2101 }
2102
2103 if (!iter->pg)
2104 return NULL;
2105
2106 return iter;
2107}
2108
2109/**
2110 * ftrace_rec_iter_record, get the record at the iterator location
2111 * @iter: The current iterator location
2112 *
2113 * Returns the record that the current @iter is at.
2114 */
2115struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2116{
2117 return &iter->pg->records[iter->index];
2118}
2119
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302120static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002121ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002122{
2123 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002124 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002125
2126 ip = rec->ip;
2127
Steven Rostedt45a4a232011-04-21 23:16:46 -04002128 if (unlikely(ftrace_disabled))
2129 return 0;
2130
Shaohua Li25aac9d2009-01-09 11:29:40 +08002131 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002132 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08002133 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302134 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002135 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302136 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002137}
2138
Steven Rostedt000ab692009-02-17 13:35:06 -05002139/*
2140 * archs can override this function if they must do something
2141 * before the modifying code is performed.
2142 */
2143int __weak ftrace_arch_code_modify_prepare(void)
2144{
2145 return 0;
2146}
2147
2148/*
2149 * archs can override this function if they must do something
2150 * after the modifying code is performed.
2151 */
2152int __weak ftrace_arch_code_modify_post_process(void)
2153{
2154 return 0;
2155}
2156
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002157void ftrace_modify_all_code(int command)
2158{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002159 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002160 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002161
2162 /*
2163 * If the ftrace_caller calls a ftrace_ops func directly,
2164 * we need to make sure that it only traces functions it
2165 * expects to trace. When doing the switch of functions,
2166 * we need to update to the ftrace_ops_list_func first
2167 * before the transition between old and new calls are set,
2168 * as the ftrace_ops_list_func will check the ops hashes
2169 * to make sure the ops are having the right functions
2170 * traced.
2171 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002172 if (update) {
2173 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2174 if (FTRACE_WARN_ON(err))
2175 return;
2176 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002177
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002178 if (command & FTRACE_UPDATE_CALLS)
2179 ftrace_replace_code(1);
2180 else if (command & FTRACE_DISABLE_CALLS)
2181 ftrace_replace_code(0);
2182
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002183 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2184 function_trace_op = set_function_trace_op;
2185 smp_wmb();
2186 /* If irqs are disabled, we are in stop machine */
2187 if (!irqs_disabled())
2188 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002189 err = ftrace_update_ftrace_func(ftrace_trace_function);
2190 if (FTRACE_WARN_ON(err))
2191 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002192 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002193
2194 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002195 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002196 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002197 err = ftrace_disable_ftrace_graph_caller();
2198 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002199}
2200
Ingo Molnare309b412008-05-12 21:20:51 +02002201static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002202{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002203 int *command = data;
2204
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002205 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002206
Steven Rostedtc88fd862011-08-16 09:53:39 -04002207 return 0;
2208}
2209
2210/**
2211 * ftrace_run_stop_machine, go back to the stop machine method
2212 * @command: The command to tell ftrace what to do
2213 *
2214 * If an arch needs to fall back to the stop machine method, the
2215 * it can call this function.
2216 */
2217void ftrace_run_stop_machine(int command)
2218{
2219 stop_machine(__ftrace_modify_code, &command, NULL);
2220}
2221
2222/**
2223 * arch_ftrace_update_code, modify the code to trace or not trace
2224 * @command: The command that needs to be done
2225 *
2226 * Archs can override this function if it does not need to
2227 * run stop_machine() to modify code.
2228 */
2229void __weak arch_ftrace_update_code(int command)
2230{
2231 ftrace_run_stop_machine(command);
2232}
2233
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002234static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2235{
2236 struct ftrace_page *pg;
2237 struct dyn_ftrace *rec;
2238 int size, bits;
2239 int ret;
2240
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04002241 size = ops->nr_trampolines;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002242 bits = 0;
2243 /*
2244 * Make the hash size about 1/2 the # found
2245 */
2246 for (size /= 2; size; size >>= 1)
2247 bits++;
2248
2249 ops->tramp_hash = alloc_ftrace_hash(bits);
2250 /*
2251 * TODO: a failed allocation is going to screw up
2252 * the accounting of what needs to be modified
2253 * and not. For now, we kill ftrace if we fail
2254 * to allocate here. But there are ways around this,
2255 * but that will take a little more work.
2256 */
2257 if (!ops->tramp_hash)
2258 return -ENOMEM;
2259
2260 do_for_each_ftrace_rec(pg, rec) {
2261 if (ftrace_rec_count(rec) == 1 &&
2262 ftrace_ops_test(ops, rec->ip, rec)) {
2263
Steven Rostedt (Red Hat)2a0343b2014-07-24 09:56:27 -04002264 /*
2265 * If another ops adds to a rec, the rec will
2266 * lose its trampoline and never get it back
2267 * until all ops are off of it.
2268 */
2269 if (!(rec->flags & FTRACE_FL_TRAMP))
2270 continue;
2271
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002272 /* This record had better have a trampoline */
2273 if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2274 return -1;
2275
2276 ret = add_hash_entry(ops->tramp_hash, rec->ip);
2277 if (ret < 0)
2278 return ret;
2279 }
2280 } while_for_each_ftrace_rec();
2281
Steven Rostedt (Red Hat)dc6f03f2014-07-24 11:26:11 -04002282 /* The number of recs in the hash must match nr_trampolines */
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002283 if (FTRACE_WARN_ON(ops->tramp_hash->count != ops->nr_trampolines))
2284 pr_warn("count=%ld trampolines=%d\n",
2285 ops->tramp_hash->count,
2286 ops->nr_trampolines);
Steven Rostedt (Red Hat)dc6f03f2014-07-24 11:26:11 -04002287
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002288 return 0;
2289}
2290
2291static int ftrace_save_tramp_hashes(void)
2292{
2293 struct ftrace_ops *op;
2294 int ret;
2295
2296 /*
2297 * Now that any trampoline is being used, we need to save the
2298 * hashes for the ops that have them. This allows the mapping
2299 * back from the record to the ops that has the trampoline to
2300 * know what code is being replaced. Modifying code must always
2301 * verify what it is changing.
2302 */
2303 do_for_each_ftrace_op(op, ftrace_ops_list) {
2304
2305 /* The tramp_hash is recreated each time. */
2306 free_ftrace_hash(op->tramp_hash);
2307 op->tramp_hash = NULL;
2308
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04002309 if (op->nr_trampolines) {
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002310 ret = ftrace_save_ops_tramp_hash(op);
2311 if (ret)
2312 return ret;
2313 }
2314
2315 } while_for_each_ftrace_op(op);
2316
2317 return 0;
2318}
2319
Steven Rostedtc88fd862011-08-16 09:53:39 -04002320static void ftrace_run_update_code(int command)
2321{
2322 int ret;
2323
2324 ret = ftrace_arch_code_modify_prepare();
2325 FTRACE_WARN_ON(ret);
2326 if (ret)
2327 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002328
2329 /*
2330 * By default we use stop_machine() to modify the code.
2331 * But archs can do what ever they want as long as it
2332 * is safe. The stop_machine() is the safest, but also
2333 * produces the most overhead.
2334 */
2335 arch_ftrace_update_code(command);
2336
Steven Rostedt000ab692009-02-17 13:35:06 -05002337 ret = ftrace_arch_code_modify_post_process();
2338 FTRACE_WARN_ON(ret);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002339
2340 ret = ftrace_save_tramp_hashes();
2341 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002342}
2343
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002344static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002345static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002346
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002347static void control_ops_free(struct ftrace_ops *ops)
2348{
2349 free_percpu(ops->disabled);
2350}
2351
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002352static void ftrace_startup_enable(int command)
2353{
2354 if (saved_ftrace_func != ftrace_trace_function) {
2355 saved_ftrace_func = ftrace_trace_function;
2356 command |= FTRACE_UPDATE_TRACE_FUNC;
2357 }
2358
2359 if (!command || !ftrace_enabled)
2360 return;
2361
2362 ftrace_run_update_code(command);
2363}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002364
Steven Rostedta1cd6172011-05-23 15:24:25 -04002365static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002366{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002367 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002368
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002369 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002370 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002371
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002372 ret = __register_ftrace_function(ops);
2373 if (ret)
2374 return ret;
2375
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002376 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002377 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002378
Steven Rostedted926f92011-05-03 13:25:24 -04002379 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002380
2381 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002382
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002383 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002384
2385 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002386}
2387
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002388static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002389{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002390 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002391
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002392 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002393 return -ENODEV;
2394
2395 ret = __unregister_ftrace_function(ops);
2396 if (ret)
2397 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002398
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002399 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002400 /*
2401 * Just warn in case of unbalance, no need to kill ftrace, it's not
2402 * critical but the ftrace_call callers may be never nopped again after
2403 * further ftrace uses.
2404 */
2405 WARN_ON_ONCE(ftrace_start_up < 0);
2406
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002407 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002408
Namhyung Kima737e6d2014-06-12 23:56:12 +09002409 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002410
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002411 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002412
2413 if (saved_ftrace_func != ftrace_trace_function) {
2414 saved_ftrace_func = ftrace_trace_function;
2415 command |= FTRACE_UPDATE_TRACE_FUNC;
2416 }
2417
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002418 if (!command || !ftrace_enabled) {
2419 /*
2420 * If these are control ops, they still need their
2421 * per_cpu field freed. Since, function tracing is
2422 * not currently active, we can just free them
2423 * without synchronizing all CPUs.
2424 */
2425 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2426 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002427 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002428 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002429
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002430 /*
2431 * If the ops uses a trampoline, then it needs to be
2432 * tested first on update.
2433 */
2434 removed_ops = ops;
2435
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002436 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002437
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002438 removed_ops = NULL;
2439
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002440 /*
2441 * Dynamic ops may be freed, we must make sure that all
2442 * callers are done before leaving this function.
2443 * The same goes for freeing the per_cpu data of the control
2444 * ops.
2445 *
2446 * Again, normal synchronize_sched() is not good enough.
2447 * We need to do a hard force of sched synchronization.
2448 * This is because we use preempt_disable() to do RCU, but
2449 * the function tracers can be called where RCU is not watching
2450 * (like before user_exit()). We can not rely on the RCU
2451 * infrastructure to do the synchronization, thus we must do it
2452 * ourselves.
2453 */
2454 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2455 schedule_on_each_cpu(ftrace_sync);
2456
2457 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2458 control_ops_free(ops);
2459 }
2460
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002461 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002462}
2463
Ingo Molnare309b412008-05-12 21:20:51 +02002464static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002465{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002466 if (unlikely(ftrace_disabled))
2467 return;
2468
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002469 /* Force update next time */
2470 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002471 /* ftrace_start_up is true if we want ftrace running */
2472 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002473 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002474}
2475
Ingo Molnare309b412008-05-12 21:20:51 +02002476static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002477{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002478 if (unlikely(ftrace_disabled))
2479 return;
2480
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002481 /* ftrace_start_up is true if ftrace is running */
2482 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002483 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002484}
2485
Steven Rostedt3d083392008-05-12 21:20:42 +02002486static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002487unsigned long ftrace_update_tot_cnt;
2488
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002489static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002490{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002491 /*
2492 * Filter_hash being empty will default to trace module.
2493 * But notrace hash requires a test of individual module functions.
2494 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002495 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2496 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002497}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002498
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002499/*
2500 * Check if the current ops references the record.
2501 *
2502 * If the ops traces all functions, then it was already accounted for.
2503 * If the ops does not trace the current record function, skip it.
2504 * If the ops ignores the function via notrace filter, skip it.
2505 */
2506static inline bool
2507ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2508{
2509 /* If ops isn't enabled, ignore it */
2510 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2511 return 0;
2512
2513 /* If ops traces all mods, we already accounted for it */
2514 if (ops_traces_mod(ops))
2515 return 0;
2516
2517 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002518 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2519 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002520 return 0;
2521
2522 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002523 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002524 return 0;
2525
2526 return 1;
2527}
2528
2529static int referenced_filters(struct dyn_ftrace *rec)
2530{
2531 struct ftrace_ops *ops;
2532 int cnt = 0;
2533
2534 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2535 if (ops_references_rec(ops, rec))
2536 cnt++;
2537 }
2538
2539 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002540}
2541
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002542static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002543{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002544 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002545 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302546 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002547 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002548 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002549 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002550 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002551
2552 /*
2553 * When adding a module, we need to check if tracers are
2554 * currently enabled and if they are set to trace all functions.
2555 * If they are, we need to enable the module functions as well
2556 * as update the reference counts for those function records.
2557 */
2558 if (mod) {
2559 struct ftrace_ops *ops;
2560
2561 for (ops = ftrace_ops_list;
2562 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002563 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2564 if (ops_traces_mod(ops))
2565 ref++;
2566 else
2567 test = true;
2568 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002569 }
2570 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002571
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002572 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002573
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002574 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302575
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002576 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002577 int cnt = ref;
2578
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002579 /* If something went wrong, bail without enabling anything */
2580 if (unlikely(ftrace_disabled))
2581 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002582
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002583 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002584 if (test)
2585 cnt += referenced_filters(p);
2586 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302587
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002588 /*
2589 * Do the initial record conversion from mcount jump
2590 * to the NOP instructions.
2591 */
2592 if (!ftrace_code_disable(mod, p))
2593 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002594
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002595 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002596
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002597 /*
2598 * If the tracing is enabled, go ahead and enable the record.
2599 *
2600 * The reason not to enable the record immediatelly is the
2601 * inherent check of ftrace_make_nop/ftrace_make_call for
2602 * correct previous instructions. Making first the NOP
2603 * conversion puts the module to the correct state, thus
2604 * passing the ftrace_make_call check.
2605 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002606 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002607 int failed = __ftrace_replace_code(p, 1);
2608 if (failed)
2609 ftrace_bug(failed, p->ip);
2610 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002611 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002612 }
2613
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002614 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002615 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002616 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002617
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002618 return 0;
2619}
2620
Steven Rostedta7900872011-12-16 16:23:44 -05002621static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002622{
Steven Rostedta7900872011-12-16 16:23:44 -05002623 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002624 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002625
Steven Rostedta7900872011-12-16 16:23:44 -05002626 if (WARN_ON(!count))
2627 return -EINVAL;
2628
2629 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002630
2631 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002632 * We want to fill as much as possible. No more than a page
2633 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002634 */
Steven Rostedta7900872011-12-16 16:23:44 -05002635 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2636 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002637
Steven Rostedta7900872011-12-16 16:23:44 -05002638 again:
2639 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2640
2641 if (!pg->records) {
2642 /* if we can't allocate this size, try something smaller */
2643 if (!order)
2644 return -ENOMEM;
2645 order >>= 1;
2646 goto again;
2647 }
2648
2649 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2650 pg->size = cnt;
2651
2652 if (cnt > count)
2653 cnt = count;
2654
2655 return cnt;
2656}
2657
2658static struct ftrace_page *
2659ftrace_allocate_pages(unsigned long num_to_init)
2660{
2661 struct ftrace_page *start_pg;
2662 struct ftrace_page *pg;
2663 int order;
2664 int cnt;
2665
2666 if (!num_to_init)
2667 return 0;
2668
2669 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2670 if (!pg)
2671 return NULL;
2672
2673 /*
2674 * Try to allocate as much as possible in one continues
2675 * location that fills in all of the space. We want to
2676 * waste as little space as possible.
2677 */
2678 for (;;) {
2679 cnt = ftrace_allocate_records(pg, num_to_init);
2680 if (cnt < 0)
2681 goto free_pages;
2682
2683 num_to_init -= cnt;
2684 if (!num_to_init)
2685 break;
2686
2687 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2688 if (!pg->next)
2689 goto free_pages;
2690
2691 pg = pg->next;
2692 }
2693
2694 return start_pg;
2695
2696 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09002697 pg = start_pg;
2698 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05002699 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2700 free_pages((unsigned long)pg->records, order);
2701 start_pg = pg->next;
2702 kfree(pg);
2703 pg = start_pg;
2704 }
2705 pr_info("ftrace: FAILED to allocate memory for functions\n");
2706 return NULL;
2707}
2708
Steven Rostedt5072c592008-05-12 21:20:43 +02002709#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2710
2711struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002712 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002713 loff_t func_pos;
2714 struct ftrace_page *pg;
2715 struct dyn_ftrace *func;
2716 struct ftrace_func_probe *probe;
2717 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002718 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002719 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002720 int hidx;
2721 int idx;
2722 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002723};
2724
Ingo Molnare309b412008-05-12 21:20:51 +02002725static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002726t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002727{
2728 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002729 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002730 struct hlist_head *hhd;
2731
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002732 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002733 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002734
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002735 if (iter->probe)
2736 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002737 retry:
2738 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2739 return NULL;
2740
2741 hhd = &ftrace_func_hash[iter->hidx];
2742
2743 if (hlist_empty(hhd)) {
2744 iter->hidx++;
2745 hnd = NULL;
2746 goto retry;
2747 }
2748
2749 if (!hnd)
2750 hnd = hhd->first;
2751 else {
2752 hnd = hnd->next;
2753 if (!hnd) {
2754 iter->hidx++;
2755 goto retry;
2756 }
2757 }
2758
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002759 if (WARN_ON_ONCE(!hnd))
2760 return NULL;
2761
2762 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2763
2764 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002765}
2766
2767static void *t_hash_start(struct seq_file *m, loff_t *pos)
2768{
2769 struct ftrace_iterator *iter = m->private;
2770 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002771 loff_t l;
2772
Steven Rostedt69a30832011-12-19 15:21:16 -05002773 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2774 return NULL;
2775
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002776 if (iter->func_pos > *pos)
2777 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002778
Li Zefand82d6242009-06-24 09:54:54 +08002779 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002780 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002781 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002782 if (!p)
2783 break;
2784 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002785 if (!p)
2786 return NULL;
2787
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002788 /* Only set this if we have an item */
2789 iter->flags |= FTRACE_ITER_HASH;
2790
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002791 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002792}
2793
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002794static int
2795t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002796{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002797 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002798
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002799 rec = iter->probe;
2800 if (WARN_ON_ONCE(!rec))
2801 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002802
Steven Rostedt809dcf22009-02-16 23:06:01 -05002803 if (rec->ops->print)
2804 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2805
Steven Rostedtb375a112009-09-17 00:05:58 -04002806 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002807
2808 if (rec->data)
2809 seq_printf(m, ":%p", rec->data);
2810 seq_putc(m, '\n');
2811
2812 return 0;
2813}
2814
2815static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002816t_next(struct seq_file *m, void *v, loff_t *pos)
2817{
2818 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002819 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002820 struct dyn_ftrace *rec = NULL;
2821
Steven Rostedt45a4a232011-04-21 23:16:46 -04002822 if (unlikely(ftrace_disabled))
2823 return NULL;
2824
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002825 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002826 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002827
Steven Rostedt5072c592008-05-12 21:20:43 +02002828 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002829 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002830
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002831 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002832 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002833
Steven Rostedt5072c592008-05-12 21:20:43 +02002834 retry:
2835 if (iter->idx >= iter->pg->index) {
2836 if (iter->pg->next) {
2837 iter->pg = iter->pg->next;
2838 iter->idx = 0;
2839 goto retry;
2840 }
2841 } else {
2842 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05002843 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002844 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05002845
Steven Rostedt41c52c02008-05-22 11:46:33 -04002846 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002847 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04002848
2849 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002850 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002851
Steven Rostedt5072c592008-05-12 21:20:43 +02002852 rec = NULL;
2853 goto retry;
2854 }
2855 }
2856
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002857 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002858 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002859
2860 iter->func = rec;
2861
2862 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002863}
2864
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002865static void reset_iter_read(struct ftrace_iterator *iter)
2866{
2867 iter->pos = 0;
2868 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03002869 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002870}
2871
2872static void *t_start(struct seq_file *m, loff_t *pos)
2873{
2874 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002875 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002876 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002877 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002878
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002879 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002880
2881 if (unlikely(ftrace_disabled))
2882 return NULL;
2883
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002884 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002885 * If an lseek was done, then reset and start from beginning.
2886 */
2887 if (*pos < iter->pos)
2888 reset_iter_read(iter);
2889
2890 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002891 * For set_ftrace_filter reading, if we have the filter
2892 * off, we can short cut and just print out that all
2893 * functions are enabled.
2894 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002895 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002896 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002897 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002898 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002899 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002900 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002901 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002902 /* reset in case of seek/pread */
2903 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002904 return iter;
2905 }
2906
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002907 if (iter->flags & FTRACE_ITER_HASH)
2908 return t_hash_start(m, pos);
2909
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002910 /*
2911 * Unfortunately, we need to restart at ftrace_pages_start
2912 * every time we let go of the ftrace_mutex. This is because
2913 * those pointers can change without the lock.
2914 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002915 iter->pg = ftrace_pages_start;
2916 iter->idx = 0;
2917 for (l = 0; l <= *pos; ) {
2918 p = t_next(m, p, &l);
2919 if (!p)
2920 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002921 }
walimis5821e1b2008-11-15 15:19:06 +08002922
Steven Rostedt69a30832011-12-19 15:21:16 -05002923 if (!p)
2924 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002925
2926 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002927}
2928
2929static void t_stop(struct seq_file *m, void *p)
2930{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002931 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002932}
2933
2934static int t_show(struct seq_file *m, void *v)
2935{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002936 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002937 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002938
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002939 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002940 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002941
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002942 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002943 if (iter->flags & FTRACE_ITER_NOTRACE)
2944 seq_printf(m, "#### no functions disabled ####\n");
2945 else
2946 seq_printf(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002947 return 0;
2948 }
2949
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002950 rec = iter->func;
2951
Steven Rostedt5072c592008-05-12 21:20:43 +02002952 if (!rec)
2953 return 0;
2954
Steven Rostedt647bcd02011-05-03 14:39:21 -04002955 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002956 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002957 seq_printf(m, " (%ld)%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002958 ftrace_rec_count(rec),
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002959 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2960 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2961 struct ftrace_ops *ops;
2962
2963 ops = ftrace_find_tramp_ops_curr(rec);
2964 if (ops && ops->trampoline)
2965 seq_printf(m, "\ttramp: %pS",
2966 (void *)ops->trampoline);
2967 else
2968 seq_printf(m, "\ttramp: ERROR!");
2969 }
2970 }
2971
Steven Rostedt647bcd02011-05-03 14:39:21 -04002972 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002973
2974 return 0;
2975}
2976
James Morris88e9d342009-09-22 16:43:43 -07002977static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002978 .start = t_start,
2979 .next = t_next,
2980 .stop = t_stop,
2981 .show = t_show,
2982};
2983
Ingo Molnare309b412008-05-12 21:20:51 +02002984static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002985ftrace_avail_open(struct inode *inode, struct file *file)
2986{
2987 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002988
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002989 if (unlikely(ftrace_disabled))
2990 return -ENODEV;
2991
Jiri Olsa50e18b92012-04-25 10:23:39 +02002992 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2993 if (iter) {
2994 iter->pg = ftrace_pages_start;
2995 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002996 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002997
Jiri Olsa50e18b92012-04-25 10:23:39 +02002998 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002999}
3000
Steven Rostedt647bcd02011-05-03 14:39:21 -04003001static int
3002ftrace_enabled_open(struct inode *inode, struct file *file)
3003{
3004 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003005
3006 if (unlikely(ftrace_disabled))
3007 return -ENODEV;
3008
Jiri Olsa50e18b92012-04-25 10:23:39 +02003009 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3010 if (iter) {
3011 iter->pg = ftrace_pages_start;
3012 iter->flags = FTRACE_ITER_ENABLED;
3013 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003014 }
3015
Jiri Olsa50e18b92012-04-25 10:23:39 +02003016 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003017}
3018
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003019/**
3020 * ftrace_regex_open - initialize function tracer filter files
3021 * @ops: The ftrace_ops that hold the hash filters
3022 * @flag: The type of filter to process
3023 * @inode: The inode, usually passed in to your open routine
3024 * @file: The file, usually passed in to your open routine
3025 *
3026 * ftrace_regex_open() initializes the filter files for the
3027 * @ops. Depending on @flag it may process the filter hash or
3028 * the notrace hash of @ops. With this called from the open
3029 * routine, you can use ftrace_filter_write() for the write
3030 * routine if @flag has FTRACE_ITER_FILTER set, or
3031 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003032 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003033 * release must call ftrace_regex_release().
3034 */
3035int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003036ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003037 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003038{
3039 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003040 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003041 int ret = 0;
3042
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003043 ftrace_ops_init(ops);
3044
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003045 if (unlikely(ftrace_disabled))
3046 return -ENODEV;
3047
Steven Rostedt5072c592008-05-12 21:20:43 +02003048 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3049 if (!iter)
3050 return -ENOMEM;
3051
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003052 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3053 kfree(iter);
3054 return -ENOMEM;
3055 }
3056
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003057 iter->ops = ops;
3058 iter->flags = flag;
3059
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003060 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003061
Steven Rostedtf45948e2011-05-02 12:29:25 -04003062 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003063 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003064 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003065 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003066
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003067 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003068 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3069
3070 if (file->f_flags & O_TRUNC)
3071 iter->hash = alloc_ftrace_hash(size_bits);
3072 else
3073 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3074
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003075 if (!iter->hash) {
3076 trace_parser_put(&iter->parser);
3077 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003078 ret = -ENOMEM;
3079 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003080 }
3081 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003082
Steven Rostedt5072c592008-05-12 21:20:43 +02003083 if (file->f_mode & FMODE_READ) {
3084 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003085
3086 ret = seq_open(file, &show_ftrace_seq_ops);
3087 if (!ret) {
3088 struct seq_file *m = file->private_data;
3089 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003090 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003091 /* Failed */
3092 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003093 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003094 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003095 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003096 } else
3097 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003098
3099 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003100 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003101
3102 return ret;
3103}
3104
Steven Rostedt41c52c02008-05-22 11:46:33 -04003105static int
3106ftrace_filter_open(struct inode *inode, struct file *file)
3107{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003108 struct ftrace_ops *ops = inode->i_private;
3109
3110 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003111 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3112 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003113}
3114
3115static int
3116ftrace_notrace_open(struct inode *inode, struct file *file)
3117{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003118 struct ftrace_ops *ops = inode->i_private;
3119
3120 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003121 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003122}
3123
Steven Rostedt64e7c442009-02-13 17:08:48 -05003124static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003125{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003126 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003127 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003128
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003129 switch (type) {
3130 case MATCH_FULL:
3131 if (strcmp(str, regex) == 0)
3132 matched = 1;
3133 break;
3134 case MATCH_FRONT_ONLY:
3135 if (strncmp(str, regex, len) == 0)
3136 matched = 1;
3137 break;
3138 case MATCH_MIDDLE_ONLY:
3139 if (strstr(str, regex))
3140 matched = 1;
3141 break;
3142 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003143 slen = strlen(str);
3144 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003145 matched = 1;
3146 break;
3147 }
3148
3149 return matched;
3150}
3151
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003152static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003153enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003154{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003155 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003156 int ret = 0;
3157
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003158 entry = ftrace_lookup_ip(hash, rec->ip);
3159 if (not) {
3160 /* Do nothing if it doesn't exist */
3161 if (!entry)
3162 return 0;
3163
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003164 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003165 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003166 /* Do nothing if it exists */
3167 if (entry)
3168 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003169
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003170 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003171 }
3172 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003173}
3174
Steven Rostedt64e7c442009-02-13 17:08:48 -05003175static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003176ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3177 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003178{
3179 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003180 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003181
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003182 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3183
3184 if (mod) {
3185 /* module lookup requires matching the module */
3186 if (!modname || strcmp(modname, mod))
3187 return 0;
3188
3189 /* blank search means to match all funcs in the mod */
3190 if (!len)
3191 return 1;
3192 }
3193
Steven Rostedt64e7c442009-02-13 17:08:48 -05003194 return ftrace_match(str, regex, len, type);
3195}
3196
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003197static int
3198match_records(struct ftrace_hash *hash, char *buff,
3199 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003200{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003201 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003202 struct ftrace_page *pg;
3203 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003204 int type = MATCH_FULL;
3205 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003206 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003207 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003208
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003209 if (len) {
3210 type = filter_parse_regex(buff, len, &search, &not);
3211 search_len = strlen(search);
3212 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003213
Steven Rostedt52baf112009-02-14 01:15:39 -05003214 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003215
3216 if (unlikely(ftrace_disabled))
3217 goto out_unlock;
3218
Steven Rostedt265c8312009-02-13 12:43:56 -05003219 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003220 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003221 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003222 if (ret < 0) {
3223 found = ret;
3224 goto out_unlock;
3225 }
Li Zefan311d16d2009-12-08 11:15:11 +08003226 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003227 }
3228 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003229 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003230 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003231
3232 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003233}
3234
Steven Rostedt64e7c442009-02-13 17:08:48 -05003235static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003236ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003237{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003238 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003239}
3240
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003241static int
3242ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003243{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003244 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003245
Steven Rostedt64e7c442009-02-13 17:08:48 -05003246 /* blank or '*' mean the same */
3247 if (strcmp(buff, "*") == 0)
3248 buff[0] = 0;
3249
3250 /* handle the case of 'dont filter this module' */
3251 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3252 buff[0] = 0;
3253 not = 1;
3254 }
3255
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003256 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003257}
3258
Steven Rostedtf6180772009-02-14 00:40:25 -05003259/*
3260 * We register the module command as a template to show others how
3261 * to register the a command as well.
3262 */
3263
3264static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003265ftrace_mod_callback(struct ftrace_hash *hash,
3266 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003267{
3268 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003269 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003270
3271 /*
3272 * cmd == 'mod' because we only registered this func
3273 * for the 'mod' ftrace_func_command.
3274 * But if you register one func with multiple commands,
3275 * you can tell which command was used by the cmd
3276 * parameter.
3277 */
3278
3279 /* we must have a module name */
3280 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003281 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003282
3283 mod = strsep(&param, ":");
3284 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003285 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003286
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003287 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003288 if (!ret)
3289 ret = -EINVAL;
3290 if (ret < 0)
3291 return ret;
3292
3293 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003294}
3295
3296static struct ftrace_func_command ftrace_mod_cmd = {
3297 .name = "mod",
3298 .func = ftrace_mod_callback,
3299};
3300
3301static int __init ftrace_mod_cmd_init(void)
3302{
3303 return register_ftrace_command(&ftrace_mod_cmd);
3304}
Steven Rostedt6f415672012-10-05 12:13:07 -04003305core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003306
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003307static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003308 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003309{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003310 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003311 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003312 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003313
3314 key = hash_long(ip, FTRACE_HASH_BITS);
3315
3316 hhd = &ftrace_func_hash[key];
3317
3318 if (hlist_empty(hhd))
3319 return;
3320
3321 /*
3322 * Disable preemption for these calls to prevent a RCU grace
3323 * period. This syncs the hash iteration and freeing of items
3324 * on the hash. rcu_read_lock is too dangerous here.
3325 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003326 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003327 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003328 if (entry->ip == ip)
3329 entry->ops->func(ip, parent_ip, &entry->data);
3330 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003331 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003332}
3333
Steven Rostedtb6887d72009-02-17 12:32:04 -05003334static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003335{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003336 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003337 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003338 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003339};
3340
Steven Rostedtb6887d72009-02-17 12:32:04 -05003341static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003342
Steven Rostedtb6887d72009-02-17 12:32:04 -05003343static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003344{
Steven Rostedtb8489142011-05-04 09:27:52 -04003345 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003346 int i;
3347
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003348 if (ftrace_probe_registered) {
3349 /* still need to update the function call sites */
3350 if (ftrace_enabled)
3351 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003352 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003353 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003354
3355 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3356 struct hlist_head *hhd = &ftrace_func_hash[i];
3357 if (hhd->first)
3358 break;
3359 }
3360 /* Nothing registered? */
3361 if (i == FTRACE_FUNC_HASHSIZE)
3362 return;
3363
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003364 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003365
Steven Rostedtb6887d72009-02-17 12:32:04 -05003366 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003367}
3368
Steven Rostedtb6887d72009-02-17 12:32:04 -05003369static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003370{
3371 int i;
3372
Steven Rostedtb6887d72009-02-17 12:32:04 -05003373 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003374 return;
3375
3376 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3377 struct hlist_head *hhd = &ftrace_func_hash[i];
3378 if (hhd->first)
3379 return;
3380 }
3381
3382 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003383 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003384
Steven Rostedtb6887d72009-02-17 12:32:04 -05003385 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003386}
3387
3388
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003389static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003390{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003391 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003392 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003393 kfree(entry);
3394}
3395
Steven Rostedt59df055f2009-02-14 15:29:06 -05003396int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003397register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003398 void *data)
3399{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003400 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003401 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003402 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003403 struct ftrace_page *pg;
3404 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003405 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003406 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003407 int count = 0;
3408 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003409 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003410
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003411 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003412 len = strlen(search);
3413
Steven Rostedtb6887d72009-02-17 12:32:04 -05003414 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003415 if (WARN_ON(not))
3416 return -EINVAL;
3417
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003418 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003419
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003420 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3421 if (!hash) {
3422 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003423 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003424 }
3425
3426 if (unlikely(ftrace_disabled)) {
3427 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003428 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003429 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003430
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003431 mutex_lock(&ftrace_lock);
3432
Steven Rostedt59df055f2009-02-14 15:29:06 -05003433 do_for_each_ftrace_rec(pg, rec) {
3434
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003435 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003436 continue;
3437
3438 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3439 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003440 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003441 if (!count)
3442 count = -ENOMEM;
3443 goto out_unlock;
3444 }
3445
3446 count++;
3447
3448 entry->data = data;
3449
3450 /*
3451 * The caller might want to do something special
3452 * for each function we find. We call the callback
3453 * to give the caller an opportunity to do so.
3454 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003455 if (ops->init) {
3456 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003457 /* caller does not like this func */
3458 kfree(entry);
3459 continue;
3460 }
3461 }
3462
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003463 ret = enter_record(hash, rec, 0);
3464 if (ret < 0) {
3465 kfree(entry);
3466 count = ret;
3467 goto out_unlock;
3468 }
3469
Steven Rostedt59df055f2009-02-14 15:29:06 -05003470 entry->ops = ops;
3471 entry->ip = rec->ip;
3472
3473 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3474 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3475
3476 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003477
3478 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3479 if (ret < 0)
3480 count = ret;
3481
Steven Rostedtb6887d72009-02-17 12:32:04 -05003482 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003483
3484 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003485 mutex_unlock(&ftrace_lock);
3486 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003487 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003488 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003489
3490 return count;
3491}
3492
3493enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003494 PROBE_TEST_FUNC = 1,
3495 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003496};
3497
3498static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003499__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003500 void *data, int flags)
3501{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003502 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003503 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003504 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003505 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003506 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003507 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003508 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003509 char str[KSYM_SYMBOL_LEN];
3510 int type = MATCH_FULL;
3511 int i, len = 0;
3512 char *search;
3513
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003514 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003515 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003516 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003517 int not;
3518
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003519 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003520 len = strlen(search);
3521
Steven Rostedtb6887d72009-02-17 12:32:04 -05003522 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003523 if (WARN_ON(not))
3524 return;
3525 }
3526
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003527 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003528
3529 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3530 if (!hash)
3531 /* Hmm, should report this somehow */
3532 goto out_unlock;
3533
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003534 INIT_LIST_HEAD(&free_list);
3535
Steven Rostedt59df055f2009-02-14 15:29:06 -05003536 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3537 struct hlist_head *hhd = &ftrace_func_hash[i];
3538
Sasha Levinb67bfe02013-02-27 17:06:00 -08003539 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003540
3541 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003542 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003543 continue;
3544
Steven Rostedtb6887d72009-02-17 12:32:04 -05003545 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003546 continue;
3547
3548 /* do this last, since it is the most expensive */
3549 if (glob) {
3550 kallsyms_lookup(entry->ip, NULL, NULL,
3551 NULL, str);
3552 if (!ftrace_match(str, glob, len, type))
3553 continue;
3554 }
3555
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003556 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3557 /* It is possible more than one entry had this ip */
3558 if (rec_entry)
3559 free_hash_entry(hash, rec_entry);
3560
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003561 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003562 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003563 }
3564 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003565 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003566 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003567 /*
3568 * Remove after the disable is called. Otherwise, if the last
3569 * probe is removed, a null hash means *all enabled*.
3570 */
3571 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003572 synchronize_sched();
3573 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3574 list_del(&entry->free_list);
3575 ftrace_free_entry(entry);
3576 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003577 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003578
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003579 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003580 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003581 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003582}
3583
3584void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003585unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003586 void *data)
3587{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003588 __unregister_ftrace_function_probe(glob, ops, data,
3589 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003590}
3591
3592void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003593unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003594{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003595 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003596}
3597
Steven Rostedtb6887d72009-02-17 12:32:04 -05003598void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003599{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003600 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003601}
3602
Steven Rostedtf6180772009-02-14 00:40:25 -05003603static LIST_HEAD(ftrace_commands);
3604static DEFINE_MUTEX(ftrace_cmd_mutex);
3605
Tom Zanussi38de93a2013-10-24 08:34:18 -05003606/*
3607 * Currently we only register ftrace commands from __init, so mark this
3608 * __init too.
3609 */
3610__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003611{
3612 struct ftrace_func_command *p;
3613 int ret = 0;
3614
3615 mutex_lock(&ftrace_cmd_mutex);
3616 list_for_each_entry(p, &ftrace_commands, list) {
3617 if (strcmp(cmd->name, p->name) == 0) {
3618 ret = -EBUSY;
3619 goto out_unlock;
3620 }
3621 }
3622 list_add(&cmd->list, &ftrace_commands);
3623 out_unlock:
3624 mutex_unlock(&ftrace_cmd_mutex);
3625
3626 return ret;
3627}
3628
Tom Zanussi38de93a2013-10-24 08:34:18 -05003629/*
3630 * Currently we only unregister ftrace commands from __init, so mark
3631 * this __init too.
3632 */
3633__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003634{
3635 struct ftrace_func_command *p, *n;
3636 int ret = -ENODEV;
3637
3638 mutex_lock(&ftrace_cmd_mutex);
3639 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3640 if (strcmp(cmd->name, p->name) == 0) {
3641 ret = 0;
3642 list_del_init(&p->list);
3643 goto out_unlock;
3644 }
3645 }
3646 out_unlock:
3647 mutex_unlock(&ftrace_cmd_mutex);
3648
3649 return ret;
3650}
3651
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003652static int ftrace_process_regex(struct ftrace_hash *hash,
3653 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003654{
Steven Rostedtf6180772009-02-14 00:40:25 -05003655 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003656 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003657 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003658
3659 func = strsep(&next, ":");
3660
3661 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003662 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003663 if (!ret)
3664 ret = -EINVAL;
3665 if (ret < 0)
3666 return ret;
3667 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003668 }
3669
Steven Rostedtf6180772009-02-14 00:40:25 -05003670 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003671
3672 command = strsep(&next, ":");
3673
Steven Rostedtf6180772009-02-14 00:40:25 -05003674 mutex_lock(&ftrace_cmd_mutex);
3675 list_for_each_entry(p, &ftrace_commands, list) {
3676 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003677 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003678 goto out_unlock;
3679 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003680 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003681 out_unlock:
3682 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003683
Steven Rostedtf6180772009-02-14 00:40:25 -05003684 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003685}
3686
Ingo Molnare309b412008-05-12 21:20:51 +02003687static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003688ftrace_regex_write(struct file *file, const char __user *ubuf,
3689 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003690{
3691 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003692 struct trace_parser *parser;
3693 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003694
Li Zefan4ba79782009-09-22 13:52:20 +08003695 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003696 return 0;
3697
Steven Rostedt5072c592008-05-12 21:20:43 +02003698 if (file->f_mode & FMODE_READ) {
3699 struct seq_file *m = file->private_data;
3700 iter = m->private;
3701 } else
3702 iter = file->private_data;
3703
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003704 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003705 return -ENODEV;
3706
3707 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003708
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003709 parser = &iter->parser;
3710 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003711
Li Zefan4ba79782009-09-22 13:52:20 +08003712 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003713 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003714 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003715 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003716 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003717 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003718 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003719 }
3720
Steven Rostedt5072c592008-05-12 21:20:43 +02003721 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003722 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003723 return ret;
3724}
3725
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003726ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003727ftrace_filter_write(struct file *file, const char __user *ubuf,
3728 size_t cnt, loff_t *ppos)
3729{
3730 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3731}
3732
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003733ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003734ftrace_notrace_write(struct file *file, const char __user *ubuf,
3735 size_t cnt, loff_t *ppos)
3736{
3737 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3738}
3739
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003740static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003741ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3742{
3743 struct ftrace_func_entry *entry;
3744
3745 if (!ftrace_location(ip))
3746 return -EINVAL;
3747
3748 if (remove) {
3749 entry = ftrace_lookup_ip(hash, ip);
3750 if (!entry)
3751 return -ENOENT;
3752 free_hash_entry(hash, entry);
3753 return 0;
3754 }
3755
3756 return add_hash_entry(hash, ip);
3757}
3758
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003759static void ftrace_ops_update_code(struct ftrace_ops *ops)
3760{
3761 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3762 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3763}
3764
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003765static int
3766ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3767 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003768{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003769 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003770 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003771 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003772
Steven Rostedt41c52c02008-05-22 11:46:33 -04003773 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003774 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003775
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003776 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003777
Steven Rostedtf45948e2011-05-02 12:29:25 -04003778 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003779 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003780 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003781 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003782
Wang Nanb972cc52014-07-15 08:40:20 +08003783 if (reset)
3784 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3785 else
3786 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3787
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003788 if (!hash) {
3789 ret = -ENOMEM;
3790 goto out_regex_unlock;
3791 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003792
Jiri Olsaac483c42012-01-02 10:04:14 +01003793 if (buf && !ftrace_match_records(hash, buf, len)) {
3794 ret = -EINVAL;
3795 goto out_regex_unlock;
3796 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003797 if (ip) {
3798 ret = ftrace_match_addr(hash, ip, remove);
3799 if (ret < 0)
3800 goto out_regex_unlock;
3801 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003802
3803 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003804 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003805 if (!ret)
3806 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003807
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003808 mutex_unlock(&ftrace_lock);
3809
Jiri Olsaac483c42012-01-02 10:04:14 +01003810 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003811 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003812
3813 free_ftrace_hash(hash);
3814 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003815}
3816
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003817static int
3818ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3819 int reset, int enable)
3820{
3821 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3822}
3823
3824/**
3825 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3826 * @ops - the ops to set the filter with
3827 * @ip - the address to add to or remove from the filter.
3828 * @remove - non zero to remove the ip from the filter
3829 * @reset - non zero to reset all filters before applying this filter.
3830 *
3831 * Filters denote which functions should be enabled when tracing is enabled
3832 * If @ip is NULL, it failes to update filter.
3833 */
3834int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3835 int remove, int reset)
3836{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003837 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003838 return ftrace_set_addr(ops, ip, remove, reset, 1);
3839}
3840EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3841
3842static int
3843ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3844 int reset, int enable)
3845{
3846 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3847}
3848
Steven Rostedt77a2b372008-05-12 21:20:45 +02003849/**
3850 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003851 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003852 * @buf - the string that holds the function filter text.
3853 * @len - the length of the string.
3854 * @reset - non zero to reset all filters before applying this filter.
3855 *
3856 * Filters denote which functions should be enabled when tracing is enabled.
3857 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3858 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003859int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003860 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003861{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003862 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003863 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003864}
Steven Rostedt936e0742011-05-05 22:54:01 -04003865EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003866
Steven Rostedt41c52c02008-05-22 11:46:33 -04003867/**
3868 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003869 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003870 * @buf - the string that holds the function notrace text.
3871 * @len - the length of the string.
3872 * @reset - non zero to reset all filters before applying this filter.
3873 *
3874 * Notrace Filters denote which functions should not be enabled when tracing
3875 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3876 * for tracing.
3877 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003878int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003879 int len, int reset)
3880{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003881 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003882 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003883}
3884EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3885/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003886 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003887 * @buf - the string that holds the function filter text.
3888 * @len - the length of the string.
3889 * @reset - non zero to reset all filters before applying this filter.
3890 *
3891 * Filters denote which functions should be enabled when tracing is enabled.
3892 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3893 */
3894void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3895{
3896 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3897}
3898EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3899
3900/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003901 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003902 * @buf - the string that holds the function notrace text.
3903 * @len - the length of the string.
3904 * @reset - non zero to reset all filters before applying this filter.
3905 *
3906 * Notrace Filters denote which functions should not be enabled when tracing
3907 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3908 * for tracing.
3909 */
3910void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003911{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003912 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003913}
Steven Rostedt936e0742011-05-05 22:54:01 -04003914EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003915
Steven Rostedt2af15d62009-05-28 13:37:24 -04003916/*
3917 * command line interface to allow users to set filters on boot up.
3918 */
3919#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3920static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3921static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3922
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003923/* Used by function selftest to not test if filter is set */
3924bool ftrace_filter_param __initdata;
3925
Steven Rostedt2af15d62009-05-28 13:37:24 -04003926static int __init set_ftrace_notrace(char *str)
3927{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003928 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003929 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003930 return 1;
3931}
3932__setup("ftrace_notrace=", set_ftrace_notrace);
3933
3934static int __init set_ftrace_filter(char *str)
3935{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003936 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003937 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003938 return 1;
3939}
3940__setup("ftrace_filter=", set_ftrace_filter);
3941
Stefan Assmann369bc182009-10-12 22:17:21 +02003942#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003943static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003944static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003945static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003946
Stefan Assmann369bc182009-10-12 22:17:21 +02003947static int __init set_graph_function(char *str)
3948{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003949 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003950 return 1;
3951}
3952__setup("ftrace_graph_filter=", set_graph_function);
3953
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003954static int __init set_graph_notrace_function(char *str)
3955{
3956 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
3957 return 1;
3958}
3959__setup("ftrace_graph_notrace=", set_graph_notrace_function);
3960
3961static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02003962{
3963 int ret;
3964 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003965 unsigned long *table = ftrace_graph_funcs;
3966 int *count = &ftrace_graph_count;
3967
3968 if (!enable) {
3969 table = ftrace_graph_notrace_funcs;
3970 count = &ftrace_graph_notrace_count;
3971 }
Stefan Assmann369bc182009-10-12 22:17:21 +02003972
3973 while (buf) {
3974 func = strsep(&buf, ",");
3975 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003976 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003977 if (ret)
3978 printk(KERN_DEBUG "ftrace: function %s not "
3979 "traceable\n", func);
3980 }
3981}
3982#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3983
Steven Rostedt2a85a372011-12-19 21:57:44 -05003984void __init
3985ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003986{
3987 char *func;
3988
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003989 ftrace_ops_init(ops);
3990
Steven Rostedt2af15d62009-05-28 13:37:24 -04003991 while (buf) {
3992 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003993 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003994 }
3995}
3996
3997static void __init set_ftrace_early_filters(void)
3998{
3999 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004000 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004001 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004002 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004003#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4004 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004005 set_ftrace_early_graph(ftrace_graph_buf, 1);
4006 if (ftrace_graph_notrace_buf[0])
4007 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004008#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004009}
4010
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004011int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004012{
4013 struct seq_file *m = (struct seq_file *)file->private_data;
4014 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004015 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004016 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004017 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004018 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004019
Steven Rostedt5072c592008-05-12 21:20:43 +02004020 if (file->f_mode & FMODE_READ) {
4021 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004022 seq_release(inode, file);
4023 } else
4024 iter = file->private_data;
4025
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004026 parser = &iter->parser;
4027 if (trace_parser_loaded(parser)) {
4028 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004029 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004030 }
4031
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004032 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004033
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004034 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004035
Steven Rostedt058e2972011-04-29 22:35:33 -04004036 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004037 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4038
4039 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004040 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004041 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004042 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004043
Steven Rostedt058e2972011-04-29 22:35:33 -04004044 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004045 ret = ftrace_hash_move(iter->ops, filter_hash,
4046 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004047 if (!ret)
4048 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004049
Steven Rostedt058e2972011-04-29 22:35:33 -04004050 mutex_unlock(&ftrace_lock);
4051 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004052
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004053 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004054 free_ftrace_hash(iter->hash);
4055 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004056
Steven Rostedt5072c592008-05-12 21:20:43 +02004057 return 0;
4058}
4059
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004060static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004061 .open = ftrace_avail_open,
4062 .read = seq_read,
4063 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004064 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004065};
4066
Steven Rostedt647bcd02011-05-03 14:39:21 -04004067static const struct file_operations ftrace_enabled_fops = {
4068 .open = ftrace_enabled_open,
4069 .read = seq_read,
4070 .llseek = seq_lseek,
4071 .release = seq_release_private,
4072};
4073
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004074static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004075 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004076 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004077 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004078 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004079 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004080};
4081
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004082static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004083 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004084 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004085 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004086 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004087 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004088};
4089
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004090#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4091
4092static DEFINE_MUTEX(graph_lock);
4093
4094int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004095int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004096unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004097unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004098
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004099struct ftrace_graph_data {
4100 unsigned long *table;
4101 size_t size;
4102 int *count;
4103 const struct seq_operations *seq_ops;
4104};
4105
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004106static void *
Li Zefan85951842009-06-24 09:54:00 +08004107__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004108{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004109 struct ftrace_graph_data *fgd = m->private;
4110
4111 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004112 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004113 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004114}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004115
Li Zefan85951842009-06-24 09:54:00 +08004116static void *
4117g_next(struct seq_file *m, void *v, loff_t *pos)
4118{
4119 (*pos)++;
4120 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004121}
4122
4123static void *g_start(struct seq_file *m, loff_t *pos)
4124{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004125 struct ftrace_graph_data *fgd = m->private;
4126
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004127 mutex_lock(&graph_lock);
4128
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004129 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004130 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004131 return (void *)1;
4132
Li Zefan85951842009-06-24 09:54:00 +08004133 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004134}
4135
4136static void g_stop(struct seq_file *m, void *p)
4137{
4138 mutex_unlock(&graph_lock);
4139}
4140
4141static int g_show(struct seq_file *m, void *v)
4142{
4143 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004144
4145 if (!ptr)
4146 return 0;
4147
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004148 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004149 struct ftrace_graph_data *fgd = m->private;
4150
4151 if (fgd->table == ftrace_graph_funcs)
4152 seq_printf(m, "#### all functions enabled ####\n");
4153 else
4154 seq_printf(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004155 return 0;
4156 }
4157
Steven Rostedtb375a112009-09-17 00:05:58 -04004158 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004159
4160 return 0;
4161}
4162
James Morris88e9d342009-09-22 16:43:43 -07004163static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004164 .start = g_start,
4165 .next = g_next,
4166 .stop = g_stop,
4167 .show = g_show,
4168};
4169
4170static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004171__ftrace_graph_open(struct inode *inode, struct file *file,
4172 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004173{
4174 int ret = 0;
4175
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004176 mutex_lock(&graph_lock);
4177 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004178 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004179 *fgd->count = 0;
4180 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004181 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004182 mutex_unlock(&graph_lock);
4183
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004184 if (file->f_mode & FMODE_READ) {
4185 ret = seq_open(file, fgd->seq_ops);
4186 if (!ret) {
4187 struct seq_file *m = file->private_data;
4188 m->private = fgd;
4189 }
4190 } else
4191 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004192
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004193 return ret;
4194}
4195
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004196static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004197ftrace_graph_open(struct inode *inode, struct file *file)
4198{
4199 struct ftrace_graph_data *fgd;
4200
4201 if (unlikely(ftrace_disabled))
4202 return -ENODEV;
4203
4204 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4205 if (fgd == NULL)
4206 return -ENOMEM;
4207
4208 fgd->table = ftrace_graph_funcs;
4209 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4210 fgd->count = &ftrace_graph_count;
4211 fgd->seq_ops = &ftrace_graph_seq_ops;
4212
4213 return __ftrace_graph_open(inode, file, fgd);
4214}
4215
4216static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004217ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4218{
4219 struct ftrace_graph_data *fgd;
4220
4221 if (unlikely(ftrace_disabled))
4222 return -ENODEV;
4223
4224 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4225 if (fgd == NULL)
4226 return -ENOMEM;
4227
4228 fgd->table = ftrace_graph_notrace_funcs;
4229 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4230 fgd->count = &ftrace_graph_notrace_count;
4231 fgd->seq_ops = &ftrace_graph_seq_ops;
4232
4233 return __ftrace_graph_open(inode, file, fgd);
4234}
4235
4236static int
Li Zefan87827112009-07-23 11:29:11 +08004237ftrace_graph_release(struct inode *inode, struct file *file)
4238{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004239 if (file->f_mode & FMODE_READ) {
4240 struct seq_file *m = file->private_data;
4241
4242 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004243 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004244 } else {
4245 kfree(file->private_data);
4246 }
4247
Li Zefan87827112009-07-23 11:29:11 +08004248 return 0;
4249}
4250
4251static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004252ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004253{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004254 struct dyn_ftrace *rec;
4255 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004256 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004257 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004258 int type, not;
4259 char *search;
4260 bool exists;
4261 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004262
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004263 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004264 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004265 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004266 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004267
4268 search_len = strlen(search);
4269
Steven Rostedt52baf112009-02-14 01:15:39 -05004270 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004271
4272 if (unlikely(ftrace_disabled)) {
4273 mutex_unlock(&ftrace_lock);
4274 return -ENODEV;
4275 }
4276
Steven Rostedt265c8312009-02-13 12:43:56 -05004277 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004278
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004279 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004280 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004281 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004282 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004283 if (array[i] == rec->ip) {
4284 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004285 break;
4286 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004287 }
4288
4289 if (!not) {
4290 fail = 0;
4291 if (!exists) {
4292 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004293 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004294 goto out;
4295 }
4296 } else {
4297 if (exists) {
4298 array[i] = array[--(*idx)];
4299 array[*idx] = 0;
4300 fail = 0;
4301 }
4302 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004303 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004304 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004305out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004306 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004307
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004308 if (fail)
4309 return -EINVAL;
4310
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004311 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004312}
4313
4314static ssize_t
4315ftrace_graph_write(struct file *file, const char __user *ubuf,
4316 size_t cnt, loff_t *ppos)
4317{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004318 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004319 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004320 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004321
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004322 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004323 return 0;
4324
Namhyung Kim6a101082013-10-14 17:24:25 +09004325 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4326 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004327
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004328 read = trace_get_user(&parser, ubuf, cnt, ppos);
4329
Li Zefan4ba79782009-09-22 13:52:20 +08004330 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004331 parser.buffer[parser.idx] = 0;
4332
Namhyung Kim6a101082013-10-14 17:24:25 +09004333 mutex_lock(&graph_lock);
4334
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004335 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004336 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4337 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004338
4339 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004340 }
4341
Namhyung Kim6a101082013-10-14 17:24:25 +09004342 if (!ret)
4343 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004344
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004345 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004346
4347 return ret;
4348}
4349
4350static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004351 .open = ftrace_graph_open,
4352 .read = seq_read,
4353 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004354 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004355 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004356};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004357
4358static const struct file_operations ftrace_graph_notrace_fops = {
4359 .open = ftrace_graph_notrace_open,
4360 .read = seq_read,
4361 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004362 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004363 .release = ftrace_graph_release,
4364};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004365#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4366
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004367void ftrace_create_filter_files(struct ftrace_ops *ops,
4368 struct dentry *parent)
4369{
4370
4371 trace_create_file("set_ftrace_filter", 0644, parent,
4372 ops, &ftrace_filter_fops);
4373
4374 trace_create_file("set_ftrace_notrace", 0644, parent,
4375 ops, &ftrace_notrace_fops);
4376}
4377
4378/*
4379 * The name "destroy_filter_files" is really a misnomer. Although
4380 * in the future, it may actualy delete the files, but this is
4381 * really intended to make sure the ops passed in are disabled
4382 * and that when this function returns, the caller is free to
4383 * free the ops.
4384 *
4385 * The "destroy" name is only to match the "create" name that this
4386 * should be paired with.
4387 */
4388void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4389{
4390 mutex_lock(&ftrace_lock);
4391 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4392 ftrace_shutdown(ops, 0);
4393 ops->flags |= FTRACE_OPS_FL_DELETED;
4394 mutex_unlock(&ftrace_lock);
4395}
4396
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004397static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004398{
Steven Rostedt5072c592008-05-12 21:20:43 +02004399
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004400 trace_create_file("available_filter_functions", 0444,
4401 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004402
Steven Rostedt647bcd02011-05-03 14:39:21 -04004403 trace_create_file("enabled_functions", 0444,
4404 d_tracer, NULL, &ftrace_enabled_fops);
4405
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004406 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004407
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004408#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004409 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004410 NULL,
4411 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004412 trace_create_file("set_graph_notrace", 0444, d_tracer,
4413 NULL,
4414 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004415#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4416
Steven Rostedt5072c592008-05-12 21:20:43 +02004417 return 0;
4418}
4419
Steven Rostedt9fd49322012-04-24 22:32:06 -04004420static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004421{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004422 const unsigned long *ipa = a;
4423 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004424
Steven Rostedt9fd49322012-04-24 22:32:06 -04004425 if (*ipa > *ipb)
4426 return 1;
4427 if (*ipa < *ipb)
4428 return -1;
4429 return 0;
4430}
4431
4432static void ftrace_swap_ips(void *a, void *b, int size)
4433{
4434 unsigned long *ipa = a;
4435 unsigned long *ipb = b;
4436 unsigned long t;
4437
4438 t = *ipa;
4439 *ipa = *ipb;
4440 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004441}
4442
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004443static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004444 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004445 unsigned long *end)
4446{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004447 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004448 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004449 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004450 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004451 unsigned long *p;
4452 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004453 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004454 int ret = -ENOMEM;
4455
4456 count = end - start;
4457
4458 if (!count)
4459 return 0;
4460
Steven Rostedt9fd49322012-04-24 22:32:06 -04004461 sort(start, count, sizeof(*start),
4462 ftrace_cmp_ips, ftrace_swap_ips);
4463
Steven Rostedt706c81f2012-04-24 23:45:26 -04004464 start_pg = ftrace_allocate_pages(count);
4465 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004466 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004467
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004468 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004469
Steven Rostedt320823092011-12-16 14:42:37 -05004470 /*
4471 * Core and each module needs their own pages, as
4472 * modules will free them when they are removed.
4473 * Force a new page to be allocated for modules.
4474 */
Steven Rostedta7900872011-12-16 16:23:44 -05004475 if (!mod) {
4476 WARN_ON(ftrace_pages || ftrace_pages_start);
4477 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004478 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004479 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05004480 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004481 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05004482
Steven Rostedta7900872011-12-16 16:23:44 -05004483 if (WARN_ON(ftrace_pages->next)) {
4484 /* Hmm, we have free pages? */
4485 while (ftrace_pages->next)
4486 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05004487 }
Steven Rostedta7900872011-12-16 16:23:44 -05004488
Steven Rostedt706c81f2012-04-24 23:45:26 -04004489 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05004490 }
4491
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004492 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004493 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004494 while (p < end) {
4495 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004496 /*
4497 * Some architecture linkers will pad between
4498 * the different mcount_loc sections of different
4499 * object files to satisfy alignments.
4500 * Skip any NULL pointers.
4501 */
4502 if (!addr)
4503 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004504
4505 if (pg->index == pg->size) {
4506 /* We should have allocated enough */
4507 if (WARN_ON(!pg->next))
4508 break;
4509 pg = pg->next;
4510 }
4511
4512 rec = &pg->records[pg->index++];
4513 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004514 }
4515
Steven Rostedt706c81f2012-04-24 23:45:26 -04004516 /* We should have used all pages */
4517 WARN_ON(pg->next);
4518
4519 /* Assign the last page to ftrace_pages */
4520 ftrace_pages = pg;
4521
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004522 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004523 * We only need to disable interrupts on start up
4524 * because we are modifying code that an interrupt
4525 * may execute, and the modification is not atomic.
4526 * But for modules, nothing runs the code we modify
4527 * until we are finished with it, and there's no
4528 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004529 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004530 if (!mod)
4531 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004532 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004533 if (!mod)
4534 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004535 ret = 0;
4536 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004537 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004538
Steven Rostedta7900872011-12-16 16:23:44 -05004539 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004540}
4541
Steven Rostedt93eb6772009-04-15 13:24:06 -04004542#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05004543
4544#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4545
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004546void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004547{
4548 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05004549 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004550 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004551 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004552
Steven Rostedt93eb6772009-04-15 13:24:06 -04004553 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004554
4555 if (ftrace_disabled)
4556 goto out_unlock;
4557
Steven Rostedt320823092011-12-16 14:42:37 -05004558 /*
4559 * Each module has its own ftrace_pages, remove
4560 * them from the list.
4561 */
4562 last_pg = &ftrace_pages_start;
4563 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4564 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004565 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004566 /*
Steven Rostedt320823092011-12-16 14:42:37 -05004567 * As core pages are first, the first
4568 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004569 */
Steven Rostedt320823092011-12-16 14:42:37 -05004570 if (WARN_ON(pg == ftrace_pages_start))
4571 goto out_unlock;
4572
4573 /* Check if we are deleting the last page */
4574 if (pg == ftrace_pages)
4575 ftrace_pages = next_to_ftrace_page(last_pg);
4576
4577 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004578 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4579 free_pages((unsigned long)pg->records, order);
4580 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05004581 } else
4582 last_pg = &pg->next;
4583 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004584 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004585 mutex_unlock(&ftrace_lock);
4586}
4587
4588static void ftrace_init_module(struct module *mod,
4589 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004590{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004591 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004592 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004593 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004594}
4595
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004596void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004597{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004598 ftrace_init_module(mod, mod->ftrace_callsites,
4599 mod->ftrace_callsites +
4600 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004601}
4602
4603static int ftrace_module_notify_exit(struct notifier_block *self,
4604 unsigned long val, void *data)
4605{
4606 struct module *mod = data;
4607
4608 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004609 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004610
4611 return 0;
4612}
4613#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004614static int ftrace_module_notify_exit(struct notifier_block *self,
4615 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004616{
4617 return 0;
4618}
4619#endif /* CONFIG_MODULES */
4620
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004621struct notifier_block ftrace_module_exit_nb = {
4622 .notifier_call = ftrace_module_notify_exit,
4623 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4624};
4625
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004626void __init ftrace_init(void)
4627{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004628 extern unsigned long __start_mcount_loc[];
4629 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004630 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004631 int ret;
4632
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004633 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004634 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004635 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004636 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004637 goto failed;
4638
4639 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004640 if (!count) {
4641 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004642 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004643 }
4644
4645 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4646 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004647
4648 last_ftrace_enabled = ftrace_enabled = 1;
4649
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004650 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004651 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004652 __stop_mcount_loc);
4653
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004654 ret = register_module_notifier(&ftrace_module_exit_nb);
4655 if (ret)
4656 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004657
Steven Rostedt2af15d62009-05-28 13:37:24 -04004658 set_ftrace_early_filters();
4659
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004660 return;
4661 failed:
4662 ftrace_disabled = 1;
4663}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004664
Steven Rostedt3d083392008-05-12 21:20:42 +02004665#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004666
Steven Rostedt2b499382011-05-03 22:49:52 -04004667static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004668 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004669 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004670};
4671
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004672static int __init ftrace_nodyn_init(void)
4673{
4674 ftrace_enabled = 1;
4675 return 0;
4676}
Steven Rostedt6f415672012-10-05 12:13:07 -04004677core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004678
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004679static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4680static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004681/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004682# define ftrace_startup(ops, command) \
4683 ({ \
4684 int ___ret = __register_ftrace_function(ops); \
4685 if (!___ret) \
4686 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4687 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004688 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004689# define ftrace_shutdown(ops, command) \
4690 ({ \
4691 int ___ret = __unregister_ftrace_function(ops); \
4692 if (!___ret) \
4693 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4694 ___ret; \
4695 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004696
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004697# define ftrace_startup_sysctl() do { } while (0)
4698# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004699
4700static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004701ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004702{
4703 return 1;
4704}
4705
Steven Rostedt3d083392008-05-12 21:20:42 +02004706#endif /* CONFIG_DYNAMIC_FTRACE */
4707
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004708__init void ftrace_init_global_array_ops(struct trace_array *tr)
4709{
4710 tr->ops = &global_ops;
4711 tr->ops->private = tr;
4712}
4713
4714void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4715{
4716 /* If we filter on pids, update to use the pid function */
4717 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4718 if (WARN_ON(tr->ops->func != ftrace_stub))
4719 printk("ftrace ops had %pS for function\n",
4720 tr->ops->func);
4721 /* Only the top level instance does pid tracing */
4722 if (!list_empty(&ftrace_pids)) {
4723 set_ftrace_pid_function(func);
4724 func = ftrace_pid_func;
4725 }
4726 }
4727 tr->ops->func = func;
4728 tr->ops->private = tr;
4729}
4730
4731void ftrace_reset_array_ops(struct trace_array *tr)
4732{
4733 tr->ops->func = ftrace_stub;
4734}
4735
Steven Rostedtb8489142011-05-04 09:27:52 -04004736static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004737ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004738 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004739{
Jiri Olsae2484912012-02-15 15:51:48 +01004740 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4741 return;
4742
4743 /*
4744 * Some of the ops may be dynamically allocated,
4745 * they must be freed after a synchronize_sched().
4746 */
4747 preempt_disable_notrace();
4748 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004749
4750 /*
4751 * Control funcs (perf) uses RCU. Only trace if
4752 * RCU is currently active.
4753 */
4754 if (!rcu_is_watching())
4755 goto out;
4756
Steven Rostedt0a016402012-11-02 17:03:03 -04004757 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004758 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4759 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004760 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004761 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004762 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004763 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004764 trace_recursion_clear(TRACE_CONTROL_BIT);
4765 preempt_enable_notrace();
4766}
4767
4768static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004769 .func = ftrace_ops_control_func,
4770 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004771 INIT_OPS_HASH(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004772};
4773
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004774static inline void
4775__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004776 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004777{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004778 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004779 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004780
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004781 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4782 if (bit < 0)
4783 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004784
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004785 /*
4786 * Some of the ops may be dynamically allocated,
4787 * they must be freed after a synchronize_sched().
4788 */
4789 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004790 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004791 if (ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04004792 if (FTRACE_WARN_ON(!op->func)) {
4793 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004794 goto out;
4795 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004796 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004797 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004798 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004799out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004800 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004801 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004802}
4803
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004804/*
4805 * Some archs only support passing ip and parent_ip. Even though
4806 * the list function ignores the op parameter, we do not want any
4807 * C side effects, where a function is called without the caller
4808 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004809 * Archs are to support both the regs and ftrace_ops at the same time.
4810 * If they support ftrace_ops, it is assumed they support regs.
4811 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004812 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4813 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004814 * An architecture can pass partial regs with ftrace_ops and still
4815 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004816 */
4817#if ARCH_SUPPORTS_FTRACE_OPS
4818static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004819 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004820{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004821 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004822}
4823#else
4824static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4825{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004826 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004827}
4828#endif
4829
Steven Rostedte32d8952008-12-04 00:26:41 -05004830static void clear_ftrace_swapper(void)
4831{
4832 struct task_struct *p;
4833 int cpu;
4834
4835 get_online_cpus();
4836 for_each_online_cpu(cpu) {
4837 p = idle_task(cpu);
4838 clear_tsk_trace_trace(p);
4839 }
4840 put_online_cpus();
4841}
4842
4843static void set_ftrace_swapper(void)
4844{
4845 struct task_struct *p;
4846 int cpu;
4847
4848 get_online_cpus();
4849 for_each_online_cpu(cpu) {
4850 p = idle_task(cpu);
4851 set_tsk_trace_trace(p);
4852 }
4853 put_online_cpus();
4854}
4855
4856static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004857{
4858 struct task_struct *p;
4859
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004860 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004861 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004862 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004863 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004864 rcu_read_unlock();
4865
Steven Rostedte32d8952008-12-04 00:26:41 -05004866 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004867}
4868
Steven Rostedte32d8952008-12-04 00:26:41 -05004869static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004870{
4871 struct task_struct *p;
4872
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004873 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004874 do_each_pid_task(pid, PIDTYPE_PID, p) {
4875 set_tsk_trace_trace(p);
4876 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004877 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004878}
4879
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004880static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004881{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004882 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004883 clear_ftrace_swapper();
4884 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004885 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004886}
4887
4888static void set_ftrace_pid_task(struct pid *pid)
4889{
4890 if (pid == ftrace_swapper_pid)
4891 set_ftrace_swapper();
4892 else
4893 set_ftrace_pid(pid);
4894}
4895
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004896static int ftrace_pid_add(int p)
4897{
4898 struct pid *pid;
4899 struct ftrace_pid *fpid;
4900 int ret = -EINVAL;
4901
4902 mutex_lock(&ftrace_lock);
4903
4904 if (!p)
4905 pid = ftrace_swapper_pid;
4906 else
4907 pid = find_get_pid(p);
4908
4909 if (!pid)
4910 goto out;
4911
4912 ret = 0;
4913
4914 list_for_each_entry(fpid, &ftrace_pids, list)
4915 if (fpid->pid == pid)
4916 goto out_put;
4917
4918 ret = -ENOMEM;
4919
4920 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4921 if (!fpid)
4922 goto out_put;
4923
4924 list_add(&fpid->list, &ftrace_pids);
4925 fpid->pid = pid;
4926
4927 set_ftrace_pid_task(pid);
4928
4929 ftrace_update_pid_func();
4930 ftrace_startup_enable(0);
4931
4932 mutex_unlock(&ftrace_lock);
4933 return 0;
4934
4935out_put:
4936 if (pid != ftrace_swapper_pid)
4937 put_pid(pid);
4938
4939out:
4940 mutex_unlock(&ftrace_lock);
4941 return ret;
4942}
4943
4944static void ftrace_pid_reset(void)
4945{
4946 struct ftrace_pid *fpid, *safe;
4947
4948 mutex_lock(&ftrace_lock);
4949 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4950 struct pid *pid = fpid->pid;
4951
4952 clear_ftrace_pid_task(pid);
4953
4954 list_del(&fpid->list);
4955 kfree(fpid);
4956 }
4957
4958 ftrace_update_pid_func();
4959 ftrace_startup_enable(0);
4960
4961 mutex_unlock(&ftrace_lock);
4962}
4963
4964static void *fpid_start(struct seq_file *m, loff_t *pos)
4965{
4966 mutex_lock(&ftrace_lock);
4967
4968 if (list_empty(&ftrace_pids) && (!*pos))
4969 return (void *) 1;
4970
4971 return seq_list_start(&ftrace_pids, *pos);
4972}
4973
4974static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4975{
4976 if (v == (void *)1)
4977 return NULL;
4978
4979 return seq_list_next(v, &ftrace_pids, pos);
4980}
4981
4982static void fpid_stop(struct seq_file *m, void *p)
4983{
4984 mutex_unlock(&ftrace_lock);
4985}
4986
4987static int fpid_show(struct seq_file *m, void *v)
4988{
4989 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4990
4991 if (v == (void *)1) {
4992 seq_printf(m, "no pid\n");
4993 return 0;
4994 }
4995
4996 if (fpid->pid == ftrace_swapper_pid)
4997 seq_printf(m, "swapper tasks\n");
4998 else
4999 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5000
5001 return 0;
5002}
5003
5004static const struct seq_operations ftrace_pid_sops = {
5005 .start = fpid_start,
5006 .next = fpid_next,
5007 .stop = fpid_stop,
5008 .show = fpid_show,
5009};
5010
5011static int
5012ftrace_pid_open(struct inode *inode, struct file *file)
5013{
5014 int ret = 0;
5015
5016 if ((file->f_mode & FMODE_WRITE) &&
5017 (file->f_flags & O_TRUNC))
5018 ftrace_pid_reset();
5019
5020 if (file->f_mode & FMODE_READ)
5021 ret = seq_open(file, &ftrace_pid_sops);
5022
5023 return ret;
5024}
5025
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005026static ssize_t
5027ftrace_pid_write(struct file *filp, const char __user *ubuf,
5028 size_t cnt, loff_t *ppos)
5029{
Ingo Molnar457dc922009-11-23 11:03:28 +01005030 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005031 long val;
5032 int ret;
5033
5034 if (cnt >= sizeof(buf))
5035 return -EINVAL;
5036
5037 if (copy_from_user(&buf, ubuf, cnt))
5038 return -EFAULT;
5039
5040 buf[cnt] = 0;
5041
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005042 /*
5043 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5044 * to clean the filter quietly.
5045 */
Ingo Molnar457dc922009-11-23 11:03:28 +01005046 tmp = strstrip(buf);
5047 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005048 return 1;
5049
Daniel Walterbcd83ea2012-09-26 22:08:38 +02005050 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005051 if (ret < 0)
5052 return ret;
5053
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005054 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005055
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005056 return ret ? ret : cnt;
5057}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005058
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005059static int
5060ftrace_pid_release(struct inode *inode, struct file *file)
5061{
5062 if (file->f_mode & FMODE_READ)
5063 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005064
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005065 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005066}
5067
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005068static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005069 .open = ftrace_pid_open,
5070 .write = ftrace_pid_write,
5071 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005072 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005073 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005074};
5075
5076static __init int ftrace_init_debugfs(void)
5077{
5078 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005079
5080 d_tracer = tracing_init_dentry();
5081 if (!d_tracer)
5082 return 0;
5083
5084 ftrace_init_dyn_debugfs(d_tracer);
5085
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005086 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5087 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005088
5089 ftrace_profile_debugfs(d_tracer);
5090
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005091 return 0;
5092}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005093fs_initcall(ftrace_init_debugfs);
5094
Steven Rostedt3d083392008-05-12 21:20:42 +02005095/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005096 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005097 *
5098 * This function should be used by panic code. It stops ftrace
5099 * but in a not so nice way. If you need to simply kill ftrace
5100 * from a non-atomic section, use ftrace_kill.
5101 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005102void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005103{
5104 ftrace_disabled = 1;
5105 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005106 clear_ftrace_function();
5107}
5108
5109/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005110 * Test if ftrace is dead or not.
5111 */
5112int ftrace_is_dead(void)
5113{
5114 return ftrace_disabled;
5115}
5116
5117/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005118 * register_ftrace_function - register a function for profiling
5119 * @ops - ops structure that holds the function for profiling.
5120 *
5121 * Register a function to be called by all functions in the
5122 * kernel.
5123 *
5124 * Note: @ops->func and all the functions it calls must be labeled
5125 * with "notrace", otherwise it will go into a
5126 * recursive loop.
5127 */
5128int register_ftrace_function(struct ftrace_ops *ops)
5129{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005130 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005131
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005132 ftrace_ops_init(ops);
5133
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005134 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005135
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005136 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005137
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005138 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005139
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005140 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005141}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005142EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005143
5144/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005145 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005146 * @ops - ops structure that holds the function to unregister
5147 *
5148 * Unregister a function that was added to be called by ftrace profiling.
5149 */
5150int unregister_ftrace_function(struct ftrace_ops *ops)
5151{
5152 int ret;
5153
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005154 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005155 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005156 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005157
5158 return ret;
5159}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005160EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005161
Ingo Molnare309b412008-05-12 21:20:51 +02005162int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005163ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005164 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005165 loff_t *ppos)
5166{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005167 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005168
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005169 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005170
Steven Rostedt45a4a232011-04-21 23:16:46 -04005171 if (unlikely(ftrace_disabled))
5172 goto out;
5173
5174 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005175
Li Zefana32c7762009-06-26 16:55:51 +08005176 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005177 goto out;
5178
Li Zefana32c7762009-06-26 16:55:51 +08005179 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005180
5181 if (ftrace_enabled) {
5182
5183 ftrace_startup_sysctl();
5184
5185 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005186 if (ftrace_ops_list != &ftrace_list_end)
5187 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005188
5189 } else {
5190 /* stopping ftrace calls (just send to ftrace_stub) */
5191 ftrace_trace_function = ftrace_stub;
5192
5193 ftrace_shutdown_sysctl();
5194 }
5195
5196 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005197 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005198 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005199}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005200
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005201#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005202
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005203static struct ftrace_ops graph_ops = {
5204 .func = ftrace_stub,
5205 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5206 FTRACE_OPS_FL_INITIALIZED |
5207 FTRACE_OPS_FL_STUB,
5208#ifdef FTRACE_GRAPH_TRAMP_ADDR
5209 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5210#endif
5211 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5212};
5213
Steven Rostedt597af812009-04-03 15:24:12 -04005214static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005215
Steven Rostedte49dc192008-12-02 23:50:05 -05005216int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5217{
5218 return 0;
5219}
5220
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005221/* The callbacks that hook a function */
5222trace_func_graph_ret_t ftrace_graph_return =
5223 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005224trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005225static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005226
5227/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5228static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5229{
5230 int i;
5231 int ret = 0;
5232 unsigned long flags;
5233 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5234 struct task_struct *g, *t;
5235
5236 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5237 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5238 * sizeof(struct ftrace_ret_stack),
5239 GFP_KERNEL);
5240 if (!ret_stack_list[i]) {
5241 start = 0;
5242 end = i;
5243 ret = -ENOMEM;
5244 goto free;
5245 }
5246 }
5247
5248 read_lock_irqsave(&tasklist_lock, flags);
5249 do_each_thread(g, t) {
5250 if (start == end) {
5251 ret = -EAGAIN;
5252 goto unlock;
5253 }
5254
5255 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005256 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005257 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005258 t->curr_ret_stack = -1;
5259 /* Make sure the tasks see the -1 first: */
5260 smp_wmb();
5261 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005262 }
5263 } while_each_thread(g, t);
5264
5265unlock:
5266 read_unlock_irqrestore(&tasklist_lock, flags);
5267free:
5268 for (i = start; i < end; i++)
5269 kfree(ret_stack_list[i]);
5270 return ret;
5271}
5272
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005273static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005274ftrace_graph_probe_sched_switch(void *ignore,
5275 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005276{
5277 unsigned long long timestamp;
5278 int index;
5279
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005280 /*
5281 * Does the user want to count the time a function was asleep.
5282 * If so, do not update the time stamps.
5283 */
5284 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5285 return;
5286
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005287 timestamp = trace_clock_local();
5288
5289 prev->ftrace_timestamp = timestamp;
5290
5291 /* only process tasks that we timestamped */
5292 if (!next->ftrace_timestamp)
5293 return;
5294
5295 /*
5296 * Update all the counters in next to make up for the
5297 * time next was sleeping.
5298 */
5299 timestamp -= next->ftrace_timestamp;
5300
5301 for (index = next->curr_ret_stack; index >= 0; index--)
5302 next->ret_stack[index].calltime += timestamp;
5303}
5304
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005305/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005306static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005307{
5308 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005309 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005310
5311 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5312 sizeof(struct ftrace_ret_stack *),
5313 GFP_KERNEL);
5314
5315 if (!ret_stack_list)
5316 return -ENOMEM;
5317
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005318 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005319 for_each_online_cpu(cpu) {
5320 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005321 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005322 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005323
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005324 do {
5325 ret = alloc_retstack_tasklist(ret_stack_list);
5326 } while (ret == -EAGAIN);
5327
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005328 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005329 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005330 if (ret)
5331 pr_info("ftrace_graph: Couldn't activate tracepoint"
5332 " probe to kernel_sched_switch\n");
5333 }
5334
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005335 kfree(ret_stack_list);
5336 return ret;
5337}
5338
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005339/*
5340 * Hibernation protection.
5341 * The state of the current task is too much unstable during
5342 * suspend/restore to disk. We want to protect against that.
5343 */
5344static int
5345ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5346 void *unused)
5347{
5348 switch (state) {
5349 case PM_HIBERNATION_PREPARE:
5350 pause_graph_tracing();
5351 break;
5352
5353 case PM_POST_HIBERNATION:
5354 unpause_graph_tracing();
5355 break;
5356 }
5357 return NOTIFY_DONE;
5358}
5359
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005360static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5361{
5362 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5363 return 0;
5364 return __ftrace_graph_entry(trace);
5365}
5366
5367/*
5368 * The function graph tracer should only trace the functions defined
5369 * by set_ftrace_filter and set_ftrace_notrace. If another function
5370 * tracer ops is registered, the graph tracer requires testing the
5371 * function against the global ops, and not just trace any function
5372 * that any ftrace_ops registered.
5373 */
5374static void update_function_graph_func(void)
5375{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005376 struct ftrace_ops *op;
5377 bool do_test = false;
5378
5379 /*
5380 * The graph and global ops share the same set of functions
5381 * to test. If any other ops is on the list, then
5382 * the graph tracing needs to test if its the function
5383 * it should call.
5384 */
5385 do_for_each_ftrace_op(op, ftrace_ops_list) {
5386 if (op != &global_ops && op != &graph_ops &&
5387 op != &ftrace_list_end) {
5388 do_test = true;
5389 /* in double loop, break out with goto */
5390 goto out;
5391 }
5392 } while_for_each_ftrace_op(op);
5393 out:
5394 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005395 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005396 else
5397 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005398}
5399
Mathias Krause8275f692014-03-30 15:31:50 +02005400static struct notifier_block ftrace_suspend_notifier = {
5401 .notifier_call = ftrace_suspend_notifier_call,
5402};
5403
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005404int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5405 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005406{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005407 int ret = 0;
5408
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005409 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005410
Steven Rostedt05ce5812009-03-24 00:18:31 -04005411 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005412 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005413 ret = -EBUSY;
5414 goto out;
5415 }
5416
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005417 register_pm_notifier(&ftrace_suspend_notifier);
5418
Steven Rostedt597af812009-04-03 15:24:12 -04005419 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005420 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005421 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005422 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005423 goto out;
5424 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005425
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005426 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005427
5428 /*
5429 * Update the indirect function to the entryfunc, and the
5430 * function that gets called to the entry_test first. Then
5431 * call the update fgraph entry function to determine if
5432 * the entryfunc should be called directly or not.
5433 */
5434 __ftrace_graph_entry = entryfunc;
5435 ftrace_graph_entry = ftrace_graph_entry_test;
5436 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005437
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005438 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005439
5440out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005441 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005442 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005443}
5444
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005445void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005446{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005447 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005448
Steven Rostedt597af812009-04-03 15:24:12 -04005449 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005450 goto out;
5451
Steven Rostedt597af812009-04-03 15:24:12 -04005452 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005453 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005454 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005455 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005456 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005457 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005458 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005459
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005460 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005461 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005462}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005463
Steven Rostedt868baf02011-02-10 21:26:13 -05005464static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5465
5466static void
5467graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5468{
5469 atomic_set(&t->tracing_graph_pause, 0);
5470 atomic_set(&t->trace_overrun, 0);
5471 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005472 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005473 smp_wmb();
5474 t->ret_stack = ret_stack;
5475}
5476
5477/*
5478 * Allocate a return stack for the idle task. May be the first
5479 * time through, or it may be done by CPU hotplug online.
5480 */
5481void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5482{
5483 t->curr_ret_stack = -1;
5484 /*
5485 * The idle task has no parent, it either has its own
5486 * stack or no stack at all.
5487 */
5488 if (t->ret_stack)
5489 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5490
5491 if (ftrace_graph_active) {
5492 struct ftrace_ret_stack *ret_stack;
5493
5494 ret_stack = per_cpu(idle_ret_stack, cpu);
5495 if (!ret_stack) {
5496 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5497 * sizeof(struct ftrace_ret_stack),
5498 GFP_KERNEL);
5499 if (!ret_stack)
5500 return;
5501 per_cpu(idle_ret_stack, cpu) = ret_stack;
5502 }
5503 graph_init_task(t, ret_stack);
5504 }
5505}
5506
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005507/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005508void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005509{
Steven Rostedt84047e32009-06-02 16:51:55 -04005510 /* Make sure we do not use the parent ret_stack */
5511 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005512 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005513
Steven Rostedt597af812009-04-03 15:24:12 -04005514 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005515 struct ftrace_ret_stack *ret_stack;
5516
5517 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005518 * sizeof(struct ftrace_ret_stack),
5519 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005520 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005521 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005522 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005523 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005524}
5525
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005526void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005527{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005528 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5529
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005530 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005531 /* NULL must become visible to IRQs before we free it: */
5532 barrier();
5533
5534 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005535}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005536#endif