blob: 858ac16f84920ab6238db2ddcd383cfa1972baa5 [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 Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -0400116static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
117 struct ftrace_ops *op, struct pt_regs *regs);
118
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400119#if ARCH_SUPPORTS_FTRACE_OPS
120static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400121 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400122#else
123/* See comment below, where ftrace_ops_list_func is defined */
124static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
125#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
126#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400127
Steven Rostedt0a016402012-11-02 17:03:03 -0400128/*
129 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400130 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400131 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400132 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400133 * concurrent insertions into the ftrace_global_list.
134 *
135 * Silly Alpha and silly pointer-speculation compiler optimizations!
136 */
137#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400138 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400139 do
140
141/*
142 * Optimized for just a single item in the list (as that is the normal case).
143 */
144#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400145 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400146 unlikely((op) != &ftrace_list_end))
147
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900148static inline void ftrace_ops_init(struct ftrace_ops *ops)
149{
150#ifdef CONFIG_DYNAMIC_FTRACE
151 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400152 mutex_init(&ops->local_hash.regex_lock);
153 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900154 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
155 }
156#endif
157}
158
Steven Rostedtea701f12012-07-20 13:08:05 -0400159/**
160 * ftrace_nr_registered_ops - return number of ops registered
161 *
162 * Returns the number of ftrace_ops registered and tracing functions
163 */
164int ftrace_nr_registered_ops(void)
165{
166 struct ftrace_ops *ops;
167 int cnt = 0;
168
169 mutex_lock(&ftrace_lock);
170
171 for (ops = ftrace_ops_list;
172 ops != &ftrace_list_end; ops = ops->next)
173 cnt++;
174
175 mutex_unlock(&ftrace_lock);
176
177 return cnt;
178}
179
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400180static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400181 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500182{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500183 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500184 return;
185
Steven Rostedta1e2e312011-08-09 12:50:46 -0400186 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500187}
188
189static void set_ftrace_pid_function(ftrace_func_t func)
190{
191 /* do not set ftrace_pid_function to itself! */
192 if (func != ftrace_pid_func)
193 ftrace_pid_function = func;
194}
195
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200196/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200197 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200198 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200199 * This NULLs the ftrace function and in essence stops
200 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200201 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200202void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200203{
Steven Rostedt3d083392008-05-12 21:20:42 +0200204 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500205 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200206}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200207
Jiri Olsae2484912012-02-15 15:51:48 +0100208static void control_ops_disable_all(struct ftrace_ops *ops)
209{
210 int cpu;
211
212 for_each_possible_cpu(cpu)
213 *per_cpu_ptr(ops->disabled, cpu) = 1;
214}
215
216static int control_ops_alloc(struct ftrace_ops *ops)
217{
218 int __percpu *disabled;
219
220 disabled = alloc_percpu(int);
221 if (!disabled)
222 return -ENOMEM;
223
224 ops->disabled = disabled;
225 control_ops_disable_all(ops);
226 return 0;
227}
228
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500229static void ftrace_sync(struct work_struct *work)
230{
231 /*
232 * This function is just a stub to implement a hard force
233 * of synchronize_sched(). This requires synchronizing
234 * tasks even in userspace and idle.
235 *
236 * Yes, function tracing is rude.
237 */
238}
239
240static void ftrace_sync_ipi(void *data)
241{
242 /* Probably not needed, but do it anyway */
243 smp_rmb();
244}
245
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500246#ifdef CONFIG_FUNCTION_GRAPH_TRACER
247static void update_function_graph_func(void);
248#else
249static inline void update_function_graph_func(void) { }
250#endif
251
Steven Rostedt2b499382011-05-03 22:49:52 -0400252static void update_ftrace_function(void)
253{
254 ftrace_func_t func;
255
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400256 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400257 * Prepare the ftrace_ops that the arch callback will use.
258 * If there's only one ftrace_ops registered, the ftrace_ops_list
259 * will point to the ops we want.
260 */
261 set_function_trace_op = ftrace_ops_list;
262
263 /* If there's no ftrace_ops registered, just call the stub function */
264 if (ftrace_ops_list == &ftrace_list_end) {
265 func = ftrace_stub;
266
267 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400268 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400269 * recursion safe and not dynamic and the arch supports passing ops,
270 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400271 */
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400272 } else if (ftrace_ops_list->next == &ftrace_list_end) {
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -0400273 func = ftrace_ops_get_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400274
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400275 } else {
276 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500277 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400278 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400279 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400280
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400281 update_function_graph_func();
282
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500283 /* If there's no change, then do nothing more here */
284 if (ftrace_trace_function == func)
285 return;
286
287 /*
288 * If we are using the list function, it doesn't care
289 * about the function_trace_ops.
290 */
291 if (func == ftrace_ops_list_func) {
292 ftrace_trace_function = func;
293 /*
294 * Don't even bother setting function_trace_ops,
295 * it would be racy to do so anyway.
296 */
297 return;
298 }
299
300#ifndef CONFIG_DYNAMIC_FTRACE
301 /*
302 * For static tracing, we need to be a bit more careful.
303 * The function change takes affect immediately. Thus,
304 * we need to coorditate the setting of the function_trace_ops
305 * with the setting of the ftrace_trace_function.
306 *
307 * Set the function to the list ops, which will call the
308 * function we want, albeit indirectly, but it handles the
309 * ftrace_ops and doesn't depend on function_trace_op.
310 */
311 ftrace_trace_function = ftrace_ops_list_func;
312 /*
313 * Make sure all CPUs see this. Yes this is slow, but static
314 * tracing is slow and nasty to have enabled.
315 */
316 schedule_on_each_cpu(ftrace_sync);
317 /* Now all cpus are using the list ops. */
318 function_trace_op = set_function_trace_op;
319 /* Make sure the function_trace_op is visible on all CPUs */
320 smp_wmb();
321 /* Nasty way to force a rmb on all cpus */
322 smp_call_function(ftrace_sync_ipi, NULL, 1);
323 /* OK, we are all set to update the ftrace_trace_function now! */
324#endif /* !CONFIG_DYNAMIC_FTRACE */
325
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400326 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400327}
328
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800329int using_ftrace_ops_list_func(void)
330{
331 return ftrace_trace_function == ftrace_ops_list_func;
332}
333
Steven Rostedt2b499382011-05-03 22:49:52 -0400334static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200335{
Steven Rostedt2b499382011-05-03 22:49:52 -0400336 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200337 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400338 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200339 * CPU might be walking that list. We need to make sure
340 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400341 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200342 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400343 rcu_assign_pointer(*list, ops);
344}
Steven Rostedt3d083392008-05-12 21:20:42 +0200345
Steven Rostedt2b499382011-05-03 22:49:52 -0400346static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
347{
348 struct ftrace_ops **p;
349
350 /*
351 * If we are removing the last function, then simply point
352 * to the ftrace_stub.
353 */
354 if (*list == ops && ops->next == &ftrace_list_end) {
355 *list = &ftrace_list_end;
356 return 0;
357 }
358
359 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
360 if (*p == ops)
361 break;
362
363 if (*p != ops)
364 return -1;
365
366 *p = (*p)->next;
367 return 0;
368}
369
Jiri Olsae2484912012-02-15 15:51:48 +0100370static void add_ftrace_list_ops(struct ftrace_ops **list,
371 struct ftrace_ops *main_ops,
372 struct ftrace_ops *ops)
373{
374 int first = *list == &ftrace_list_end;
375 add_ftrace_ops(list, ops);
376 if (first)
377 add_ftrace_ops(&ftrace_ops_list, main_ops);
378}
379
380static int remove_ftrace_list_ops(struct ftrace_ops **list,
381 struct ftrace_ops *main_ops,
382 struct ftrace_ops *ops)
383{
384 int ret = remove_ftrace_ops(list, ops);
385 if (!ret && *list == &ftrace_list_end)
386 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
387 return ret;
388}
389
Steven Rostedt2b499382011-05-03 22:49:52 -0400390static int __register_ftrace_function(struct ftrace_ops *ops)
391{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500392 if (ops->flags & FTRACE_OPS_FL_DELETED)
393 return -EINVAL;
394
Steven Rostedtb8489142011-05-04 09:27:52 -0400395 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
396 return -EBUSY;
397
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900398#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400399 /*
400 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
401 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
402 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
403 */
404 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
405 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
406 return -EINVAL;
407
408 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
409 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
410#endif
411
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400412 if (!core_kernel_data((unsigned long)ops))
413 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
414
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500415 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100416 if (control_ops_alloc(ops))
417 return -ENOMEM;
418 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400419 } else
420 add_ftrace_ops(&ftrace_ops_list, ops);
421
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400422 if (ftrace_enabled)
423 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200424
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200425 return 0;
426}
427
Ingo Molnare309b412008-05-12 21:20:51 +0200428static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200429{
Steven Rostedt2b499382011-05-03 22:49:52 -0400430 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200431
Steven Rostedtb8489142011-05-04 09:27:52 -0400432 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
433 return -EBUSY;
434
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500435 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100436 ret = remove_ftrace_list_ops(&ftrace_control_list,
437 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400438 } else
439 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
440
Steven Rostedt2b499382011-05-03 22:49:52 -0400441 if (ret < 0)
442 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400443
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400444 if (ftrace_enabled)
445 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200446
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500447 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200448}
449
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500450static void ftrace_update_pid_func(void)
451{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400452 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500453 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900454 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500455
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400456 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500457}
458
Steven Rostedt493762f2009-03-23 17:12:36 -0400459#ifdef CONFIG_FUNCTION_PROFILER
460struct ftrace_profile {
461 struct hlist_node node;
462 unsigned long ip;
463 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400464#ifdef CONFIG_FUNCTION_GRAPH_TRACER
465 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400466 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400467#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400468};
469
470struct ftrace_profile_page {
471 struct ftrace_profile_page *next;
472 unsigned long index;
473 struct ftrace_profile records[];
474};
475
Steven Rostedtcafb1682009-03-24 20:50:39 -0400476struct ftrace_profile_stat {
477 atomic_t disabled;
478 struct hlist_head *hash;
479 struct ftrace_profile_page *pages;
480 struct ftrace_profile_page *start;
481 struct tracer_stat stat;
482};
483
Steven Rostedt493762f2009-03-23 17:12:36 -0400484#define PROFILE_RECORDS_SIZE \
485 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
486
487#define PROFILES_PER_PAGE \
488 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
489
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400490static int ftrace_profile_enabled __read_mostly;
491
492/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400493static DEFINE_MUTEX(ftrace_profile_lock);
494
Steven Rostedtcafb1682009-03-24 20:50:39 -0400495static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400496
Namhyung Kim20079eb2013-04-10 08:55:50 +0900497#define FTRACE_PROFILE_HASH_BITS 10
498#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400499
Steven Rostedt493762f2009-03-23 17:12:36 -0400500static void *
501function_stat_next(void *v, int idx)
502{
503 struct ftrace_profile *rec = v;
504 struct ftrace_profile_page *pg;
505
506 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
507
508 again:
Li Zefan0296e422009-06-26 11:15:37 +0800509 if (idx != 0)
510 rec++;
511
Steven Rostedt493762f2009-03-23 17:12:36 -0400512 if ((void *)rec >= (void *)&pg->records[pg->index]) {
513 pg = pg->next;
514 if (!pg)
515 return NULL;
516 rec = &pg->records[0];
517 if (!rec->counter)
518 goto again;
519 }
520
521 return rec;
522}
523
524static void *function_stat_start(struct tracer_stat *trace)
525{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400526 struct ftrace_profile_stat *stat =
527 container_of(trace, struct ftrace_profile_stat, stat);
528
529 if (!stat || !stat->start)
530 return NULL;
531
532 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400533}
534
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400535#ifdef CONFIG_FUNCTION_GRAPH_TRACER
536/* function graph compares on total time */
537static int function_stat_cmp(void *p1, void *p2)
538{
539 struct ftrace_profile *a = p1;
540 struct ftrace_profile *b = p2;
541
542 if (a->time < b->time)
543 return -1;
544 if (a->time > b->time)
545 return 1;
546 else
547 return 0;
548}
549#else
550/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400551static int function_stat_cmp(void *p1, void *p2)
552{
553 struct ftrace_profile *a = p1;
554 struct ftrace_profile *b = p2;
555
556 if (a->counter < b->counter)
557 return -1;
558 if (a->counter > b->counter)
559 return 1;
560 else
561 return 0;
562}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400563#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400564
565static int function_stat_headers(struct seq_file *m)
566{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400567#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400568 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400569 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400570 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400571 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400572#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400573 seq_printf(m, " Function Hit\n"
574 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400575#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400576 return 0;
577}
578
579static int function_stat_show(struct seq_file *m, void *v)
580{
581 struct ftrace_profile *rec = v;
582 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800583 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400584#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400585 static struct trace_seq s;
586 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400587 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400588#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800589 mutex_lock(&ftrace_profile_lock);
590
591 /* we raced with function_profile_reset() */
592 if (unlikely(rec->counter == 0)) {
593 ret = -EBUSY;
594 goto out;
595 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400596
597 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400598 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400599
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400600#ifdef CONFIG_FUNCTION_GRAPH_TRACER
601 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400602 avg = rec->time;
603 do_div(avg, rec->counter);
604
Chase Douglase330b3b2010-04-26 14:02:05 -0400605 /* Sample standard deviation (s^2) */
606 if (rec->counter <= 1)
607 stddev = 0;
608 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200609 /*
610 * Apply Welford's method:
611 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
612 */
613 stddev = rec->counter * rec->time_squared -
614 rec->time * rec->time;
615
Chase Douglase330b3b2010-04-26 14:02:05 -0400616 /*
617 * Divide only 1000 for ns^2 -> us^2 conversion.
618 * trace_print_graph_duration will divide 1000 again.
619 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200620 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400621 }
622
Steven Rostedt34886c82009-03-25 21:00:47 -0400623 trace_seq_init(&s);
624 trace_print_graph_duration(rec->time, &s);
625 trace_seq_puts(&s, " ");
626 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400627 trace_seq_puts(&s, " ");
628 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400629 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400630#endif
631 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800632out:
633 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400634
Li Zefan3aaba202010-08-23 16:50:12 +0800635 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400636}
637
Steven Rostedtcafb1682009-03-24 20:50:39 -0400638static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400639{
640 struct ftrace_profile_page *pg;
641
Steven Rostedtcafb1682009-03-24 20:50:39 -0400642 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400643
644 while (pg) {
645 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
646 pg->index = 0;
647 pg = pg->next;
648 }
649
Steven Rostedtcafb1682009-03-24 20:50:39 -0400650 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400651 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
652}
653
Steven Rostedtcafb1682009-03-24 20:50:39 -0400654int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400655{
656 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400657 int functions;
658 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400659 int i;
660
661 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400662 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400663 return 0;
664
Steven Rostedtcafb1682009-03-24 20:50:39 -0400665 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
666 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400667 return -ENOMEM;
668
Steven Rostedt318e0a72009-03-25 20:06:34 -0400669#ifdef CONFIG_DYNAMIC_FTRACE
670 functions = ftrace_update_tot_cnt;
671#else
672 /*
673 * We do not know the number of functions that exist because
674 * dynamic tracing is what counts them. With past experience
675 * we have around 20K functions. That should be more than enough.
676 * It is highly unlikely we will execute every function in
677 * the kernel.
678 */
679 functions = 20000;
680#endif
681
Steven Rostedtcafb1682009-03-24 20:50:39 -0400682 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400683
Steven Rostedt318e0a72009-03-25 20:06:34 -0400684 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
685
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900686 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400687 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400688 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400689 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400690 pg = pg->next;
691 }
692
693 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400694
695 out_free:
696 pg = stat->start;
697 while (pg) {
698 unsigned long tmp = (unsigned long)pg;
699
700 pg = pg->next;
701 free_page(tmp);
702 }
703
Steven Rostedt318e0a72009-03-25 20:06:34 -0400704 stat->pages = NULL;
705 stat->start = NULL;
706
707 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400708}
709
Steven Rostedtcafb1682009-03-24 20:50:39 -0400710static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400711{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400712 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400713 int size;
714
Steven Rostedtcafb1682009-03-24 20:50:39 -0400715 stat = &per_cpu(ftrace_profile_stats, cpu);
716
717 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400718 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400719 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400720 return 0;
721 }
722
723 /*
724 * We are profiling all functions, but usually only a few thousand
725 * functions are hit. We'll make a hash of 1024 items.
726 */
727 size = FTRACE_PROFILE_HASH_SIZE;
728
Steven Rostedtcafb1682009-03-24 20:50:39 -0400729 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400730
Steven Rostedtcafb1682009-03-24 20:50:39 -0400731 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400732 return -ENOMEM;
733
Steven Rostedt318e0a72009-03-25 20:06:34 -0400734 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400735 if (ftrace_profile_pages_init(stat) < 0) {
736 kfree(stat->hash);
737 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400738 return -ENOMEM;
739 }
740
741 return 0;
742}
743
Steven Rostedtcafb1682009-03-24 20:50:39 -0400744static int ftrace_profile_init(void)
745{
746 int cpu;
747 int ret = 0;
748
Miao Xiec4602c12013-12-16 15:20:01 +0800749 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750 ret = ftrace_profile_init_cpu(cpu);
751 if (ret)
752 break;
753 }
754
755 return ret;
756}
757
Steven Rostedt493762f2009-03-23 17:12:36 -0400758/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400759static struct ftrace_profile *
760ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400761{
762 struct ftrace_profile *rec;
763 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400764 unsigned long key;
765
Namhyung Kim20079eb2013-04-10 08:55:50 +0900766 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400767 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400768
769 if (hlist_empty(hhd))
770 return NULL;
771
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400772 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400773 if (rec->ip == ip)
774 return rec;
775 }
776
777 return NULL;
778}
779
Steven Rostedtcafb1682009-03-24 20:50:39 -0400780static void ftrace_add_profile(struct ftrace_profile_stat *stat,
781 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400782{
783 unsigned long key;
784
Namhyung Kim20079eb2013-04-10 08:55:50 +0900785 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400786 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400787}
788
Steven Rostedt318e0a72009-03-25 20:06:34 -0400789/*
790 * The memory is already allocated, this simply finds a new record to use.
791 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400792static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400793ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400794{
795 struct ftrace_profile *rec = NULL;
796
Steven Rostedt318e0a72009-03-25 20:06:34 -0400797 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400798 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400799 goto out;
800
Steven Rostedt493762f2009-03-23 17:12:36 -0400801 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400802 * Try to find the function again since an NMI
803 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400804 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400805 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400806 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400807 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400808
Steven Rostedtcafb1682009-03-24 20:50:39 -0400809 if (stat->pages->index == PROFILES_PER_PAGE) {
810 if (!stat->pages->next)
811 goto out;
812 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400813 }
814
Steven Rostedtcafb1682009-03-24 20:50:39 -0400815 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400816 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400817 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400818
Steven Rostedt493762f2009-03-23 17:12:36 -0400819 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400820 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400821
822 return rec;
823}
824
Steven Rostedt493762f2009-03-23 17:12:36 -0400825static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400826function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400827 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400828{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400829 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400830 struct ftrace_profile *rec;
831 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400832
833 if (!ftrace_profile_enabled)
834 return;
835
Steven Rostedt493762f2009-03-23 17:12:36 -0400836 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400837
Christoph Lameterbdffd892014-04-29 14:17:40 -0500838 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400839 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400840 goto out;
841
842 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400843 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400844 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400845 if (!rec)
846 goto out;
847 }
848
849 rec->counter++;
850 out:
851 local_irq_restore(flags);
852}
853
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400854#ifdef CONFIG_FUNCTION_GRAPH_TRACER
855static int profile_graph_entry(struct ftrace_graph_ent *trace)
856{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400857 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400858 return 1;
859}
860
861static void profile_graph_return(struct ftrace_graph_ret *trace)
862{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400863 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400864 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400865 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400866 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400867
868 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500869 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400870 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400871 goto out;
872
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400873 /* If the calltime was zero'd ignore it */
874 if (!trace->calltime)
875 goto out;
876
Steven Rostedta2a16d62009-03-24 23:17:58 -0400877 calltime = trace->rettime - trace->calltime;
878
879 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
880 int index;
881
882 index = trace->depth;
883
884 /* Append this call time to the parent time to subtract */
885 if (index)
886 current->ret_stack[index - 1].subtime += calltime;
887
888 if (current->ret_stack[index].subtime < calltime)
889 calltime -= current->ret_stack[index].subtime;
890 else
891 calltime = 0;
892 }
893
Steven Rostedtcafb1682009-03-24 20:50:39 -0400894 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400895 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400896 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400897 rec->time_squared += calltime * calltime;
898 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400899
Steven Rostedtcafb1682009-03-24 20:50:39 -0400900 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400901 local_irq_restore(flags);
902}
903
904static int register_ftrace_profiler(void)
905{
906 return register_ftrace_graph(&profile_graph_return,
907 &profile_graph_entry);
908}
909
910static void unregister_ftrace_profiler(void)
911{
912 unregister_ftrace_graph();
913}
914#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100915static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400916 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900917 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400918 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400919};
920
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400921static int register_ftrace_profiler(void)
922{
923 return register_ftrace_function(&ftrace_profile_ops);
924}
925
926static void unregister_ftrace_profiler(void)
927{
928 unregister_ftrace_function(&ftrace_profile_ops);
929}
930#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
931
Steven Rostedt493762f2009-03-23 17:12:36 -0400932static ssize_t
933ftrace_profile_write(struct file *filp, const char __user *ubuf,
934 size_t cnt, loff_t *ppos)
935{
936 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400937 int ret;
938
Peter Huewe22fe9b52011-06-07 21:58:27 +0200939 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
940 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400941 return ret;
942
943 val = !!val;
944
945 mutex_lock(&ftrace_profile_lock);
946 if (ftrace_profile_enabled ^ val) {
947 if (val) {
948 ret = ftrace_profile_init();
949 if (ret < 0) {
950 cnt = ret;
951 goto out;
952 }
953
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400954 ret = register_ftrace_profiler();
955 if (ret < 0) {
956 cnt = ret;
957 goto out;
958 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400959 ftrace_profile_enabled = 1;
960 } else {
961 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400962 /*
963 * unregister_ftrace_profiler calls stop_machine
964 * so this acts like an synchronize_sched.
965 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400966 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400967 }
968 }
969 out:
970 mutex_unlock(&ftrace_profile_lock);
971
Jiri Olsacf8517c2009-10-23 19:36:16 -0400972 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400973
974 return cnt;
975}
976
977static ssize_t
978ftrace_profile_read(struct file *filp, char __user *ubuf,
979 size_t cnt, loff_t *ppos)
980{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400981 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400982 int r;
983
984 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
985 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
986}
987
988static const struct file_operations ftrace_profile_fops = {
989 .open = tracing_open_generic,
990 .read = ftrace_profile_read,
991 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200992 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400993};
994
Steven Rostedtcafb1682009-03-24 20:50:39 -0400995/* used to initialize the real stat files */
996static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400997 .name = "functions",
998 .stat_start = function_stat_start,
999 .stat_next = function_stat_next,
1000 .stat_cmp = function_stat_cmp,
1001 .stat_headers = function_stat_headers,
1002 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001003};
1004
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001005static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001006{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001007 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001008 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001009 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001010 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001011 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001012
Steven Rostedtcafb1682009-03-24 20:50:39 -04001013 for_each_possible_cpu(cpu) {
1014 stat = &per_cpu(ftrace_profile_stats, cpu);
1015
1016 /* allocate enough for function name + cpu number */
1017 name = kmalloc(32, GFP_KERNEL);
1018 if (!name) {
1019 /*
1020 * The files created are permanent, if something happens
1021 * we still do not free memory.
1022 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001023 WARN(1,
1024 "Could not allocate stat file for cpu %d\n",
1025 cpu);
1026 return;
1027 }
1028 stat->stat = function_stats;
1029 snprintf(name, 32, "function%d", cpu);
1030 stat->stat.name = name;
1031 ret = register_stat_tracer(&stat->stat);
1032 if (ret) {
1033 WARN(1,
1034 "Could not register function stat for cpu %d\n",
1035 cpu);
1036 kfree(name);
1037 return;
1038 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001039 }
1040
1041 entry = debugfs_create_file("function_profile_enabled", 0644,
1042 d_tracer, NULL, &ftrace_profile_fops);
1043 if (!entry)
1044 pr_warning("Could not create debugfs "
1045 "'function_profile_enabled' entry\n");
1046}
1047
1048#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001049static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001050{
1051}
1052#endif /* CONFIG_FUNCTION_PROFILER */
1053
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001054static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1055
Steven Rostedt3d083392008-05-12 21:20:42 +02001056#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001057
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001058static struct ftrace_ops *removed_ops;
1059
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001060#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001061# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001062#endif
1063
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001064static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1065
Steven Rostedtb6887d72009-02-17 12:32:04 -05001066struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001067 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001068 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001069 unsigned long flags;
1070 unsigned long ip;
1071 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001072 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001073};
1074
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001075struct ftrace_func_entry {
1076 struct hlist_node hlist;
1077 unsigned long ip;
1078};
1079
1080struct ftrace_hash {
1081 unsigned long size_bits;
1082 struct hlist_head *buckets;
1083 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001084 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001085};
1086
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001087/*
1088 * We make these constant because no one should touch them,
1089 * but they are used as the default "empty hash", to avoid allocating
1090 * it all the time. These are in a read only section such that if
1091 * anyone does try to modify it, it will cause an exception.
1092 */
1093static const struct hlist_head empty_buckets[1];
1094static const struct ftrace_hash empty_hash = {
1095 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001096};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001097#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001098
Steven Rostedt2b499382011-05-03 22:49:52 -04001099static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001100 .func = ftrace_stub,
1101 .local_hash.notrace_hash = EMPTY_HASH,
1102 .local_hash.filter_hash = EMPTY_HASH,
1103 INIT_OPS_HASH(global_ops)
1104 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1105 FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001106};
1107
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001108struct ftrace_page {
1109 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001110 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001111 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001112 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001113};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001114
Steven Rostedta7900872011-12-16 16:23:44 -05001115#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1116#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001117
1118/* estimate from running different kernels */
1119#define NR_TO_INIT 10000
1120
1121static struct ftrace_page *ftrace_pages_start;
1122static struct ftrace_page *ftrace_pages;
1123
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001124static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001125{
1126 return !hash || !hash->count;
1127}
1128
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001129static struct ftrace_func_entry *
1130ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1131{
1132 unsigned long key;
1133 struct ftrace_func_entry *entry;
1134 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001135
Steven Rostedt06a51d92011-12-19 19:07:36 -05001136 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001137 return NULL;
1138
1139 if (hash->size_bits > 0)
1140 key = hash_long(ip, hash->size_bits);
1141 else
1142 key = 0;
1143
1144 hhd = &hash->buckets[key];
1145
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001146 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001147 if (entry->ip == ip)
1148 return entry;
1149 }
1150 return NULL;
1151}
1152
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001153static void __add_hash_entry(struct ftrace_hash *hash,
1154 struct ftrace_func_entry *entry)
1155{
1156 struct hlist_head *hhd;
1157 unsigned long key;
1158
1159 if (hash->size_bits)
1160 key = hash_long(entry->ip, hash->size_bits);
1161 else
1162 key = 0;
1163
1164 hhd = &hash->buckets[key];
1165 hlist_add_head(&entry->hlist, hhd);
1166 hash->count++;
1167}
1168
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001169static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1170{
1171 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001172
1173 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1174 if (!entry)
1175 return -ENOMEM;
1176
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001177 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001178 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001179
1180 return 0;
1181}
1182
1183static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001184free_hash_entry(struct ftrace_hash *hash,
1185 struct ftrace_func_entry *entry)
1186{
1187 hlist_del(&entry->hlist);
1188 kfree(entry);
1189 hash->count--;
1190}
1191
1192static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001193remove_hash_entry(struct ftrace_hash *hash,
1194 struct ftrace_func_entry *entry)
1195{
1196 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001197 hash->count--;
1198}
1199
1200static void ftrace_hash_clear(struct ftrace_hash *hash)
1201{
1202 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001203 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001204 struct ftrace_func_entry *entry;
1205 int size = 1 << hash->size_bits;
1206 int i;
1207
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001208 if (!hash->count)
1209 return;
1210
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001211 for (i = 0; i < size; i++) {
1212 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001213 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001214 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001215 }
1216 FTRACE_WARN_ON(hash->count);
1217}
1218
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001219static void free_ftrace_hash(struct ftrace_hash *hash)
1220{
1221 if (!hash || hash == EMPTY_HASH)
1222 return;
1223 ftrace_hash_clear(hash);
1224 kfree(hash->buckets);
1225 kfree(hash);
1226}
1227
Steven Rostedt07fd5512011-05-05 18:03:47 -04001228static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1229{
1230 struct ftrace_hash *hash;
1231
1232 hash = container_of(rcu, struct ftrace_hash, rcu);
1233 free_ftrace_hash(hash);
1234}
1235
1236static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1237{
1238 if (!hash || hash == EMPTY_HASH)
1239 return;
1240 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1241}
1242
Jiri Olsa5500fa52012-02-15 15:51:54 +01001243void ftrace_free_filter(struct ftrace_ops *ops)
1244{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001245 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001246 free_ftrace_hash(ops->func_hash->filter_hash);
1247 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001248}
1249
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001250static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1251{
1252 struct ftrace_hash *hash;
1253 int size;
1254
1255 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1256 if (!hash)
1257 return NULL;
1258
1259 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001260 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001261
1262 if (!hash->buckets) {
1263 kfree(hash);
1264 return NULL;
1265 }
1266
1267 hash->size_bits = size_bits;
1268
1269 return hash;
1270}
1271
1272static struct ftrace_hash *
1273alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1274{
1275 struct ftrace_func_entry *entry;
1276 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001277 int size;
1278 int ret;
1279 int i;
1280
1281 new_hash = alloc_ftrace_hash(size_bits);
1282 if (!new_hash)
1283 return NULL;
1284
1285 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001286 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001287 return new_hash;
1288
1289 size = 1 << hash->size_bits;
1290 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001291 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001292 ret = add_hash_entry(new_hash, entry->ip);
1293 if (ret < 0)
1294 goto free_hash;
1295 }
1296 }
1297
1298 FTRACE_WARN_ON(new_hash->count != hash->count);
1299
1300 return new_hash;
1301
1302 free_hash:
1303 free_ftrace_hash(new_hash);
1304 return NULL;
1305}
1306
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001307static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001308ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001309static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001310ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001311
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001312static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001313ftrace_hash_move(struct ftrace_ops *ops, int enable,
1314 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001315{
1316 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001317 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001318 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001319 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001320 int size = src->count;
1321 int bits = 0;
1322 int i;
1323
1324 /*
1325 * If the new source is empty, just free dst and assign it
1326 * the empty_hash.
1327 */
1328 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001329 new_hash = EMPTY_HASH;
1330 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001331 }
1332
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001333 /*
1334 * Make the hash size about 1/2 the # found
1335 */
1336 for (size /= 2; size; size >>= 1)
1337 bits++;
1338
1339 /* Don't allocate too much */
1340 if (bits > FTRACE_HASH_MAX_BITS)
1341 bits = FTRACE_HASH_MAX_BITS;
1342
Steven Rostedt07fd5512011-05-05 18:03:47 -04001343 new_hash = alloc_ftrace_hash(bits);
1344 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001345 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001346
1347 size = 1 << src->size_bits;
1348 for (i = 0; i < size; i++) {
1349 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001350 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001351 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001352 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001353 }
1354 }
1355
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001356update:
1357 /*
1358 * Remove the current set, update the hash and add
1359 * them back.
1360 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001361 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001362
Steven Rostedt07fd5512011-05-05 18:03:47 -04001363 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001364
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001365 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001366
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001367 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001368}
1369
Steven Rostedt265c8312009-02-13 12:43:56 -05001370/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001371 * Test the hashes for this ops to see if we want to call
1372 * the ops->func or not.
1373 *
1374 * It's a match if the ip is in the ops->filter_hash or
1375 * the filter_hash does not exist or is empty,
1376 * AND
1377 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001378 *
1379 * This needs to be called with preemption disabled as
1380 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001381 */
1382static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001383ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001384{
1385 struct ftrace_hash *filter_hash;
1386 struct ftrace_hash *notrace_hash;
1387 int ret;
1388
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001389#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1390 /*
1391 * There's a small race when adding ops that the ftrace handler
1392 * that wants regs, may be called without them. We can not
1393 * allow that handler to be called if regs is NULL.
1394 */
1395 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1396 return 0;
1397#endif
1398
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001399 filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1400 notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001401
Steven Rostedt06a51d92011-12-19 19:07:36 -05001402 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001403 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001404 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001405 !ftrace_lookup_ip(notrace_hash, ip)))
1406 ret = 1;
1407 else
1408 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001409
1410 return ret;
1411}
1412
1413/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001414 * This is a double for. Do not use 'break' to break out of the loop,
1415 * you must use a goto.
1416 */
1417#define do_for_each_ftrace_rec(pg, rec) \
1418 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1419 int _____i; \
1420 for (_____i = 0; _____i < pg->index; _____i++) { \
1421 rec = &pg->records[_____i];
1422
1423#define while_for_each_ftrace_rec() \
1424 } \
1425 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301426
Steven Rostedt5855fea2011-12-16 19:27:42 -05001427
1428static int ftrace_cmp_recs(const void *a, const void *b)
1429{
Steven Rostedta650e022012-04-25 13:48:13 -04001430 const struct dyn_ftrace *key = a;
1431 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001432
Steven Rostedta650e022012-04-25 13:48:13 -04001433 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001434 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001435 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1436 return 1;
1437 return 0;
1438}
1439
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001440static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001441{
1442 struct ftrace_page *pg;
1443 struct dyn_ftrace *rec;
1444 struct dyn_ftrace key;
1445
1446 key.ip = start;
1447 key.flags = end; /* overload flags, as it is unsigned long */
1448
1449 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1450 if (end < pg->records[0].ip ||
1451 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1452 continue;
1453 rec = bsearch(&key, pg->records, pg->index,
1454 sizeof(struct dyn_ftrace),
1455 ftrace_cmp_recs);
1456 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001457 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001458 }
1459
Steven Rostedt5855fea2011-12-16 19:27:42 -05001460 return 0;
1461}
1462
Steven Rostedtc88fd862011-08-16 09:53:39 -04001463/**
1464 * ftrace_location - return true if the ip giving is a traced location
1465 * @ip: the instruction pointer to check
1466 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001467 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001468 * That is, the instruction that is either a NOP or call to
1469 * the function tracer. It checks the ftrace internal tables to
1470 * determine if the address belongs or not.
1471 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001472unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001473{
Steven Rostedta650e022012-04-25 13:48:13 -04001474 return ftrace_location_range(ip, ip);
1475}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001476
Steven Rostedta650e022012-04-25 13:48:13 -04001477/**
1478 * ftrace_text_reserved - return true if range contains an ftrace location
1479 * @start: start of range to search
1480 * @end: end of range to search (inclusive). @end points to the last byte to check.
1481 *
1482 * Returns 1 if @start and @end contains a ftrace location.
1483 * That is, the instruction that is either a NOP or call to
1484 * the function tracer. It checks the ftrace internal tables to
1485 * determine if the address belongs or not.
1486 */
Sasha Levind88471c2013-01-09 18:09:20 -05001487int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001488{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001489 unsigned long ret;
1490
1491 ret = ftrace_location_range((unsigned long)start,
1492 (unsigned long)end);
1493
1494 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001495}
1496
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001497/* Test if ops registered to this rec needs regs */
1498static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1499{
1500 struct ftrace_ops *ops;
1501 bool keep_regs = false;
1502
1503 for (ops = ftrace_ops_list;
1504 ops != &ftrace_list_end; ops = ops->next) {
1505 /* pass rec in as regs to have non-NULL val */
1506 if (ftrace_ops_test(ops, rec->ip, rec)) {
1507 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1508 keep_regs = true;
1509 break;
1510 }
1511 }
1512 }
1513
1514 return keep_regs;
1515}
1516
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001517static void ftrace_remove_tramp(struct ftrace_ops *ops,
1518 struct dyn_ftrace *rec)
1519{
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001520 /* If TRAMP is not set, no ops should have a trampoline for this */
1521 if (!(rec->flags & FTRACE_FL_TRAMP))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001522 return;
1523
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001524 rec->flags &= ~FTRACE_FL_TRAMP;
1525
1526 if ((!ftrace_hash_empty(ops->func_hash->filter_hash) &&
1527 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip)) ||
1528 ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
1529 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001530 /*
1531 * The tramp_hash entry will be removed at time
1532 * of update.
1533 */
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04001534 ops->nr_trampolines--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001535}
1536
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001537static void ftrace_clear_tramps(struct dyn_ftrace *rec, struct ftrace_ops *ops)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001538{
1539 struct ftrace_ops *op;
1540
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001541 /* If TRAMP is not set, no ops should have a trampoline for this */
1542 if (!(rec->flags & FTRACE_FL_TRAMP))
1543 return;
1544
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001545 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001546 /*
1547 * This function is called to clear other tramps
1548 * not the one that is being updated.
1549 */
1550 if (op == ops)
1551 continue;
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04001552 if (op->nr_trampolines)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001553 ftrace_remove_tramp(op, rec);
1554 } while_for_each_ftrace_op(op);
1555}
1556
Steven Rostedted926f92011-05-03 13:25:24 -04001557static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1558 int filter_hash,
1559 bool inc)
1560{
1561 struct ftrace_hash *hash;
1562 struct ftrace_hash *other_hash;
1563 struct ftrace_page *pg;
1564 struct dyn_ftrace *rec;
1565 int count = 0;
1566 int all = 0;
1567
1568 /* Only update if the ops has been registered */
1569 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1570 return;
1571
1572 /*
1573 * In the filter_hash case:
1574 * If the count is zero, we update all records.
1575 * Otherwise we just update the items in the hash.
1576 *
1577 * In the notrace_hash case:
1578 * We enable the update in the hash.
1579 * As disabling notrace means enabling the tracing,
1580 * and enabling notrace means disabling, the inc variable
1581 * gets inversed.
1582 */
1583 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001584 hash = ops->func_hash->filter_hash;
1585 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001586 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001587 all = 1;
1588 } else {
1589 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001590 hash = ops->func_hash->notrace_hash;
1591 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001592 /*
1593 * If the notrace hash has no items,
1594 * then there's nothing to do.
1595 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001596 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001597 return;
1598 }
1599
1600 do_for_each_ftrace_rec(pg, rec) {
1601 int in_other_hash = 0;
1602 int in_hash = 0;
1603 int match = 0;
1604
1605 if (all) {
1606 /*
1607 * Only the filter_hash affects all records.
1608 * Update if the record is not in the notrace hash.
1609 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001610 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001611 match = 1;
1612 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001613 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1614 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001615
1616 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001617 * If filter_hash is set, we want to match all functions
1618 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001619 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001620 * If filter_hash is not set, then we are decrementing.
1621 * That means we match anything that is in the hash
1622 * and also in the other_hash. That is, we need to turn
1623 * off functions in the other hash because they are disabled
1624 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001625 */
1626 if (filter_hash && in_hash && !in_other_hash)
1627 match = 1;
1628 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001629 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001630 match = 1;
1631 }
1632 if (!match)
1633 continue;
1634
1635 if (inc) {
1636 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001637 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001638 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001639
1640 /*
1641 * If there's only a single callback registered to a
1642 * function, and the ops has a trampoline registered
1643 * for it, then we can call it directly.
1644 */
1645 if (ftrace_rec_count(rec) == 1 && ops->trampoline) {
1646 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04001647 ops->nr_trampolines++;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001648 } else {
1649 /*
1650 * If we are adding another function callback
1651 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001652 * custom trampoline in use, then we need to go
1653 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001654 */
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001655 ftrace_clear_tramps(rec, ops);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001656 }
1657
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001658 /*
1659 * If any ops wants regs saved for this function
1660 * then all ops will get saved regs.
1661 */
1662 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1663 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001664 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001665 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Steven Rostedted926f92011-05-03 13:25:24 -04001666 return;
1667 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001668
1669 if (ops->trampoline && !ftrace_rec_count(rec))
1670 ftrace_remove_tramp(ops, rec);
1671
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001672 /*
1673 * If the rec had REGS enabled and the ops that is
1674 * being removed had REGS set, then see if there is
1675 * still any ops for this record that wants regs.
1676 * If not, we can stop recording them.
1677 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001678 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001679 rec->flags & FTRACE_FL_REGS &&
1680 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1681 if (!test_rec_ops_needs_regs(rec))
1682 rec->flags &= ~FTRACE_FL_REGS;
1683 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001684
1685 /*
1686 * flags will be cleared in ftrace_check_record()
1687 * if rec count is zero.
1688 */
Steven Rostedted926f92011-05-03 13:25:24 -04001689 }
1690 count++;
1691 /* Shortcut, if we handled all records, we are done. */
1692 if (!all && count == hash->count)
1693 return;
1694 } while_for_each_ftrace_rec();
1695}
1696
1697static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1698 int filter_hash)
1699{
1700 __ftrace_hash_rec_update(ops, filter_hash, 0);
1701}
1702
1703static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1704 int filter_hash)
1705{
1706 __ftrace_hash_rec_update(ops, filter_hash, 1);
1707}
1708
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001709static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1710 int filter_hash, int inc)
1711{
1712 struct ftrace_ops *op;
1713
1714 __ftrace_hash_rec_update(ops, filter_hash, inc);
1715
1716 if (ops->func_hash != &global_ops.local_hash)
1717 return;
1718
1719 /*
1720 * If the ops shares the global_ops hash, then we need to update
1721 * all ops that are enabled and use this hash.
1722 */
1723 do_for_each_ftrace_op(op, ftrace_ops_list) {
1724 /* Already done */
1725 if (op == ops)
1726 continue;
1727 if (op->func_hash == &global_ops.local_hash)
1728 __ftrace_hash_rec_update(op, filter_hash, inc);
1729 } while_for_each_ftrace_op(op);
1730}
1731
1732static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1733 int filter_hash)
1734{
1735 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1736}
1737
1738static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1739 int filter_hash)
1740{
1741 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1742}
1743
Steven Rostedt05736a42008-09-22 14:55:47 -07001744static void print_ip_ins(const char *fmt, unsigned char *p)
1745{
1746 int i;
1747
1748 printk(KERN_CONT "%s", fmt);
1749
1750 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1751 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1752}
1753
Steven Rostedtc88fd862011-08-16 09:53:39 -04001754/**
1755 * ftrace_bug - report and shutdown function tracer
1756 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1757 * @ip: The address that failed
1758 *
1759 * The arch code that enables or disables the function tracing
1760 * can call ftrace_bug() when it has detected a problem in
1761 * modifying the code. @failed should be one of either:
1762 * EFAULT - if the problem happens on reading the @ip address
1763 * EINVAL - if what is read at @ip is not what was expected
1764 * EPERM - if the problem happens on writting to the @ip address
1765 */
1766void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001767{
1768 switch (failed) {
1769 case -EFAULT:
1770 FTRACE_WARN_ON_ONCE(1);
1771 pr_info("ftrace faulted on modifying ");
1772 print_ip_sym(ip);
1773 break;
1774 case -EINVAL:
1775 FTRACE_WARN_ON_ONCE(1);
1776 pr_info("ftrace failed to modify ");
1777 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001778 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001779 printk(KERN_CONT "\n");
1780 break;
1781 case -EPERM:
1782 FTRACE_WARN_ON_ONCE(1);
1783 pr_info("ftrace faulted on writing ");
1784 print_ip_sym(ip);
1785 break;
1786 default:
1787 FTRACE_WARN_ON_ONCE(1);
1788 pr_info("ftrace faulted on unknown error ");
1789 print_ip_sym(ip);
1790 }
1791}
1792
Steven Rostedtc88fd862011-08-16 09:53:39 -04001793static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001794{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001795 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001796
Steven Rostedt982c3502008-11-15 16:31:41 -05001797 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001798 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001799 *
Steven Rostedted926f92011-05-03 13:25:24 -04001800 * If the record has a ref count, then we need to enable it
1801 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001802 *
Steven Rostedted926f92011-05-03 13:25:24 -04001803 * Otherwise we make sure its disabled.
1804 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001805 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001806 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001807 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001808 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04001809 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001810
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001811 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001812 * If enabling and the REGS flag does not match the REGS_EN, or
1813 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1814 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001815 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001816 if (flag) {
1817 if (!(rec->flags & FTRACE_FL_REGS) !=
1818 !(rec->flags & FTRACE_FL_REGS_EN))
1819 flag |= FTRACE_FL_REGS;
1820
1821 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1822 !(rec->flags & FTRACE_FL_TRAMP_EN))
1823 flag |= FTRACE_FL_TRAMP;
1824 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001825
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001826 /* If the state of this record hasn't changed, then do nothing */
1827 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001828 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001829
1830 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001831 /* Save off if rec is being enabled (for return value) */
1832 flag ^= rec->flags & FTRACE_FL_ENABLED;
1833
1834 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001835 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001836 if (flag & FTRACE_FL_REGS) {
1837 if (rec->flags & FTRACE_FL_REGS)
1838 rec->flags |= FTRACE_FL_REGS_EN;
1839 else
1840 rec->flags &= ~FTRACE_FL_REGS_EN;
1841 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001842 if (flag & FTRACE_FL_TRAMP) {
1843 if (rec->flags & FTRACE_FL_TRAMP)
1844 rec->flags |= FTRACE_FL_TRAMP_EN;
1845 else
1846 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1847 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001848 }
1849
1850 /*
1851 * If this record is being updated from a nop, then
1852 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001853 * Otherwise,
1854 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001855 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001856 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001857 */
1858 if (flag & FTRACE_FL_ENABLED)
1859 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001860
1861 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001862 }
1863
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001864 if (update) {
1865 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001866 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001867 rec->flags = 0;
1868 else
1869 /* Just disable the record (keep REGS state) */
1870 rec->flags &= ~FTRACE_FL_ENABLED;
1871 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001872
1873 return FTRACE_UPDATE_MAKE_NOP;
1874}
1875
1876/**
1877 * ftrace_update_record, set a record that now is tracing or not
1878 * @rec: the record to update
1879 * @enable: set to 1 if the record is tracing, zero to force disable
1880 *
1881 * The records that represent all functions that can be traced need
1882 * to be updated when tracing has been enabled.
1883 */
1884int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1885{
1886 return ftrace_check_record(rec, enable, 1);
1887}
1888
1889/**
1890 * ftrace_test_record, check if the record has been enabled or not
1891 * @rec: the record to test
1892 * @enable: set to 1 to check if enabled, 0 if it is disabled
1893 *
1894 * The arch code may need to test if a record is already set to
1895 * tracing to determine how to modify the function code that it
1896 * represents.
1897 */
1898int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1899{
1900 return ftrace_check_record(rec, enable, 0);
1901}
1902
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001903static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04001904ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
1905{
1906 struct ftrace_ops *op;
1907
1908 do_for_each_ftrace_op(op, ftrace_ops_list) {
1909
1910 if (!op->trampoline)
1911 continue;
1912
1913 if (ftrace_lookup_ip(op->func_hash->filter_hash, rec->ip) &&
1914 (ftrace_hash_empty(op->func_hash->notrace_hash) ||
1915 !ftrace_lookup_ip(op->func_hash->notrace_hash, rec->ip)))
1916 return op;
1917 } while_for_each_ftrace_op(op);
1918
1919 return NULL;
1920}
1921
1922static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001923ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1924{
1925 struct ftrace_ops *op;
1926
1927 /* Removed ops need to be tested first */
1928 if (removed_ops && removed_ops->tramp_hash) {
1929 if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip))
1930 return removed_ops;
1931 }
1932
1933 do_for_each_ftrace_op(op, ftrace_ops_list) {
1934 if (!op->tramp_hash)
1935 continue;
1936
1937 if (ftrace_lookup_ip(op->tramp_hash, rec->ip))
1938 return op;
1939
1940 } while_for_each_ftrace_op(op);
1941
1942 return NULL;
1943}
1944
1945static struct ftrace_ops *
1946ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1947{
1948 struct ftrace_ops *op;
1949
1950 do_for_each_ftrace_op(op, ftrace_ops_list) {
1951 /* pass rec in as regs to have non-NULL val */
1952 if (ftrace_ops_test(op, rec->ip, rec))
1953 return op;
1954 } while_for_each_ftrace_op(op);
1955
1956 return NULL;
1957}
1958
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001959/**
1960 * ftrace_get_addr_new - Get the call address to set to
1961 * @rec: The ftrace record descriptor
1962 *
1963 * If the record has the FTRACE_FL_REGS set, that means that it
1964 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1965 * is not not set, then it wants to convert to the normal callback.
1966 *
1967 * Returns the address of the trampoline to set to
1968 */
1969unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1970{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001971 struct ftrace_ops *ops;
1972
1973 /* Trampolines take precedence over regs */
1974 if (rec->flags & FTRACE_FL_TRAMP) {
1975 ops = ftrace_find_tramp_ops_new(rec);
1976 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001977 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
1978 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001979 /* Ftrace is shutting down, return anything */
1980 return (unsigned long)FTRACE_ADDR;
1981 }
1982 return ops->trampoline;
1983 }
1984
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001985 if (rec->flags & FTRACE_FL_REGS)
1986 return (unsigned long)FTRACE_REGS_ADDR;
1987 else
1988 return (unsigned long)FTRACE_ADDR;
1989}
1990
1991/**
1992 * ftrace_get_addr_curr - Get the call address that is already there
1993 * @rec: The ftrace record descriptor
1994 *
1995 * The FTRACE_FL_REGS_EN is set when the record already points to
1996 * a function that saves all the regs. Basically the '_EN' version
1997 * represents the current state of the function.
1998 *
1999 * Returns the address of the trampoline that is currently being called
2000 */
2001unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2002{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002003 struct ftrace_ops *ops;
2004
2005 /* Trampolines take precedence over regs */
2006 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2007 ops = ftrace_find_tramp_ops_curr(rec);
2008 if (FTRACE_WARN_ON(!ops)) {
2009 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
2010 (void *)rec->ip, (void *)rec->ip);
2011 /* Ftrace is shutting down, return anything */
2012 return (unsigned long)FTRACE_ADDR;
2013 }
2014 return ops->trampoline;
2015 }
2016
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002017 if (rec->flags & FTRACE_FL_REGS_EN)
2018 return (unsigned long)FTRACE_REGS_ADDR;
2019 else
2020 return (unsigned long)FTRACE_ADDR;
2021}
2022
Steven Rostedtc88fd862011-08-16 09:53:39 -04002023static int
2024__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2025{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002026 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002027 unsigned long ftrace_addr;
2028 int ret;
2029
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002030 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002031
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002032 /* This needs to be done before we call ftrace_update_record */
2033 ftrace_old_addr = ftrace_get_addr_curr(rec);
2034
2035 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002036
Steven Rostedtc88fd862011-08-16 09:53:39 -04002037 switch (ret) {
2038 case FTRACE_UPDATE_IGNORE:
2039 return 0;
2040
2041 case FTRACE_UPDATE_MAKE_CALL:
2042 return ftrace_make_call(rec, ftrace_addr);
2043
2044 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002045 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002046
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002047 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002048 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002049 }
2050
2051 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002052}
2053
Steven Rostedte4f5d542012-04-27 09:13:18 -04002054void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002055{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002056 struct dyn_ftrace *rec;
2057 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002058 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002059
Steven Rostedt45a4a232011-04-21 23:16:46 -04002060 if (unlikely(ftrace_disabled))
2061 return;
2062
Steven Rostedt265c8312009-02-13 12:43:56 -05002063 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04002064 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002065 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04002066 ftrace_bug(failed, rec->ip);
2067 /* Stop processing */
2068 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002069 }
2070 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002071}
2072
Steven Rostedtc88fd862011-08-16 09:53:39 -04002073struct ftrace_rec_iter {
2074 struct ftrace_page *pg;
2075 int index;
2076};
2077
2078/**
2079 * ftrace_rec_iter_start, start up iterating over traced functions
2080 *
2081 * Returns an iterator handle that is used to iterate over all
2082 * the records that represent address locations where functions
2083 * are traced.
2084 *
2085 * May return NULL if no records are available.
2086 */
2087struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2088{
2089 /*
2090 * We only use a single iterator.
2091 * Protected by the ftrace_lock mutex.
2092 */
2093 static struct ftrace_rec_iter ftrace_rec_iter;
2094 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2095
2096 iter->pg = ftrace_pages_start;
2097 iter->index = 0;
2098
2099 /* Could have empty pages */
2100 while (iter->pg && !iter->pg->index)
2101 iter->pg = iter->pg->next;
2102
2103 if (!iter->pg)
2104 return NULL;
2105
2106 return iter;
2107}
2108
2109/**
2110 * ftrace_rec_iter_next, get the next record to process.
2111 * @iter: The handle to the iterator.
2112 *
2113 * Returns the next iterator after the given iterator @iter.
2114 */
2115struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2116{
2117 iter->index++;
2118
2119 if (iter->index >= iter->pg->index) {
2120 iter->pg = iter->pg->next;
2121 iter->index = 0;
2122
2123 /* Could have empty pages */
2124 while (iter->pg && !iter->pg->index)
2125 iter->pg = iter->pg->next;
2126 }
2127
2128 if (!iter->pg)
2129 return NULL;
2130
2131 return iter;
2132}
2133
2134/**
2135 * ftrace_rec_iter_record, get the record at the iterator location
2136 * @iter: The current iterator location
2137 *
2138 * Returns the record that the current @iter is at.
2139 */
2140struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2141{
2142 return &iter->pg->records[iter->index];
2143}
2144
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302145static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002146ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002147{
2148 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002149 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002150
2151 ip = rec->ip;
2152
Steven Rostedt45a4a232011-04-21 23:16:46 -04002153 if (unlikely(ftrace_disabled))
2154 return 0;
2155
Shaohua Li25aac9d2009-01-09 11:29:40 +08002156 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002157 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08002158 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302159 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002160 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302161 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002162}
2163
Steven Rostedt000ab692009-02-17 13:35:06 -05002164/*
2165 * archs can override this function if they must do something
2166 * before the modifying code is performed.
2167 */
2168int __weak ftrace_arch_code_modify_prepare(void)
2169{
2170 return 0;
2171}
2172
2173/*
2174 * archs can override this function if they must do something
2175 * after the modifying code is performed.
2176 */
2177int __weak ftrace_arch_code_modify_post_process(void)
2178{
2179 return 0;
2180}
2181
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002182void ftrace_modify_all_code(int command)
2183{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002184 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002185 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002186
2187 /*
2188 * If the ftrace_caller calls a ftrace_ops func directly,
2189 * we need to make sure that it only traces functions it
2190 * expects to trace. When doing the switch of functions,
2191 * we need to update to the ftrace_ops_list_func first
2192 * before the transition between old and new calls are set,
2193 * as the ftrace_ops_list_func will check the ops hashes
2194 * to make sure the ops are having the right functions
2195 * traced.
2196 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002197 if (update) {
2198 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2199 if (FTRACE_WARN_ON(err))
2200 return;
2201 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002202
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002203 if (command & FTRACE_UPDATE_CALLS)
2204 ftrace_replace_code(1);
2205 else if (command & FTRACE_DISABLE_CALLS)
2206 ftrace_replace_code(0);
2207
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002208 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2209 function_trace_op = set_function_trace_op;
2210 smp_wmb();
2211 /* If irqs are disabled, we are in stop machine */
2212 if (!irqs_disabled())
2213 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002214 err = ftrace_update_ftrace_func(ftrace_trace_function);
2215 if (FTRACE_WARN_ON(err))
2216 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002217 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002218
2219 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002220 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002221 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002222 err = ftrace_disable_ftrace_graph_caller();
2223 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002224}
2225
Ingo Molnare309b412008-05-12 21:20:51 +02002226static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002227{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002228 int *command = data;
2229
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002230 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002231
Steven Rostedtc88fd862011-08-16 09:53:39 -04002232 return 0;
2233}
2234
2235/**
2236 * ftrace_run_stop_machine, go back to the stop machine method
2237 * @command: The command to tell ftrace what to do
2238 *
2239 * If an arch needs to fall back to the stop machine method, the
2240 * it can call this function.
2241 */
2242void ftrace_run_stop_machine(int command)
2243{
2244 stop_machine(__ftrace_modify_code, &command, NULL);
2245}
2246
2247/**
2248 * arch_ftrace_update_code, modify the code to trace or not trace
2249 * @command: The command that needs to be done
2250 *
2251 * Archs can override this function if it does not need to
2252 * run stop_machine() to modify code.
2253 */
2254void __weak arch_ftrace_update_code(int command)
2255{
2256 ftrace_run_stop_machine(command);
2257}
2258
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002259static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2260{
2261 struct ftrace_page *pg;
2262 struct dyn_ftrace *rec;
2263 int size, bits;
2264 int ret;
2265
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04002266 size = ops->nr_trampolines;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002267 bits = 0;
2268 /*
2269 * Make the hash size about 1/2 the # found
2270 */
2271 for (size /= 2; size; size >>= 1)
2272 bits++;
2273
2274 ops->tramp_hash = alloc_ftrace_hash(bits);
2275 /*
2276 * TODO: a failed allocation is going to screw up
2277 * the accounting of what needs to be modified
2278 * and not. For now, we kill ftrace if we fail
2279 * to allocate here. But there are ways around this,
2280 * but that will take a little more work.
2281 */
2282 if (!ops->tramp_hash)
2283 return -ENOMEM;
2284
2285 do_for_each_ftrace_rec(pg, rec) {
2286 if (ftrace_rec_count(rec) == 1 &&
2287 ftrace_ops_test(ops, rec->ip, rec)) {
2288
Steven Rostedt (Red Hat)2a0343b2014-07-24 09:56:27 -04002289 /*
2290 * If another ops adds to a rec, the rec will
2291 * lose its trampoline and never get it back
2292 * until all ops are off of it.
2293 */
2294 if (!(rec->flags & FTRACE_FL_TRAMP))
2295 continue;
2296
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002297 /* This record had better have a trampoline */
2298 if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2299 return -1;
2300
2301 ret = add_hash_entry(ops->tramp_hash, rec->ip);
2302 if (ret < 0)
2303 return ret;
2304 }
2305 } while_for_each_ftrace_rec();
2306
Steven Rostedt (Red Hat)dc6f03f2014-07-24 11:26:11 -04002307 /* The number of recs in the hash must match nr_trampolines */
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002308 if (FTRACE_WARN_ON(ops->tramp_hash->count != ops->nr_trampolines))
2309 pr_warn("count=%ld trampolines=%d\n",
2310 ops->tramp_hash->count,
2311 ops->nr_trampolines);
Steven Rostedt (Red Hat)dc6f03f2014-07-24 11:26:11 -04002312
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002313 return 0;
2314}
2315
2316static int ftrace_save_tramp_hashes(void)
2317{
2318 struct ftrace_ops *op;
2319 int ret;
2320
2321 /*
2322 * Now that any trampoline is being used, we need to save the
2323 * hashes for the ops that have them. This allows the mapping
2324 * back from the record to the ops that has the trampoline to
2325 * know what code is being replaced. Modifying code must always
2326 * verify what it is changing.
2327 */
2328 do_for_each_ftrace_op(op, ftrace_ops_list) {
2329
2330 /* The tramp_hash is recreated each time. */
2331 free_ftrace_hash(op->tramp_hash);
2332 op->tramp_hash = NULL;
2333
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04002334 if (op->nr_trampolines) {
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002335 ret = ftrace_save_ops_tramp_hash(op);
2336 if (ret)
2337 return ret;
2338 }
2339
2340 } while_for_each_ftrace_op(op);
2341
2342 return 0;
2343}
2344
Steven Rostedtc88fd862011-08-16 09:53:39 -04002345static void ftrace_run_update_code(int command)
2346{
2347 int ret;
2348
2349 ret = ftrace_arch_code_modify_prepare();
2350 FTRACE_WARN_ON(ret);
2351 if (ret)
2352 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002353
2354 /*
2355 * By default we use stop_machine() to modify the code.
2356 * But archs can do what ever they want as long as it
2357 * is safe. The stop_machine() is the safest, but also
2358 * produces the most overhead.
2359 */
2360 arch_ftrace_update_code(command);
2361
Steven Rostedt000ab692009-02-17 13:35:06 -05002362 ret = ftrace_arch_code_modify_post_process();
2363 FTRACE_WARN_ON(ret);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002364
2365 ret = ftrace_save_tramp_hashes();
2366 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002367}
2368
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002369static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002370static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002371
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002372static void control_ops_free(struct ftrace_ops *ops)
2373{
2374 free_percpu(ops->disabled);
2375}
2376
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002377static void ftrace_startup_enable(int command)
2378{
2379 if (saved_ftrace_func != ftrace_trace_function) {
2380 saved_ftrace_func = ftrace_trace_function;
2381 command |= FTRACE_UPDATE_TRACE_FUNC;
2382 }
2383
2384 if (!command || !ftrace_enabled)
2385 return;
2386
2387 ftrace_run_update_code(command);
2388}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002389
Steven Rostedta1cd6172011-05-23 15:24:25 -04002390static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002391{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002392 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002393
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002394 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002395 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002396
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002397 ret = __register_ftrace_function(ops);
2398 if (ret)
2399 return ret;
2400
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002401 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002402 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002403
Steven Rostedted926f92011-05-03 13:25:24 -04002404 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002405
2406 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002407
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002408 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002409
2410 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002411}
2412
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002413static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002414{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002415 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002416
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002417 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002418 return -ENODEV;
2419
2420 ret = __unregister_ftrace_function(ops);
2421 if (ret)
2422 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002423
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002424 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002425 /*
2426 * Just warn in case of unbalance, no need to kill ftrace, it's not
2427 * critical but the ftrace_call callers may be never nopped again after
2428 * further ftrace uses.
2429 */
2430 WARN_ON_ONCE(ftrace_start_up < 0);
2431
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002432 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002433
Namhyung Kima737e6d2014-06-12 23:56:12 +09002434 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002435
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002436 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002437
2438 if (saved_ftrace_func != ftrace_trace_function) {
2439 saved_ftrace_func = ftrace_trace_function;
2440 command |= FTRACE_UPDATE_TRACE_FUNC;
2441 }
2442
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002443 if (!command || !ftrace_enabled) {
2444 /*
2445 * If these are control ops, they still need their
2446 * per_cpu field freed. Since, function tracing is
2447 * not currently active, we can just free them
2448 * without synchronizing all CPUs.
2449 */
2450 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2451 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002452 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002453 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002454
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002455 /*
2456 * If the ops uses a trampoline, then it needs to be
2457 * tested first on update.
2458 */
2459 removed_ops = ops;
2460
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002461 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002462
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002463 removed_ops = NULL;
2464
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002465 /*
2466 * Dynamic ops may be freed, we must make sure that all
2467 * callers are done before leaving this function.
2468 * The same goes for freeing the per_cpu data of the control
2469 * ops.
2470 *
2471 * Again, normal synchronize_sched() is not good enough.
2472 * We need to do a hard force of sched synchronization.
2473 * This is because we use preempt_disable() to do RCU, but
2474 * the function tracers can be called where RCU is not watching
2475 * (like before user_exit()). We can not rely on the RCU
2476 * infrastructure to do the synchronization, thus we must do it
2477 * ourselves.
2478 */
2479 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2480 schedule_on_each_cpu(ftrace_sync);
2481
2482 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2483 control_ops_free(ops);
2484 }
2485
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002486 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002487}
2488
Ingo Molnare309b412008-05-12 21:20:51 +02002489static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002490{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002491 if (unlikely(ftrace_disabled))
2492 return;
2493
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002494 /* Force update next time */
2495 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002496 /* ftrace_start_up is true if we want ftrace running */
2497 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002498 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002499}
2500
Ingo Molnare309b412008-05-12 21:20:51 +02002501static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002502{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002503 if (unlikely(ftrace_disabled))
2504 return;
2505
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002506 /* ftrace_start_up is true if ftrace is running */
2507 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002508 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002509}
2510
Steven Rostedt3d083392008-05-12 21:20:42 +02002511static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002512unsigned long ftrace_update_tot_cnt;
2513
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002514static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002515{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002516 /*
2517 * Filter_hash being empty will default to trace module.
2518 * But notrace hash requires a test of individual module functions.
2519 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002520 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2521 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002522}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002523
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002524/*
2525 * Check if the current ops references the record.
2526 *
2527 * If the ops traces all functions, then it was already accounted for.
2528 * If the ops does not trace the current record function, skip it.
2529 * If the ops ignores the function via notrace filter, skip it.
2530 */
2531static inline bool
2532ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2533{
2534 /* If ops isn't enabled, ignore it */
2535 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2536 return 0;
2537
2538 /* If ops traces all mods, we already accounted for it */
2539 if (ops_traces_mod(ops))
2540 return 0;
2541
2542 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002543 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2544 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002545 return 0;
2546
2547 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002548 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002549 return 0;
2550
2551 return 1;
2552}
2553
2554static int referenced_filters(struct dyn_ftrace *rec)
2555{
2556 struct ftrace_ops *ops;
2557 int cnt = 0;
2558
2559 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2560 if (ops_references_rec(ops, rec))
2561 cnt++;
2562 }
2563
2564 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002565}
2566
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002567static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002568{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002569 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002570 struct dyn_ftrace *p;
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302571 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002572 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002573 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002574 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002575 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002576
2577 /*
2578 * When adding a module, we need to check if tracers are
2579 * currently enabled and if they are set to trace all functions.
2580 * If they are, we need to enable the module functions as well
2581 * as update the reference counts for those function records.
2582 */
2583 if (mod) {
2584 struct ftrace_ops *ops;
2585
2586 for (ops = ftrace_ops_list;
2587 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002588 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2589 if (ops_traces_mod(ops))
2590 ref++;
2591 else
2592 test = true;
2593 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002594 }
2595 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002596
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002597 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002598
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002599 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302600
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002601 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002602 int cnt = ref;
2603
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002604 /* If something went wrong, bail without enabling anything */
2605 if (unlikely(ftrace_disabled))
2606 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002607
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002608 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002609 if (test)
2610 cnt += referenced_filters(p);
2611 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302612
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002613 /*
2614 * Do the initial record conversion from mcount jump
2615 * to the NOP instructions.
2616 */
2617 if (!ftrace_code_disable(mod, p))
2618 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002619
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002620 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002621
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002622 /*
2623 * If the tracing is enabled, go ahead and enable the record.
2624 *
2625 * The reason not to enable the record immediatelly is the
2626 * inherent check of ftrace_make_nop/ftrace_make_call for
2627 * correct previous instructions. Making first the NOP
2628 * conversion puts the module to the correct state, thus
2629 * passing the ftrace_make_call check.
2630 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002631 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002632 int failed = __ftrace_replace_code(p, 1);
2633 if (failed)
2634 ftrace_bug(failed, p->ip);
2635 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002636 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002637 }
2638
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002639 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002640 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002641 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002642
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002643 return 0;
2644}
2645
Steven Rostedta7900872011-12-16 16:23:44 -05002646static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002647{
Steven Rostedta7900872011-12-16 16:23:44 -05002648 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002649 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002650
Steven Rostedta7900872011-12-16 16:23:44 -05002651 if (WARN_ON(!count))
2652 return -EINVAL;
2653
2654 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002655
2656 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002657 * We want to fill as much as possible. No more than a page
2658 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002659 */
Steven Rostedta7900872011-12-16 16:23:44 -05002660 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2661 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002662
Steven Rostedta7900872011-12-16 16:23:44 -05002663 again:
2664 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2665
2666 if (!pg->records) {
2667 /* if we can't allocate this size, try something smaller */
2668 if (!order)
2669 return -ENOMEM;
2670 order >>= 1;
2671 goto again;
2672 }
2673
2674 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2675 pg->size = cnt;
2676
2677 if (cnt > count)
2678 cnt = count;
2679
2680 return cnt;
2681}
2682
2683static struct ftrace_page *
2684ftrace_allocate_pages(unsigned long num_to_init)
2685{
2686 struct ftrace_page *start_pg;
2687 struct ftrace_page *pg;
2688 int order;
2689 int cnt;
2690
2691 if (!num_to_init)
2692 return 0;
2693
2694 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2695 if (!pg)
2696 return NULL;
2697
2698 /*
2699 * Try to allocate as much as possible in one continues
2700 * location that fills in all of the space. We want to
2701 * waste as little space as possible.
2702 */
2703 for (;;) {
2704 cnt = ftrace_allocate_records(pg, num_to_init);
2705 if (cnt < 0)
2706 goto free_pages;
2707
2708 num_to_init -= cnt;
2709 if (!num_to_init)
2710 break;
2711
2712 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2713 if (!pg->next)
2714 goto free_pages;
2715
2716 pg = pg->next;
2717 }
2718
2719 return start_pg;
2720
2721 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09002722 pg = start_pg;
2723 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05002724 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2725 free_pages((unsigned long)pg->records, order);
2726 start_pg = pg->next;
2727 kfree(pg);
2728 pg = start_pg;
2729 }
2730 pr_info("ftrace: FAILED to allocate memory for functions\n");
2731 return NULL;
2732}
2733
Steven Rostedt5072c592008-05-12 21:20:43 +02002734#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2735
2736struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002737 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002738 loff_t func_pos;
2739 struct ftrace_page *pg;
2740 struct dyn_ftrace *func;
2741 struct ftrace_func_probe *probe;
2742 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002743 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002744 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002745 int hidx;
2746 int idx;
2747 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002748};
2749
Ingo Molnare309b412008-05-12 21:20:51 +02002750static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002751t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002752{
2753 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002754 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002755 struct hlist_head *hhd;
2756
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002757 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002758 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002759
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002760 if (iter->probe)
2761 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002762 retry:
2763 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2764 return NULL;
2765
2766 hhd = &ftrace_func_hash[iter->hidx];
2767
2768 if (hlist_empty(hhd)) {
2769 iter->hidx++;
2770 hnd = NULL;
2771 goto retry;
2772 }
2773
2774 if (!hnd)
2775 hnd = hhd->first;
2776 else {
2777 hnd = hnd->next;
2778 if (!hnd) {
2779 iter->hidx++;
2780 goto retry;
2781 }
2782 }
2783
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002784 if (WARN_ON_ONCE(!hnd))
2785 return NULL;
2786
2787 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2788
2789 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002790}
2791
2792static void *t_hash_start(struct seq_file *m, loff_t *pos)
2793{
2794 struct ftrace_iterator *iter = m->private;
2795 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002796 loff_t l;
2797
Steven Rostedt69a30832011-12-19 15:21:16 -05002798 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2799 return NULL;
2800
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002801 if (iter->func_pos > *pos)
2802 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002803
Li Zefand82d6242009-06-24 09:54:54 +08002804 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002805 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002806 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002807 if (!p)
2808 break;
2809 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002810 if (!p)
2811 return NULL;
2812
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002813 /* Only set this if we have an item */
2814 iter->flags |= FTRACE_ITER_HASH;
2815
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002816 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002817}
2818
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002819static int
2820t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002821{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002822 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002823
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002824 rec = iter->probe;
2825 if (WARN_ON_ONCE(!rec))
2826 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002827
Steven Rostedt809dcf22009-02-16 23:06:01 -05002828 if (rec->ops->print)
2829 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2830
Steven Rostedtb375a112009-09-17 00:05:58 -04002831 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002832
2833 if (rec->data)
2834 seq_printf(m, ":%p", rec->data);
2835 seq_putc(m, '\n');
2836
2837 return 0;
2838}
2839
2840static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002841t_next(struct seq_file *m, void *v, loff_t *pos)
2842{
2843 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002844 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002845 struct dyn_ftrace *rec = NULL;
2846
Steven Rostedt45a4a232011-04-21 23:16:46 -04002847 if (unlikely(ftrace_disabled))
2848 return NULL;
2849
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002850 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002851 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002852
Steven Rostedt5072c592008-05-12 21:20:43 +02002853 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002854 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002855
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002856 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002857 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002858
Steven Rostedt5072c592008-05-12 21:20:43 +02002859 retry:
2860 if (iter->idx >= iter->pg->index) {
2861 if (iter->pg->next) {
2862 iter->pg = iter->pg->next;
2863 iter->idx = 0;
2864 goto retry;
2865 }
2866 } else {
2867 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05002868 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002869 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05002870
Steven Rostedt41c52c02008-05-22 11:46:33 -04002871 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002872 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04002873
2874 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002875 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002876
Steven Rostedt5072c592008-05-12 21:20:43 +02002877 rec = NULL;
2878 goto retry;
2879 }
2880 }
2881
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002882 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002883 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002884
2885 iter->func = rec;
2886
2887 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002888}
2889
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002890static void reset_iter_read(struct ftrace_iterator *iter)
2891{
2892 iter->pos = 0;
2893 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03002894 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002895}
2896
2897static void *t_start(struct seq_file *m, loff_t *pos)
2898{
2899 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002900 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002901 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002902 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002903
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002904 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002905
2906 if (unlikely(ftrace_disabled))
2907 return NULL;
2908
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002909 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002910 * If an lseek was done, then reset and start from beginning.
2911 */
2912 if (*pos < iter->pos)
2913 reset_iter_read(iter);
2914
2915 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002916 * For set_ftrace_filter reading, if we have the filter
2917 * off, we can short cut and just print out that all
2918 * functions are enabled.
2919 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002920 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002921 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002922 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002923 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002924 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002925 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002926 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002927 /* reset in case of seek/pread */
2928 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002929 return iter;
2930 }
2931
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002932 if (iter->flags & FTRACE_ITER_HASH)
2933 return t_hash_start(m, pos);
2934
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002935 /*
2936 * Unfortunately, we need to restart at ftrace_pages_start
2937 * every time we let go of the ftrace_mutex. This is because
2938 * those pointers can change without the lock.
2939 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002940 iter->pg = ftrace_pages_start;
2941 iter->idx = 0;
2942 for (l = 0; l <= *pos; ) {
2943 p = t_next(m, p, &l);
2944 if (!p)
2945 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002946 }
walimis5821e1b2008-11-15 15:19:06 +08002947
Steven Rostedt69a30832011-12-19 15:21:16 -05002948 if (!p)
2949 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002950
2951 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002952}
2953
2954static void t_stop(struct seq_file *m, void *p)
2955{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002956 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002957}
2958
2959static int t_show(struct seq_file *m, void *v)
2960{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002961 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002962 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002963
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002964 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002965 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002966
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002967 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002968 if (iter->flags & FTRACE_ITER_NOTRACE)
2969 seq_printf(m, "#### no functions disabled ####\n");
2970 else
2971 seq_printf(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002972 return 0;
2973 }
2974
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002975 rec = iter->func;
2976
Steven Rostedt5072c592008-05-12 21:20:43 +02002977 if (!rec)
2978 return 0;
2979
Steven Rostedt647bcd02011-05-03 14:39:21 -04002980 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002981 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002982 seq_printf(m, " (%ld)%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002983 ftrace_rec_count(rec),
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002984 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2985 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2986 struct ftrace_ops *ops;
2987
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002988 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002989 if (ops && ops->trampoline)
2990 seq_printf(m, "\ttramp: %pS",
2991 (void *)ops->trampoline);
2992 else
2993 seq_printf(m, "\ttramp: ERROR!");
2994 }
2995 }
2996
Steven Rostedt647bcd02011-05-03 14:39:21 -04002997 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002998
2999 return 0;
3000}
3001
James Morris88e9d342009-09-22 16:43:43 -07003002static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003003 .start = t_start,
3004 .next = t_next,
3005 .stop = t_stop,
3006 .show = t_show,
3007};
3008
Ingo Molnare309b412008-05-12 21:20:51 +02003009static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003010ftrace_avail_open(struct inode *inode, struct file *file)
3011{
3012 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003013
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003014 if (unlikely(ftrace_disabled))
3015 return -ENODEV;
3016
Jiri Olsa50e18b92012-04-25 10:23:39 +02003017 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3018 if (iter) {
3019 iter->pg = ftrace_pages_start;
3020 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003021 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003022
Jiri Olsa50e18b92012-04-25 10:23:39 +02003023 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003024}
3025
Steven Rostedt647bcd02011-05-03 14:39:21 -04003026static int
3027ftrace_enabled_open(struct inode *inode, struct file *file)
3028{
3029 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003030
3031 if (unlikely(ftrace_disabled))
3032 return -ENODEV;
3033
Jiri Olsa50e18b92012-04-25 10:23:39 +02003034 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3035 if (iter) {
3036 iter->pg = ftrace_pages_start;
3037 iter->flags = FTRACE_ITER_ENABLED;
3038 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003039 }
3040
Jiri Olsa50e18b92012-04-25 10:23:39 +02003041 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003042}
3043
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003044/**
3045 * ftrace_regex_open - initialize function tracer filter files
3046 * @ops: The ftrace_ops that hold the hash filters
3047 * @flag: The type of filter to process
3048 * @inode: The inode, usually passed in to your open routine
3049 * @file: The file, usually passed in to your open routine
3050 *
3051 * ftrace_regex_open() initializes the filter files for the
3052 * @ops. Depending on @flag it may process the filter hash or
3053 * the notrace hash of @ops. With this called from the open
3054 * routine, you can use ftrace_filter_write() for the write
3055 * routine if @flag has FTRACE_ITER_FILTER set, or
3056 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003057 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003058 * release must call ftrace_regex_release().
3059 */
3060int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003061ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003062 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003063{
3064 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003065 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003066 int ret = 0;
3067
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003068 ftrace_ops_init(ops);
3069
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003070 if (unlikely(ftrace_disabled))
3071 return -ENODEV;
3072
Steven Rostedt5072c592008-05-12 21:20:43 +02003073 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3074 if (!iter)
3075 return -ENOMEM;
3076
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003077 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3078 kfree(iter);
3079 return -ENOMEM;
3080 }
3081
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003082 iter->ops = ops;
3083 iter->flags = flag;
3084
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003085 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003086
Steven Rostedtf45948e2011-05-02 12:29:25 -04003087 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003088 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003089 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003090 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003091
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003092 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003093 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3094
3095 if (file->f_flags & O_TRUNC)
3096 iter->hash = alloc_ftrace_hash(size_bits);
3097 else
3098 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3099
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003100 if (!iter->hash) {
3101 trace_parser_put(&iter->parser);
3102 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003103 ret = -ENOMEM;
3104 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003105 }
3106 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003107
Steven Rostedt5072c592008-05-12 21:20:43 +02003108 if (file->f_mode & FMODE_READ) {
3109 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003110
3111 ret = seq_open(file, &show_ftrace_seq_ops);
3112 if (!ret) {
3113 struct seq_file *m = file->private_data;
3114 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003115 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003116 /* Failed */
3117 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003118 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003119 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003120 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003121 } else
3122 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003123
3124 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003125 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003126
3127 return ret;
3128}
3129
Steven Rostedt41c52c02008-05-22 11:46:33 -04003130static int
3131ftrace_filter_open(struct inode *inode, struct file *file)
3132{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003133 struct ftrace_ops *ops = inode->i_private;
3134
3135 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003136 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3137 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003138}
3139
3140static int
3141ftrace_notrace_open(struct inode *inode, struct file *file)
3142{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003143 struct ftrace_ops *ops = inode->i_private;
3144
3145 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003146 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003147}
3148
Steven Rostedt64e7c442009-02-13 17:08:48 -05003149static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003150{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003151 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003152 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003153
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003154 switch (type) {
3155 case MATCH_FULL:
3156 if (strcmp(str, regex) == 0)
3157 matched = 1;
3158 break;
3159 case MATCH_FRONT_ONLY:
3160 if (strncmp(str, regex, len) == 0)
3161 matched = 1;
3162 break;
3163 case MATCH_MIDDLE_ONLY:
3164 if (strstr(str, regex))
3165 matched = 1;
3166 break;
3167 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003168 slen = strlen(str);
3169 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003170 matched = 1;
3171 break;
3172 }
3173
3174 return matched;
3175}
3176
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003177static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003178enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003179{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003180 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003181 int ret = 0;
3182
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003183 entry = ftrace_lookup_ip(hash, rec->ip);
3184 if (not) {
3185 /* Do nothing if it doesn't exist */
3186 if (!entry)
3187 return 0;
3188
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003189 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003190 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003191 /* Do nothing if it exists */
3192 if (entry)
3193 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003194
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003195 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003196 }
3197 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003198}
3199
Steven Rostedt64e7c442009-02-13 17:08:48 -05003200static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003201ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3202 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003203{
3204 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003205 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003206
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003207 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3208
3209 if (mod) {
3210 /* module lookup requires matching the module */
3211 if (!modname || strcmp(modname, mod))
3212 return 0;
3213
3214 /* blank search means to match all funcs in the mod */
3215 if (!len)
3216 return 1;
3217 }
3218
Steven Rostedt64e7c442009-02-13 17:08:48 -05003219 return ftrace_match(str, regex, len, type);
3220}
3221
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003222static int
3223match_records(struct ftrace_hash *hash, char *buff,
3224 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003225{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003226 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003227 struct ftrace_page *pg;
3228 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003229 int type = MATCH_FULL;
3230 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003231 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003232 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003233
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003234 if (len) {
3235 type = filter_parse_regex(buff, len, &search, &not);
3236 search_len = strlen(search);
3237 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003238
Steven Rostedt52baf112009-02-14 01:15:39 -05003239 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003240
3241 if (unlikely(ftrace_disabled))
3242 goto out_unlock;
3243
Steven Rostedt265c8312009-02-13 12:43:56 -05003244 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003245 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003246 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003247 if (ret < 0) {
3248 found = ret;
3249 goto out_unlock;
3250 }
Li Zefan311d16d2009-12-08 11:15:11 +08003251 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003252 }
3253 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003254 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003255 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003256
3257 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003258}
3259
Steven Rostedt64e7c442009-02-13 17:08:48 -05003260static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003261ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003262{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003263 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003264}
3265
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003266static int
3267ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003268{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003269 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003270
Steven Rostedt64e7c442009-02-13 17:08:48 -05003271 /* blank or '*' mean the same */
3272 if (strcmp(buff, "*") == 0)
3273 buff[0] = 0;
3274
3275 /* handle the case of 'dont filter this module' */
3276 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3277 buff[0] = 0;
3278 not = 1;
3279 }
3280
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003281 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003282}
3283
Steven Rostedtf6180772009-02-14 00:40:25 -05003284/*
3285 * We register the module command as a template to show others how
3286 * to register the a command as well.
3287 */
3288
3289static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003290ftrace_mod_callback(struct ftrace_hash *hash,
3291 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003292{
3293 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003294 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003295
3296 /*
3297 * cmd == 'mod' because we only registered this func
3298 * for the 'mod' ftrace_func_command.
3299 * But if you register one func with multiple commands,
3300 * you can tell which command was used by the cmd
3301 * parameter.
3302 */
3303
3304 /* we must have a module name */
3305 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003306 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003307
3308 mod = strsep(&param, ":");
3309 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003310 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003311
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003312 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003313 if (!ret)
3314 ret = -EINVAL;
3315 if (ret < 0)
3316 return ret;
3317
3318 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003319}
3320
3321static struct ftrace_func_command ftrace_mod_cmd = {
3322 .name = "mod",
3323 .func = ftrace_mod_callback,
3324};
3325
3326static int __init ftrace_mod_cmd_init(void)
3327{
3328 return register_ftrace_command(&ftrace_mod_cmd);
3329}
Steven Rostedt6f415672012-10-05 12:13:07 -04003330core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003331
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003332static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003333 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003334{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003335 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003336 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003337 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003338
3339 key = hash_long(ip, FTRACE_HASH_BITS);
3340
3341 hhd = &ftrace_func_hash[key];
3342
3343 if (hlist_empty(hhd))
3344 return;
3345
3346 /*
3347 * Disable preemption for these calls to prevent a RCU grace
3348 * period. This syncs the hash iteration and freeing of items
3349 * on the hash. rcu_read_lock is too dangerous here.
3350 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003351 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003352 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003353 if (entry->ip == ip)
3354 entry->ops->func(ip, parent_ip, &entry->data);
3355 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003356 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003357}
3358
Steven Rostedtb6887d72009-02-17 12:32:04 -05003359static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003360{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003361 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003362 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003363 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003364};
3365
Steven Rostedtb6887d72009-02-17 12:32:04 -05003366static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003367
Steven Rostedtb6887d72009-02-17 12:32:04 -05003368static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003369{
Steven Rostedtb8489142011-05-04 09:27:52 -04003370 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003371 int i;
3372
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003373 if (ftrace_probe_registered) {
3374 /* still need to update the function call sites */
3375 if (ftrace_enabled)
3376 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003377 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003378 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003379
3380 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3381 struct hlist_head *hhd = &ftrace_func_hash[i];
3382 if (hhd->first)
3383 break;
3384 }
3385 /* Nothing registered? */
3386 if (i == FTRACE_FUNC_HASHSIZE)
3387 return;
3388
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003389 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003390
Steven Rostedtb6887d72009-02-17 12:32:04 -05003391 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003392}
3393
Steven Rostedtb6887d72009-02-17 12:32:04 -05003394static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003395{
3396 int i;
3397
Steven Rostedtb6887d72009-02-17 12:32:04 -05003398 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003399 return;
3400
3401 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3402 struct hlist_head *hhd = &ftrace_func_hash[i];
3403 if (hhd->first)
3404 return;
3405 }
3406
3407 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003408 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003409
Steven Rostedtb6887d72009-02-17 12:32:04 -05003410 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003411}
3412
3413
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003414static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003415{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003416 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003417 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003418 kfree(entry);
3419}
3420
Steven Rostedt59df055f2009-02-14 15:29:06 -05003421int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003422register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003423 void *data)
3424{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003425 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003426 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003427 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003428 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003429 struct ftrace_page *pg;
3430 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003431 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003432 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003433 int count = 0;
3434 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003435 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003436
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003437 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003438 len = strlen(search);
3439
Steven Rostedtb6887d72009-02-17 12:32:04 -05003440 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003441 if (WARN_ON(not))
3442 return -EINVAL;
3443
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003444 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003445
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003446 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003447 if (!hash) {
3448 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003449 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003450 }
3451
3452 if (unlikely(ftrace_disabled)) {
3453 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003454 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003455 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003456
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003457 mutex_lock(&ftrace_lock);
3458
Steven Rostedt59df055f2009-02-14 15:29:06 -05003459 do_for_each_ftrace_rec(pg, rec) {
3460
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003461 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003462 continue;
3463
3464 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3465 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003466 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003467 if (!count)
3468 count = -ENOMEM;
3469 goto out_unlock;
3470 }
3471
3472 count++;
3473
3474 entry->data = data;
3475
3476 /*
3477 * The caller might want to do something special
3478 * for each function we find. We call the callback
3479 * to give the caller an opportunity to do so.
3480 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003481 if (ops->init) {
3482 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003483 /* caller does not like this func */
3484 kfree(entry);
3485 continue;
3486 }
3487 }
3488
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003489 ret = enter_record(hash, rec, 0);
3490 if (ret < 0) {
3491 kfree(entry);
3492 count = ret;
3493 goto out_unlock;
3494 }
3495
Steven Rostedt59df055f2009-02-14 15:29:06 -05003496 entry->ops = ops;
3497 entry->ip = rec->ip;
3498
3499 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3500 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3501
3502 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003503
3504 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003505 if (!ret)
3506 free_ftrace_hash_rcu(old_hash);
3507 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003508 count = ret;
3509
Steven Rostedtb6887d72009-02-17 12:32:04 -05003510 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003511
3512 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003513 mutex_unlock(&ftrace_lock);
3514 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003515 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003516 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003517
3518 return count;
3519}
3520
3521enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003522 PROBE_TEST_FUNC = 1,
3523 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003524};
3525
3526static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003527__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003528 void *data, int flags)
3529{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003530 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003531 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003532 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003533 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003534 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003535 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003536 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003537 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003538 char str[KSYM_SYMBOL_LEN];
3539 int type = MATCH_FULL;
3540 int i, len = 0;
3541 char *search;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003542 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003543
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003544 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003545 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003546 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003547 int not;
3548
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003549 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003550 len = strlen(search);
3551
Steven Rostedtb6887d72009-02-17 12:32:04 -05003552 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003553 if (WARN_ON(not))
3554 return;
3555 }
3556
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003557 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003558
3559 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3560 if (!hash)
3561 /* Hmm, should report this somehow */
3562 goto out_unlock;
3563
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003564 INIT_LIST_HEAD(&free_list);
3565
Steven Rostedt59df055f2009-02-14 15:29:06 -05003566 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3567 struct hlist_head *hhd = &ftrace_func_hash[i];
3568
Sasha Levinb67bfe02013-02-27 17:06:00 -08003569 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003570
3571 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003572 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003573 continue;
3574
Steven Rostedtb6887d72009-02-17 12:32:04 -05003575 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003576 continue;
3577
3578 /* do this last, since it is the most expensive */
3579 if (glob) {
3580 kallsyms_lookup(entry->ip, NULL, NULL,
3581 NULL, str);
3582 if (!ftrace_match(str, glob, len, type))
3583 continue;
3584 }
3585
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003586 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3587 /* It is possible more than one entry had this ip */
3588 if (rec_entry)
3589 free_hash_entry(hash, rec_entry);
3590
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003591 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003592 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003593 }
3594 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003595 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003596 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003597 /*
3598 * Remove after the disable is called. Otherwise, if the last
3599 * probe is removed, a null hash means *all enabled*.
3600 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003601 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003602 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003603 if (!ret)
3604 free_ftrace_hash_rcu(old_hash);
3605
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003606 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3607 list_del(&entry->free_list);
3608 ftrace_free_entry(entry);
3609 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003610 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003611
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003612 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003613 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003614 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003615}
3616
3617void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003618unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003619 void *data)
3620{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003621 __unregister_ftrace_function_probe(glob, ops, data,
3622 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003623}
3624
3625void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003626unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003627{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003628 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003629}
3630
Steven Rostedtb6887d72009-02-17 12:32:04 -05003631void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003632{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003633 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003634}
3635
Steven Rostedtf6180772009-02-14 00:40:25 -05003636static LIST_HEAD(ftrace_commands);
3637static DEFINE_MUTEX(ftrace_cmd_mutex);
3638
Tom Zanussi38de93a2013-10-24 08:34:18 -05003639/*
3640 * Currently we only register ftrace commands from __init, so mark this
3641 * __init too.
3642 */
3643__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003644{
3645 struct ftrace_func_command *p;
3646 int ret = 0;
3647
3648 mutex_lock(&ftrace_cmd_mutex);
3649 list_for_each_entry(p, &ftrace_commands, list) {
3650 if (strcmp(cmd->name, p->name) == 0) {
3651 ret = -EBUSY;
3652 goto out_unlock;
3653 }
3654 }
3655 list_add(&cmd->list, &ftrace_commands);
3656 out_unlock:
3657 mutex_unlock(&ftrace_cmd_mutex);
3658
3659 return ret;
3660}
3661
Tom Zanussi38de93a2013-10-24 08:34:18 -05003662/*
3663 * Currently we only unregister ftrace commands from __init, so mark
3664 * this __init too.
3665 */
3666__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003667{
3668 struct ftrace_func_command *p, *n;
3669 int ret = -ENODEV;
3670
3671 mutex_lock(&ftrace_cmd_mutex);
3672 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3673 if (strcmp(cmd->name, p->name) == 0) {
3674 ret = 0;
3675 list_del_init(&p->list);
3676 goto out_unlock;
3677 }
3678 }
3679 out_unlock:
3680 mutex_unlock(&ftrace_cmd_mutex);
3681
3682 return ret;
3683}
3684
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003685static int ftrace_process_regex(struct ftrace_hash *hash,
3686 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003687{
Steven Rostedtf6180772009-02-14 00:40:25 -05003688 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003689 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003690 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003691
3692 func = strsep(&next, ":");
3693
3694 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003695 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003696 if (!ret)
3697 ret = -EINVAL;
3698 if (ret < 0)
3699 return ret;
3700 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003701 }
3702
Steven Rostedtf6180772009-02-14 00:40:25 -05003703 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003704
3705 command = strsep(&next, ":");
3706
Steven Rostedtf6180772009-02-14 00:40:25 -05003707 mutex_lock(&ftrace_cmd_mutex);
3708 list_for_each_entry(p, &ftrace_commands, list) {
3709 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003710 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003711 goto out_unlock;
3712 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003713 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003714 out_unlock:
3715 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003716
Steven Rostedtf6180772009-02-14 00:40:25 -05003717 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003718}
3719
Ingo Molnare309b412008-05-12 21:20:51 +02003720static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003721ftrace_regex_write(struct file *file, const char __user *ubuf,
3722 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003723{
3724 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003725 struct trace_parser *parser;
3726 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003727
Li Zefan4ba79782009-09-22 13:52:20 +08003728 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003729 return 0;
3730
Steven Rostedt5072c592008-05-12 21:20:43 +02003731 if (file->f_mode & FMODE_READ) {
3732 struct seq_file *m = file->private_data;
3733 iter = m->private;
3734 } else
3735 iter = file->private_data;
3736
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003737 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003738 return -ENODEV;
3739
3740 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003741
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003742 parser = &iter->parser;
3743 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003744
Li Zefan4ba79782009-09-22 13:52:20 +08003745 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003746 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003747 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003748 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003749 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003750 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003751 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003752 }
3753
Steven Rostedt5072c592008-05-12 21:20:43 +02003754 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003755 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003756 return ret;
3757}
3758
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003759ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003760ftrace_filter_write(struct file *file, const char __user *ubuf,
3761 size_t cnt, loff_t *ppos)
3762{
3763 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3764}
3765
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003766ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003767ftrace_notrace_write(struct file *file, const char __user *ubuf,
3768 size_t cnt, loff_t *ppos)
3769{
3770 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3771}
3772
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003773static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003774ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3775{
3776 struct ftrace_func_entry *entry;
3777
3778 if (!ftrace_location(ip))
3779 return -EINVAL;
3780
3781 if (remove) {
3782 entry = ftrace_lookup_ip(hash, ip);
3783 if (!entry)
3784 return -ENOENT;
3785 free_hash_entry(hash, entry);
3786 return 0;
3787 }
3788
3789 return add_hash_entry(hash, ip);
3790}
3791
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003792static void ftrace_ops_update_code(struct ftrace_ops *ops)
3793{
3794 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3795 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3796}
3797
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003798static int
3799ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3800 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003801{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003802 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003803 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003804 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003805 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003806
Steven Rostedt41c52c02008-05-22 11:46:33 -04003807 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003808 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003809
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003810 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003811
Steven Rostedtf45948e2011-05-02 12:29:25 -04003812 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003813 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003814 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003815 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003816
Wang Nanb972cc52014-07-15 08:40:20 +08003817 if (reset)
3818 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3819 else
3820 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3821
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003822 if (!hash) {
3823 ret = -ENOMEM;
3824 goto out_regex_unlock;
3825 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003826
Jiri Olsaac483c42012-01-02 10:04:14 +01003827 if (buf && !ftrace_match_records(hash, buf, len)) {
3828 ret = -EINVAL;
3829 goto out_regex_unlock;
3830 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003831 if (ip) {
3832 ret = ftrace_match_addr(hash, ip, remove);
3833 if (ret < 0)
3834 goto out_regex_unlock;
3835 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003836
3837 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003838 old_hash = *orig_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003839 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003840 if (!ret) {
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003841 ftrace_ops_update_code(ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003842 free_ftrace_hash_rcu(old_hash);
3843 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003844 mutex_unlock(&ftrace_lock);
3845
Jiri Olsaac483c42012-01-02 10:04:14 +01003846 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003847 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003848
3849 free_ftrace_hash(hash);
3850 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003851}
3852
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003853static int
3854ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3855 int reset, int enable)
3856{
3857 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3858}
3859
3860/**
3861 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3862 * @ops - the ops to set the filter with
3863 * @ip - the address to add to or remove from the filter.
3864 * @remove - non zero to remove the ip from the filter
3865 * @reset - non zero to reset all filters before applying this filter.
3866 *
3867 * Filters denote which functions should be enabled when tracing is enabled
3868 * If @ip is NULL, it failes to update filter.
3869 */
3870int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3871 int remove, int reset)
3872{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003873 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003874 return ftrace_set_addr(ops, ip, remove, reset, 1);
3875}
3876EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3877
3878static int
3879ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3880 int reset, int enable)
3881{
3882 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3883}
3884
Steven Rostedt77a2b372008-05-12 21:20:45 +02003885/**
3886 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003887 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003888 * @buf - the string that holds the function filter text.
3889 * @len - the length of the string.
3890 * @reset - non zero to reset all filters before applying this filter.
3891 *
3892 * Filters denote which functions should be enabled when tracing is enabled.
3893 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3894 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003895int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003896 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003897{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003898 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003899 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003900}
Steven Rostedt936e0742011-05-05 22:54:01 -04003901EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003902
Steven Rostedt41c52c02008-05-22 11:46:33 -04003903/**
3904 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003905 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003906 * @buf - the string that holds the function notrace text.
3907 * @len - the length of the string.
3908 * @reset - non zero to reset all filters before applying this filter.
3909 *
3910 * Notrace Filters denote which functions should not be enabled when tracing
3911 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3912 * for tracing.
3913 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003914int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003915 int len, int reset)
3916{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003917 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003918 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003919}
3920EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3921/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003922 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003923 * @buf - the string that holds the function filter text.
3924 * @len - the length of the string.
3925 * @reset - non zero to reset all filters before applying this filter.
3926 *
3927 * Filters denote which functions should be enabled when tracing is enabled.
3928 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3929 */
3930void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3931{
3932 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3933}
3934EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3935
3936/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003937 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003938 * @buf - the string that holds the function notrace text.
3939 * @len - the length of the string.
3940 * @reset - non zero to reset all filters before applying this filter.
3941 *
3942 * Notrace Filters denote which functions should not be enabled when tracing
3943 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3944 * for tracing.
3945 */
3946void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003947{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003948 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003949}
Steven Rostedt936e0742011-05-05 22:54:01 -04003950EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003951
Steven Rostedt2af15d62009-05-28 13:37:24 -04003952/*
3953 * command line interface to allow users to set filters on boot up.
3954 */
3955#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3956static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3957static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3958
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003959/* Used by function selftest to not test if filter is set */
3960bool ftrace_filter_param __initdata;
3961
Steven Rostedt2af15d62009-05-28 13:37:24 -04003962static int __init set_ftrace_notrace(char *str)
3963{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003964 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003965 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003966 return 1;
3967}
3968__setup("ftrace_notrace=", set_ftrace_notrace);
3969
3970static int __init set_ftrace_filter(char *str)
3971{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003972 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003973 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003974 return 1;
3975}
3976__setup("ftrace_filter=", set_ftrace_filter);
3977
Stefan Assmann369bc182009-10-12 22:17:21 +02003978#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003979static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003980static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003981static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003982
Stefan Assmann369bc182009-10-12 22:17:21 +02003983static int __init set_graph_function(char *str)
3984{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003985 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003986 return 1;
3987}
3988__setup("ftrace_graph_filter=", set_graph_function);
3989
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003990static int __init set_graph_notrace_function(char *str)
3991{
3992 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
3993 return 1;
3994}
3995__setup("ftrace_graph_notrace=", set_graph_notrace_function);
3996
3997static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02003998{
3999 int ret;
4000 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004001 unsigned long *table = ftrace_graph_funcs;
4002 int *count = &ftrace_graph_count;
4003
4004 if (!enable) {
4005 table = ftrace_graph_notrace_funcs;
4006 count = &ftrace_graph_notrace_count;
4007 }
Stefan Assmann369bc182009-10-12 22:17:21 +02004008
4009 while (buf) {
4010 func = strsep(&buf, ",");
4011 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004012 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004013 if (ret)
4014 printk(KERN_DEBUG "ftrace: function %s not "
4015 "traceable\n", func);
4016 }
4017}
4018#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4019
Steven Rostedt2a85a372011-12-19 21:57:44 -05004020void __init
4021ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004022{
4023 char *func;
4024
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004025 ftrace_ops_init(ops);
4026
Steven Rostedt2af15d62009-05-28 13:37:24 -04004027 while (buf) {
4028 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004029 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004030 }
4031}
4032
4033static void __init set_ftrace_early_filters(void)
4034{
4035 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004036 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004037 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004038 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004039#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4040 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004041 set_ftrace_early_graph(ftrace_graph_buf, 1);
4042 if (ftrace_graph_notrace_buf[0])
4043 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004044#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004045}
4046
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004047int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004048{
4049 struct seq_file *m = (struct seq_file *)file->private_data;
4050 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004051 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004052 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004053 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004054 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004055 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004056
Steven Rostedt5072c592008-05-12 21:20:43 +02004057 if (file->f_mode & FMODE_READ) {
4058 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004059 seq_release(inode, file);
4060 } else
4061 iter = file->private_data;
4062
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004063 parser = &iter->parser;
4064 if (trace_parser_loaded(parser)) {
4065 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004066 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004067 }
4068
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004069 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004070
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004071 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004072
Steven Rostedt058e2972011-04-29 22:35:33 -04004073 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004074 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4075
4076 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004077 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004078 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004079 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004080
Steven Rostedt058e2972011-04-29 22:35:33 -04004081 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004082 old_hash = *orig_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004083 ret = ftrace_hash_move(iter->ops, filter_hash,
4084 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004085 if (!ret) {
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004086 ftrace_ops_update_code(iter->ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004087 free_ftrace_hash_rcu(old_hash);
4088 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004089 mutex_unlock(&ftrace_lock);
4090 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004091
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004092 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004093 free_ftrace_hash(iter->hash);
4094 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004095
Steven Rostedt5072c592008-05-12 21:20:43 +02004096 return 0;
4097}
4098
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004099static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004100 .open = ftrace_avail_open,
4101 .read = seq_read,
4102 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004103 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004104};
4105
Steven Rostedt647bcd02011-05-03 14:39:21 -04004106static const struct file_operations ftrace_enabled_fops = {
4107 .open = ftrace_enabled_open,
4108 .read = seq_read,
4109 .llseek = seq_lseek,
4110 .release = seq_release_private,
4111};
4112
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004113static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004114 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004115 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004116 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004117 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004118 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004119};
4120
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004121static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004122 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004123 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004124 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004125 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004126 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004127};
4128
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004129#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4130
4131static DEFINE_MUTEX(graph_lock);
4132
4133int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004134int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004135unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004136unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004137
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004138struct ftrace_graph_data {
4139 unsigned long *table;
4140 size_t size;
4141 int *count;
4142 const struct seq_operations *seq_ops;
4143};
4144
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004145static void *
Li Zefan85951842009-06-24 09:54:00 +08004146__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004147{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004148 struct ftrace_graph_data *fgd = m->private;
4149
4150 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004151 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004152 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004153}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004154
Li Zefan85951842009-06-24 09:54:00 +08004155static void *
4156g_next(struct seq_file *m, void *v, loff_t *pos)
4157{
4158 (*pos)++;
4159 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004160}
4161
4162static void *g_start(struct seq_file *m, loff_t *pos)
4163{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004164 struct ftrace_graph_data *fgd = m->private;
4165
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004166 mutex_lock(&graph_lock);
4167
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004168 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004169 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004170 return (void *)1;
4171
Li Zefan85951842009-06-24 09:54:00 +08004172 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004173}
4174
4175static void g_stop(struct seq_file *m, void *p)
4176{
4177 mutex_unlock(&graph_lock);
4178}
4179
4180static int g_show(struct seq_file *m, void *v)
4181{
4182 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004183
4184 if (!ptr)
4185 return 0;
4186
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004187 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004188 struct ftrace_graph_data *fgd = m->private;
4189
4190 if (fgd->table == ftrace_graph_funcs)
4191 seq_printf(m, "#### all functions enabled ####\n");
4192 else
4193 seq_printf(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004194 return 0;
4195 }
4196
Steven Rostedtb375a112009-09-17 00:05:58 -04004197 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004198
4199 return 0;
4200}
4201
James Morris88e9d342009-09-22 16:43:43 -07004202static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004203 .start = g_start,
4204 .next = g_next,
4205 .stop = g_stop,
4206 .show = g_show,
4207};
4208
4209static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004210__ftrace_graph_open(struct inode *inode, struct file *file,
4211 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004212{
4213 int ret = 0;
4214
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004215 mutex_lock(&graph_lock);
4216 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004217 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004218 *fgd->count = 0;
4219 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004220 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004221 mutex_unlock(&graph_lock);
4222
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004223 if (file->f_mode & FMODE_READ) {
4224 ret = seq_open(file, fgd->seq_ops);
4225 if (!ret) {
4226 struct seq_file *m = file->private_data;
4227 m->private = fgd;
4228 }
4229 } else
4230 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004231
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004232 return ret;
4233}
4234
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004235static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004236ftrace_graph_open(struct inode *inode, struct file *file)
4237{
4238 struct ftrace_graph_data *fgd;
4239
4240 if (unlikely(ftrace_disabled))
4241 return -ENODEV;
4242
4243 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4244 if (fgd == NULL)
4245 return -ENOMEM;
4246
4247 fgd->table = ftrace_graph_funcs;
4248 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4249 fgd->count = &ftrace_graph_count;
4250 fgd->seq_ops = &ftrace_graph_seq_ops;
4251
4252 return __ftrace_graph_open(inode, file, fgd);
4253}
4254
4255static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004256ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4257{
4258 struct ftrace_graph_data *fgd;
4259
4260 if (unlikely(ftrace_disabled))
4261 return -ENODEV;
4262
4263 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4264 if (fgd == NULL)
4265 return -ENOMEM;
4266
4267 fgd->table = ftrace_graph_notrace_funcs;
4268 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4269 fgd->count = &ftrace_graph_notrace_count;
4270 fgd->seq_ops = &ftrace_graph_seq_ops;
4271
4272 return __ftrace_graph_open(inode, file, fgd);
4273}
4274
4275static int
Li Zefan87827112009-07-23 11:29:11 +08004276ftrace_graph_release(struct inode *inode, struct file *file)
4277{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004278 if (file->f_mode & FMODE_READ) {
4279 struct seq_file *m = file->private_data;
4280
4281 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004282 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004283 } else {
4284 kfree(file->private_data);
4285 }
4286
Li Zefan87827112009-07-23 11:29:11 +08004287 return 0;
4288}
4289
4290static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004291ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004292{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004293 struct dyn_ftrace *rec;
4294 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004295 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004296 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004297 int type, not;
4298 char *search;
4299 bool exists;
4300 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004301
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004302 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004303 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004304 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004305 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004306
4307 search_len = strlen(search);
4308
Steven Rostedt52baf112009-02-14 01:15:39 -05004309 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004310
4311 if (unlikely(ftrace_disabled)) {
4312 mutex_unlock(&ftrace_lock);
4313 return -ENODEV;
4314 }
4315
Steven Rostedt265c8312009-02-13 12:43:56 -05004316 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004317
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004318 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004319 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004320 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004321 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004322 if (array[i] == rec->ip) {
4323 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004324 break;
4325 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004326 }
4327
4328 if (!not) {
4329 fail = 0;
4330 if (!exists) {
4331 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004332 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004333 goto out;
4334 }
4335 } else {
4336 if (exists) {
4337 array[i] = array[--(*idx)];
4338 array[*idx] = 0;
4339 fail = 0;
4340 }
4341 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004342 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004343 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004344out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004345 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004346
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004347 if (fail)
4348 return -EINVAL;
4349
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004350 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004351}
4352
4353static ssize_t
4354ftrace_graph_write(struct file *file, const char __user *ubuf,
4355 size_t cnt, loff_t *ppos)
4356{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004357 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004358 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004359 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004360
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004361 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004362 return 0;
4363
Namhyung Kim6a101082013-10-14 17:24:25 +09004364 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4365 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004366
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004367 read = trace_get_user(&parser, ubuf, cnt, ppos);
4368
Li Zefan4ba79782009-09-22 13:52:20 +08004369 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004370 parser.buffer[parser.idx] = 0;
4371
Namhyung Kim6a101082013-10-14 17:24:25 +09004372 mutex_lock(&graph_lock);
4373
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004374 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004375 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4376 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004377
4378 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004379 }
4380
Namhyung Kim6a101082013-10-14 17:24:25 +09004381 if (!ret)
4382 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004383
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004384 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004385
4386 return ret;
4387}
4388
4389static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004390 .open = ftrace_graph_open,
4391 .read = seq_read,
4392 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004393 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004394 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004395};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004396
4397static const struct file_operations ftrace_graph_notrace_fops = {
4398 .open = ftrace_graph_notrace_open,
4399 .read = seq_read,
4400 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004401 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004402 .release = ftrace_graph_release,
4403};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004404#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4405
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004406void ftrace_create_filter_files(struct ftrace_ops *ops,
4407 struct dentry *parent)
4408{
4409
4410 trace_create_file("set_ftrace_filter", 0644, parent,
4411 ops, &ftrace_filter_fops);
4412
4413 trace_create_file("set_ftrace_notrace", 0644, parent,
4414 ops, &ftrace_notrace_fops);
4415}
4416
4417/*
4418 * The name "destroy_filter_files" is really a misnomer. Although
4419 * in the future, it may actualy delete the files, but this is
4420 * really intended to make sure the ops passed in are disabled
4421 * and that when this function returns, the caller is free to
4422 * free the ops.
4423 *
4424 * The "destroy" name is only to match the "create" name that this
4425 * should be paired with.
4426 */
4427void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4428{
4429 mutex_lock(&ftrace_lock);
4430 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4431 ftrace_shutdown(ops, 0);
4432 ops->flags |= FTRACE_OPS_FL_DELETED;
4433 mutex_unlock(&ftrace_lock);
4434}
4435
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004436static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004437{
Steven Rostedt5072c592008-05-12 21:20:43 +02004438
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004439 trace_create_file("available_filter_functions", 0444,
4440 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004441
Steven Rostedt647bcd02011-05-03 14:39:21 -04004442 trace_create_file("enabled_functions", 0444,
4443 d_tracer, NULL, &ftrace_enabled_fops);
4444
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004445 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004446
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004447#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004448 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004449 NULL,
4450 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004451 trace_create_file("set_graph_notrace", 0444, d_tracer,
4452 NULL,
4453 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004454#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4455
Steven Rostedt5072c592008-05-12 21:20:43 +02004456 return 0;
4457}
4458
Steven Rostedt9fd49322012-04-24 22:32:06 -04004459static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004460{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004461 const unsigned long *ipa = a;
4462 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004463
Steven Rostedt9fd49322012-04-24 22:32:06 -04004464 if (*ipa > *ipb)
4465 return 1;
4466 if (*ipa < *ipb)
4467 return -1;
4468 return 0;
4469}
4470
4471static void ftrace_swap_ips(void *a, void *b, int size)
4472{
4473 unsigned long *ipa = a;
4474 unsigned long *ipb = b;
4475 unsigned long t;
4476
4477 t = *ipa;
4478 *ipa = *ipb;
4479 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004480}
4481
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004482static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004483 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004484 unsigned long *end)
4485{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004486 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004487 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004488 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004489 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004490 unsigned long *p;
4491 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004492 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004493 int ret = -ENOMEM;
4494
4495 count = end - start;
4496
4497 if (!count)
4498 return 0;
4499
Steven Rostedt9fd49322012-04-24 22:32:06 -04004500 sort(start, count, sizeof(*start),
4501 ftrace_cmp_ips, ftrace_swap_ips);
4502
Steven Rostedt706c81f2012-04-24 23:45:26 -04004503 start_pg = ftrace_allocate_pages(count);
4504 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004505 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004506
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004507 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004508
Steven Rostedt320823092011-12-16 14:42:37 -05004509 /*
4510 * Core and each module needs their own pages, as
4511 * modules will free them when they are removed.
4512 * Force a new page to be allocated for modules.
4513 */
Steven Rostedta7900872011-12-16 16:23:44 -05004514 if (!mod) {
4515 WARN_ON(ftrace_pages || ftrace_pages_start);
4516 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004517 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004518 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05004519 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004520 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05004521
Steven Rostedta7900872011-12-16 16:23:44 -05004522 if (WARN_ON(ftrace_pages->next)) {
4523 /* Hmm, we have free pages? */
4524 while (ftrace_pages->next)
4525 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05004526 }
Steven Rostedta7900872011-12-16 16:23:44 -05004527
Steven Rostedt706c81f2012-04-24 23:45:26 -04004528 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05004529 }
4530
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004531 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004532 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004533 while (p < end) {
4534 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004535 /*
4536 * Some architecture linkers will pad between
4537 * the different mcount_loc sections of different
4538 * object files to satisfy alignments.
4539 * Skip any NULL pointers.
4540 */
4541 if (!addr)
4542 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004543
4544 if (pg->index == pg->size) {
4545 /* We should have allocated enough */
4546 if (WARN_ON(!pg->next))
4547 break;
4548 pg = pg->next;
4549 }
4550
4551 rec = &pg->records[pg->index++];
4552 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004553 }
4554
Steven Rostedt706c81f2012-04-24 23:45:26 -04004555 /* We should have used all pages */
4556 WARN_ON(pg->next);
4557
4558 /* Assign the last page to ftrace_pages */
4559 ftrace_pages = pg;
4560
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004561 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004562 * We only need to disable interrupts on start up
4563 * because we are modifying code that an interrupt
4564 * may execute, and the modification is not atomic.
4565 * But for modules, nothing runs the code we modify
4566 * until we are finished with it, and there's no
4567 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004568 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004569 if (!mod)
4570 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004571 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004572 if (!mod)
4573 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004574 ret = 0;
4575 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004576 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004577
Steven Rostedta7900872011-12-16 16:23:44 -05004578 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004579}
4580
Steven Rostedt93eb6772009-04-15 13:24:06 -04004581#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05004582
4583#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4584
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004585void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004586{
4587 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05004588 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004589 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004590 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004591
Steven Rostedt93eb6772009-04-15 13:24:06 -04004592 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004593
4594 if (ftrace_disabled)
4595 goto out_unlock;
4596
Steven Rostedt320823092011-12-16 14:42:37 -05004597 /*
4598 * Each module has its own ftrace_pages, remove
4599 * them from the list.
4600 */
4601 last_pg = &ftrace_pages_start;
4602 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4603 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004604 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004605 /*
Steven Rostedt320823092011-12-16 14:42:37 -05004606 * As core pages are first, the first
4607 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004608 */
Steven Rostedt320823092011-12-16 14:42:37 -05004609 if (WARN_ON(pg == ftrace_pages_start))
4610 goto out_unlock;
4611
4612 /* Check if we are deleting the last page */
4613 if (pg == ftrace_pages)
4614 ftrace_pages = next_to_ftrace_page(last_pg);
4615
4616 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004617 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4618 free_pages((unsigned long)pg->records, order);
4619 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05004620 } else
4621 last_pg = &pg->next;
4622 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004623 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004624 mutex_unlock(&ftrace_lock);
4625}
4626
4627static void ftrace_init_module(struct module *mod,
4628 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004629{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004630 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004631 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004632 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004633}
4634
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004635void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004636{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004637 ftrace_init_module(mod, mod->ftrace_callsites,
4638 mod->ftrace_callsites +
4639 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004640}
4641
4642static int ftrace_module_notify_exit(struct notifier_block *self,
4643 unsigned long val, void *data)
4644{
4645 struct module *mod = data;
4646
4647 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004648 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004649
4650 return 0;
4651}
4652#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004653static int ftrace_module_notify_exit(struct notifier_block *self,
4654 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004655{
4656 return 0;
4657}
4658#endif /* CONFIG_MODULES */
4659
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004660struct notifier_block ftrace_module_exit_nb = {
4661 .notifier_call = ftrace_module_notify_exit,
4662 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4663};
4664
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004665void __init ftrace_init(void)
4666{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004667 extern unsigned long __start_mcount_loc[];
4668 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004669 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004670 int ret;
4671
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004672 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004673 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004674 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004675 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004676 goto failed;
4677
4678 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004679 if (!count) {
4680 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004681 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004682 }
4683
4684 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4685 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004686
4687 last_ftrace_enabled = ftrace_enabled = 1;
4688
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004689 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004690 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004691 __stop_mcount_loc);
4692
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004693 ret = register_module_notifier(&ftrace_module_exit_nb);
4694 if (ret)
4695 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004696
Steven Rostedt2af15d62009-05-28 13:37:24 -04004697 set_ftrace_early_filters();
4698
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004699 return;
4700 failed:
4701 ftrace_disabled = 1;
4702}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004703
Steven Rostedt3d083392008-05-12 21:20:42 +02004704#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004705
Steven Rostedt2b499382011-05-03 22:49:52 -04004706static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004707 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004708 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004709};
4710
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004711static int __init ftrace_nodyn_init(void)
4712{
4713 ftrace_enabled = 1;
4714 return 0;
4715}
Steven Rostedt6f415672012-10-05 12:13:07 -04004716core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004717
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004718static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4719static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004720/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004721# define ftrace_startup(ops, command) \
4722 ({ \
4723 int ___ret = __register_ftrace_function(ops); \
4724 if (!___ret) \
4725 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4726 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004727 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004728# define ftrace_shutdown(ops, command) \
4729 ({ \
4730 int ___ret = __unregister_ftrace_function(ops); \
4731 if (!___ret) \
4732 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4733 ___ret; \
4734 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004735
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004736# define ftrace_startup_sysctl() do { } while (0)
4737# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004738
4739static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004740ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004741{
4742 return 1;
4743}
4744
Steven Rostedt3d083392008-05-12 21:20:42 +02004745#endif /* CONFIG_DYNAMIC_FTRACE */
4746
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004747__init void ftrace_init_global_array_ops(struct trace_array *tr)
4748{
4749 tr->ops = &global_ops;
4750 tr->ops->private = tr;
4751}
4752
4753void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4754{
4755 /* If we filter on pids, update to use the pid function */
4756 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4757 if (WARN_ON(tr->ops->func != ftrace_stub))
4758 printk("ftrace ops had %pS for function\n",
4759 tr->ops->func);
4760 /* Only the top level instance does pid tracing */
4761 if (!list_empty(&ftrace_pids)) {
4762 set_ftrace_pid_function(func);
4763 func = ftrace_pid_func;
4764 }
4765 }
4766 tr->ops->func = func;
4767 tr->ops->private = tr;
4768}
4769
4770void ftrace_reset_array_ops(struct trace_array *tr)
4771{
4772 tr->ops->func = ftrace_stub;
4773}
4774
Steven Rostedtb8489142011-05-04 09:27:52 -04004775static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004776ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004777 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004778{
Jiri Olsae2484912012-02-15 15:51:48 +01004779 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4780 return;
4781
4782 /*
4783 * Some of the ops may be dynamically allocated,
4784 * they must be freed after a synchronize_sched().
4785 */
4786 preempt_disable_notrace();
4787 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004788
4789 /*
4790 * Control funcs (perf) uses RCU. Only trace if
4791 * RCU is currently active.
4792 */
4793 if (!rcu_is_watching())
4794 goto out;
4795
Steven Rostedt0a016402012-11-02 17:03:03 -04004796 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004797 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4798 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004799 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004800 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004801 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004802 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004803 trace_recursion_clear(TRACE_CONTROL_BIT);
4804 preempt_enable_notrace();
4805}
4806
4807static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004808 .func = ftrace_ops_control_func,
4809 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004810 INIT_OPS_HASH(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004811};
4812
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004813static inline void
4814__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004815 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004816{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004817 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004818 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004819
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004820 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4821 if (bit < 0)
4822 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004823
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004824 /*
4825 * Some of the ops may be dynamically allocated,
4826 * they must be freed after a synchronize_sched().
4827 */
4828 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004829 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004830 if (ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04004831 if (FTRACE_WARN_ON(!op->func)) {
4832 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004833 goto out;
4834 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004835 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004836 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004837 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004838out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004839 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004840 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004841}
4842
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004843/*
4844 * Some archs only support passing ip and parent_ip. Even though
4845 * the list function ignores the op parameter, we do not want any
4846 * C side effects, where a function is called without the caller
4847 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004848 * Archs are to support both the regs and ftrace_ops at the same time.
4849 * If they support ftrace_ops, it is assumed they support regs.
4850 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004851 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4852 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004853 * An architecture can pass partial regs with ftrace_ops and still
4854 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004855 */
4856#if ARCH_SUPPORTS_FTRACE_OPS
4857static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004858 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004859{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004860 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004861}
4862#else
4863static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4864{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004865 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004866}
4867#endif
4868
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04004869/*
4870 * If there's only one function registered but it does not support
4871 * recursion, this function will be called by the mcount trampoline.
4872 * This function will handle recursion protection.
4873 */
4874static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
4875 struct ftrace_ops *op, struct pt_regs *regs)
4876{
4877 int bit;
4878
4879 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4880 if (bit < 0)
4881 return;
4882
4883 op->func(ip, parent_ip, op, regs);
4884
4885 trace_clear_recursion(bit);
4886}
4887
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04004888/**
4889 * ftrace_ops_get_func - get the function a trampoline should call
4890 * @ops: the ops to get the function for
4891 *
4892 * Normally the mcount trampoline will call the ops->func, but there
4893 * are times that it should not. For example, if the ops does not
4894 * have its own recursion protection, then it should call the
4895 * ftrace_ops_recurs_func() instead.
4896 *
4897 * Returns the function that the trampoline should call for @ops.
4898 */
4899ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
4900{
4901 /*
4902 * If this is a dynamic ops or we force list func,
4903 * then it needs to call the list anyway.
4904 */
4905 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
4906 return ftrace_ops_list_func;
4907
4908 /*
4909 * If the func handles its own recursion, call it directly.
4910 * Otherwise call the recursion protected function that
4911 * will call the ftrace ops function.
4912 */
4913 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
4914 return ftrace_ops_recurs_func;
4915
4916 return ops->func;
4917}
4918
Steven Rostedte32d8952008-12-04 00:26:41 -05004919static void clear_ftrace_swapper(void)
4920{
4921 struct task_struct *p;
4922 int cpu;
4923
4924 get_online_cpus();
4925 for_each_online_cpu(cpu) {
4926 p = idle_task(cpu);
4927 clear_tsk_trace_trace(p);
4928 }
4929 put_online_cpus();
4930}
4931
4932static void set_ftrace_swapper(void)
4933{
4934 struct task_struct *p;
4935 int cpu;
4936
4937 get_online_cpus();
4938 for_each_online_cpu(cpu) {
4939 p = idle_task(cpu);
4940 set_tsk_trace_trace(p);
4941 }
4942 put_online_cpus();
4943}
4944
4945static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004946{
4947 struct task_struct *p;
4948
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004949 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004950 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004951 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004952 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004953 rcu_read_unlock();
4954
Steven Rostedte32d8952008-12-04 00:26:41 -05004955 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004956}
4957
Steven Rostedte32d8952008-12-04 00:26:41 -05004958static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004959{
4960 struct task_struct *p;
4961
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004962 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004963 do_each_pid_task(pid, PIDTYPE_PID, p) {
4964 set_tsk_trace_trace(p);
4965 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004966 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004967}
4968
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004969static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004970{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004971 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004972 clear_ftrace_swapper();
4973 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004974 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004975}
4976
4977static void set_ftrace_pid_task(struct pid *pid)
4978{
4979 if (pid == ftrace_swapper_pid)
4980 set_ftrace_swapper();
4981 else
4982 set_ftrace_pid(pid);
4983}
4984
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004985static int ftrace_pid_add(int p)
4986{
4987 struct pid *pid;
4988 struct ftrace_pid *fpid;
4989 int ret = -EINVAL;
4990
4991 mutex_lock(&ftrace_lock);
4992
4993 if (!p)
4994 pid = ftrace_swapper_pid;
4995 else
4996 pid = find_get_pid(p);
4997
4998 if (!pid)
4999 goto out;
5000
5001 ret = 0;
5002
5003 list_for_each_entry(fpid, &ftrace_pids, list)
5004 if (fpid->pid == pid)
5005 goto out_put;
5006
5007 ret = -ENOMEM;
5008
5009 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
5010 if (!fpid)
5011 goto out_put;
5012
5013 list_add(&fpid->list, &ftrace_pids);
5014 fpid->pid = pid;
5015
5016 set_ftrace_pid_task(pid);
5017
5018 ftrace_update_pid_func();
5019 ftrace_startup_enable(0);
5020
5021 mutex_unlock(&ftrace_lock);
5022 return 0;
5023
5024out_put:
5025 if (pid != ftrace_swapper_pid)
5026 put_pid(pid);
5027
5028out:
5029 mutex_unlock(&ftrace_lock);
5030 return ret;
5031}
5032
5033static void ftrace_pid_reset(void)
5034{
5035 struct ftrace_pid *fpid, *safe;
5036
5037 mutex_lock(&ftrace_lock);
5038 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
5039 struct pid *pid = fpid->pid;
5040
5041 clear_ftrace_pid_task(pid);
5042
5043 list_del(&fpid->list);
5044 kfree(fpid);
5045 }
5046
5047 ftrace_update_pid_func();
5048 ftrace_startup_enable(0);
5049
5050 mutex_unlock(&ftrace_lock);
5051}
5052
5053static void *fpid_start(struct seq_file *m, loff_t *pos)
5054{
5055 mutex_lock(&ftrace_lock);
5056
5057 if (list_empty(&ftrace_pids) && (!*pos))
5058 return (void *) 1;
5059
5060 return seq_list_start(&ftrace_pids, *pos);
5061}
5062
5063static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5064{
5065 if (v == (void *)1)
5066 return NULL;
5067
5068 return seq_list_next(v, &ftrace_pids, pos);
5069}
5070
5071static void fpid_stop(struct seq_file *m, void *p)
5072{
5073 mutex_unlock(&ftrace_lock);
5074}
5075
5076static int fpid_show(struct seq_file *m, void *v)
5077{
5078 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
5079
5080 if (v == (void *)1) {
5081 seq_printf(m, "no pid\n");
5082 return 0;
5083 }
5084
5085 if (fpid->pid == ftrace_swapper_pid)
5086 seq_printf(m, "swapper tasks\n");
5087 else
5088 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5089
5090 return 0;
5091}
5092
5093static const struct seq_operations ftrace_pid_sops = {
5094 .start = fpid_start,
5095 .next = fpid_next,
5096 .stop = fpid_stop,
5097 .show = fpid_show,
5098};
5099
5100static int
5101ftrace_pid_open(struct inode *inode, struct file *file)
5102{
5103 int ret = 0;
5104
5105 if ((file->f_mode & FMODE_WRITE) &&
5106 (file->f_flags & O_TRUNC))
5107 ftrace_pid_reset();
5108
5109 if (file->f_mode & FMODE_READ)
5110 ret = seq_open(file, &ftrace_pid_sops);
5111
5112 return ret;
5113}
5114
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005115static ssize_t
5116ftrace_pid_write(struct file *filp, const char __user *ubuf,
5117 size_t cnt, loff_t *ppos)
5118{
Ingo Molnar457dc922009-11-23 11:03:28 +01005119 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005120 long val;
5121 int ret;
5122
5123 if (cnt >= sizeof(buf))
5124 return -EINVAL;
5125
5126 if (copy_from_user(&buf, ubuf, cnt))
5127 return -EFAULT;
5128
5129 buf[cnt] = 0;
5130
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005131 /*
5132 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5133 * to clean the filter quietly.
5134 */
Ingo Molnar457dc922009-11-23 11:03:28 +01005135 tmp = strstrip(buf);
5136 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005137 return 1;
5138
Daniel Walterbcd83ea2012-09-26 22:08:38 +02005139 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005140 if (ret < 0)
5141 return ret;
5142
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005143 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005144
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005145 return ret ? ret : cnt;
5146}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005147
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005148static int
5149ftrace_pid_release(struct inode *inode, struct file *file)
5150{
5151 if (file->f_mode & FMODE_READ)
5152 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005153
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005154 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005155}
5156
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005157static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005158 .open = ftrace_pid_open,
5159 .write = ftrace_pid_write,
5160 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005161 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005162 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005163};
5164
5165static __init int ftrace_init_debugfs(void)
5166{
5167 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005168
5169 d_tracer = tracing_init_dentry();
5170 if (!d_tracer)
5171 return 0;
5172
5173 ftrace_init_dyn_debugfs(d_tracer);
5174
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005175 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5176 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005177
5178 ftrace_profile_debugfs(d_tracer);
5179
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005180 return 0;
5181}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005182fs_initcall(ftrace_init_debugfs);
5183
Steven Rostedt3d083392008-05-12 21:20:42 +02005184/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005185 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005186 *
5187 * This function should be used by panic code. It stops ftrace
5188 * but in a not so nice way. If you need to simply kill ftrace
5189 * from a non-atomic section, use ftrace_kill.
5190 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005191void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005192{
5193 ftrace_disabled = 1;
5194 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005195 clear_ftrace_function();
5196}
5197
5198/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005199 * Test if ftrace is dead or not.
5200 */
5201int ftrace_is_dead(void)
5202{
5203 return ftrace_disabled;
5204}
5205
5206/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005207 * register_ftrace_function - register a function for profiling
5208 * @ops - ops structure that holds the function for profiling.
5209 *
5210 * Register a function to be called by all functions in the
5211 * kernel.
5212 *
5213 * Note: @ops->func and all the functions it calls must be labeled
5214 * with "notrace", otherwise it will go into a
5215 * recursive loop.
5216 */
5217int register_ftrace_function(struct ftrace_ops *ops)
5218{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005219 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005220
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005221 ftrace_ops_init(ops);
5222
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005223 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005224
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005225 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005226
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005227 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005228
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005229 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005230}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005231EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005232
5233/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005234 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005235 * @ops - ops structure that holds the function to unregister
5236 *
5237 * Unregister a function that was added to be called by ftrace profiling.
5238 */
5239int unregister_ftrace_function(struct ftrace_ops *ops)
5240{
5241 int ret;
5242
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005243 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005244 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005245 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005246
5247 return ret;
5248}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005249EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005250
Ingo Molnare309b412008-05-12 21:20:51 +02005251int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005252ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005253 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005254 loff_t *ppos)
5255{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005256 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005257
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005258 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005259
Steven Rostedt45a4a232011-04-21 23:16:46 -04005260 if (unlikely(ftrace_disabled))
5261 goto out;
5262
5263 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005264
Li Zefana32c7762009-06-26 16:55:51 +08005265 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005266 goto out;
5267
Li Zefana32c7762009-06-26 16:55:51 +08005268 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005269
5270 if (ftrace_enabled) {
5271
5272 ftrace_startup_sysctl();
5273
5274 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005275 if (ftrace_ops_list != &ftrace_list_end)
5276 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005277
5278 } else {
5279 /* stopping ftrace calls (just send to ftrace_stub) */
5280 ftrace_trace_function = ftrace_stub;
5281
5282 ftrace_shutdown_sysctl();
5283 }
5284
5285 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005286 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005287 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005288}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005289
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005290#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005291
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005292static struct ftrace_ops graph_ops = {
5293 .func = ftrace_stub,
5294 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5295 FTRACE_OPS_FL_INITIALIZED |
5296 FTRACE_OPS_FL_STUB,
5297#ifdef FTRACE_GRAPH_TRAMP_ADDR
5298 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5299#endif
5300 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5301};
5302
Steven Rostedt597af812009-04-03 15:24:12 -04005303static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005304
Steven Rostedte49dc192008-12-02 23:50:05 -05005305int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5306{
5307 return 0;
5308}
5309
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005310/* The callbacks that hook a function */
5311trace_func_graph_ret_t ftrace_graph_return =
5312 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005313trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005314static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005315
5316/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5317static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5318{
5319 int i;
5320 int ret = 0;
5321 unsigned long flags;
5322 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5323 struct task_struct *g, *t;
5324
5325 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5326 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5327 * sizeof(struct ftrace_ret_stack),
5328 GFP_KERNEL);
5329 if (!ret_stack_list[i]) {
5330 start = 0;
5331 end = i;
5332 ret = -ENOMEM;
5333 goto free;
5334 }
5335 }
5336
5337 read_lock_irqsave(&tasklist_lock, flags);
5338 do_each_thread(g, t) {
5339 if (start == end) {
5340 ret = -EAGAIN;
5341 goto unlock;
5342 }
5343
5344 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005345 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005346 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005347 t->curr_ret_stack = -1;
5348 /* Make sure the tasks see the -1 first: */
5349 smp_wmb();
5350 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005351 }
5352 } while_each_thread(g, t);
5353
5354unlock:
5355 read_unlock_irqrestore(&tasklist_lock, flags);
5356free:
5357 for (i = start; i < end; i++)
5358 kfree(ret_stack_list[i]);
5359 return ret;
5360}
5361
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005362static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005363ftrace_graph_probe_sched_switch(void *ignore,
5364 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005365{
5366 unsigned long long timestamp;
5367 int index;
5368
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005369 /*
5370 * Does the user want to count the time a function was asleep.
5371 * If so, do not update the time stamps.
5372 */
5373 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5374 return;
5375
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005376 timestamp = trace_clock_local();
5377
5378 prev->ftrace_timestamp = timestamp;
5379
5380 /* only process tasks that we timestamped */
5381 if (!next->ftrace_timestamp)
5382 return;
5383
5384 /*
5385 * Update all the counters in next to make up for the
5386 * time next was sleeping.
5387 */
5388 timestamp -= next->ftrace_timestamp;
5389
5390 for (index = next->curr_ret_stack; index >= 0; index--)
5391 next->ret_stack[index].calltime += timestamp;
5392}
5393
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005394/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005395static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005396{
5397 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005398 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005399
5400 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5401 sizeof(struct ftrace_ret_stack *),
5402 GFP_KERNEL);
5403
5404 if (!ret_stack_list)
5405 return -ENOMEM;
5406
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005407 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005408 for_each_online_cpu(cpu) {
5409 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005410 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005411 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005412
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005413 do {
5414 ret = alloc_retstack_tasklist(ret_stack_list);
5415 } while (ret == -EAGAIN);
5416
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005417 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005418 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005419 if (ret)
5420 pr_info("ftrace_graph: Couldn't activate tracepoint"
5421 " probe to kernel_sched_switch\n");
5422 }
5423
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005424 kfree(ret_stack_list);
5425 return ret;
5426}
5427
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005428/*
5429 * Hibernation protection.
5430 * The state of the current task is too much unstable during
5431 * suspend/restore to disk. We want to protect against that.
5432 */
5433static int
5434ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5435 void *unused)
5436{
5437 switch (state) {
5438 case PM_HIBERNATION_PREPARE:
5439 pause_graph_tracing();
5440 break;
5441
5442 case PM_POST_HIBERNATION:
5443 unpause_graph_tracing();
5444 break;
5445 }
5446 return NOTIFY_DONE;
5447}
5448
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005449static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5450{
5451 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5452 return 0;
5453 return __ftrace_graph_entry(trace);
5454}
5455
5456/*
5457 * The function graph tracer should only trace the functions defined
5458 * by set_ftrace_filter and set_ftrace_notrace. If another function
5459 * tracer ops is registered, the graph tracer requires testing the
5460 * function against the global ops, and not just trace any function
5461 * that any ftrace_ops registered.
5462 */
5463static void update_function_graph_func(void)
5464{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005465 struct ftrace_ops *op;
5466 bool do_test = false;
5467
5468 /*
5469 * The graph and global ops share the same set of functions
5470 * to test. If any other ops is on the list, then
5471 * the graph tracing needs to test if its the function
5472 * it should call.
5473 */
5474 do_for_each_ftrace_op(op, ftrace_ops_list) {
5475 if (op != &global_ops && op != &graph_ops &&
5476 op != &ftrace_list_end) {
5477 do_test = true;
5478 /* in double loop, break out with goto */
5479 goto out;
5480 }
5481 } while_for_each_ftrace_op(op);
5482 out:
5483 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005484 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005485 else
5486 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005487}
5488
Mathias Krause8275f692014-03-30 15:31:50 +02005489static struct notifier_block ftrace_suspend_notifier = {
5490 .notifier_call = ftrace_suspend_notifier_call,
5491};
5492
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005493int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5494 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005495{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005496 int ret = 0;
5497
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005498 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005499
Steven Rostedt05ce5812009-03-24 00:18:31 -04005500 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005501 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005502 ret = -EBUSY;
5503 goto out;
5504 }
5505
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005506 register_pm_notifier(&ftrace_suspend_notifier);
5507
Steven Rostedt597af812009-04-03 15:24:12 -04005508 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005509 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005510 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005511 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005512 goto out;
5513 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005514
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005515 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005516
5517 /*
5518 * Update the indirect function to the entryfunc, and the
5519 * function that gets called to the entry_test first. Then
5520 * call the update fgraph entry function to determine if
5521 * the entryfunc should be called directly or not.
5522 */
5523 __ftrace_graph_entry = entryfunc;
5524 ftrace_graph_entry = ftrace_graph_entry_test;
5525 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005526
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005527 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005528
5529out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005530 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005531 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005532}
5533
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005534void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005535{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005536 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005537
Steven Rostedt597af812009-04-03 15:24:12 -04005538 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005539 goto out;
5540
Steven Rostedt597af812009-04-03 15:24:12 -04005541 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005542 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005543 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005544 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005545 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005546 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005547 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005548
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005549 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005550 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005551}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005552
Steven Rostedt868baf02011-02-10 21:26:13 -05005553static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5554
5555static void
5556graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5557{
5558 atomic_set(&t->tracing_graph_pause, 0);
5559 atomic_set(&t->trace_overrun, 0);
5560 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005561 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005562 smp_wmb();
5563 t->ret_stack = ret_stack;
5564}
5565
5566/*
5567 * Allocate a return stack for the idle task. May be the first
5568 * time through, or it may be done by CPU hotplug online.
5569 */
5570void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5571{
5572 t->curr_ret_stack = -1;
5573 /*
5574 * The idle task has no parent, it either has its own
5575 * stack or no stack at all.
5576 */
5577 if (t->ret_stack)
5578 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5579
5580 if (ftrace_graph_active) {
5581 struct ftrace_ret_stack *ret_stack;
5582
5583 ret_stack = per_cpu(idle_ret_stack, cpu);
5584 if (!ret_stack) {
5585 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5586 * sizeof(struct ftrace_ret_stack),
5587 GFP_KERNEL);
5588 if (!ret_stack)
5589 return;
5590 per_cpu(idle_ret_stack, cpu) = ret_stack;
5591 }
5592 graph_init_task(t, ret_stack);
5593 }
5594}
5595
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005596/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005597void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005598{
Steven Rostedt84047e32009-06-02 16:51:55 -04005599 /* Make sure we do not use the parent ret_stack */
5600 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005601 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005602
Steven Rostedt597af812009-04-03 15:24:12 -04005603 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005604 struct ftrace_ret_stack *ret_stack;
5605
5606 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005607 * sizeof(struct ftrace_ret_stack),
5608 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005609 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005610 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005611 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005612 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005613}
5614
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005615void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005616{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005617 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5618
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005619 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005620 /* NULL must become visible to IRQs before we free it: */
5621 barrier();
5622
5623 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005624}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005625#endif